Skip to content

Commit

Permalink
Demo Admin: Implement DateTime field in handmade product form (#2639)
Browse files Browse the repository at this point in the history
DateTime fields will be needed for the admin form generator. This is the demo to preface that.

An example use case is: Save start and end time of events.
  • Loading branch information
andrearutrecht authored Dec 9, 2024
1 parent bf6b03f commit 573a08f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
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 @@ -37,6 +37,7 @@ export const productFormFragment = gql`
min
max
}
lastCheckedAt
dimensions {
width
height
Expand Down
9 changes: 8 additions & 1 deletion demo/admin/src/products/ProductForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
useFormApiRef,
useStackSwitchApi,
} from "@comet/admin";
import { DateField } from "@comet/admin-date-time";
import { DateField, DateTimeField } from "@comet/admin-date-time";
import { BlockState, createFinalFormBlock } from "@comet/blocks-admin";
import {
DamImageBlock,
Expand Down Expand Up @@ -111,6 +111,7 @@ export function ProductForm({ id, width }: FormProps) {
id: filteredData.manufacturerCountry?.addressAsEmbeddable.country,
}
: undefined,
lastCheckedAt: filteredData.lastCheckedAt ? new Date(filteredData.lastCheckedAt) : null,
};
}, [data, width]);

Expand Down Expand Up @@ -140,6 +141,7 @@ export function ProductForm({ id, width }: FormProps) {
priceList: formValues.priceList ? formValues.priceList.id : null,
datasheets: formValues.datasheets?.map(({ id }) => id),
manufacturer: formValues.manufacturer?.id,
lastCheckedAt: formValues.lastCheckedAt ? formValues.lastCheckedAt.toISOString() : null,
};

if (mode === "edit") {
Expand Down Expand Up @@ -357,6 +359,11 @@ export function ProductForm({ id, width }: FormProps) {
fullWidth
layout="grid"
/>
<DateTimeField
label={<FormattedMessage id="product.lastCheckedAt" defaultMessage="Last checked at" />}
name="lastCheckedAt"
fullWidth
/>
</>
)}
</FinalForm>
Expand Down

0 comments on commit 573a08f

Please sign in to comment.