Skip to content

Commit ed734d0

Browse files
committed
Setup pinia
1 parent e80fecf commit ed734d0

File tree

4 files changed

+52
-47
lines changed

4 files changed

+52
-47
lines changed

resources/js/Pages/Home.vue

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,9 @@
107107
</template>
108108

109109
<script lang="ts">
110-
import { mapState, mapMutations } from 'vuex';
111110
import { Head as InertiaHead } from '@inertiajs/inertia-vue';
111+
import { mapState } from 'pinia';
112+
import { useStore } from '../store';
112113
import {
113114
Button as WikitButton,
114115
Dialog as WikitDialog,
@@ -194,14 +195,13 @@
194195
if(this.validationError) {
195196
return;
196197
}
197-
198-
this.saveSearchedIds( this.form.itemsInput );
198+
const store = useStore();
199+
store.saveSearchedIds( this.form.itemsInput );
199200
this.$inertia.get( '/results', { ids: this.serializeInput() } );
200201
},
201202
showRandom(): void {
202203
this.$inertia.get( '/random' );
203204
},
204-
...mapMutations(['saveSearchedIds'])
205205
},
206206
computed: {
207207
serversideValidationError() {
@@ -213,14 +213,15 @@
213213
return (flashMessages.errors && flashMessages.errors.unexpected);
214214
},
215215
// spread to combine with local computed props
216-
// only mapping 'loading' and not 'lastSearchedIds' because computed
216+
// only mapping 'loading' and not 'lastSearchedIds' because computed
217217
//properties are not available when data is processed in vue's lifecycle
218-
...mapState(['loading']),
218+
...mapState(useStore, ['loading']),
219219
},
220220
data(): HomeState {
221+
const store = useStore();
221222
return {
222223
form: {
223-
itemsInput: this.$store.state.lastSearchedIds
224+
itemsInput: store.lastSearchedIds
224225
},
225226
validationError: null
226227
}

resources/js/Pages/Results.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135

136136
<script lang="ts">
137137
import { PropType } from 'vue';
138-
import { mapMutations } from 'vuex';
138+
import { useStore } from '../store';
139139
import isEmpty from 'lodash/isEmpty';
140140
import { Head as InertiaHead } from '@inertiajs/inertia-vue';
141141
import {
@@ -226,8 +226,9 @@
226226
},
227227
},
228228
mounted(){
229-
if(!this.$store.state.lastSearchedIds) {
230-
this.saveSearchedIds( this.item_ids.join('\n') );
229+
const store = useStore();
230+
if(!store.lastSearchedIds) {
231+
store.saveSearchedIds( this.item_ids.join('\n') );
231232
}
232233
233234
this.pageDirection = window.getComputedStyle(document.body).direction;
@@ -371,8 +372,7 @@
371372
}
372373
373374
dialog.hide();
374-
},
375-
...mapMutations(['saveSearchedIds'])
375+
}
376376
}
377377
});
378378
</script>

resources/js/app.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import './bootstrap';
2-
import { createStore } from './store';
32
import {createApp, h} from 'vue';
3+
import {createPinia} from 'pinia';
44
import {createInertiaApp} from '@inertiajs/inertia-vue3';
55
import i18nMessages, { I18nMessages } from './lib/i18n';
66
import {createI18n} from 'vue-banana-i18n'
@@ -19,9 +19,9 @@ async function setupI18n(locale: string): Promise<I18nMessages>{
1919
// component otherwise
2020
(async () => {
2121
try {
22-
const store = createStore();
2322
const locale = document.documentElement.lang;
2423
const i18nMessages = await setupI18n(locale);
24+
const pinia = createPinia();
2525
const i18nPlugin = createI18n({
2626
locale: locale,
2727
messages: i18nMessages
@@ -39,6 +39,7 @@ async function setupI18n(locale: string): Promise<I18nMessages>{
3939
render: () => h(app, props)
4040
})
4141
.use(i18nPlugin)
42+
.use(pinia)
4243
.use(plugin)
4344
.mount(el)
4445
}

resources/js/store/index.ts

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,44 @@
1-
import Vue from 'vue';
2-
import Vuex, {Store} from 'vuex';
1+
import { defineStore } from 'pinia';
32
import RootState from './RootState';
43
import { Inertia } from '@inertiajs/inertia';
5-
import mutations from './mutations';
64

7-
export function getInitialState(): RootState {
8-
return {
9-
loading: false,
10-
lastSearchedIds: ''
11-
}
12-
}
13-
14-
export function createStore(): Store<RootState>{
15-
Vue.use(Vuex);
16-
17-
const store = new Store({
18-
state: getInitialState(),
19-
mutations
20-
});
5+
export const useStore = defineStore('storeId', {
6+
state: () : RootState => {
7+
return {
8+
loading: false,
9+
lastSearchedIds: ''
10+
}
11+
},
12+
actions: {
13+
startLoader () {
14+
this.loading = true;
15+
},
16+
stopLoader () {
17+
this.loading = false;
18+
},
19+
saveSearchedIds (searchedIds: string) {
20+
this.lastSearchedIds = searchedIds;
21+
}
22+
},
2123

22-
let timer: ReturnType<typeof setTimeout>;
24+
});
2325

24-
Inertia.on('start', () => {
25-
// Only instantiate loading state after 250ms. This is done to
26-
// prevent a flash of the loader in case loading is nearly
27-
// immediate, which can be visually distracting.
28-
timer = setTimeout(() => store.commit('startLoader'), 250);
29-
});
26+
let timer: ReturnType<typeof setTimeout>;
3027

31-
Inertia.on('finish', (event) => {
32-
clearTimeout(timer);
33-
const status = event.detail.visit;
28+
Inertia.on('start', () => {
29+
// Only instantiate loading state after 250ms. This is done to
30+
// prevent a flash of the loader in case loading is nearly
31+
// immediate, which can be visually distracting.
32+
const store = useStore();
33+
timer = setTimeout(() => store.startLoader(), 250);
34+
});
3435

35-
if (status.completed || status.cancelled) {
36-
store.commit('stopLoader');
37-
}
38-
});
36+
Inertia.on('finish', (event) => {
37+
clearTimeout(timer);
38+
const status = event.detail.visit;
3939

40-
return store;
41-
}
40+
if (status.completed || status.cancelled) {
41+
const store = useStore();
42+
store.stopLoader();
43+
}
44+
});

0 commit comments

Comments
 (0)