67 lines
1.7 KiB
TypeScript
67 lines
1.7 KiB
TypeScript
import { type RecipeDashboard } from '@/models/recipe/schema'
|
|
import { create } from 'zustand'
|
|
import taoAxios from '@/lib/taoAxios'
|
|
|
|
export interface RecipeDashboardFilterQuery {
|
|
countryID: string
|
|
filename: string
|
|
}
|
|
|
|
interface materialDashboard {
|
|
lebel: string
|
|
value: string
|
|
}
|
|
|
|
interface RecipeDashboardHook {
|
|
selectedRecipe: string
|
|
setSelectedRecipe: (recipe: string) => void
|
|
getRecipesDashboard: (filter?: RecipeDashboardFilterQuery) => Promise<RecipeDashboard[] | []>
|
|
getMaterials: (filter?: RecipeDashboardFilterQuery) => Promise<materialDashboard[] | []>
|
|
getRecipe: (productCode: string) => Promise<RecipeDashboard | null>
|
|
}
|
|
|
|
const useRecipeDashboard = create<RecipeDashboardHook>(() => ({
|
|
selectedRecipe: '',
|
|
setSelectedRecipe: id => {
|
|
useRecipeDashboard.setState({ selectedRecipe: id })
|
|
},
|
|
async getRecipesDashboard(filter) {
|
|
return taoAxios
|
|
.get<RecipeDashboard[]>('/v2/recipes/dashboard', {
|
|
params: filter
|
|
? {
|
|
country_id: filter.countryID,
|
|
filename: filter.filename
|
|
}
|
|
: undefined
|
|
})
|
|
.then(res => {
|
|
return res.data
|
|
})
|
|
.catch(() => [])
|
|
},
|
|
async getMaterials(filter) {
|
|
return taoAxios
|
|
.get<materialDashboard[]>('/v2/materials/dashboard', {
|
|
params: filter
|
|
? {
|
|
country_id: filter.countryID,
|
|
filename: filter.filename
|
|
}
|
|
: undefined
|
|
})
|
|
.then(res => {
|
|
return res.data
|
|
})
|
|
},
|
|
async getRecipe(productCode) {
|
|
return taoAxios
|
|
.get<RecipeDashboard>(`/v2/recipes/${productCode}`)
|
|
.then(res => {
|
|
return res.data
|
|
})
|
|
.catch(() => null)
|
|
}
|
|
}))
|
|
|
|
export default useRecipeDashboard
|