Skip to content

Commit b45f604

Browse files
committed
refactor(filter): simplify time store model logic and fix next-offset bug
1 parent 226a455 commit b45f604

1 file changed

Lines changed: 56 additions & 118 deletions

File tree

src/plugins/filter/stores/time.ts

Lines changed: 56 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import type { Time } from '../types'
22

33
import { t } from 'i18next'
44
import { acceptHMRUpdate, defineStore } from 'pinia'
5-
import { computed, ref } from 'vue'
5+
import { computed, ref, watch } from 'vue'
66

77
import { useCoreStore } from '@/core/stores'
88

99
import { PluginId } from '../types'
1010
import { useFilterMainStore } from './main'
1111

12+
type TimeModel = 'all' | 'custom' | `last-${string}` | `next-${string}`
13+
1214
export const useFilterTimeStore = defineStore('plugins/filter/time', () => {
1315
const minDate = new Date(-8640000000000000)
1416
const maxDate = new Date(8640000000000000)
@@ -19,142 +21,78 @@ export const useFilterTimeStore = defineStore('plugins/filter/time', () => {
1921
const configuration = computed(
2022
() => filterMainStore.selectedLayerConfiguration.time || ({} as Time)
2123
)
22-
const state = computed(
23-
() => filterMainStore.selectedLayerState?.timeSpan || null
24-
)
2524

2625
const targetProperty = computed(
2726
() => configuration.value.targetProperty || ''
2827
)
2928
const pattern = computed(() => configuration.value.pattern || 'YYYY-MM-DD')
30-
const timeState = computed(
31-
() =>
32-
state.value?.[targetProperty.value] ?? {
33-
from: minDate,
34-
until: maxDate,
35-
pattern: pattern.value,
36-
}
29+
30+
const model = ref<TimeModel>('all')
31+
const customModelStart = ref<Date | null>(null)
32+
const customModelEnd = ref<Date | null>(null)
33+
34+
/** Reset time filter selection when the active layer changes. */
35+
watch(
36+
() => filterMainStore.selectedLayerId,
37+
() => {
38+
model.value = 'all'
39+
customModelStart.value = null
40+
customModelEnd.value = null
41+
}
3742
)
3843

39-
const customModelStart = computed({
40-
get: () =>
41-
timeState.value.from.getTime() === minDate.getTime()
42-
? null
43-
: timeState.value.from,
44-
set: (value) => {
45-
if (!filterMainStore.selectedLayerState) {
46-
return
47-
}
44+
watch(
45+
[model, customModelStart, customModelEnd],
46+
([value, start, end]) => {
4847
const layerState = filterMainStore.selectedLayerState
49-
const spanState = (layerState.timeSpan ??= {})
50-
const propState = (spanState[targetProperty.value] ??= {
51-
from: minDate,
52-
until: maxDate,
53-
pattern: pattern.value,
54-
})
55-
propState.from = value || minDate
56-
},
57-
})
58-
const customModelEnd = computed({
59-
get: () => {
60-
if (timeState.value.until.getTime() === maxDate.getTime()) {
61-
return null
62-
}
63-
const value = new Date(timeState.value.until)
64-
value.setDate(value.getDate() - 1)
65-
return value
66-
},
67-
set: (value) => {
68-
if (!filterMainStore.selectedLayerState) {
48+
if (!layerState || !targetProperty.value) {
6949
return
7050
}
71-
const layerState = filterMainStore.selectedLayerState
72-
const spanState = (layerState.timeSpan ??= {})
73-
const propState = (spanState[targetProperty.value] ??= {
74-
from: minDate,
75-
until: maxDate,
76-
pattern: pattern.value,
77-
})
78-
if (value === null) {
79-
propState.until = maxDate
80-
return
81-
}
82-
const newValue = new Date(value)
83-
newValue.setDate(newValue.getDate() + 1)
84-
propState.until = newValue
85-
},
86-
})
51+
layerState.timeSpan ??= {}
8752

88-
function checkDate(offset: number, date: Date) {
89-
const referenceDate = new Date()
90-
referenceDate.setDate(referenceDate.getDate() + offset)
91-
return date.toDateString() === referenceDate.toDateString()
92-
}
53+
const now = new Date()
54+
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate())
55+
let from: Date
56+
let until: Date
9357

94-
const isCustom = ref(false)
95-
const model = computed<
96-
'all' | 'custom' | `last-${string}` | `next-${string}`
97-
>({
98-
get: () => {
99-
if (isCustom.value) {
100-
return 'custom'
101-
}
102-
if (customModelStart.value === null && customModelEnd.value === null) {
103-
return 'all'
104-
}
105-
if (customModelStart.value && customModelEnd.value) {
106-
for (const offset of filterMainStore.selectedLayerConfiguration.time
107-
?.last || []) {
108-
if (
109-
checkDate(0, customModelEnd.value) &&
110-
checkDate(-offset, customModelStart.value)
111-
) {
112-
return `last-${offset}` as `last-${string}`
113-
}
114-
}
115-
for (const offset of filterMainStore.selectedLayerConfiguration.time
116-
?.next || []) {
117-
if (
118-
checkDate(0, customModelStart.value) &&
119-
checkDate(offset, customModelEnd.value)
120-
) {
121-
return `next-${offset}` as `next-${string}`
122-
}
123-
}
124-
}
125-
return 'custom'
126-
},
127-
set: (value) => {
128-
if (value === 'all' || value === 'custom') {
129-
customModelStart.value = null
130-
customModelEnd.value = null
58+
if (value === 'all') {
59+
from = minDate
60+
until = maxDate
61+
} else if (value === 'custom') {
62+
from = start || minDate
63+
until = end
64+
? new Date(end.getFullYear(), end.getMonth(), end.getDate() + 1)
65+
: maxDate
13166
} else if (value.startsWith('last-')) {
13267
const offset = Number(value.substring(5))
133-
const now = new Date()
134-
const from = new Date(
135-
now.getFullYear(),
136-
now.getMonth(),
137-
now.getDate() - offset
68+
from = new Date(
69+
today.getFullYear(),
70+
today.getMonth(),
71+
today.getDate() - offset
72+
)
73+
until = new Date(
74+
today.getFullYear(),
75+
today.getMonth(),
76+
today.getDate() + 1
13877
)
139-
const until = new Date(now.getFullYear(), now.getMonth(), now.getDate())
140-
customModelStart.value = from
141-
customModelEnd.value = until
142-
} else if (value.startsWith('next-')) {
78+
} else {
14379
const offset = Number(value.substring(5))
144-
const now = new Date()
145-
const from = new Date(now.getFullYear(), now.getMonth(), now.getDate())
146-
const until = new Date(
147-
now.getFullYear(),
148-
now.getMonth(),
149-
now.getDate() + offset
80+
from = today
81+
until = new Date(
82+
today.getFullYear(),
83+
today.getMonth(),
84+
today.getDate() + offset + 1
15085
)
151-
until.setDate(until.getDate() + offset)
152-
customModelStart.value = from
153-
customModelEnd.value = until
15486
}
155-
isCustom.value = value === 'custom'
87+
88+
layerState.timeSpan[targetProperty.value] = {
89+
from,
90+
until,
91+
pattern: pattern.value,
92+
}
15693
},
157-
})
94+
{ immediate: true }
95+
)
15896

15997
const timeConstraints = computed(
16098
() =>

0 commit comments

Comments
 (0)