|
1 | 1 | import { defineStore } from 'pinia' |
| 2 | +import { ref } from 'vue' |
2 | 3 | import axios from 'axios' |
3 | 4 |
|
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 |
42 | 33 | } |
43 | 34 | } |
| 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 | + } |
44 | 69 | }) |
0 commit comments