Settings.jsx 9.4 KB

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