EditStation.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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' && station.genres"
  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, mapActions } from "vuex";
  153. import { Toast } from "vue-roaster";
  154. import Modal from "./Modal.vue";
  155. import io from "../../io";
  156. import validation from "../../validation";
  157. export default {
  158. computed: mapState({
  159. editing(state) {
  160. return this.$props.store.split("/").reduce((a, v) => a[v], state)
  161. .editing;
  162. },
  163. station(state) {
  164. return this.$props.store.split("/").reduce((a, v) => a[v], state)
  165. .station;
  166. }
  167. }),
  168. mounted() {
  169. io.getSocket(socket => {
  170. this.socket = socket;
  171. return socket;
  172. });
  173. },
  174. props: ["store"],
  175. methods: {
  176. update() {
  177. if (this.station.name !== this.editing.name) this.updateName();
  178. if (this.station.displayName !== this.editing.displayName)
  179. this.updateDisplayName();
  180. if (this.station.description !== this.editing.description)
  181. this.updateDescription();
  182. if (this.station.privacy !== this.editing.privacy)
  183. this.updatePrivacy();
  184. if (this.station.partyMode !== this.editing.partyMode)
  185. this.updatePartyMode();
  186. if (this.$props.store !== "station") {
  187. if (
  188. this.station.genres.toString() !==
  189. this.editing.genres.toString()
  190. )
  191. this.updateGenres();
  192. if (
  193. this.station.blacklistedGenres.toString() !==
  194. this.editing.blacklistedGenres.toString()
  195. )
  196. this.updateBlacklistedGenres();
  197. }
  198. },
  199. updateName() {
  200. const { name } = this.editing;
  201. if (!validation.isLength(name, 2, 16))
  202. return Toast.methods.addToast(
  203. "Name must have between 2 and 16 characters.",
  204. 8000
  205. );
  206. if (!validation.regex.az09_.test(name))
  207. return Toast.methods.addToast(
  208. "Invalid name format. Allowed characters: a-z, 0-9 and _.",
  209. 8000
  210. );
  211. return this.socket.emit(
  212. "stations.updateName",
  213. this.editing._id,
  214. name,
  215. res => {
  216. if (res.status === "success") {
  217. if (this.station) this.station.name = name;
  218. this.$parent.stations.forEach((station, index) => {
  219. if (station._id === this.editing._id) {
  220. this.$parent.stations[index].name = name;
  221. return name;
  222. }
  223. return false;
  224. });
  225. }
  226. Toast.methods.addToast(res.message, 8000);
  227. }
  228. );
  229. },
  230. updateDisplayName() {
  231. const { displayName } = this.editing;
  232. if (!validation.isLength(displayName, 2, 32))
  233. return Toast.methods.addToast(
  234. "Display name must have between 2 and 32 characters.",
  235. 8000
  236. );
  237. if (!validation.regex.ascii.test(displayName))
  238. return Toast.methods.addToast(
  239. "Invalid display name format. Only ASCII characters are allowed.",
  240. 8000
  241. );
  242. return this.socket.emit(
  243. "stations.updateDisplayName",
  244. this.editing._id,
  245. displayName,
  246. res => {
  247. if (res.status === "success") {
  248. if (this.station) {
  249. this.station.displayName = displayName;
  250. return displayName;
  251. }
  252. this.$parent.stations.forEach((station, index) => {
  253. if (station._id === this.editing._id) {
  254. this.$parent.stations[
  255. index
  256. ].displayName = displayName;
  257. return displayName;
  258. }
  259. return false;
  260. });
  261. }
  262. return Toast.methods.addToast(res.message, 8000);
  263. }
  264. );
  265. },
  266. updateDescription() {
  267. const { description } = this.editing;
  268. if (!validation.isLength(description, 2, 200))
  269. return Toast.methods.addToast(
  270. "Description must have between 2 and 200 characters.",
  271. 8000
  272. );
  273. let characters = description.split("");
  274. characters = characters.filter(character => {
  275. return character.charCodeAt(0) === 21328;
  276. });
  277. if (characters.length !== 0)
  278. return Toast.methods.addToast(
  279. "Invalid description format.",
  280. 8000
  281. );
  282. return this.socket.emit(
  283. "stations.updateDescription",
  284. this.editing._id,
  285. description,
  286. res => {
  287. if (res.status === "success") {
  288. if (this.station) {
  289. this.station.description = description;
  290. return description;
  291. }
  292. this.$parent.stations.forEach((station, index) => {
  293. if (station._id === this.editing._id) {
  294. this.$parent.stations[
  295. index
  296. ].description = description;
  297. return description;
  298. }
  299. return false;
  300. });
  301. return Toast.methods.addToast(res.message, 4000);
  302. }
  303. return Toast.methods.addToast(res.message, 8000);
  304. }
  305. );
  306. },
  307. updatePrivacy() {
  308. this.socket.emit(
  309. "stations.updatePrivacy",
  310. this.editing._id,
  311. this.editing.privacy,
  312. res => {
  313. if (res.status === "success") {
  314. if (this.station)
  315. this.station.privacy = this.editing.privacy;
  316. else {
  317. this.$parent.stations.forEach((station, index) => {
  318. if (station._id === this.editing._id) {
  319. this.$parent.stations[
  320. index
  321. ].privacy = this.editing.privacy;
  322. return this.editing.privacy;
  323. }
  324. return false;
  325. });
  326. }
  327. return Toast.methods.addToast(res.message, 4000);
  328. }
  329. return Toast.methods.addToast(res.message, 8000);
  330. }
  331. );
  332. },
  333. updateGenres() {
  334. this.socket.emit(
  335. "stations.updateGenres",
  336. this.editing._id,
  337. this.editing.genres,
  338. res => {
  339. if (res.status === "success") {
  340. const genres = JSON.parse(
  341. JSON.stringify(this.editing.genres)
  342. );
  343. if (this.station) this.station.genres = genres;
  344. this.$parent.stations.forEach((station, index) => {
  345. if (station._id === this.editing._id) {
  346. this.$parent.stations[index].genres = genres;
  347. return genres;
  348. }
  349. return false;
  350. });
  351. return Toast.methods.addToast(res.message, 4000);
  352. }
  353. return Toast.methods.addToast(res.message, 8000);
  354. }
  355. );
  356. },
  357. updateBlacklistedGenres() {
  358. this.socket.emit(
  359. "stations.updateBlacklistedGenres",
  360. this.editing._id,
  361. this.editing.blacklistedGenres,
  362. res => {
  363. if (res.status === "success") {
  364. const blacklistedGenres = JSON.parse(
  365. JSON.stringify(this.editing.blacklistedGenres)
  366. );
  367. if (this.station)
  368. this.station.blacklistedGenres = blacklistedGenres;
  369. this.$parent.stations.forEach((station, index) => {
  370. if (station._id === this.editing._id) {
  371. this.$parent.stations[
  372. index
  373. ].blacklistedGenres = blacklistedGenres;
  374. return blacklistedGenres;
  375. }
  376. return false;
  377. });
  378. return Toast.methods.addToast(res.message, 4000);
  379. }
  380. return Toast.methods.addToast(res.message, 8000);
  381. }
  382. );
  383. },
  384. updatePartyMode() {
  385. this.socket.emit(
  386. "stations.updatePartyMode",
  387. this.editing._id,
  388. this.editing.partyMode,
  389. res => {
  390. if (res.status === "success") {
  391. // if (this.station)
  392. // this.station.partyMode = this.editing.partyMode;
  393. // this.$parent.stations.forEach((station, index) => {
  394. // if (station._id === this.editing._id) {
  395. // this.$parent.stations[
  396. // index
  397. // ].partyMode = this.editing.partyMode;
  398. // return this.editing.partyMode;
  399. // }
  400. // return false;
  401. // });
  402. return Toast.methods.addToast(res.message, 4000);
  403. }
  404. return Toast.methods.addToast(res.message, 8000);
  405. }
  406. );
  407. },
  408. addGenre() {
  409. const genre = document
  410. .getElementById(`new-genre-edit`)
  411. .value.toLowerCase()
  412. .trim();
  413. if (this.editing.genres.indexOf(genre) !== -1)
  414. return Toast.methods.addToast("Genre already exists", 3000);
  415. if (genre) {
  416. this.editing.genres.push(genre);
  417. document.getElementById(`new-genre-edit`).value = "";
  418. return true;
  419. }
  420. return Toast.methods.addToast("Genre cannot be empty", 3000);
  421. },
  422. removeGenre(index) {
  423. this.editing.genres.splice(index, 1);
  424. },
  425. addBlacklistedGenre() {
  426. const genre = document
  427. .getElementById(`new-blacklisted$pa-genre-edit`)
  428. .value.toLowerCase()
  429. .trim();
  430. if (this.editing.blacklistedGenres.indexOf(genre) !== -1)
  431. return Toast.methods.addToast("Genre already exists", 3000);
  432. if (genre) {
  433. this.editing.blacklistedGenres.push(genre);
  434. document.getElementById(`new-blacklisted-genre-edit`).value =
  435. "";
  436. return true;
  437. }
  438. return Toast.methods.addToast("Genre cannot be empty", 3000);
  439. },
  440. removeBlacklistedGenre(index) {
  441. this.editing.blacklistedGenres.splice(index, 1);
  442. },
  443. deleteStation() {
  444. this.socket.emit("stations.remove", this.editing._id, res => {
  445. if (res.status === "success")
  446. this.closeModal({
  447. sector: "station",
  448. modal: "editStation"
  449. });
  450. return Toast.methods.addToast(res.message, 8000);
  451. });
  452. },
  453. ...mapActions("modals", ["closeModal"])
  454. },
  455. components: { Modal }
  456. };
  457. </script>
  458. <style lang="scss" scoped>
  459. @import "scss/variables/colors.scss";
  460. .controls {
  461. display: flex;
  462. a {
  463. display: flex;
  464. align-items: center;
  465. }
  466. }
  467. .table {
  468. margin-bottom: 0;
  469. }
  470. h5 {
  471. padding: 20px 0;
  472. }
  473. .party-mode-inner,
  474. .party-mode-outer {
  475. display: flex;
  476. align-items: center;
  477. }
  478. .select:after {
  479. border-color: $primary-color;
  480. }
  481. </style>