EditStation.vue 12 KB

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