Stations.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <template>
  2. <div>
  3. <metadata title="Admin | Stations" />
  4. <div class="container">
  5. <table class="table is-striped">
  6. <thead>
  7. <tr>
  8. <td>ID</td>
  9. <td>Name</td>
  10. <td>Type</td>
  11. <td>Display Name</td>
  12. <td>Description</td>
  13. <td>Owner</td>
  14. <td>Options</td>
  15. </tr>
  16. </thead>
  17. <tbody>
  18. <tr v-for="(station, index) in stations" :key="index">
  19. <td>
  20. <span>{{ station._id }}</span>
  21. </td>
  22. <td>
  23. <span>
  24. <router-link
  25. :to="{
  26. name: 'station',
  27. params: { id: station.name }
  28. }"
  29. >
  30. {{ station.name }}
  31. </router-link>
  32. </span>
  33. </td>
  34. <td>
  35. <span>{{ station.type }}</span>
  36. </td>
  37. <td>
  38. <span>{{ station.displayName }}</span>
  39. </td>
  40. <td>
  41. <span>{{ station.description }}</span>
  42. </td>
  43. <td>
  44. <span
  45. v-if="station.type === 'official'"
  46. title="Musare"
  47. >Musare</span
  48. >
  49. <user-id-to-username
  50. v-else
  51. :userId="station.owner"
  52. :link="true"
  53. />
  54. </td>
  55. <td>
  56. <a class="button is-info" v-on:click="edit(station)"
  57. >Edit</a
  58. >
  59. <a
  60. class="button is-danger"
  61. href="#"
  62. @click="removeStation(index)"
  63. >Remove</a
  64. >
  65. </td>
  66. </tr>
  67. </tbody>
  68. </table>
  69. </div>
  70. <div class="container">
  71. <div class="card is-fullwidth">
  72. <header class="card-header">
  73. <p class="card-header-title">
  74. Create official station
  75. </p>
  76. </header>
  77. <div class="card-content">
  78. <div class="content">
  79. <div class="control is-horizontal">
  80. <div class="control is-grouped">
  81. <p class="control is-expanded">
  82. <input
  83. v-model="newStation.name"
  84. class="input"
  85. type="text"
  86. placeholder="Name"
  87. />
  88. </p>
  89. <p class="control is-expanded">
  90. <input
  91. v-model="newStation.displayName"
  92. class="input"
  93. type="text"
  94. placeholder="Display Name"
  95. />
  96. </p>
  97. </div>
  98. </div>
  99. <label class="label">Description</label>
  100. <p class="control is-expanded">
  101. <input
  102. v-model="newStation.description"
  103. class="input"
  104. type="text"
  105. placeholder="Short description"
  106. />
  107. </p>
  108. <div class="control is-grouped genre-wrapper">
  109. <div class="sector">
  110. <p class="control has-addons">
  111. <input
  112. id="new-genre"
  113. class="input"
  114. type="text"
  115. placeholder="Genre"
  116. @keyup.enter="addGenre()"
  117. />
  118. <a
  119. class="button is-info"
  120. href="#"
  121. @click="addGenre()"
  122. >Add genre</a
  123. >
  124. </p>
  125. <span
  126. v-for="(genre, index) in newStation.genres"
  127. :key="index"
  128. class="tag is-info"
  129. >
  130. {{ genre }}
  131. <button
  132. class="delete is-info"
  133. @click="removeGenre(index)"
  134. />
  135. </span>
  136. </div>
  137. <div class="sector">
  138. <p class="control has-addons">
  139. <input
  140. id="new-blacklisted-genre"
  141. class="input"
  142. type="text"
  143. placeholder="Blacklisted Genre"
  144. @keyup.enter="addBlacklistedGenre()"
  145. />
  146. <a
  147. class="button is-info"
  148. href="#"
  149. @click="addBlacklistedGenre()"
  150. >Add blacklisted genre</a
  151. >
  152. </p>
  153. <span
  154. v-for="(genre,
  155. index) in newStation.blacklistedGenres"
  156. :key="index"
  157. class="tag is-info"
  158. >
  159. {{ genre }}
  160. <button
  161. class="delete is-info"
  162. @click="removeBlacklistedGenre(index)"
  163. />
  164. </span>
  165. </div>
  166. </div>
  167. </div>
  168. </div>
  169. <footer class="card-footer">
  170. <a
  171. class="card-footer-item"
  172. href="#"
  173. @click="createStation()"
  174. >Create</a
  175. >
  176. </footer>
  177. </div>
  178. </div>
  179. <edit-station v-if="modals.editStation" store="admin/stations" />
  180. </div>
  181. </template>
  182. <script>
  183. import { mapState, mapActions } from "vuex";
  184. import Toast from "toasters";
  185. import io from "../../io";
  186. import EditStation from "../Modals/EditStation.vue";
  187. import UserIdToUsername from "../UserIdToUsername.vue";
  188. export default {
  189. components: { EditStation, UserIdToUsername },
  190. data() {
  191. return {
  192. newStation: {
  193. genres: [],
  194. blacklistedGenres: []
  195. }
  196. };
  197. },
  198. computed: {
  199. ...mapState("admin/stations", {
  200. stations: state => state.stations
  201. }),
  202. ...mapState("modals", {
  203. modals: state => state.modals.station
  204. })
  205. },
  206. methods: {
  207. createStation() {
  208. const {
  209. newStation: {
  210. name,
  211. displayName,
  212. description,
  213. genres,
  214. blacklistedGenres
  215. }
  216. } = this;
  217. if (name === undefined)
  218. return new Toast({
  219. content: "Field (Name) cannot be empty",
  220. timeout: 3000
  221. });
  222. if (displayName === undefined)
  223. return new Toast({
  224. content: "Field (Display Name) cannot be empty",
  225. timeout: 3000
  226. });
  227. if (description === undefined)
  228. return new Toast({
  229. content: "Field (Description) cannot be empty",
  230. timeout: 3000
  231. });
  232. return this.socket.emit(
  233. "stations.create",
  234. {
  235. name,
  236. type: "official",
  237. displayName,
  238. description,
  239. genres,
  240. blacklistedGenres
  241. },
  242. result => {
  243. new Toast({ content: result.message, timeout: 3000 });
  244. if (result.status === "success")
  245. this.newStation = {
  246. genres: [],
  247. blacklistedGenres: []
  248. };
  249. }
  250. );
  251. },
  252. removeStation(index) {
  253. this.socket.emit(
  254. "stations.remove",
  255. this.stations[index]._id,
  256. res => {
  257. new Toast({ content: res.message, timeout: 3000 });
  258. }
  259. );
  260. },
  261. edit(station) {
  262. this.editStation({
  263. _id: station._id,
  264. name: station.name,
  265. type: station.type,
  266. partyMode: station.partyMode,
  267. description: station.description,
  268. privacy: station.privacy,
  269. displayName: station.displayName,
  270. genres: station.genres,
  271. blacklistedGenres: station.blacklistedGenres
  272. });
  273. this.openModal({
  274. sector: "station",
  275. modal: "editStation"
  276. });
  277. },
  278. addGenre() {
  279. const genre = document
  280. .getElementById(`new-genre`)
  281. .value.toLowerCase()
  282. .trim();
  283. if (this.newStation.genres.indexOf(genre) !== -1)
  284. return new Toast({
  285. content: "Genre already exists",
  286. timeout: 3000
  287. });
  288. if (genre) {
  289. this.newStation.genres.push(genre);
  290. document.getElementById(`new-genre`).value = "";
  291. return true;
  292. }
  293. return new Toast({
  294. content: "Genre cannot be empty",
  295. timeout: 3000
  296. });
  297. },
  298. removeGenre(index) {
  299. this.newStation.genres.splice(index, 1);
  300. },
  301. addBlacklistedGenre() {
  302. const genre = document
  303. .getElementById(`new-blacklisted-genre`)
  304. .value.toLowerCase()
  305. .trim();
  306. if (this.newStation.blacklistedGenres.indexOf(genre) !== -1)
  307. return new Toast({
  308. content: "Genre already exists",
  309. timeout: 3000
  310. });
  311. if (genre) {
  312. this.newStation.blacklistedGenres.push(genre);
  313. document.getElementById(`new-blacklisted-genre`).value = "";
  314. return true;
  315. }
  316. return new Toast({
  317. content: "Genre cannot be empty",
  318. timeout: 3000
  319. });
  320. },
  321. removeBlacklistedGenre(index) {
  322. this.newStation.blacklistedGenres.splice(index, 1);
  323. },
  324. init() {
  325. this.socket.emit("stations.index", data => {
  326. this.loadStations(data.stations);
  327. });
  328. this.socket.emit("apis.joinAdminRoom", "stations", () => {});
  329. },
  330. ...mapActions("modals", ["openModal"]),
  331. ...mapActions("admin/stations", [
  332. "editStation",
  333. "loadStations",
  334. "stationRemoved",
  335. "stationAdded"
  336. ])
  337. },
  338. mounted() {
  339. io.getSocket(socket => {
  340. this.socket = socket;
  341. if (this.socket.connected) this.init();
  342. this.socket.on("event:admin.station.added", station => {
  343. this.stationAdded(station);
  344. });
  345. this.socket.on("event:admin.station.removed", stationId => {
  346. this.stationRemoved(stationId);
  347. });
  348. io.onConnect(() => {
  349. this.init();
  350. });
  351. });
  352. }
  353. };
  354. </script>
  355. <style lang="scss" scoped>
  356. @import "styles/global.scss";
  357. .night-mode {
  358. .table {
  359. color: #ddd;
  360. background-color: #222;
  361. thead tr {
  362. background: $night-mode-secondary;
  363. td {
  364. color: #fff;
  365. }
  366. }
  367. tbody tr:hover {
  368. background-color: #111 !important;
  369. }
  370. tbody tr:nth-child(even) {
  371. background-color: #444;
  372. }
  373. strong {
  374. color: #ddd;
  375. }
  376. }
  377. .card {
  378. background: #222;
  379. .card-header {
  380. box-shadow: 0 1px 2px rgba(10, 10, 10, 0.8);
  381. }
  382. p,
  383. .label {
  384. color: #ddd;
  385. }
  386. }
  387. }
  388. .tag {
  389. margin-top: 5px;
  390. &:not(:last-child) {
  391. margin-right: 5px;
  392. }
  393. }
  394. td {
  395. word-wrap: break-word;
  396. max-width: 10vw;
  397. vertical-align: middle;
  398. }
  399. .is-info:focus {
  400. background-color: $primary-color;
  401. }
  402. .genre-wrapper {
  403. display: flex;
  404. justify-content: space-around;
  405. }
  406. .card-footer-item {
  407. color: $primary-color;
  408. }
  409. </style>