updated InfoModal.vue + infoState.ts
This commit is contained in:
@@ -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";
|
||||||
|
|
||||||
export default {
|
const infoStateStore = useInfoStateStore();
|
||||||
name: "InfoModal",
|
|
||||||
setup() {
|
|
||||||
const store = useStore();
|
|
||||||
const infos = reactive({
|
|
||||||
data: [],
|
|
||||||
});
|
|
||||||
|
|
||||||
const closeModal = (id) => {
|
const infos = reactive({
|
||||||
store.commit("removeInfoState", id);
|
data: [] as infoState[],
|
||||||
};
|
count: 0,
|
||||||
|
});
|
||||||
|
|
||||||
onMounted(() => {
|
const closeModal = (id: number) => {
|
||||||
store.subscribe((mutation, state) => {
|
infoStateStore.removeInfo(id);
|
||||||
if (mutation.type === "changeInfoState") {
|
|
||||||
infos.data = state.info;
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
closeModal(store.state.info.length - 1);
|
|
||||||
}, 5000);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
return { infos, closeModal };
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
infos.count = infoStateStore.infoCount;
|
||||||
|
|
||||||
|
infoStateStore.$subscribe((mutation, state) => {
|
||||||
|
if (state.infoCount !== infos.count) {
|
||||||
|
infos.data = state.infoState;
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
closeModal(infoStateStore.infoState.length - 1);
|
||||||
|
}, 5000);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
<style lang="scss" scoped>
|
||||||
|
@@ -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: {},
|
||||||
|
Reference in New Issue
Block a user