Skip to content

Commit 8bfa91e

Browse files
committed
upd: main store use
1 parent ba9b924 commit 8bfa91e

File tree

3 files changed

+65
-48
lines changed

3 files changed

+65
-48
lines changed

src/layouts/LayoutAuthenticated.vue

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { ref } from 'vue'
44
import { useRouter } from 'vue-router'
55
import menuAside from '@/menuAside.js'
66
import menuNavBar from '@/menuNavBar.js'
7-
import { useMainStore } from '@/stores/main.js'
87
import { useStyleStore } from '@/stores/style.js'
98
import BaseIcon from '@/components/BaseIcon.vue'
109
import FormControl from '@/components/FormControl.vue'
@@ -13,13 +12,6 @@ import NavBarItemPlain from '@/components/NavBarItemPlain.vue'
1312
import AsideMenu from '@/components/AsideMenu.vue'
1413
import FooterBar from '@/components/FooterBar.vue'
1514
16-
useMainStore().setUser({
17-
name: 'John Doe',
18-
19-
avatar:
20-
'https://avatars.dicebear.com/api/avataaars/example.svg?options[top][]=shortHair&options[accessoriesChance]=93'
21-
})
22-
2315
const layoutAsidePadding = 'xl:pl-60'
2416
2517
const styleStore = useStyleStore()

src/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ const mainStore = useMainStore(pinia)
2020
const styleStore = useStyleStore(pinia)
2121

2222
/* Fetch sample data */
23-
mainStore.fetch('clients')
24-
mainStore.fetch('history')
23+
mainStore.fetchSampleClients()
24+
mainStore.fetchSampleHistory()
2525

2626
/* App style */
2727
styleStore.setStyle(localStorage[styleKey] ?? 'basic')

src/stores/main.js

Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,69 @@
11
import { defineStore } from 'pinia'
2+
import { ref } from 'vue'
23
import axios from 'axios'
34

4-
export const useMainStore = defineStore('main', {
5-
state: () => ({
6-
/* User */
7-
userName: null,
8-
userEmail: null,
9-
userAvatar: null,
10-
11-
/* Field focus with ctrl+k (to register only once) */
12-
isFieldFocusRegistered: false,
13-
14-
/* Sample data (commonly used) */
15-
clients: [],
16-
history: []
17-
}),
18-
actions: {
19-
setUser(payload) {
20-
if (payload.name) {
21-
this.userName = payload.name
22-
}
23-
if (payload.email) {
24-
this.userEmail = payload.email
25-
}
26-
if (payload.avatar) {
27-
this.userAvatar = payload.avatar
28-
}
29-
},
30-
31-
fetch(sampleDataKey) {
32-
axios
33-
.get(`data-sources/${sampleDataKey}.json`)
34-
.then((r) => {
35-
if (r.data && r.data.data) {
36-
this[sampleDataKey] = r.data.data
37-
}
38-
})
39-
.catch((error) => {
40-
alert(error.message)
41-
})
5+
export const useMainStore = defineStore('main', () => {
6+
const userName = ref('john doe')
7+
const userEmail = ref('[email protected]')
8+
const userAvatar = ref('https://avatars.dicebear.com/api/avataaars/example.svg')
9+
10+
// ... or you can use computed:
11+
// const userAvatar = computed(
12+
// () =>
13+
// `https://avatars.dicebear.com/api/avataaars/${userName.value.replace(
14+
// /[^a-z0-9]+/i,
15+
// ''
16+
// )}.svg`
17+
// )
18+
19+
const isFieldFocusRegistered = ref(false)
20+
21+
const clients = ref([])
22+
const history = ref([])
23+
24+
function setUser(payload) {
25+
if (payload.name) {
26+
userName.value = payload.name
27+
}
28+
if (payload.email) {
29+
userEmail.value = payload.email
30+
}
31+
if (payload.avatar) {
32+
userAvatar.value = payload.avatar
4233
}
4334
}
35+
36+
function fetchSampleClients() {
37+
axios
38+
.get(`data-sources/clients.json`)
39+
.then((result) => {
40+
clients.value = result?.data?.data
41+
})
42+
.catch((error) => {
43+
alert(error.message)
44+
})
45+
}
46+
47+
function fetchSampleHistory() {
48+
axios
49+
.get(`data-sources/history.json`)
50+
.then((result) => {
51+
history.value = result?.data?.data
52+
})
53+
.catch((error) => {
54+
alert(error.message)
55+
})
56+
}
57+
58+
return {
59+
userName,
60+
userEmail,
61+
userAvatar,
62+
isFieldFocusRegistered,
63+
clients,
64+
history,
65+
setUser,
66+
fetchSampleClients,
67+
fetchSampleHistory
68+
}
4469
})

0 commit comments

Comments
 (0)