67 lines
2.5 KiB
TypeScript
67 lines
2.5 KiB
TypeScript
|
|
import type { RouteLocationRaw } from 'vue-router'
|
||
|
|
import { getHouseholdSlug } from '@/api/client'
|
||
|
|
|
||
|
|
// Centralized builders for slug-scoped routes to keep navigation consistent
|
||
|
|
|
||
|
|
// When an options object is provided, require an explicit slug.
|
||
|
|
// Callers may also omit the argument entirely to infer from context.
|
||
|
|
type WithSlug = { slug: string }
|
||
|
|
|
||
|
|
function resolveSlug(input?: WithSlug): string {
|
||
|
|
if (input && typeof input.slug === 'string') return input.slug
|
||
|
|
return getHouseholdSlug() ?? ''
|
||
|
|
}
|
||
|
|
|
||
|
|
// Pages without ids
|
||
|
|
export function toMealPlan(slugOrOpts?: WithSlug): RouteLocationRaw {
|
||
|
|
const slug = resolveSlug(slugOrOpts)
|
||
|
|
return { name: 'mealplan', params: { householdSlug: slug } }
|
||
|
|
}
|
||
|
|
|
||
|
|
export function toRecipes(slugOrOpts?: WithSlug): RouteLocationRaw {
|
||
|
|
const slug = resolveSlug(slugOrOpts)
|
||
|
|
return { name: 'recipes', params: { householdSlug: slug } }
|
||
|
|
}
|
||
|
|
|
||
|
|
export function toRecipeAdd(slugOrOpts?: WithSlug): RouteLocationRaw {
|
||
|
|
const slug = resolveSlug(slugOrOpts)
|
||
|
|
return { name: 'recipe-add', params: { householdSlug: slug } }
|
||
|
|
}
|
||
|
|
|
||
|
|
export function toMealAdd(slugOrOpts?: WithSlug): RouteLocationRaw {
|
||
|
|
const slug = resolveSlug(slugOrOpts)
|
||
|
|
return { name: 'meal-add', params: { householdSlug: slug } }
|
||
|
|
}
|
||
|
|
|
||
|
|
export function toShoppingCurrent(slugOrOpts?: WithSlug): RouteLocationRaw {
|
||
|
|
const slug = resolveSlug(slugOrOpts)
|
||
|
|
return { name: 'shopping-current', params: { householdSlug: slug } }
|
||
|
|
}
|
||
|
|
|
||
|
|
export function toHouseholdSettings(slugOrOpts?: WithSlug): RouteLocationRaw {
|
||
|
|
const slug = resolveSlug(slugOrOpts)
|
||
|
|
return { name: 'household-settings', params: { householdSlug: slug } }
|
||
|
|
}
|
||
|
|
|
||
|
|
// Pages with ids
|
||
|
|
type WithId = { id: number | string }
|
||
|
|
type IdOrOpts = number | string | (WithId & WithSlug)
|
||
|
|
|
||
|
|
export function toRecipeEdit(idOrOpts: IdOrOpts): RouteLocationRaw {
|
||
|
|
const id = typeof idOrOpts === 'object' ? idOrOpts.id : idOrOpts
|
||
|
|
const slug = resolveSlug(typeof idOrOpts === 'object' ? idOrOpts : undefined)
|
||
|
|
return { name: 'recipe-edit', params: { householdSlug: slug, id } }
|
||
|
|
}
|
||
|
|
|
||
|
|
export function toMealEdit(idOrOpts: IdOrOpts): RouteLocationRaw {
|
||
|
|
const id = typeof idOrOpts === 'object' ? idOrOpts.id : idOrOpts
|
||
|
|
const slug = resolveSlug(typeof idOrOpts === 'object' ? idOrOpts : undefined)
|
||
|
|
return { name: 'meal-edit', params: { householdSlug: slug, id } }
|
||
|
|
}
|
||
|
|
|
||
|
|
export function toShoppingList(idOrOpts: IdOrOpts): RouteLocationRaw {
|
||
|
|
const id = typeof idOrOpts === 'object' ? idOrOpts.id : idOrOpts
|
||
|
|
const slug = resolveSlug(typeof idOrOpts === 'object' ? idOrOpts : undefined)
|
||
|
|
return { name: 'shopping-list', params: { householdSlug: slug, id } }
|
||
|
|
}
|