Stations.vue 8.7 KB

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