EditStation.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. <template>
  2. <modal title="Edit Station">
  3. <template v-slot:body>
  4. <label class="label">Name</label>
  5. <p class="control">
  6. <input
  7. v-model="editing.name"
  8. class="input"
  9. type="text"
  10. placeholder="Station Name"
  11. />
  12. </p>
  13. <label class="label">Display name</label>
  14. <p class="control">
  15. <input
  16. v-model="editing.displayName"
  17. class="input"
  18. type="text"
  19. placeholder="Station Display Name"
  20. />
  21. </p>
  22. <label class="label">Description</label>
  23. <p class="control">
  24. <input
  25. v-model="editing.description"
  26. class="input"
  27. type="text"
  28. placeholder="Station Description"
  29. />
  30. </p>
  31. <label class="label">Privacy</label>
  32. <p class="control">
  33. <span class="select">
  34. <select v-model="editing.privacy">
  35. <option value="public">Public</option>
  36. <option value="unlisted">Unlisted</option>
  37. <option value="private">Private</option>
  38. </select>
  39. </span>
  40. </p>
  41. <br />
  42. <p class="control" v-if="station.type === 'community'">
  43. <label class="checkbox party-mode-inner">
  44. <input v-model="editing.partyMode" type="checkbox" />
  45. &nbsp;Party mode
  46. </label>
  47. </p>
  48. <small v-if="station.type === 'community'"
  49. >With party mode enabled, people can add songs to a queue that
  50. plays. With party mode disabled you can play a private playlist
  51. on loop.</small
  52. >
  53. <br />
  54. <div v-if="station.type === 'community' && station.partyMode">
  55. <br />
  56. <br />
  57. <label class="label">Queue lock</label>
  58. <small v-if="station.partyMode"
  59. >With the queue locked, only owners (you) can add songs to
  60. the queue.</small
  61. >
  62. <br />
  63. <button
  64. v-if="!station.locked"
  65. class="button is-danger"
  66. @click="$parent.toggleLock()"
  67. >
  68. Lock the queue
  69. </button>
  70. <button
  71. v-if="station.locked"
  72. class="button is-success"
  73. @click="$parent.toggleLock()"
  74. >
  75. Unlock the queue
  76. </button>
  77. </div>
  78. <div
  79. v-if="station.type === 'official'"
  80. class="control is-grouped genre-wrapper"
  81. >
  82. <div class="sector">
  83. <p class="control has-addons">
  84. <input
  85. id="new-genre-edit"
  86. class="input"
  87. type="text"
  88. placeholder="Genre"
  89. @keyup.enter="addGenre()"
  90. />
  91. <a class="button is-info" href="#" @click="addGenre()"
  92. >Add genre</a
  93. >
  94. </p>
  95. <span
  96. v-for="(genre, index) in editing.genres"
  97. :key="index"
  98. class="tag is-info"
  99. >
  100. {{ genre }}
  101. <button
  102. class="delete is-info"
  103. @click="removeGenre(index)"
  104. />
  105. </span>
  106. </div>
  107. <div class="sector">
  108. <p class="control has-addons">
  109. <input
  110. id="new-blacklisted-genre-edit"
  111. class="input"
  112. type="text"
  113. placeholder="Blacklisted Genre"
  114. @keyup.enter="addBlacklistedGenre()"
  115. />
  116. <a
  117. class="button is-info"
  118. href="#"
  119. @click="addBlacklistedGenre()"
  120. >Add blacklisted genre</a
  121. >
  122. </p>
  123. <span
  124. v-for="(genre, index) in editing.blacklistedGenres"
  125. :key="index"
  126. class="tag is-info"
  127. >
  128. {{ genre }}
  129. <button
  130. class="delete is-info"
  131. @click="removeBlacklistedGenre(index)"
  132. />
  133. </span>
  134. </div>
  135. </div>
  136. </template>
  137. <template v-slot:footer>
  138. <button class="button is-success" v-on:click="update()">
  139. Update Settings
  140. </button>
  141. <button
  142. v-if="station.type === 'community'"
  143. class="button is-danger"
  144. @click="deleteStation()"
  145. >
  146. Delete station
  147. </button>
  148. </template>
  149. </modal>
  150. </template>
  151. <script>
  152. import { mapState } from "vuex";
  153. import { Toast } from "vue-roaster";
  154. import Modal from "../Modals/Modal.vue";
  155. import io from "../../io";
  156. import validation from "../../validation";
  157. export default {
  158. computed: mapState("admin/stations", {
  159. station: state => state.station,
  160. editing: state => state.editing
  161. }),
  162. mounted() {
  163. const _this = this;
  164. io.getSocket(socket => {
  165. _this.socket = socket;
  166. return socket;
  167. });
  168. },
  169. methods: {
  170. update() {
  171. if (this.station.name !== this.editing.name) this.updateName();
  172. if (this.station.displayName !== this.editing.displayName)
  173. this.updateDisplayName();
  174. if (this.station.description !== this.editing.description)
  175. this.updateDescription();
  176. if (this.station.privacy !== this.editing.privacy)
  177. this.updatePrivacy();
  178. if (this.station.partyMode !== this.editing.partyMode)
  179. this.updatePartyMode();
  180. if (
  181. this.station.genres.toString() !==
  182. this.editing.genres.toString()
  183. )
  184. this.updateGenres();
  185. if (
  186. this.station.blacklistedGenres.toString() !==
  187. this.editing.blacklistedGenres.toString()
  188. )
  189. this.updateBlacklistedGenres();
  190. },
  191. updateName() {
  192. const { name } = this.editing;
  193. if (!validation.isLength(name, 2, 16))
  194. return Toast.methods.addToast(
  195. "Name must have between 2 and 16 characters.",
  196. 8000
  197. );
  198. if (!validation.regex.az09_.test(name))
  199. return Toast.methods.addToast(
  200. "Invalid name format. Allowed characters: a-z, 0-9 and _.",
  201. 8000
  202. );
  203. return this.socket.emit(
  204. "stations.updateName",
  205. this.editing._id,
  206. name,
  207. res => {
  208. if (res.status === "success") {
  209. if (this.station) this.station.name = name;
  210. this.$parent.stations.forEach((station, index) => {
  211. if (station._id === this.editing._id) {
  212. this.$parent.stations[index].name = name;
  213. return name;
  214. }
  215. return false;
  216. });
  217. }
  218. Toast.methods.addToast(res.message, 8000);
  219. }
  220. );
  221. },
  222. updateDisplayName() {
  223. const { displayName } = this.editing;
  224. if (!validation.isLength(displayName, 2, 32))
  225. return Toast.methods.addToast(
  226. "Display name must have between 2 and 32 characters.",
  227. 8000
  228. );
  229. if (!validation.regex.azAZ09_.test(displayName))
  230. return Toast.methods.addToast(
  231. "Invalid display name format. Allowed characters: a-z, A-Z, 0-9 and _.",
  232. 8000
  233. );
  234. return this.socket.emit(
  235. "stations.updateDisplayName",
  236. this.editing._id,
  237. displayName,
  238. res => {
  239. if (res.status === "success") {
  240. if (this.station) {
  241. this.station.displayName = displayName;
  242. return displayName;
  243. }
  244. this.$parent.stations.forEach((station, index) => {
  245. if (station._id === this.editing._id) {
  246. this.$parent.stations[
  247. index
  248. ].displayName = displayName;
  249. return displayName;
  250. }
  251. return false;
  252. });
  253. }
  254. return Toast.methods.addToast(res.message, 8000);
  255. }
  256. );
  257. },
  258. updateDescription() {
  259. const _this = this;
  260. const { description } = this.editing;
  261. if (!validation.isLength(description, 2, 200))
  262. return Toast.methods.addToast(
  263. "Description must have between 2 and 200 characters.",
  264. 8000
  265. );
  266. let characters = description.split("");
  267. characters = characters.filter(character => {
  268. return character.charCodeAt(0) === 21328;
  269. });
  270. if (characters.length !== 0)
  271. return Toast.methods.addToast(
  272. "Invalid description format. Swastika's are not allowed.",
  273. 8000
  274. );
  275. return this.socket.emit(
  276. "stations.updateDescription",
  277. this.editing._id,
  278. description,
  279. res => {
  280. if (res.status === "success") {
  281. if (_this.station) {
  282. _this.station.description = description;
  283. return description;
  284. }
  285. _this.$parent.stations.forEach((station, index) => {
  286. if (station._id === _this.editing._id) {
  287. _this.$parent.stations[
  288. index
  289. ].description = description;
  290. return description;
  291. }
  292. return false;
  293. });
  294. return Toast.methods.addToast(res.message, 4000);
  295. }
  296. return Toast.methods.addToast(res.message, 8000);
  297. }
  298. );
  299. },
  300. updatePrivacy() {
  301. const _this = this;
  302. this.socket.emit(
  303. "stations.updatePrivacy",
  304. this.editing._id,
  305. this.editing.privacy,
  306. res => {
  307. if (res.status === "success") {
  308. if (_this.station)
  309. _this.station.privacy = _this.editing.privacy;
  310. else {
  311. _this.$parent.stations.forEach((station, index) => {
  312. if (station._id === _this.editing._id) {
  313. _this.$parent.stations[index].privacy =
  314. _this.editing.privacy;
  315. return _this.editing.privacy;
  316. }
  317. return false;
  318. });
  319. }
  320. return Toast.methods.addToast(res.message, 4000);
  321. }
  322. return Toast.methods.addToast(res.message, 8000);
  323. }
  324. );
  325. },
  326. updateGenres() {
  327. const _this = this;
  328. this.socket.emit(
  329. "stations.updateGenres",
  330. this.editing._id,
  331. this.editing.genres,
  332. res => {
  333. if (res.status === "success") {
  334. const genres = JSON.parse(
  335. JSON.stringify(_this.editing.genres)
  336. );
  337. if (_this.station) _this.station.genres = genres;
  338. _this.$parent.stations.forEach((station, index) => {
  339. if (station._id === _this.editing._id) {
  340. _this.$parent.stations[index].genres = genres;
  341. return genres;
  342. }
  343. return false;
  344. });
  345. return Toast.methods.addToast(res.message, 4000);
  346. }
  347. return Toast.methods.addToast(res.message, 8000);
  348. }
  349. );
  350. },
  351. updateBlacklistedGenres() {
  352. const _this = this;
  353. this.socket.emit(
  354. "stations.updateBlacklistedGenres",
  355. this.editing._id,
  356. this.editing.blacklistedGenres,
  357. res => {
  358. if (res.status === "success") {
  359. const blacklistedGenres = JSON.parse(
  360. JSON.stringify(_this.editing.blacklistedGenres)
  361. );
  362. if (_this.station)
  363. _this.station.blacklistedGenres = blacklistedGenres;
  364. _this.$parent.stations.forEach((station, index) => {
  365. if (station._id === _this.editing._id) {
  366. _this.$parent.stations[
  367. index
  368. ].blacklistedGenres = blacklistedGenres;
  369. return blacklistedGenres;
  370. }
  371. return false;
  372. });
  373. return Toast.methods.addToast(res.message, 4000);
  374. }
  375. return Toast.methods.addToast(res.message, 8000);
  376. }
  377. );
  378. },
  379. updatePartyMode() {
  380. const _this = this;
  381. this.socket.emit(
  382. "stations.updatePartyMode",
  383. this.editing._id,
  384. this.editing.partyMode,
  385. res => {
  386. if (res.status === "success") {
  387. if (_this.station)
  388. _this.station.partyMode = _this.editing.partyMode;
  389. _this.$parent.stations.forEach((station, index) => {
  390. if (station._id === _this.editing._id) {
  391. _this.$parent.stations[index].partyMode =
  392. _this.editing.partyMode;
  393. return _this.editing.partyMode;
  394. }
  395. return false;
  396. });
  397. return Toast.methods.addToast(res.message, 4000);
  398. }
  399. return Toast.methods.addToast(res.message, 8000);
  400. }
  401. );
  402. },
  403. addGenre() {
  404. const genre = document
  405. .getElementById(`new-genre-edit`)
  406. .value.toLowerCase()
  407. .trim();
  408. if (this.editing.genres.indexOf(genre) !== -1)
  409. return Toast.methods.addToast("Genre already exists", 3000);
  410. if (genre) {
  411. this.editing.genres.push(genre);
  412. document.getElementById(`new-genre`).value = "";
  413. return true;
  414. }
  415. return Toast.methods.addToast("Genre cannot be empty", 3000);
  416. },
  417. removeGenre(index) {
  418. this.editing.genres.splice(index, 1);
  419. },
  420. addBlacklistedGenre() {
  421. const genre = document
  422. .getElementById(`new-blacklisted-genre-edit`)
  423. .value.toLowerCase()
  424. .trim();
  425. if (this.editing.blacklistedGenres.indexOf(genre) !== -1)
  426. return Toast.methods.addToast("Genre already exists", 3000);
  427. if (genre) {
  428. this.editing.blacklistedGenres.push(genre);
  429. document.getElementById(`new-blacklisted-genre`).value = "";
  430. return true;
  431. }
  432. return Toast.methods.addToast("Genre cannot be empty", 3000);
  433. },
  434. removeBlacklistedGenre(index) {
  435. this.editing.blacklistedGenres.splice(index, 1);
  436. },
  437. deleteStation() {
  438. this.socket.emit("stations.remove", this.editing._id, res => {
  439. Toast.methods.addToast(res.message, 8000);
  440. });
  441. }
  442. },
  443. components: { Modal }
  444. };
  445. </script>
  446. <style lang="scss" scoped>
  447. .controls {
  448. display: flex;
  449. a {
  450. display: flex;
  451. align-items: center;
  452. }
  453. }
  454. .table {
  455. margin-bottom: 0;
  456. }
  457. h5 {
  458. padding: 20px 0;
  459. }
  460. .party-mode-inner,
  461. .party-mode-outer {
  462. display: flex;
  463. align-items: center;
  464. }
  465. .select:after {
  466. border-color: #029ce3;
  467. }
  468. </style>