diff --git a/bun.lockb b/bun.lockb index ccb6ac2..655da00 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/src/components/matrix/MaProductForm.tsx b/src/components/matrix/MaProductForm.tsx new file mode 100644 index 0000000..f0aed9c --- /dev/null +++ b/src/components/matrix/MaProductForm.tsx @@ -0,0 +1,87 @@ +import { Collapse, Alert, TextField, Button, Box } from '@mui/material' +import { useRouter } from 'next-nprogress-bar' +import { useState } from 'react' +import { useForm } from 'react-hook-form' +import { MaProduct } from 'solar-js-sdk' + +import ErrorIcon from '@mui/icons-material/Error' + +export interface MatrixProductForm { + name: string + alias: string + description: string + introduction: string +} + +export default function MaProductForm({ + onSubmit, + onSuccess, + defaultValue, +}: { + onSubmit: (data: MatrixProductForm) => Promise + onSuccess?: () => void + defaultValue?: MaProduct +}) { + const { handleSubmit, register } = useForm({ + defaultValues: { + name: defaultValue?.name ?? '', + alias: defaultValue?.alias ?? '', + description: defaultValue?.description ?? '', + introduction: defaultValue?.meta?.introduction ?? '', + }, + }) + + const router = useRouter() + + const [error, setError] = useState(null) + const [busy, setBusy] = useState(false) + + function callback() { + if (onSuccess) { + onSuccess() + } else { + router.push('/console/matrix') + } + } + + async function submit(data: MatrixProductForm) { + try { + setBusy(true) + await onSubmit(data) + callback() + } catch (err: any) { + setError(err.toString()) + } finally { + setBusy(false) + } + } + + return ( +
+ + + } severity="error"> + {error} + + + + + + + + + + + + + + + + +
+ ) +} diff --git a/src/pages/console/matrix/index.tsx b/src/pages/console/matrix/index.tsx index bc13e0e..bd86384 100644 --- a/src/pages/console/matrix/index.tsx +++ b/src/pages/console/matrix/index.tsx @@ -48,7 +48,9 @@ export default function MatrixMarketplace() { - + + + diff --git a/src/pages/console/matrix/products/[id]/edit.tsx b/src/pages/console/matrix/products/[id]/edit.tsx new file mode 100644 index 0000000..0c0a3cf --- /dev/null +++ b/src/pages/console/matrix/products/[id]/edit.tsx @@ -0,0 +1,39 @@ +import { ConsoleLayout, getConsoleStaticProps } from '@/components/layouts/ConsoleLayout' +import { Typography, Container, Box } from '@mui/material' +import { MaProduct, sni } from 'solar-js-sdk' +import { GetServerSideProps, InferGetServerSidePropsType } from 'next' +import MaProductForm, { MatrixProductForm } from '@/components/matrix/MaProductForm' + +export const getServerSideProps: GetServerSideProps = (async (context) => { + const id = context.params!.id + + const { data } = await sni.get('/cgi/ma/products/' + id) + + return getConsoleStaticProps({ + props: { + title: `Edit Product "${data.name}"`, + product: data, + }, + }) +}) satisfies GetServerSideProps<{ product: MaProduct }> + +export default function ProductEdit({ product }: InferGetServerSidePropsType) { + async function onSubmit(data: MatrixProductForm) { + await sni.put('/cgi/ma/products/' + product.id, data) + } + + return ( + + + + + Edit product + + {product.name} + + + + + + ) +} diff --git a/src/pages/console/matrix/products/new.tsx b/src/pages/console/matrix/products/new.tsx index 834f87d..378db1a 100644 --- a/src/pages/console/matrix/products/new.tsx +++ b/src/pages/console/matrix/products/new.tsx @@ -1,12 +1,8 @@ import { ConsoleLayout, getConsoleStaticProps } from '@/components/layouts/ConsoleLayout' -import { Typography, Container, Box, Button, TextField, Collapse, Alert } from '@mui/material' -import { useForm } from 'react-hook-form' -import { useState } from 'react' -import { useRouter } from 'next/router' +import { Typography, Container } from '@mui/material' import { sni } from 'solar-js-sdk' -import NextLink from 'next/link' -import ErrorIcon from '@mui/icons-material/Error' +import MaProductForm, { MatrixProductForm } from '@/components/matrix/MaProductForm' export async function getStaticProps() { return getConsoleStaticProps({ @@ -16,31 +12,9 @@ export async function getStaticProps() { }) } -interface MatrixProductNewForm { - name: string - alias: string - description: string - introduction: string -} - export default function ProductNew() { - const { handleSubmit, register } = useForm() - - const router = useRouter() - - const [error, setError] = useState(null) - const [busy, setBusy] = useState(false) - - async function onSubmit(data: any) { - try { - setBusy(true) - await sni.post('/cgi/ma/products', data) - router.push('/console/matrix') - } catch (err: any) { - setError(err.toString()) - } finally { - setBusy(false) - } + async function onSubmit(data: MatrixProductForm) { + await sni.post('/cgi/ma/products', data) } return ( @@ -50,32 +24,7 @@ export default function ProductNew() { Create a product -
- - - } severity="error"> - {error} - - - - - - - - - - - - - - - - - - -
+ )