83 lines
2.3 KiB
TypeScript
83 lines
2.3 KiB
TypeScript
import React from 'react'
|
|
import { createFileRoute, useNavigate } from '@tanstack/react-router'
|
|
import { AppContainer, p as MyText } from 'web-components'
|
|
import StoreForm, { type StoreFormData } from '../components/StoreForm'
|
|
import { trpc } from '@/lib/trpc-client'
|
|
import { ErrorToast } from '@/services/toaster'
|
|
|
|
export const Route = createFileRoute('/dashboard/stores/edit')({
|
|
validateSearch: (s: Record<string, unknown>) => ({
|
|
id: (s.id as string) ?? ''
|
|
}),
|
|
component: EditStore,
|
|
})
|
|
|
|
function EditStore() {
|
|
const navigate = useNavigate()
|
|
const { id } = Route.useSearch()
|
|
|
|
const storeId = parseInt(id as string)
|
|
|
|
const { data: storeData, isLoading: isLoadingStore, refetch } = trpc.admin.store.getStoreById.useQuery(
|
|
{ id: storeId },
|
|
{ enabled: !!storeId }
|
|
)
|
|
|
|
const updateStoreMutation = trpc.admin.store.updateStore.useMutation()
|
|
|
|
const handleSubmit = (values: StoreFormData) => {
|
|
updateStoreMutation.mutate({ id: storeId, ...values }, {
|
|
onSuccess: (data) => {
|
|
refetch()
|
|
window.alert(`Success: ${data.message}`)
|
|
navigate({ to: '/dashboard/stores' } as any)
|
|
},
|
|
onError: (error: any) => {
|
|
ErrorToast(error.message || 'Failed to update store')
|
|
},
|
|
})
|
|
}
|
|
|
|
if (isLoadingStore) {
|
|
return (
|
|
<AppContainer>
|
|
<div className="flex flex-1 flex-col items-center justify-center">
|
|
<MyText>Loading store...</MyText>
|
|
</div>
|
|
</AppContainer>
|
|
)
|
|
}
|
|
|
|
if (!storeData?.store) {
|
|
return (
|
|
<AppContainer>
|
|
<div className="flex flex-1 flex-col items-center justify-center">
|
|
<MyText>Store not found</MyText>
|
|
</div>
|
|
</AppContainer>
|
|
)
|
|
}
|
|
|
|
const initialValues = {
|
|
name: storeData.store.name,
|
|
description: storeData.store.description || '',
|
|
imageUrl: storeData.store.imageUrl || '',
|
|
owner: storeData.store.owner.id,
|
|
products: [], // Will be set by StoreForm
|
|
}
|
|
|
|
return (
|
|
<AppContainer>
|
|
<div>
|
|
<MyText className="mb-6 block text-2xl font-bold text-gray-800">Edit Store</MyText>
|
|
<StoreForm
|
|
mode="edit"
|
|
initialValues={initialValues}
|
|
onSubmit={handleSubmit}
|
|
isLoading={updateStoreMutation.isPending}
|
|
storeId={storeId}
|
|
/>
|
|
</div>
|
|
</AppContainer>
|
|
)
|
|
}
|