46 lines
1.4 KiB
TypeScript
46 lines
1.4 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/new')({
|
|
component: AddStore,
|
|
})
|
|
|
|
function AddStore() {
|
|
const navigate = useNavigate()
|
|
|
|
const createStoreMutation = trpc.admin.store.createStore.useMutation()
|
|
const { refetch: refetchStores } = trpc.admin.store.getStores.useQuery(undefined, {
|
|
enabled: false,
|
|
})
|
|
|
|
const handleSubmit = (values: StoreFormData) => {
|
|
createStoreMutation.mutate(values, {
|
|
onSuccess: async (data) => {
|
|
await refetchStores()
|
|
window.alert(`Success: ${data.message}`)
|
|
navigate({ to: '/dashboard/stores' }) // Navigate back to stores list
|
|
},
|
|
onError: (error: any) => {
|
|
ErrorToast(error.message || 'Failed to create store')
|
|
},
|
|
})
|
|
}
|
|
|
|
return (
|
|
<AppContainer>
|
|
<div>
|
|
<MyText className="mb-6 block text-2xl font-bold text-gray-800">Add New Store</MyText>
|
|
<StoreForm
|
|
mode="create"
|
|
initialValues={{ name: '', description: '', products: [] }}
|
|
onSubmit={handleSubmit}
|
|
isLoading={createStoreMutation.isPending}
|
|
/>
|
|
</div>
|
|
</AppContainer>
|
|
)
|
|
}
|