Settings.jsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. import CustomInput from "components/CustomInput.jsx";
  4. import CustomErrors from "components/CustomMessages.jsx";
  5. import { connect } from "react-redux";
  6. import { closeOverlay1 } from "actions/stationOverlay";
  7. import io from "io";
  8. @connect(state => ({
  9. stationId: state.station.get("id"),
  10. name: state.station.get("name"),
  11. displayName: state.station.get("displayName"),
  12. description: state.station.get("description"),
  13. privacy: state.station.get("privacy"),
  14. partyEnabled: state.station.get("partyMode"),
  15. queueLocked: state.station.get("locked"),
  16. }))
  17. export default class Settings extends Component {
  18. constructor(props) {
  19. super(props);
  20. CustomInput.initialize(this);
  21. this.state = {
  22. privacy: props.privacy,
  23. mode: this.getModeTemp(props.partyEnabled, props.queueLocked),
  24. deleteButtonText: "Delete this station", //TODO Improve this
  25. };
  26. }
  27. close = () => {
  28. this.props.dispatch(closeOverlay1());
  29. };
  30. changeName = () => {
  31. this.messages.clearErrorSuccess();
  32. if (CustomInput.hasInvalidInput(this.input, ["name"])) {
  33. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  34. } else {
  35. io.getSocket(socket => {
  36. socket.emit("stations.updateName", this.props.stationId, this.input.name.getValue(), res => {
  37. if (res.status === "success") {
  38. this.messages.clearAddSuccess("Successfully changed name.");
  39. } else {
  40. this.messages.addError(res.message);
  41. }
  42. });
  43. });
  44. }
  45. };
  46. changeDisplayName = () => {
  47. this.messages.clearErrorSuccess();
  48. if (CustomInput.hasInvalidInput(this.input, ["displayName"])) {
  49. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  50. } else {
  51. io.getSocket(socket => {
  52. socket.emit("stations.updateDisplayName", this.props.stationId, this.input.displayName.getValue(), res => {
  53. if (res.status === "success") {
  54. this.messages.clearAddSuccess("Successfully changed display name.");
  55. } else {
  56. this.messages.addError(res.message);
  57. }
  58. });
  59. });
  60. }
  61. };
  62. changeDescription = () => {
  63. this.messages.clearErrorSuccess();
  64. if (CustomInput.hasInvalidInput(this.input, ["description"])) {
  65. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  66. } else {
  67. io.getSocket(socket => {
  68. socket.emit("stations.updateDescription", this.props.stationId, this.input.description.getValue(), res => {
  69. if (res.status === "success") {
  70. this.messages.clearAddSuccess("Successfully changed description.");
  71. } else {
  72. this.messages.addError(res.message);
  73. }
  74. });
  75. });
  76. }
  77. };
  78. savePrivacy = () => {
  79. this.messages.clearErrorSuccess();
  80. if (CustomInput.hasInvalidInput(this.input, ["privacy"])) {
  81. console.log(this.props);
  82. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  83. } else {
  84. io.getSocket(socket => {
  85. socket.emit("stations.updatePrivacy", this.props.stationId, this.input.privacy.getValue(), res => {
  86. if (res.status === "success") {
  87. this.messages.clearAddSuccess("Successfully saved privacy.");
  88. } else {
  89. this.messages.addError(res.message);
  90. }
  91. });
  92. });
  93. }
  94. };
  95. getModeTemp = (partyEnabled, queueLocked) => {
  96. // If party enabled
  97. // If queue locked
  98. // Mode is DJ
  99. // If queue not locked
  100. // Mode party
  101. // If party not enabled
  102. // Mode is normal
  103. if (partyEnabled) {
  104. if (queueLocked) return "dj";
  105. else return "party";
  106. } else return "normal";
  107. }
  108. // This is temporary since the backend cannot be changed in this update.
  109. changeModeHandlerTemp = (newMode, cb) => {
  110. const previousMode = this.state.mode;
  111. const disablePartyMode = (cb) => {
  112. io.getSocket((socket) => {
  113. socket.emit("stations.updatePartyMode", this.props.stationId, false, (res) => {
  114. if (res.status === "success") {
  115. cb();
  116. } else {
  117. cb(res.message);
  118. }
  119. });
  120. });
  121. }
  122. const enablePartyMode = (cb) => {
  123. io.getSocket((socket) => {
  124. socket.emit("stations.updatePartyMode", this.props.stationId, true, (res) => {
  125. if (res.status === "success") {
  126. cb();
  127. } else {
  128. cb(res.message);
  129. }
  130. });
  131. });
  132. }
  133. const disableQueueLock = (cb) => {
  134. io.getSocket((socket) => {
  135. socket.emit("stations.toggleLock", this.props.stationId, (res) => {
  136. if (res.status === "success") {
  137. if (res.data === false) {
  138. cb();
  139. } else {
  140. disableQueueLock(cb);
  141. }
  142. } else {
  143. cb(res.message);
  144. }
  145. });
  146. });
  147. }
  148. const enableQueueLock = (cb) => {
  149. io.getSocket((socket) => {
  150. socket.emit("stations.toggleLock", this.props.stationId, (res) => {
  151. if (res.status === "success") {
  152. if (res.data === true) {
  153. cb();
  154. } else {
  155. enableQueueLock(cb);
  156. }
  157. } else {
  158. cb(res.message);
  159. }
  160. });
  161. });
  162. }
  163. // Previous mode is "normal"
  164. // New mode is "party"
  165. // Enable party mode
  166. // New mode is DJ
  167. // Enable party mode
  168. // Enable queue lock
  169. // Previous mode is "party"
  170. // New mode is normal
  171. // Disable party
  172. // Disable queue lock
  173. // New mode is DJ
  174. // Enable queue lock
  175. // Previous mode is "DJ"
  176. // New mode is normal
  177. // Disable party mode
  178. // Disable queue lock
  179. // New mode is party
  180. // Disable queue lock
  181. if (previousMode === "normal") {
  182. if (newMode === "party") {
  183. enablePartyMode(cb);
  184. } else if (newMode === "dj") {
  185. enablePartyMode((err) => {
  186. if (err) return cb(err);
  187. else enableQueueLock(cb);
  188. });
  189. }
  190. } else if (previousMode === "party") {
  191. if (newMode === "normal") {
  192. disablePartyMode((err) => {
  193. if (err) return cb(err);
  194. else disableQueueLock(cb);
  195. });
  196. } else if (newMode === "dj") {
  197. enableQueueLock(cb);
  198. }
  199. } else if (previousMode === "dj") {
  200. if (newMode === "normal") {
  201. disablePartyMode((err) => {
  202. if (err) return cb(err);
  203. else disableQueueLock(cb);
  204. });
  205. } else if (newMode === "party") {
  206. disableQueueLock(cb);
  207. }
  208. }
  209. };
  210. saveMode = () => {
  211. this.messages.clearErrorSuccess();
  212. if (CustomInput.hasInvalidInput(this.input, ["mode"])) {
  213. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  214. } else {
  215. const mode = this.input.mode.getValue();
  216. this.changeModeHandlerTemp(mode, (err) => {
  217. if (err) {
  218. this.messages.addError(err);
  219. } else {
  220. this.messages.clearAddSuccess("Successfully saved mode.");
  221. this.setState({
  222. mode,
  223. });
  224. }
  225. });
  226. }
  227. };
  228. deleteStation = () => {
  229. if (this.state.deleteButtonText === "Are you sure?") {
  230. clearTimeout(this.state.deleteButtonTimeout);
  231. io.getSocket(socket => {
  232. socket.emit('stations.remove', this.props.stationId, res => {
  233. if (res.status === "success") {
  234. this.messages.clearAddSuccess("Successfully deleted station.");
  235. } else {
  236. this.messages.addError(res.message);
  237. }
  238. });
  239. });
  240. } else {
  241. this.setState({
  242. deleteButtonText: "Are you sure?",
  243. deleteButtonTimeout: setTimeout(() => {
  244. this.setState({
  245. deleteButtonText: "Delete this station",
  246. deleteButtonTimeout: null,
  247. });
  248. }, 10000),
  249. });
  250. }
  251. };
  252. render() {
  253. return (
  254. <div className="overlay">
  255. <button onClick={ this.close } className="back"><i className="material-icons">arrow_back</i></button>
  256. <div className="content">
  257. <h1>Settings</h1>
  258. <CustomErrors onRef={ ref => (this.messages = ref) } />
  259. <CustomInput key="name" showLabel={true} type="stationName" name="name" label="Station name" placeholder="Station name" original={ this.props.name } onRef={ ref => (this.input.name = ref) } />
  260. <button onClick={ this.changeName }>Change name</button>
  261. <CustomInput key="displayName" showLabel={true} type="stationDisplayName" name="displayName" label="Station display name" placeholder="Station display name" original={ this.props.displayName } onRef={ ref => (this.input.displayName = ref) } />
  262. <button onClick={ this.changeDisplayName }>Change display name</button>
  263. <CustomInput key="description" showLabel={true} type="stationDescription" name="description" label="Station description" placeholder="Station description" original={ this.props.description } onRef={ ref => (this.input.description = ref) } />
  264. <button onClick={ this.changeDescription }>Change description</button>
  265. <CustomInput key="privacy" showLabel={true} type="stationPrivacy" name="privacy" label="Station privacy" placeholder="Station privacy" original={ this.state.privacy } onRef={ ref => (this.input.privacy = ref) } />
  266. <button onClick={ this.savePrivacy }>Save privacy</button>
  267. <CustomInput key="mode" showLabel={true} type="stationMode" name="mode" label="Station mode" placeholder="Station mode" original={ this.state.mode } onRef={ ref => (this.input.mode = ref) } />
  268. <button onClick={ this.saveMode }>Save mode</button>
  269. <button onClick={ this.deleteStation } className="red-button">{ this.state.deleteButtonText }</button>
  270. </div>
  271. </div>
  272. );
  273. }
  274. }