ViewPunishment.vue 1.8 KB

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