Skip to content

Commit 7c5d59c

Browse files
committed
refactor: migrate from Vuex to Pinia
1 parent 43745d9 commit 7c5d59c

File tree

8 files changed

+56
-51
lines changed

8 files changed

+56
-51
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
"@coreui/vue-chartjs": "^3.0.0",
2828
"@popperjs/core": "^2.11.8",
2929
"chart.js": "^4.4.2",
30+
"pinia": "^2.1.7",
3031
"simplebar-vue": "^2.3.3",
3132
"vue": "^3.4.21",
32-
"vue-router": "^4.3.0",
33-
"vuex": "^4.1.0"
33+
"vue-router": "^4.3.0"
3434
},
3535
"devDependencies": {
3636
"@vitejs/plugin-vue": "^5.0.4",

src/App.vue

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
<script setup>
22
import { onBeforeMount } from 'vue'
3-
import { useStore } from 'vuex'
43
import { useColorModes } from '@coreui/vue'
54
6-
const { isColorModeSet, setColorMode } = useColorModes('coreui-free-vue-admin-template-theme')
7-
const store = useStore()
5+
import { useThemeStore } from '@/stores/theme.js'
6+
7+
const { isColorModeSet, setColorMode } = useColorModes(
8+
'coreui-free-vue-admin-template-theme',
9+
)
10+
const currentTheme = useThemeStore()
811
912
onBeforeMount(() => {
1013
const urlParams = new URLSearchParams(window.location.href.split('?')[1])
@@ -23,7 +26,7 @@ onBeforeMount(() => {
2326
return
2427
}
2528
26-
setColorMode(store.state.theme)
29+
setColorMode(currentTheme.theme)
2730
})
2831
</script>
2932

src/components/AppHeader.vue

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
<script setup>
22
import { onMounted, ref } from 'vue'
33
import { useColorModes } from '@coreui/vue'
4-
import AppBreadcrumb from './AppBreadcrumb'
5-
import AppHeaderDropdownAccnt from './AppHeaderDropdownAccnt'
4+
5+
import AppBreadcrumb from '@/components/AppBreadcrumb.vue'
6+
import AppHeaderDropdownAccnt from '@/components/AppHeaderDropdownAccnt.vue'
7+
import { useSidebarStore } from '@/stores/sidebar.js'
68
79
const headerClassNames = ref('mb-4 p-0')
810
const { colorMode, setColorMode } = useColorModes('coreui-free-vue-admin-template-theme')
11+
const sidebar = useSidebarStore()
912
1013
onMounted(() => {
1114
document.addEventListener('scroll', () => {
@@ -21,7 +24,7 @@ onMounted(() => {
2124
<template>
2225
<CHeader position="sticky" :class="headerClassNames">
2326
<CContainer class="border-bottom px-4" fluid>
24-
<CHeaderToggler @click="$store.commit('toggleSidebar')" style="margin-inline-start: -14px">
27+
<CHeaderToggler @click="sidebar.toggleVisible()" style="margin-inline-start: -14px">
2528
<CIcon icon="cil-menu" size="lg" />
2629
</CHeaderToggler>
2730
<CHeaderNav class="d-none d-md-flex">

src/components/AppSidebar.vue

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
1-
21
<script setup>
3-
import { computed } from 'vue'
42
import { RouterLink } from 'vue-router'
5-
import { useStore } from 'vuex'
6-
import { AppSidebarNav } from './AppSidebarNav'
3+
74
import { logo } from '@/assets/brand/logo'
85
import { sygnet } from '@/assets/brand/sygnet'
6+
import { AppSidebarNav } from '@/components/AppSidebarNav.js'
7+
import { useSidebarStore } from '@/stores/sidebar.js'
98
10-
const store = useStore()
11-
const sidebarUnfoldable = computed(() => store.state.sidebarUnfoldable)
12-
const sidebarVisible = computed(() => store.state.sidebarVisible)
9+
const sidebar = useSidebarStore()
1310
</script>
1411

1512
<template>
1613
<CSidebar
1714
class="border-end"
1815
colorScheme="dark"
1916
position="fixed"
20-
:unfoldable="sidebarUnfoldable"
21-
:visible="sidebarVisible"
22-
@visible-change="
23-
(event) =>
24-
$store.commit({
25-
type: 'updateSidebarVisible',
26-
value: event,
27-
})
28-
"
17+
:unfoldable="sidebar.unfoldable"
18+
:visible="sidebar.visible"
19+
@visible-change="(value) => sidebar.toggleVisible(value)"
2920
>
3021
<CSidebarHeader class="border-bottom">
3122
<RouterLink custom to="/" v-slot="{ href, navigate }">
@@ -34,11 +25,11 @@ const sidebarVisible = computed(() => store.state.sidebarVisible)
3425
<CIcon custom-class-name="sidebar-brand-narrow" :icon="sygnet" :height="32" />
3526
</CSidebarBrand>
3627
</RouterLink>
37-
<CCloseButton class="d-lg-none" dark @click="$store.commit('toggleSidebar')" />
28+
<CCloseButton class="d-lg-none" dark @click="sidebar.toggleVisible()" />
3829
</CSidebarHeader>
3930
<AppSidebarNav />
4031
<CSidebarFooter class="border-top d-none d-lg-flex">
41-
<CSidebarToggler @click="$store.commit('toggleUnfoldable')" />
32+
<CSidebarToggler @click="sidebar.toggleUnfoldable()" />
4233
</CSidebarFooter>
4334
</CSidebar>
4435
</template>

src/main.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { createApp } from 'vue'
2+
import { createPinia } from 'pinia'
3+
24
import App from './App.vue'
35
import router from './router'
4-
import store from './store'
56

67
import CoreuiVue from '@coreui/vue'
78
import CIcon from '@coreui/icons-vue'
89
import { iconsSet as icons } from '@/assets/icons'
910
import DocsExample from '@/components/DocsExample'
1011

1112
const app = createApp(App)
12-
app.use(store)
13+
app.use(createPinia())
1314
app.use(router)
1415
app.use(CoreuiVue)
1516
app.provide('icons', icons)

src/store/index.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/stores/sidebar.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { ref } from 'vue'
2+
import { defineStore } from 'pinia'
3+
4+
export const useSidebarStore = defineStore('sidebar', () => {
5+
const visible = ref(undefined)
6+
const unfoldable = ref(false)
7+
8+
const toggleVisible = (value) => {
9+
visible.value = value !== undefined ? value : !visible.value
10+
}
11+
12+
const toggleUnfoldable = () => {
13+
unfoldable.value = !unfoldable.value
14+
}
15+
16+
return { visible, unfoldable, toggleVisible, toggleUnfoldable }
17+
})

src/stores/theme.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ref } from 'vue'
2+
import { defineStore } from 'pinia'
3+
4+
export const useThemeStore = defineStore('theme', () => {
5+
const theme = ref('light')
6+
7+
const toggleTheme = (_theme) => {
8+
theme.value = _theme
9+
}
10+
11+
return { theme, toggleTheme }
12+
})

0 commit comments

Comments
 (0)