function Component(props: ComponentProps) {
const [fields, setField] = createStore(initialValues)
const [supabaseData] = createResource<Product>(
() => [params.id, isEditMode],
async () => {
const product = isEditMode ? await getProduct(params.id) : undefined
const schedules = await getSchedules<Schedule>(
user?.user_metadata?.tenant_id,
)
return { product, ...schedules }
},
)
const loading = () => supabaseData.loading
createEffect(() => {
if (loading()) return
const { product, schedules }: { product: Product; schedules: Schedule[] } =
supabaseData()
setFields(product || defaultProductData)
setProductName(product?.product_name)
const hasSchedules = product?.schedule_id === null && schedules.length > 0
setSchedules(schedules)
if (hasSchedules) {
setValues({ schedule_id: schedules[0].schedule_id })
}
})
return (
// Something
)
}
function Component(props: ComponentProps) {
const [fields, setField] = createStore(initialValues)
const [supabaseData] = createResource<Product>(
() => [params.id, isEditMode],
async () => {
const product = isEditMode ? await getProduct(params.id) : undefined
const schedules = await getSchedules<Schedule>(
user?.user_metadata?.tenant_id,
)
return { product, ...schedules }
},
)
const loading = () => supabaseData.loading
createEffect(() => {
if (loading()) return
const { product, schedules }: { product: Product; schedules: Schedule[] } =
supabaseData()
setFields(product || defaultProductData)
setProductName(product?.product_name)
const hasSchedules = product?.schedule_id === null && schedules.length > 0
setSchedules(schedules)
if (hasSchedules) {
setValues({ schedule_id: schedules[0].schedule_id })
}
})
return (
// Something
)
}