updated InfoModal.vue + infoState.ts

This commit is contained in:
2022-03-25 20:32:57 +01:00
parent ce70fa2e6f
commit 552188c8a9
2 changed files with 28 additions and 26 deletions

View File

@@ -27,37 +27,35 @@
</div> </div>
</template> </template>
<script> <script setup lang="ts">
import { useStore } from "vuex";
import { onMounted, reactive } from "vue"; import { onMounted, reactive } from "vue";
import type { infoState } from "@/stores/infoState";
import { useInfoStateStore } from "@/stores/infoState";
const infoStateStore = useInfoStateStore();
export default {
name: "InfoModal",
setup() {
const store = useStore();
const infos = reactive({ const infos = reactive({
data: [], data: [] as infoState[],
count: 0,
}); });
const closeModal = (id) => { const closeModal = (id: number) => {
store.commit("removeInfoState", id); infoStateStore.removeInfo(id);
}; };
onMounted(() => { onMounted(() => {
store.subscribe((mutation, state) => { infos.count = infoStateStore.infoCount;
if (mutation.type === "changeInfoState") {
infos.data = state.info; infoStateStore.$subscribe((mutation, state) => {
if (state.infoCount !== infos.count) {
infos.data = state.infoState;
setTimeout(() => { setTimeout(() => {
closeModal(store.state.info.length - 1); closeModal(infoStateStore.infoState.length - 1);
}, 5000); }, 5000);
} }
}); });
}); });
return { infos, closeModal };
},
};
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>

View File

@@ -8,6 +8,7 @@ export type infoState = {
export type RootState = { export type RootState = {
infoState: infoState[]; infoState: infoState[];
infoCount: number;
}; };
export const useInfoStateStore = defineStore({ export const useInfoStateStore = defineStore({
@@ -15,13 +16,16 @@ export const useInfoStateStore = defineStore({
state: () => state: () =>
({ ({
infoState: [], infoState: [],
infoCount: 0,
} as RootState), } as RootState),
actions: { actions: {
addInfo(payload: infoState) { addInfo(payload: infoState) {
this.infoState.push(payload); this.infoState.push(payload);
this.infoCount += 1;
}, },
removeInfo(id: number) { removeInfo(id: number) {
this.infoState.splice(id, 1); this.infoState.splice(id, 1);
this.infoCount -= 1;
}, },
}, },
getters: {}, getters: {},