Skip to content

Commit

Permalink
Fix handmade product form (#1713)
Browse files Browse the repository at this point in the history
- add "price" input field as it is required by the api
- add missing toolbar including save button in add form

---

Closes COM-460

---------

Co-authored-by: Phillip Lechenauer <phillip.lechenauer@vivid-planet.com>
Co-authored-by: Niko Sams <ns@vivid-planet.com>
Co-authored-by: Niko Sams <niko.sams@gmail.com>
  • Loading branch information
4 people authored and thomasdax98 committed Feb 23, 2024
1 parent f1057dd commit a605f36
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions demo/admin/src/products/ProductForm.gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const productFormFragment = gql`
id
title
}
price
}
`;

Expand Down
13 changes: 12 additions & 1 deletion demo/admin/src/products/ProductForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
CheckboxField,
Field,
FinalForm,
FinalFormInput,
FinalFormSelect,
FinalFormSubmitEvent,
Loading,
Expand Down Expand Up @@ -56,7 +57,8 @@ const rootBlocks = {
image: DamImageBlock,
};

type FormValues = Omit<GQLProductFormManualFragment, "image"> & {
type FormValues = Omit<GQLProductFormManualFragment, "image" | "price"> & {
price: string;
image: BlockState<typeof rootBlocks.image>;
};

Expand All @@ -74,6 +76,7 @@ function ProductForm({ id }: FormProps): React.ReactElement {
const initialValues: Partial<FormValues> = data?.product
? {
...filter<GQLProductFormManualFragment>(productFormFragment, data.product),
price: String(data.product.price),
image: rootBlocks.image.input2State(data.product.image),
}
: {
Expand Down Expand Up @@ -105,6 +108,7 @@ function ProductForm({ id }: FormProps): React.ReactElement {
articleNumbers: [],
discounts: [],
statistics: { views: 0 },
price: parseFloat(formValues.price),
};
if (mode === "edit") {
if (!id) throw new Error();
Expand Down Expand Up @@ -188,6 +192,13 @@ function ProductForm({ id }: FormProps): React.ReactElement {
{...tagsSelectAsyncProps}
getOptionLabel={(option: GQLProductTagsSelectFragment) => option.title}
/>
<Field
fullWidth
name="price"
component={FinalFormInput}
type="number"
label={<FormattedMessage id="product.price" defaultMessage="Price" />}
/>
<CheckboxField name="inStock" label={<FormattedMessage id="product.inStock" defaultMessage="In stock" />} fullWidth />
<Field name="image" isEqual={isEqual}>
{createFinalFormBlock(rootBlocks.image)}
Expand Down
12 changes: 11 additions & 1 deletion demo/admin/src/products/ProductsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,17 @@ const ProductsPage: React.FC = () => {
)}
</StackPage>
<StackPage name="add" title={intl.formatMessage({ id: "products.addProduct", defaultMessage: "Add product" })}>
<ProductForm />
<SaveBoundary>
<Toolbar>
<ToolbarBackButton />
<ToolbarAutomaticTitleItem />
<ToolbarFillSpace />
<ToolbarActions>
<SaveBoundarySaveButton />
</ToolbarActions>
</Toolbar>
<ProductForm />
</SaveBoundary>
</StackPage>
</StackSwitch>
</Stack>
Expand Down

0 comments on commit a605f36

Please sign in to comment.