Add rich text editor to pages
This commit is contained in:
parent
aa3559117d
commit
7c9f2a93d4
10 changed files with 246 additions and 181 deletions
|
@ -3,6 +3,7 @@ import { CollectionDetails_collection } from "@saleor/collections/types/Collecti
|
|||
import { MetadataFormData } from "@saleor/components/Metadata";
|
||||
import { RichTextEditorChange } from "@saleor/components/RichTextEditor";
|
||||
import useForm, { FormChange } from "@saleor/hooks/useForm";
|
||||
import getPublicationData from "@saleor/utils/data/getPublicationData";
|
||||
import handleFormSubmit from "@saleor/utils/handlers/handleFormSubmit";
|
||||
import { mapMetadataItemToInput } from "@saleor/utils/maps";
|
||||
import getMetadata from "@saleor/utils/metadata/getMetadata";
|
||||
|
@ -89,7 +90,7 @@ function useCollectionUpdateForm(
|
|||
const getSubmitData = (): CollectionUpdateData => ({
|
||||
...getData(),
|
||||
...getMetadata(form.data, isMetadataModified, isPrivateMetadataModified),
|
||||
isPublished: form.data.isPublished || !!form.data.publicationDate
|
||||
...getPublicationData(form.data)
|
||||
});
|
||||
|
||||
const submit = () => handleFormSubmit(getSubmitData(), onSubmit, setChanged);
|
||||
|
|
|
@ -2,6 +2,7 @@ import { WindowTitle } from "@saleor/components/WindowTitle";
|
|||
import useNavigator from "@saleor/hooks/useNavigator";
|
||||
import useNotifier from "@saleor/hooks/useNotifier";
|
||||
import { commonMessages } from "@saleor/intl";
|
||||
import getPublicationData from "@saleor/utils/data/getPublicationData";
|
||||
import createMetadataCreateHandler from "@saleor/utils/handlers/metadataCreateHandler";
|
||||
import {
|
||||
useMetadataUpdate,
|
||||
|
@ -11,9 +12,8 @@ import React from "react";
|
|||
import { useIntl } from "react-intl";
|
||||
|
||||
import { CollectionCreateInput } from "../../types/globalTypes";
|
||||
import CollectionCreatePage, {
|
||||
CollectionCreatePageFormData
|
||||
} from "../components/CollectionCreatePage/CollectionCreatePage";
|
||||
import CollectionCreatePage from "../components/CollectionCreatePage/CollectionCreatePage";
|
||||
import { CollectionCreateData } from "../components/CollectionCreatePage/form";
|
||||
import { useCollectionCreateMutation } from "../mutations";
|
||||
import { collectionListUrl, collectionUrl } from "../urls";
|
||||
|
||||
|
@ -47,15 +47,7 @@ export const CollectionCreate: React.FC = () => {
|
|||
}
|
||||
});
|
||||
|
||||
const getPublicationData = ({
|
||||
publicationDate,
|
||||
isPublished
|
||||
}: CollectionCreatePageFormData) => ({
|
||||
isPublished: !!publicationDate || isPublished,
|
||||
publicationDate: publicationDate || null
|
||||
});
|
||||
|
||||
const handleCreate = async (formData: CollectionCreatePageFormData) => {
|
||||
const handleCreate = async (formData: CollectionCreateData) => {
|
||||
const result = await createCollection({
|
||||
variables: {
|
||||
input: {
|
||||
|
|
|
@ -2,9 +2,8 @@ import AppHeader from "@saleor/components/AppHeader";
|
|||
import CardSpacer from "@saleor/components/CardSpacer";
|
||||
import { ConfirmButtonTransitionState } from "@saleor/components/ConfirmButton";
|
||||
import Container from "@saleor/components/Container";
|
||||
import Form from "@saleor/components/Form";
|
||||
import Grid from "@saleor/components/Grid";
|
||||
import Metadata, { MetadataFormData } from "@saleor/components/Metadata";
|
||||
import Metadata from "@saleor/components/Metadata";
|
||||
import PageHeader from "@saleor/components/PageHeader";
|
||||
import SaveButtonBar from "@saleor/components/SaveButtonBar";
|
||||
import SeoForm from "@saleor/components/SeoForm";
|
||||
|
@ -13,30 +12,12 @@ import { PageErrorFragment } from "@saleor/fragments/types/PageErrorFragment";
|
|||
import useDateLocalize from "@saleor/hooks/useDateLocalize";
|
||||
import { SubmitPromise } from "@saleor/hooks/useForm";
|
||||
import { sectionNames } from "@saleor/intl";
|
||||
import { mapMetadataItemToInput } from "@saleor/utils/maps";
|
||||
import useMetadataChangeTrigger from "@saleor/utils/metadata/useMetadataChangeTrigger";
|
||||
import {
|
||||
ContentState,
|
||||
convertFromRaw,
|
||||
convertToRaw,
|
||||
RawDraftContentState
|
||||
} from "draft-js";
|
||||
import React from "react";
|
||||
import { useIntl } from "react-intl";
|
||||
|
||||
import { maybe } from "../../../misc";
|
||||
import { PageDetails_page } from "../../types/PageDetails";
|
||||
import PageInfo from "../PageInfo";
|
||||
|
||||
export interface PageDetailsPageFormData extends MetadataFormData {
|
||||
content: RawDraftContentState;
|
||||
isPublished: boolean;
|
||||
publicationDate: string;
|
||||
seoDescription: string;
|
||||
seoTitle: string;
|
||||
slug: string;
|
||||
title: string;
|
||||
}
|
||||
import PageForm, { PageData } from "./form";
|
||||
|
||||
export interface PageDetailsPageProps {
|
||||
disabled: boolean;
|
||||
|
@ -46,7 +27,7 @@ export interface PageDetailsPageProps {
|
|||
saveButtonBarState: ConfirmButtonTransitionState;
|
||||
onBack: () => void;
|
||||
onRemove: () => void;
|
||||
onSubmit: (data: PageDetailsPageFormData) => SubmitPromise;
|
||||
onSubmit: (data: PageData) => SubmitPromise;
|
||||
}
|
||||
|
||||
const PageDetailsPage: React.FC<PageDetailsPageProps> = ({
|
||||
|
@ -60,51 +41,12 @@ const PageDetailsPage: React.FC<PageDetailsPageProps> = ({
|
|||
}) => {
|
||||
const intl = useIntl();
|
||||
const localizeDate = useDateLocalize();
|
||||
const {
|
||||
isMetadataModified,
|
||||
isPrivateMetadataModified,
|
||||
makeChangeHandler: makeMetadataChangeHandler
|
||||
} = useMetadataChangeTrigger();
|
||||
|
||||
const pageExists = page !== null;
|
||||
|
||||
const initialForm: PageDetailsPageFormData = {
|
||||
content: maybe(
|
||||
() => JSON.parse(page.contentJson),
|
||||
convertToRaw(ContentState.createFromText(""))
|
||||
),
|
||||
isPublished: page?.isPublished,
|
||||
metadata: pageExists ? page?.metadata?.map(mapMetadataItemToInput) : [],
|
||||
privateMetadata: pageExists
|
||||
? page?.privateMetadata?.map(mapMetadataItemToInput)
|
||||
: [],
|
||||
publicationDate: page?.publicationDate || "",
|
||||
seoDescription: page?.seoDescription || "",
|
||||
seoTitle: page?.seoTitle || "",
|
||||
slug: page?.slug || "",
|
||||
title: page?.title || ""
|
||||
};
|
||||
|
||||
const handleSubmit = (data: PageDetailsPageFormData) => {
|
||||
const metadata = isMetadataModified ? data.metadata : undefined;
|
||||
const privateMetadata = isPrivateMetadataModified
|
||||
? data.privateMetadata
|
||||
: undefined;
|
||||
|
||||
return onSubmit({
|
||||
...data,
|
||||
isPublished: data.isPublished || !!data.publicationDate,
|
||||
metadata,
|
||||
privateMetadata
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<Form initial={initialForm} onSubmit={handleSubmit}>
|
||||
{({ change, data, hasChanged, submit }) => {
|
||||
const changeMetadata = makeMetadataChangeHandler(change);
|
||||
|
||||
return (
|
||||
<PageForm page={page} onSubmit={onSubmit}>
|
||||
{({ change, data, handlers, hasChanged, submit }) => (
|
||||
<Container>
|
||||
<AppHeader onBack={onBack}>
|
||||
{intl.formatMessage(sectionNames.pages)}
|
||||
|
@ -116,7 +58,7 @@ const PageDetailsPage: React.FC<PageDetailsPageProps> = ({
|
|||
defaultMessage: "Create Page",
|
||||
description: "page header"
|
||||
})
|
||||
: maybe(() => page.title)
|
||||
: page?.title
|
||||
}
|
||||
/>
|
||||
<Grid>
|
||||
|
@ -125,8 +67,8 @@ const PageDetailsPage: React.FC<PageDetailsPageProps> = ({
|
|||
data={data}
|
||||
disabled={disabled}
|
||||
errors={errors}
|
||||
page={page}
|
||||
onChange={change}
|
||||
onContentChange={handlers.changeContent}
|
||||
/>
|
||||
<CardSpacer />
|
||||
<SeoForm
|
||||
|
@ -134,13 +76,7 @@ const PageDetailsPage: React.FC<PageDetailsPageProps> = ({
|
|||
allowEmptySlug={!pageExists}
|
||||
description={data.seoDescription}
|
||||
disabled={disabled}
|
||||
descriptionPlaceholder={maybe(
|
||||
() =>
|
||||
convertFromRaw(data.content)
|
||||
.getPlainText()
|
||||
.slice(0, 300),
|
||||
""
|
||||
)}
|
||||
descriptionPlaceholder={""} // TODO: Cast description to string and trim it
|
||||
onChange={change}
|
||||
slug={data.slug}
|
||||
slugPlaceholder={data.title}
|
||||
|
@ -152,7 +88,7 @@ const PageDetailsPage: React.FC<PageDetailsPageProps> = ({
|
|||
})}
|
||||
/>
|
||||
<CardSpacer />
|
||||
<Metadata data={data} onChange={changeMetadata} />
|
||||
<Metadata data={data} onChange={handlers.changeMetadata} />
|
||||
</div>
|
||||
<div>
|
||||
<CardSpacer />
|
||||
|
@ -191,9 +127,8 @@ const PageDetailsPage: React.FC<PageDetailsPageProps> = ({
|
|||
onSave={submit}
|
||||
/>
|
||||
</Container>
|
||||
);
|
||||
}}
|
||||
</Form>
|
||||
)}
|
||||
</PageForm>
|
||||
);
|
||||
};
|
||||
PageDetailsPage.displayName = "PageDetailsPage";
|
||||
|
|
118
src/pages/components/PageDetailsPage/form.tsx
Normal file
118
src/pages/components/PageDetailsPage/form.tsx
Normal file
|
@ -0,0 +1,118 @@
|
|||
import { OutputData } from "@editorjs/editorjs";
|
||||
import { MetadataFormData } from "@saleor/components/Metadata";
|
||||
import { RichTextEditorChange } from "@saleor/components/RichTextEditor";
|
||||
import useForm, { FormChange } from "@saleor/hooks/useForm";
|
||||
import { PageDetails_page } from "@saleor/pages/types/PageDetails";
|
||||
import getPublicationData from "@saleor/utils/data/getPublicationData";
|
||||
import handleFormSubmit from "@saleor/utils/handlers/handleFormSubmit";
|
||||
import { mapMetadataItemToInput } from "@saleor/utils/maps";
|
||||
import getMetadata from "@saleor/utils/metadata/getMetadata";
|
||||
import useMetadataChangeTrigger from "@saleor/utils/metadata/useMetadataChangeTrigger";
|
||||
import useRichText from "@saleor/utils/richText/useRichText";
|
||||
import React from "react";
|
||||
|
||||
export interface PageFormData extends MetadataFormData {
|
||||
isPublished: boolean;
|
||||
publicationDate: string;
|
||||
seoDescription: string;
|
||||
seoTitle: string;
|
||||
slug: string;
|
||||
title: string;
|
||||
}
|
||||
export interface PageData extends PageFormData {
|
||||
content: OutputData;
|
||||
}
|
||||
|
||||
interface PageUpdateHandlers {
|
||||
changeMetadata: FormChange;
|
||||
changeContent: RichTextEditorChange;
|
||||
}
|
||||
export interface UsePageUpdateFormResult {
|
||||
change: FormChange;
|
||||
data: PageData;
|
||||
handlers: PageUpdateHandlers;
|
||||
hasChanged: boolean;
|
||||
submit: () => void;
|
||||
}
|
||||
|
||||
export interface PageFormProps {
|
||||
children: (props: UsePageUpdateFormResult) => React.ReactNode;
|
||||
page: PageDetails_page;
|
||||
onSubmit: (data: PageData) => Promise<any[]>;
|
||||
}
|
||||
|
||||
function usePageForm(
|
||||
page: PageDetails_page,
|
||||
onSubmit: (data: PageData) => Promise<any[]>
|
||||
): UsePageUpdateFormResult {
|
||||
const [changed, setChanged] = React.useState(false);
|
||||
const triggerChange = () => setChanged(true);
|
||||
|
||||
const pageExists = page !== null;
|
||||
|
||||
const form = useForm<PageFormData>({
|
||||
isPublished: page?.isPublished,
|
||||
metadata: pageExists ? page?.metadata?.map(mapMetadataItemToInput) : [],
|
||||
privateMetadata: pageExists
|
||||
? page?.privateMetadata?.map(mapMetadataItemToInput)
|
||||
: [],
|
||||
publicationDate: page?.publicationDate || "",
|
||||
seoDescription: page?.seoDescription || "",
|
||||
seoTitle: page?.seoTitle || "",
|
||||
slug: page?.slug || "",
|
||||
title: page?.title || ""
|
||||
});
|
||||
const [content, changeContent] = useRichText({
|
||||
initial: pageExists ? page?.contentJson : null,
|
||||
triggerChange
|
||||
});
|
||||
|
||||
const {
|
||||
isMetadataModified,
|
||||
isPrivateMetadataModified,
|
||||
makeChangeHandler: makeMetadataChangeHandler
|
||||
} = useMetadataChangeTrigger();
|
||||
|
||||
const handleChange: FormChange = (event, cb) => {
|
||||
form.change(event, cb);
|
||||
triggerChange();
|
||||
};
|
||||
const changeMetadata = makeMetadataChangeHandler(handleChange);
|
||||
|
||||
// Need to make it function to always have content.current up to date
|
||||
const getData = (): PageData => ({
|
||||
...form.data,
|
||||
content: content.current
|
||||
});
|
||||
|
||||
const getSubmitData = (): PageData => ({
|
||||
...getData(),
|
||||
...getMetadata(form.data, isMetadataModified, isPrivateMetadataModified),
|
||||
...getPublicationData(form.data)
|
||||
});
|
||||
|
||||
const submit = () =>
|
||||
pageExists
|
||||
? handleFormSubmit(getSubmitData(), onSubmit, setChanged)
|
||||
: onSubmit(getSubmitData());
|
||||
|
||||
return {
|
||||
change: handleChange,
|
||||
data: getData(),
|
||||
handlers: {
|
||||
changeContent,
|
||||
changeMetadata
|
||||
},
|
||||
hasChanged: changed,
|
||||
submit
|
||||
};
|
||||
}
|
||||
|
||||
const PageForm: React.FC<PageFormProps> = ({ children, page, onSubmit }) => {
|
||||
const props = usePageForm(page, onSubmit);
|
||||
|
||||
return <form onSubmit={props.submit}>{children(props)}</form>;
|
||||
};
|
||||
|
||||
PageForm.displayName = "PageForm";
|
||||
export default PageForm;
|
|
@ -4,7 +4,9 @@ import { makeStyles } from "@material-ui/core/styles";
|
|||
import TextField from "@material-ui/core/TextField";
|
||||
import CardTitle from "@saleor/components/CardTitle";
|
||||
import FormSpacer from "@saleor/components/FormSpacer";
|
||||
import RichTextEditor from "@saleor/components/RichTextEditor";
|
||||
import RichTextEditor, {
|
||||
RichTextEditorChange
|
||||
} from "@saleor/components/RichTextEditor";
|
||||
import { PageErrorFragment } from "@saleor/fragments/types/PageErrorFragment";
|
||||
import { commonMessages } from "@saleor/intl";
|
||||
import { getFormErrors } from "@saleor/utils/errors";
|
||||
|
@ -12,16 +14,14 @@ import getPageErrorMessage from "@saleor/utils/errors/page";
|
|||
import React from "react";
|
||||
import { useIntl } from "react-intl";
|
||||
|
||||
import { maybe } from "../../../misc";
|
||||
import { PageDetails_page } from "../../types/PageDetails";
|
||||
import { PageDetailsPageFormData } from "../PageDetailsPage";
|
||||
import { PageData } from "../PageDetailsPage/form";
|
||||
|
||||
export interface PageInfoProps {
|
||||
data: PageDetailsPageFormData;
|
||||
data: PageData;
|
||||
disabled: boolean;
|
||||
errors: PageErrorFragment[];
|
||||
page: PageDetails_page;
|
||||
onChange: (event: React.ChangeEvent<any>) => void;
|
||||
onContentChange: RichTextEditorChange;
|
||||
}
|
||||
|
||||
const useStyles = makeStyles(
|
||||
|
@ -34,7 +34,7 @@ const useStyles = makeStyles(
|
|||
);
|
||||
|
||||
const PageInfo: React.FC<PageInfoProps> = props => {
|
||||
const { data, disabled, errors, page, onChange } = props;
|
||||
const { data, disabled, errors, onChange, onContentChange } = props;
|
||||
|
||||
const classes = useStyles(props);
|
||||
const intl = useIntl();
|
||||
|
@ -56,22 +56,22 @@ const PageInfo: React.FC<PageInfoProps> = props => {
|
|||
defaultMessage: "Title",
|
||||
description: "page title"
|
||||
})}
|
||||
name={"title" as keyof PageDetailsPageFormData}
|
||||
name={"title" as keyof PageData}
|
||||
value={data.title}
|
||||
onChange={onChange}
|
||||
/>
|
||||
<FormSpacer />
|
||||
<RichTextEditor
|
||||
data={data.content}
|
||||
disabled={disabled}
|
||||
error={!!formErrors.contentJson}
|
||||
helperText={getPageErrorMessage(formErrors.contentJson, intl)}
|
||||
initial={maybe(() => JSON.parse(page.contentJson))}
|
||||
label={intl.formatMessage({
|
||||
defaultMessage: "Content",
|
||||
description: "page content"
|
||||
})}
|
||||
name={"content" as keyof PageDetailsPageFormData}
|
||||
onChange={onChange}
|
||||
name={"content" as keyof PageData}
|
||||
onChange={onContentChange}
|
||||
/>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
|
|
@ -22,6 +22,7 @@ const pageCreate = gql`
|
|||
pageCreate(input: $input) {
|
||||
errors: pageErrors {
|
||||
...PageErrorFragment
|
||||
message
|
||||
}
|
||||
page {
|
||||
...PageDetailsFragment
|
||||
|
|
|
@ -9,9 +9,8 @@ import {
|
|||
import React from "react";
|
||||
import { useIntl } from "react-intl";
|
||||
|
||||
import PageDetailsPage, {
|
||||
PageDetailsPageFormData
|
||||
} from "../components/PageDetailsPage";
|
||||
import PageDetailsPage from "../components/PageDetailsPage";
|
||||
import { PageData } from "../components/PageDetailsPage/form";
|
||||
import { TypedPageCreate } from "../mutations";
|
||||
import { PageCreate as PageCreateData } from "../types/PageCreate";
|
||||
import { pageListUrl, pageUrl } from "../urls";
|
||||
|
@ -42,7 +41,7 @@ export const PageCreate: React.FC<PageCreateProps> = () => {
|
|||
return (
|
||||
<TypedPageCreate onCompleted={handlePageCreate}>
|
||||
{(pageCreate, pageCreateOpts) => {
|
||||
const handleCreate = async (formData: PageDetailsPageFormData) => {
|
||||
const handleCreate = async (formData: PageData) => {
|
||||
const result = await pageCreate({
|
||||
variables: {
|
||||
input: {
|
||||
|
|
16
src/utils/data/getPublicationData.ts
Normal file
16
src/utils/data/getPublicationData.ts
Normal file
|
@ -0,0 +1,16 @@
|
|||
interface PublicationData {
|
||||
publicationDate: string;
|
||||
isPublished: boolean;
|
||||
}
|
||||
|
||||
function getPublicationData({
|
||||
publicationDate,
|
||||
isPublished
|
||||
}: PublicationData): PublicationData {
|
||||
return {
|
||||
isPublished: !!publicationDate || isPublished,
|
||||
publicationDate: publicationDate || null
|
||||
};
|
||||
}
|
||||
|
||||
export default getPublicationData;
|
|
@ -39,6 +39,7 @@ describe("useRichText", () => {
|
|||
const triggerChange = jest.fn();
|
||||
const hook = renderHook(() =>
|
||||
useRichText({
|
||||
initial: null,
|
||||
triggerChange
|
||||
})
|
||||
);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { OutputData } from "@editorjs/editorjs";
|
||||
import { RichTextEditorChange } from "@saleor/components/RichTextEditor";
|
||||
import { MutableRefObject, useEffect, useRef } from "react";
|
||||
import { MutableRefObject, useEffect, useRef, useState } from "react";
|
||||
|
||||
function useRichText(opts: {
|
||||
initial: string | null;
|
||||
|
@ -9,9 +9,11 @@ function useRichText(opts: {
|
|||
const data = useRef<OutputData>(
|
||||
opts.initial === null ? { blocks: [] } : undefined
|
||||
);
|
||||
const [, setLoaded] = useState(false);
|
||||
useEffect(() => {
|
||||
try {
|
||||
data.current = JSON.parse(opts.initial);
|
||||
setLoaded(true);
|
||||
} catch {
|
||||
data.current = undefined;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue