diff --git a/store/toast.js b/store/toast.js deleted file mode 100644 index c3cd111..0000000 --- a/store/toast.js +++ /dev/null @@ -1,35 +0,0 @@ -export const state = () => ({ - show: false, - content: "", - theme: "info", - iconHTML: "", - autoDismis: true, - timeout: 5000 -}) -let timeoutId = null; -export const mutations = { - show(state, { - show, - content, - iconHTML, - theme - }) { - if (!show) { - state.show = show; - return; - } - const isAlreadyShowing = state.show; - state.content = content; - state.theme = theme || "info"; - state.iconHTML = iconHTML - state.show = content && show; - if (isAlreadyShowing && state.show && timeoutId) { - clearTimeout(timeoutId); - } - if (state.show) { - timeoutId = setTimeout(() => { - this.commit('toast/show', { show: false }) - }, 5000) - } - }, -} diff --git a/store/toast.ts b/store/toast.ts new file mode 100644 index 0000000..0ba2bd0 --- /dev/null +++ b/store/toast.ts @@ -0,0 +1,45 @@ +import { GetterTree, Store, MutationTree } from 'vuex' + +export const state = () => ({ + show: false, + content: '', + theme: 'info', + iconHTML: '', + autoDismiss: true, + timeout: 5000, +}) +let timeoutId: NodeJS.Timeout + +export type ToastState = ReturnType + +export const getters: GetterTree = { + show: (state) => state.show, + content: (state) => state.content, + theme: (state) => state.theme, + iconHTML: (state) => state.iconHTML, + autoDismiss: (state) => state.autoDismiss, + timeout: (state) => state.timeout, +} + +export const mutations: MutationTree = { + show(state, { show, content, iconHTML, theme }: ToastState) { + if (!show) { + state.show = show + return + } + const isAlreadyShowing = state.show + state.content = content + state.theme = theme || 'info' + state.iconHTML = iconHTML + state.show = !!content && show + if (isAlreadyShowing && state.show && timeoutId) { + clearTimeout(timeoutId) + } + if (state.show) { + timeoutId = setTimeout(() => { + const store = this as unknown as Store + store.commit('toast/show', { show: false }) + }, 5000) + } + }, +}