-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The predefined pages demo was in a bad shape: Loading/saving didn't work, the info tag didn't look right, etc. I've therefore updated the demo to reflect the latests changes/decisions made in the CRUD Generator. Notable changes: - Use a GraphQL enum for `type` - Add the save conflict check
- Loading branch information
1 parent
979d5f4
commit a357ed1
Showing
17 changed files
with
241 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
demo/admin/src/documents/predefinedPages/EditPredefinedPage.gql.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { gql } from "@apollo/client"; | ||
|
||
export const predefinedPageFormFragment = gql` | ||
fragment PredefinedPageForm on PredefinedPage { | ||
type | ||
} | ||
`; | ||
|
||
export const predefinedPageQuery = gql` | ||
query PredefinedPage($pageTreeNodeId: ID!) { | ||
pageTreeNode(id: $pageTreeNodeId) { | ||
id | ||
document { | ||
__typename | ||
... on DocumentInterface { | ||
id | ||
updatedAt | ||
} | ||
... on PredefinedPage { | ||
...PredefinedPageForm | ||
} | ||
} | ||
} | ||
} | ||
${predefinedPageFormFragment} | ||
`; | ||
|
||
export const savePredefinedPageMutation = gql` | ||
mutation SavePredefinedPage($id: ID!, $input: PredefinedPageInput!, $lastUpdatedAt: DateTime, $attachedPageTreeNodeId: ID!) { | ||
savePredefinedPage(id: $id, input: $input, lastUpdatedAt: $lastUpdatedAt, attachedPageTreeNodeId: $attachedPageTreeNodeId) { | ||
id | ||
updatedAt | ||
...PredefinedPageForm | ||
} | ||
} | ||
${predefinedPageFormFragment} | ||
`; |
127 changes: 127 additions & 0 deletions
127
demo/admin/src/documents/predefinedPages/EditPredefinedPage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { useApolloClient, useQuery } from "@apollo/client"; | ||
import { | ||
FinalForm, | ||
FinalFormSaveButton, | ||
Loading, | ||
MainContent, | ||
SelectField, | ||
Toolbar, | ||
ToolbarFillSpace, | ||
ToolbarItem, | ||
useFormApiRef, | ||
useStackApi, | ||
} from "@comet/admin"; | ||
import { ArrowLeft } from "@comet/admin-icons"; | ||
import { ContentScopeIndicator, PageName, queryUpdatedAt, resolveHasSaveConflict, useFormSaveConflict } from "@comet/cms-admin"; | ||
import { IconButton } from "@mui/material"; | ||
import { GQLPredefinedPageType } from "@src/graphql.generated"; | ||
import { useMemo } from "react"; | ||
import { FormattedMessage } from "react-intl"; | ||
import { v4 as uuid } from "uuid"; | ||
|
||
import { predefinedPageQuery, savePredefinedPageMutation } from "./EditPredefinedPage.gql"; | ||
import { | ||
GQLPredefinedPageQuery, | ||
GQLPredefinedPageQueryVariables, | ||
GQLSavePredefinedPageMutation, | ||
GQLSavePredefinedPageMutationVariables, | ||
} from "./EditPredefinedPage.gql.generated"; | ||
import { predefinedPageLabels } from "./predefinedPageLabels"; | ||
|
||
type FormValues = { | ||
type?: GQLPredefinedPageType; | ||
}; | ||
|
||
interface Props { | ||
id: string; | ||
} | ||
|
||
export const EditPredefinedPage = ({ id: pageTreeNodeId }: Props) => { | ||
const stackApi = useStackApi(); | ||
const client = useApolloClient(); | ||
const formApiRef = useFormApiRef<FormValues>(); | ||
|
||
const { data, error, loading, refetch } = useQuery<GQLPredefinedPageQuery, GQLPredefinedPageQueryVariables>(predefinedPageQuery, { | ||
variables: { pageTreeNodeId }, | ||
}); | ||
|
||
const initialValues = useMemo<Partial<FormValues>>(() => { | ||
if (data?.pageTreeNode?.document) { | ||
if (data.pageTreeNode.document.__typename !== "PredefinedPage") { | ||
throw new Error(`Invalid document type: ${data.pageTreeNode.document.__typename}`); | ||
} | ||
|
||
return { | ||
type: data.pageTreeNode.document.type ?? undefined, | ||
}; | ||
} | ||
|
||
return {}; | ||
}, [data]); | ||
|
||
const saveConflict = useFormSaveConflict({ | ||
checkConflict: async () => { | ||
const updatedAt = await queryUpdatedAt(client, "predefinedPage", data?.pageTreeNode?.document?.id); | ||
return resolveHasSaveConflict(data?.pageTreeNode?.document?.updatedAt, updatedAt); | ||
}, | ||
formApiRef, | ||
loadLatestVersion: async () => { | ||
await refetch(); | ||
}, | ||
}); | ||
|
||
const handleSubmit = async (formValues: FormValues) => { | ||
if (await saveConflict.checkForConflicts()) { | ||
throw new Error("Conflicts detected"); | ||
} | ||
|
||
const output = { | ||
type: formValues.type ?? null, | ||
}; | ||
|
||
const id = data?.pageTreeNode?.document?.id ?? uuid(); | ||
|
||
await client.mutate<GQLSavePredefinedPageMutation, GQLSavePredefinedPageMutationVariables>({ | ||
mutation: savePredefinedPageMutation, | ||
variables: { id, input: output, attachedPageTreeNodeId: pageTreeNodeId }, | ||
}); | ||
}; | ||
|
||
if (error) { | ||
throw error; | ||
} | ||
|
||
if (loading) { | ||
return <Loading behavior="fillPageHeight" />; | ||
} | ||
|
||
return ( | ||
<FinalForm apiRef={formApiRef} onSubmit={handleSubmit} mode="edit" initialValues={initialValues}> | ||
{() => ( | ||
<> | ||
{saveConflict.dialogs} | ||
<Toolbar scopeIndicator={<ContentScopeIndicator />}> | ||
<ToolbarItem> | ||
<IconButton onClick={stackApi?.goBack}> | ||
<ArrowLeft /> | ||
</IconButton> | ||
</ToolbarItem> | ||
<PageName pageId={pageTreeNodeId} /> | ||
<ToolbarFillSpace /> | ||
<ToolbarItem> | ||
<FinalFormSaveButton hasConflict={saveConflict.hasConflict} /> | ||
</ToolbarItem> | ||
</Toolbar> | ||
<MainContent> | ||
<SelectField | ||
name="type" | ||
label={<FormattedMessage id="predefinedPages.type.label" defaultMessage="Type" />} | ||
options={[{ value: "News", label: predefinedPageLabels.News }]} | ||
fullWidth | ||
/> | ||
</MainContent> | ||
</> | ||
)} | ||
</FinalForm> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
demo/admin/src/documents/predefinedPages/predefinedPageLabels.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { GQLPredefinedPageType } from "@src/graphql.generated"; | ||
import { ReactNode } from "react"; | ||
import { FormattedMessage } from "react-intl"; | ||
|
||
export const predefinedPageLabels: Record<GQLPredefinedPageType, ReactNode> = { | ||
News: <FormattedMessage id="predefinedPages.news" defaultMessage="News" />, | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.