updated InfoModal.vue + infoState.ts
This commit is contained in:
@@ -27,37 +27,35 @@
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { useStore } from "vuex";
|
||||
<script setup lang="ts">
|
||||
import { onMounted, reactive } from "vue";
|
||||
import type { infoState } from "@/stores/infoState";
|
||||
import { useInfoStateStore } from "@/stores/infoState";
|
||||
|
||||
export default {
|
||||
name: "InfoModal",
|
||||
setup() {
|
||||
const store = useStore();
|
||||
const infos = reactive({
|
||||
data: [],
|
||||
});
|
||||
const infoStateStore = useInfoStateStore();
|
||||
|
||||
const closeModal = (id) => {
|
||||
store.commit("removeInfoState", id);
|
||||
};
|
||||
const infos = reactive({
|
||||
data: [] as infoState[],
|
||||
count: 0,
|
||||
});
|
||||
|
||||
onMounted(() => {
|
||||
store.subscribe((mutation, state) => {
|
||||
if (mutation.type === "changeInfoState") {
|
||||
infos.data = state.info;
|
||||
|
||||
setTimeout(() => {
|
||||
closeModal(store.state.info.length - 1);
|
||||
}, 5000);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return { infos, closeModal };
|
||||
},
|
||||
const closeModal = (id: number) => {
|
||||
infoStateStore.removeInfo(id);
|
||||
};
|
||||
|
||||
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>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
|
@@ -8,6 +8,7 @@ export type infoState = {
|
||||
|
||||
export type RootState = {
|
||||
infoState: infoState[];
|
||||
infoCount: number;
|
||||
};
|
||||
|
||||
export const useInfoStateStore = defineStore({
|
||||
@@ -15,13 +16,16 @@ export const useInfoStateStore = defineStore({
|
||||
state: () =>
|
||||
({
|
||||
infoState: [],
|
||||
infoCount: 0,
|
||||
} as RootState),
|
||||
actions: {
|
||||
addInfo(payload: infoState) {
|
||||
this.infoState.push(payload);
|
||||
this.infoCount += 1;
|
||||
},
|
||||
removeInfo(id: number) {
|
||||
this.infoState.splice(id, 1);
|
||||
this.infoCount -= 1;
|
||||
},
|
||||
},
|
||||
getters: {},
|
||||
|
Reference in New Issue
Block a user