From 9b7f404a4344a29ba95a6b0e102ace391714fe69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Akg=C3=BCn?= Date: Fri, 3 Nov 2023 12:56:12 +0100 Subject: [PATCH] Setup pinia --- resources/js/Pages/Home.vue | 17 +++++---- resources/js/Pages/Results.vue | 10 ++--- resources/js/app.ts | 5 ++- resources/js/store/index.ts | 69 ++++++++++++++++++---------------- 4 files changed, 53 insertions(+), 48 deletions(-) diff --git a/resources/js/Pages/Home.vue b/resources/js/Pages/Home.vue index c1ea3906f..f62985d82 100644 --- a/resources/js/Pages/Home.vue +++ b/resources/js/Pages/Home.vue @@ -107,8 +107,9 @@ diff --git a/resources/js/app.ts b/resources/js/app.ts index fa562b4c0..2861e19eb 100644 --- a/resources/js/app.ts +++ b/resources/js/app.ts @@ -1,6 +1,6 @@ import './bootstrap'; -import { createStore } from './store'; import {createApp, h} from 'vue'; +import {createPinia} from 'pinia'; import {createInertiaApp} from '@inertiajs/inertia-vue3'; import i18nMessages, { I18nMessages } from './lib/i18n'; import {createI18n} from 'vue-banana-i18n' @@ -19,9 +19,9 @@ async function setupI18n(locale: string): Promise{ // component otherwise (async () => { try { - const store = createStore(); const locale = document.documentElement.lang; const i18nMessages = await setupI18n(locale); + const pinia = createPinia(); const i18nPlugin = createI18n({ locale: locale, messages: i18nMessages @@ -39,6 +39,7 @@ async function setupI18n(locale: string): Promise{ render: () => h(app, props) }) .use(i18nPlugin) + .use(pinia) .use(plugin) .mount(el) } diff --git a/resources/js/store/index.ts b/resources/js/store/index.ts index aededeecb..7d76ac442 100644 --- a/resources/js/store/index.ts +++ b/resources/js/store/index.ts @@ -1,41 +1,44 @@ -import Vue from 'vue'; -import Vuex, {Store} from 'vuex'; +import { defineStore } from 'pinia'; import RootState from './RootState'; import { Inertia } from '@inertiajs/inertia'; -import mutations from './mutations'; -export function getInitialState(): RootState { - return { - loading: false, - lastSearchedIds: '' - } -} - -export function createStore(): Store{ - Vue.use(Vuex); - - const store = new Store({ - state: getInitialState(), - mutations - }); +export const useStore = defineStore('storeId', { + state: () : RootState => { + return { + loading: false, + lastSearchedIds: '' + } + }, + actions: { + startLoader () { + this.loading = true; + }, + stopLoader () { + this.loading = false; + }, + saveSearchedIds (searchedIds: string) { + this.lastSearchedIds = searchedIds; + } + }, - let timer: ReturnType; +}); - Inertia.on('start', () => { - // Only instantiate loading state after 250ms. This is done to - // prevent a flash of the loader in case loading is nearly - // immediate, which can be visually distracting. - timer = setTimeout(() => store.commit('startLoader'), 250); - }); +let timer: ReturnType; - Inertia.on('finish', (event) => { - clearTimeout(timer); - const status = event.detail.visit; +Inertia.on('start', () => { + // Only instantiate loading state after 250ms. This is done to + // prevent a flash of the loader in case loading is nearly + // immediate, which can be visually distracting. + const store = useStore(); + timer = setTimeout(() => store.startLoader(), 250); +}); - if (status.completed || status.cancelled) { - store.commit('stopLoader'); - } - }); +Inertia.on('finish', (event) => { + clearTimeout(timer); + const status = event.detail.visit; - return store; -} \ No newline at end of file + if (status.completed || status.cancelled) { + const store = useStore(); + store.stopLoader(); + } +});