Skip to content

Commit 284a4c6

Browse files
committed
Use store for preserv facetsOpen status
1 parent d639965 commit 284a4c6

File tree

2 files changed

+17
-40
lines changed

2 files changed

+17
-40
lines changed

src/components/Facets.vue

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup lang="ts">
22
import { storeToRefs } from 'pinia'
3-
import { computed, ref, onMounted, onUnmounted, watch } from 'vue'
3+
import { computed, ref, onMounted, onUnmounted} from 'vue'
44
import { useFacetStore } from '../stores/facets'
55
import { useSessionStore } from '../stores/session'
66
import { useSearchStore } from '../stores/search'
@@ -16,41 +16,12 @@ const { facets } = storeToRefs(useFacetStore())
1616
const { isAdmin } = storeToRefs(useSessionStore())
1717
1818
const search = useSearchStore()
19-
20-
const isMobile = ref(window.innerWidth <= 700)
21-
const savedStatus = loadFacetsOpen()
22-
const facetsOpen = ref(
23-
savedStatus !== null ? savedStatus : !isMobile.value // first time or localStorage not availabe: mobile close, desktop open
24-
)
25-
26-
watch(facetsOpen, (val) => {
27-
saveFacetsOpen(val)
28-
})
29-
30-
function saveFacetsOpen(value: boolean) {
31-
try {
32-
localStorage.setItem('facetsOpen', String(value))
33-
} catch (e) {
34-
console.warn('Could not save facetsOpen to localStorage.', e)
35-
}
36-
}
37-
38-
function loadFacetsOpen(): boolean | null {
39-
try {
40-
const status = localStorage.getItem('facetsOpen')
41-
return status !== null ? status === 'true' : null
42-
} catch (e) {
43-
console.warn('Could not read facetsOpen from localStorage', e)
44-
return null
45-
}
46-
}
47-
48-
function toggleFacets() {
49-
facetsOpen.value = !facetsOpen.value
50-
}
19+
const { facetsOpen } = storeToRefs(useFacetStore())
20+
const { toggleFacets } = useFacetStore()
21+
const isMobile = ref(typeof window !== 'undefined' && window.innerWidth <= 700)
5122
5223
function handleWindowResize() {
53-
isMobile.value = window.innerWidth <= 700
24+
isMobile.value = typeof window !== 'undefined' && window.innerWidth <= 700
5425
}
5526
5627
onMounted(() => {

src/stores/facets.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@ export const useFacetStore = defineStore('facets', () => {
1111

1212
const facets: Ref<Facet[]> = ref([])
1313
const facetExpanded: Ref<{ [key: string]: boolean | undefined }> = ref({})
14-
const computedExpansionState: {[key: string]: WritableComputedRef<boolean> } = {}
14+
const computedExpansionState: { [key: string]: WritableComputedRef<boolean> } = {}
15+
const isMobile = typeof window !== 'undefined' && window.innerWidth <= 700
16+
const facetsOpen = ref(!isMobile)
1517

1618
// ------------------------------
1719
// Computed properties
1820

1921
const facetNames = computed(() => { return facets.value.map(f => f.name) })
2022

21-
function expanded (facetName: string) {
23+
function toggleFacets() {
24+
facetsOpen.value = !facetsOpen.value
25+
}
26+
27+
function expanded(facetName: string) {
2228
let expansionState = computedExpansionState[facetName]
2329
if (!expansionState) {
2430
expansionState = computed({
@@ -37,24 +43,24 @@ export const useFacetStore = defineStore('facets', () => {
3743
// ------------------------------
3844
// Actions
3945

40-
function expandAll (expandedNames = facetNames.value) {
46+
function expandAll(expandedNames = facetNames.value) {
4147
const expanded = { ...facetExpanded.value }
4248
for (const facetName of expandedNames) {
4349
expanded[facetName] = true
4450
}
4551
facetExpanded.value = expanded
4652
}
4753

48-
function collapseAll () {
54+
function collapseAll() {
4955
facetExpanded.value = {}
5056
}
5157

52-
function facetForName (name: string): Facet | undefined {
58+
function facetForName(name: string): Facet | undefined {
5359
return facets.value.find((f) => f.name === name)
5460
}
5561

5662
// ------------------------------
5763
// Store
5864

59-
return { facets, expanded, facetNames, facetForName, expandAll, collapseAll }
65+
return { facets, expanded, facetNames, facetForName, expandAll, collapseAll, facetsOpen, toggleFacets }
6066
})

0 commit comments

Comments
 (0)