ViewPunishment.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <script setup lang="ts">
  2. import { defineAsyncComponent, onMounted, onBeforeUnmount } from "vue";
  3. import Toast from "toasters";
  4. import { storeToRefs } from "pinia";
  5. import { useWebsocketsStore } from "@/stores/websockets";
  6. import { useModalsStore } from "@/stores/modals";
  7. import { useViewPunishmentStore } from "@/stores/viewPunishment";
  8. import ws from "@/ws";
  9. const Modal = defineAsyncComponent(() => import("@/components/Modal.vue"));
  10. const PunishmentItem = defineAsyncComponent(
  11. () => import("@/components/PunishmentItem.vue")
  12. );
  13. const props = defineProps({
  14. modalUuid: { type: String, default: "" }
  15. });
  16. const { socket } = useWebsocketsStore();
  17. const viewPunishmentStore = useViewPunishmentStore(props);
  18. const { punishmentId, punishment } = storeToRefs(viewPunishmentStore);
  19. const { viewPunishment } = viewPunishmentStore;
  20. const { closeCurrentModal } = useModalsStore();
  21. const init = () => {
  22. socket.dispatch(`punishments.findOne`, punishmentId.value, res => {
  23. if (res.status === "success") {
  24. viewPunishment(res.data.punishment);
  25. socket.dispatch(
  26. "apis.joinRoom",
  27. `view-punishment.${punishmentId.value}`
  28. );
  29. socket.on(
  30. "event:admin.punishment.updated",
  31. ({ data }) => {
  32. punishment.value = data.punishment;
  33. },
  34. { modalUuid: props.modalUuid }
  35. );
  36. } else {
  37. new Toast("Punishment with that ID not found");
  38. closeCurrentModal();
  39. }
  40. });
  41. };
  42. const deactivatePunishment = event => {
  43. event.preventDefault();
  44. socket.dispatch(
  45. "punishments.deactivatePunishment",
  46. punishmentId.value,
  47. res => {
  48. if (res.status === "success") {
  49. viewPunishmentStore.deactivatePunishment();
  50. } else {
  51. new Toast(res.message);
  52. }
  53. }
  54. );
  55. };
  56. onMounted(() => {
  57. ws.onConnect(init);
  58. });
  59. onBeforeUnmount(() => {
  60. socket.dispatch(
  61. "apis.leaveRoom",
  62. `view-punishment.${punishmentId.value}`,
  63. () => {}
  64. );
  65. // Delete the Pinia store that was created for this modal, after all other cleanup tasks are performed
  66. viewPunishmentStore.$dispose();
  67. });
  68. </script>
  69. <template>
  70. <div>
  71. <modal title="View Punishment">
  72. <template #body v-if="punishment && punishment._id">
  73. <punishment-item
  74. :punishment="punishment"
  75. @deactivate="deactivatePunishment"
  76. />
  77. </template>
  78. </modal>
  79. </div>
  80. </template>