Settings.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <script setup lang="ts">
  2. import { defineAsyncComponent, watch } from "vue";
  3. import Toast from "toasters";
  4. import { storeToRefs } from "pinia";
  5. import validation from "@/validation";
  6. import { useWebsocketsStore } from "@/stores/websockets";
  7. import { useManageStationStore } from "@/stores/manageStation";
  8. import { useForm } from "@/composables/useForm";
  9. const InfoIcon = defineAsyncComponent(
  10. () => import("@/components/InfoIcon.vue")
  11. );
  12. const props = defineProps({
  13. modalUuid: { type: String, required: true }
  14. });
  15. const { socket } = useWebsocketsStore();
  16. const manageStationStore = useManageStationStore(props);
  17. const { station } = storeToRefs(manageStationStore);
  18. const { editStation } = manageStationStore;
  19. const { inputs, save, setOriginalValue } = useForm(
  20. {
  21. name: {
  22. value: station.value.name,
  23. validate: value => {
  24. if (!validation.isLength(value, 2, 16)) {
  25. const err = "Name must have between 2 and 16 characters.";
  26. new Toast(err);
  27. return err;
  28. }
  29. if (!validation.regex.az09_.test(value)) {
  30. const err =
  31. "Invalid name format. Allowed characters: a-z, 0-9 and _.";
  32. new Toast(err);
  33. return err;
  34. }
  35. return true;
  36. }
  37. },
  38. displayName: {
  39. value: station.value.displayName,
  40. validate: value => {
  41. if (!validation.isLength(value, 2, 32)) {
  42. const err =
  43. "Display name must have between 2 and 32 characters.";
  44. new Toast(err);
  45. return err;
  46. }
  47. if (!validation.regex.ascii.test(value)) {
  48. const err =
  49. "Invalid display name format. Only ASCII characters are allowed.";
  50. new Toast(err);
  51. return err;
  52. }
  53. return true;
  54. }
  55. },
  56. description: {
  57. value: station.value.description,
  58. validate: value => {
  59. if (
  60. value
  61. .split("")
  62. .filter(character => character.charCodeAt(0) === 21328)
  63. .length !== 0
  64. ) {
  65. const err = "Invalid description format.";
  66. new Toast(err);
  67. return err;
  68. }
  69. return true;
  70. }
  71. },
  72. theme: station.value.theme,
  73. privacy: station.value.privacy,
  74. requestsEnabled: station.value.requests.enabled,
  75. requestsAccess: station.value.requests.access,
  76. requestsLimit: station.value.requests.limit,
  77. autofillEnabled: station.value.autofill.enabled,
  78. autofillLimit: station.value.autofill.limit,
  79. autofillMode: station.value.autofill.mode
  80. },
  81. (status, message, values) =>
  82. new Promise((resolve, reject) => {
  83. if (status === "success") {
  84. const oldStation = JSON.parse(JSON.stringify(station.value));
  85. const updatedStation = {
  86. ...oldStation,
  87. name: values.name,
  88. displayName: values.displayName,
  89. description: values.description,
  90. theme: values.theme,
  91. privacy: values.privacy,
  92. requests: {
  93. ...oldStation.requests,
  94. enabled: values.requestsEnabled,
  95. access: values.requestsAccess,
  96. limit: values.requestsLimit
  97. },
  98. autofill: {
  99. ...oldStation.autofill,
  100. enabled: values.autofillEnabled,
  101. limit: values.autofillLimit,
  102. mode: values.autofillMode
  103. }
  104. };
  105. socket.dispatch(
  106. "stations.update",
  107. station.value._id,
  108. updatedStation,
  109. res => {
  110. new Toast(res.message);
  111. if (res.status === "success") {
  112. editStation(updatedStation);
  113. resolve();
  114. } else reject(new Error(res.message));
  115. }
  116. );
  117. } else new Toast(message);
  118. }),
  119. {
  120. modalUuid: props.modalUuid
  121. }
  122. );
  123. watch(station, value => {
  124. setOriginalValue({
  125. name: value.name,
  126. displayName: value.displayName,
  127. description: value.description,
  128. theme: value.theme,
  129. privacy: value.privacy,
  130. requestsEnabled: value.requests.enabled,
  131. requestsAccess: value.requests.access,
  132. requestsLimit: value.requests.limit,
  133. autofillEnabled: value.autofill.enabled,
  134. autofillLimit: value.autofill.limit,
  135. autofillMode: value.autofill.mode
  136. });
  137. });
  138. </script>
  139. <template>
  140. <div class="station-settings">
  141. <label class="label">Name</label>
  142. <div class="control is-expanded">
  143. <input class="input" type="text" v-model="inputs['name'].value" />
  144. </div>
  145. <label class="label">Display Name</label>
  146. <div class="control is-expanded">
  147. <input
  148. class="input"
  149. type="text"
  150. v-model="inputs['displayName'].value"
  151. />
  152. </div>
  153. <label class="label">Description</label>
  154. <div class="control is-expanded">
  155. <input
  156. class="input"
  157. type="text"
  158. v-model="inputs['description'].value"
  159. />
  160. </div>
  161. <div class="settings-buttons">
  162. <div class="small-section">
  163. <label class="label">Theme</label>
  164. <div class="control is-expanded select">
  165. <select v-model="inputs['theme'].value">
  166. <option value="blue" selected>Blue</option>
  167. <option value="purple">Purple</option>
  168. <option value="teal">Teal</option>
  169. <option value="orange">Orange</option>
  170. <option value="red">Red</option>
  171. </select>
  172. </div>
  173. </div>
  174. <div class="small-section">
  175. <label class="label">Privacy</label>
  176. <div class="control is-expanded select">
  177. <select v-model="inputs['privacy'].value">
  178. <option value="public">Public</option>
  179. <option value="unlisted">Unlisted</option>
  180. <option value="private" selected>Private</option>
  181. </select>
  182. </div>
  183. </div>
  184. <div
  185. class="requests-settings"
  186. :class="{ enabled: inputs['requestsEnabled'].value }"
  187. >
  188. <div class="toggle-row">
  189. <label class="label">
  190. Requests
  191. <info-icon
  192. tooltip="Allow users to add songs to the queue"
  193. />
  194. </label>
  195. <p class="is-expanded checkbox-control">
  196. <label class="switch">
  197. <input
  198. type="checkbox"
  199. id="toggle-requests"
  200. v-model="inputs['requestsEnabled'].value"
  201. />
  202. <span class="slider round"></span>
  203. </label>
  204. <label for="toggle-requests">
  205. <p>
  206. {{
  207. inputs["requestsEnabled"].value
  208. ? "Enabled"
  209. : "Disabled"
  210. }}
  211. </p>
  212. </label>
  213. </p>
  214. </div>
  215. <div
  216. v-if="inputs['requestsEnabled'].value"
  217. class="small-section"
  218. >
  219. <label class="label">Minimum access</label>
  220. <div class="control is-expanded select">
  221. <select v-model="inputs['requestsAccess'].value">
  222. <option value="owner" selected>Owner</option>
  223. <option value="user">User</option>
  224. </select>
  225. </div>
  226. </div>
  227. <div
  228. v-if="inputs['requestsEnabled'].value"
  229. class="small-section"
  230. >
  231. <label class="label">Per user request limit</label>
  232. <div class="control is-expanded">
  233. <input
  234. class="input"
  235. type="number"
  236. min="1"
  237. max="50"
  238. v-model="inputs['requestsLimit'].value"
  239. />
  240. </div>
  241. </div>
  242. </div>
  243. <div
  244. class="autofill-settings"
  245. :class="{ enabled: inputs['autofillEnabled'].value }"
  246. >
  247. <div class="toggle-row">
  248. <label class="label">
  249. Autofill
  250. <info-icon
  251. tooltip="Automatically fill the queue with songs"
  252. />
  253. </label>
  254. <p class="is-expanded checkbox-control">
  255. <label class="switch">
  256. <input
  257. type="checkbox"
  258. id="toggle-autofill"
  259. v-model="inputs['autofillEnabled'].value"
  260. />
  261. <span class="slider round"></span>
  262. </label>
  263. <label for="toggle-autofill">
  264. <p>
  265. {{
  266. inputs["autofillEnabled"].value
  267. ? "Enabled"
  268. : "Disabled"
  269. }}
  270. </p>
  271. </label>
  272. </p>
  273. </div>
  274. <div
  275. v-if="inputs['autofillEnabled'].value"
  276. class="small-section"
  277. >
  278. <label class="label">Song limit</label>
  279. <div class="control is-expanded">
  280. <input
  281. class="input"
  282. type="number"
  283. min="1"
  284. max="50"
  285. v-model="inputs['autofillLimit'].value"
  286. />
  287. </div>
  288. </div>
  289. <div
  290. v-if="inputs['autofillEnabled'].value"
  291. class="small-section"
  292. >
  293. <label class="label">Play mode</label>
  294. <div class="control is-expanded select">
  295. <select v-model="inputs['autofillMode'].value">
  296. <option value="random" selected>Random</option>
  297. <option value="sequential">Sequential</option>
  298. </select>
  299. </div>
  300. </div>
  301. </div>
  302. </div>
  303. <button class="control is-expanded button is-primary" @click="save()">
  304. Save Changes
  305. </button>
  306. </div>
  307. </template>
  308. <style lang="less" scoped>
  309. .night-mode {
  310. .requests-settings,
  311. .autofill-settings {
  312. background-color: var(--dark-grey-2) !important;
  313. }
  314. }
  315. .station-settings {
  316. .settings-buttons {
  317. display: flex;
  318. justify-content: center;
  319. flex-wrap: wrap;
  320. .small-section {
  321. width: calc(50% - 10px);
  322. min-width: 150px;
  323. margin: 5px auto;
  324. &:nth-child(odd) {
  325. margin-left: 0;
  326. }
  327. &:nth-child(even) {
  328. margin-right: 0;
  329. }
  330. }
  331. }
  332. .requests-settings,
  333. .autofill-settings {
  334. display: flex;
  335. flex-wrap: wrap;
  336. width: 100%;
  337. margin: 10px 0;
  338. padding: 10px;
  339. border-radius: @border-radius;
  340. box-shadow: @box-shadow;
  341. .toggle-row {
  342. display: flex;
  343. width: 100%;
  344. line-height: 36px;
  345. .label {
  346. font-size: 18px;
  347. margin: 0;
  348. }
  349. }
  350. .label {
  351. display: flex;
  352. flex-grow: 1;
  353. }
  354. .checkbox-control {
  355. justify-content: end;
  356. }
  357. .small-section {
  358. &:nth-child(even) {
  359. margin-left: 0;
  360. margin-right: auto;
  361. }
  362. &:nth-child(odd) {
  363. margin-left: auto;
  364. margin-right: 0;
  365. }
  366. }
  367. }
  368. }
  369. </style>