fix(kiwi-a11y): persist constraint and allergy preferences to localStorage (#54)
This commit is contained in:
parent
fdc477b395
commit
91724caf96
1 changed files with 45 additions and 2 deletions
|
|
@ -21,6 +21,35 @@ const BOOKMARKS_MAX = 50
|
||||||
const MISSING_MODE_KEY = 'kiwi:builder_missing_mode'
|
const MISSING_MODE_KEY = 'kiwi:builder_missing_mode'
|
||||||
const FILTER_MODE_KEY = 'kiwi:builder_filter_mode'
|
const FILTER_MODE_KEY = 'kiwi:builder_filter_mode'
|
||||||
|
|
||||||
|
const CONSTRAINTS_KEY = 'kiwi:constraints'
|
||||||
|
const ALLERGIES_KEY = 'kiwi:allergies'
|
||||||
|
|
||||||
|
function loadConstraints(): string[] {
|
||||||
|
try {
|
||||||
|
const raw = localStorage.getItem(CONSTRAINTS_KEY)
|
||||||
|
return raw ? JSON.parse(raw) : []
|
||||||
|
} catch {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveConstraints(vals: string[]) {
|
||||||
|
localStorage.setItem(CONSTRAINTS_KEY, JSON.stringify(vals))
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadAllergies(): string[] {
|
||||||
|
try {
|
||||||
|
const raw = localStorage.getItem(ALLERGIES_KEY)
|
||||||
|
return raw ? JSON.parse(raw) : []
|
||||||
|
} catch {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveAllergies(vals: string[]) {
|
||||||
|
localStorage.setItem(ALLERGIES_KEY, JSON.stringify(vals))
|
||||||
|
}
|
||||||
|
|
||||||
type MissingIngredientMode = 'hidden' | 'greyed' | 'add-to-cart'
|
type MissingIngredientMode = 'hidden' | 'greyed' | 'add-to-cart'
|
||||||
type BuilderFilterMode = 'text' | 'tags'
|
type BuilderFilterMode = 'text' | 'tags'
|
||||||
|
|
||||||
|
|
@ -95,8 +124,8 @@ export const useRecipesStore = defineStore('recipes', () => {
|
||||||
|
|
||||||
// Request parameters
|
// Request parameters
|
||||||
const level = ref(1)
|
const level = ref(1)
|
||||||
const constraints = ref<string[]>([])
|
const constraints = ref<string[]>(loadConstraints())
|
||||||
const allergies = ref<string[]>([])
|
const allergies = ref<string[]>(loadAllergies())
|
||||||
const hardDayMode = ref(false)
|
const hardDayMode = ref(false)
|
||||||
const maxMissing = ref<number | null>(null)
|
const maxMissing = ref<number | null>(null)
|
||||||
const styleId = ref<string | null>(null)
|
const styleId = ref<string | null>(null)
|
||||||
|
|
@ -126,6 +155,8 @@ export const useRecipesStore = defineStore('recipes', () => {
|
||||||
// Persist wizard prefs on change
|
// Persist wizard prefs on change
|
||||||
watch(missingIngredientMode, (val) => localStorage.setItem(MISSING_MODE_KEY, val))
|
watch(missingIngredientMode, (val) => localStorage.setItem(MISSING_MODE_KEY, val))
|
||||||
watch(builderFilterMode, (val) => localStorage.setItem(FILTER_MODE_KEY, val))
|
watch(builderFilterMode, (val) => localStorage.setItem(FILTER_MODE_KEY, val))
|
||||||
|
watch(constraints, (val) => saveConstraints(val))
|
||||||
|
watch(allergies, (val) => saveAllergies(val))
|
||||||
|
|
||||||
const dismissedCount = computed(() => dismissedIds.value.size)
|
const dismissedCount = computed(() => dismissedIds.value.size)
|
||||||
|
|
||||||
|
|
@ -241,6 +272,16 @@ export const useRecipesStore = defineStore('recipes', () => {
|
||||||
localStorage.removeItem(BOOKMARKS_KEY)
|
localStorage.removeItem(BOOKMARKS_KEY)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function clearConstraints() {
|
||||||
|
constraints.value = []
|
||||||
|
localStorage.removeItem(CONSTRAINTS_KEY)
|
||||||
|
}
|
||||||
|
|
||||||
|
function clearAllergies() {
|
||||||
|
allergies.value = []
|
||||||
|
localStorage.removeItem(ALLERGIES_KEY)
|
||||||
|
}
|
||||||
|
|
||||||
function clearResult() {
|
function clearResult() {
|
||||||
result.value = null
|
result.value = null
|
||||||
error.value = null
|
error.value = null
|
||||||
|
|
@ -270,6 +311,8 @@ export const useRecipesStore = defineStore('recipes', () => {
|
||||||
isBookmarked,
|
isBookmarked,
|
||||||
toggleBookmark,
|
toggleBookmark,
|
||||||
clearBookmarks,
|
clearBookmarks,
|
||||||
|
clearConstraints,
|
||||||
|
clearAllergies,
|
||||||
missingIngredientMode,
|
missingIngredientMode,
|
||||||
builderFilterMode,
|
builderFilterMode,
|
||||||
suggest,
|
suggest,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue