Browse Source

refactor: Converted viewPunishment Vuex store to Pinia store

Owen Diffey 2 years ago
parent
commit
5e5bf29e58

+ 9 - 20
frontend/src/components/modals/ViewPunishment.vue

@@ -2,8 +2,9 @@
 import { useStore } from "vuex";
 import { defineAsyncComponent, onMounted, onBeforeUnmount } from "vue";
 import Toast from "toasters";
+import { storeToRefs } from "pinia";
 import { useWebsocketsStore } from "@/stores/websockets";
-import { useModalState, useModalActions } from "@/vuex_helpers";
+import { useViewPunishmentStore } from "@/stores/viewPunishment";
 import ws from "@/ws";
 
 const PunishmentItem = defineAsyncComponent(
@@ -18,29 +19,17 @@ const store = useStore();
 
 const { socket } = useWebsocketsStore();
 
-const { punishmentId, punishment } = useModalState(
-	"modals/viewPunishment/MODAL_UUID",
-	{
-		modalUuid: props.modalUuid
-	}
-);
-
-const { viewPunishment } = useModalActions(
-	"modals/viewPunishment/MODAL_UUID",
-	["viewPunishment"],
-	{
-		modalUuid: props.modalUuid
-	}
-);
+const viewPunishmentStore = useViewPunishmentStore(props);
+const { punishmentId, punishment } = storeToRefs(viewPunishmentStore);
+const { viewPunishment } = viewPunishmentStore;
 
 const closeCurrentModal = () =>
 	store.dispatch("modalVisibility/closeCurrentModal");
 
 const init = () => {
-	socket.dispatch(`punishments.findOne`, punishmentId, res => {
+	socket.dispatch(`punishments.findOne`, punishmentId.value, res => {
 		if (res.status === "success") {
-			const { punishment } = res.data;
-			viewPunishment(punishment);
+			viewPunishment(res.data.punishment);
 		} else {
 			new Toast("Punishment with that ID not found");
 			closeCurrentModal();
@@ -53,8 +42,8 @@ onMounted(() => {
 });
 
 onBeforeUnmount(() => {
-	// Delete the VueX module that was created for this modal, after all other cleanup tasks are performed
-	store.unregisterModule(["modals", "viewPunishment", props.modalUuid]);
+	// Delete the Pinia store that was created for this modal, after all other cleanup tasks are performed
+	viewPunishmentStore.$dispose();
 });
 </script>
 

+ 0 - 1
frontend/src/store/index.ts

@@ -14,7 +14,6 @@ export default createStore({
 			namespaced: true,
 			modules: {
 				whatIsNew: emptyModule,
-				viewPunishment: emptyModule,
 				viewReport: emptyModule,
 				viewYoutubeVideo: emptyModule
 			}

+ 6 - 3
frontend/src/store/modules/modalVisibility.ts

@@ -3,7 +3,6 @@ import ws from "@/ws";
 
 import whatIsNew from "./modals/whatIsNew";
 import viewReport from "./modals/viewReport";
-import viewPunishment from "./modals/viewPunishment";
 import viewYoutubeVideo from "./modals/viewYoutubeVideo";
 
 import { useEditUserStore } from "@/stores/editUser";
@@ -19,6 +18,7 @@ import { useManageStationStore } from "@/stores/manageStation";
 import { useRemoveAccountStore } from "@/stores/removeAccount";
 import { useReportStore } from "@/stores/report";
 import { useViewApiRequestStore } from "@/stores/viewApiRequest";
+import { useViewPunishmentStore } from "@/stores/viewPunishment";
 
 const state = {
 	modals: {},
@@ -38,13 +38,13 @@ const piniaStores = [
 	"manageStation",
 	"removeAccount",
 	"report",
-	"viewApiRequest"
+	"viewApiRequest",
+	"viewPunishment"
 ];
 
 const modalModules = {
 	whatIsNew,
 	viewReport,
-	viewPunishment,
 	viewYoutubeVideo
 };
 
@@ -146,6 +146,9 @@ const mutations = {
 				case "viewApiRequest":
 					store = useViewApiRequestStore({ modalUuid: uuid });
 					break;
+				case "viewPunishment":
+					store = useViewPunishmentStore({ modalUuid: uuid });
+					break;
 				default:
 					break;
 			}

+ 0 - 23
frontend/src/store/modules/modals/viewPunishment.ts

@@ -1,23 +0,0 @@
-/* eslint no-param-reassign: 0 */
-
-export default {
-	namespaced: true,
-	state: {
-		punishmentId: null,
-		punishment: {}
-	},
-	getters: {},
-	actions: {
-		init: ({ commit }, data) => commit("init", data),
-		viewPunishment: ({ commit }, punishment) =>
-			commit("viewPunishment", punishment)
-	},
-	mutations: {
-		init(state, { punishmentId }) {
-			state.punishmentId = punishmentId;
-		},
-		viewPunishment(state, punishment) {
-			state.punishment = punishment;
-		}
-	}
-};

+ 21 - 0
frontend/src/stores/viewPunishment.ts

@@ -0,0 +1,21 @@
+import { defineStore } from "pinia";
+
+// TODO fix/decide eslint rule properly
+// eslint-disable-next-line
+export const useViewPunishmentStore = props => {
+	const { modalUuid } = props;
+	return defineStore(`viewPunishment-${modalUuid}`, {
+		state: () => ({
+			punishmentId: null,
+			punishment: {}
+		}),
+		actions: {
+			init({ punishmentId }) {
+				this.punishmentId = punishmentId;
+			},
+			viewPunishment(punishment) {
+				this.punishment = punishment;
+			}
+		}
+	})();
+};