Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Rewrite page forms (#1980)

Dominik Żegleń7d06d4ec0bbe
diff --git a/saleor/static/dashboard-next/page/components/PageBaseForm.tsx b/saleor/static/dashboard-next/page/components/PageBaseForm.tsx
index 52d1e7e4ce..ee1d43f6c9 100644
--- a/saleor/static/dashboard-next/page/components/PageBaseForm.tsx
+++ b/saleor/static/dashboard-next/page/components/PageBaseForm.tsx
@@ -12,26 +12,16 @@ import FormSpacer from "../../components/FormSpacer";
import RichTextEditor from "../../components/RichTextEditor";
import i18n from "../../i18n";
-interface PageUpdateFormComponentState {
+// FIXME: Change name
+interface PageBaseFormComponentProps {
availableOn: string;
content: string;
- isVisible: boolean;
- slug: string;
- title: string;
-}
-
-interface PageUpdateFormComponentProps {
created?: string;
errors?: Array<{ field: string; message: string }>;
- formInitialValues?: PageUpdateFormComponentState;
- handleSubmit(data: any);
-}
-
-interface CombinedErrorsType {
- availableOn: string;
- content: string;
+ isVisible: boolean;
slug: string;
title: string;
+ onChange?(event: React.ChangeEvent<any>);
}
const decorate = withStyles(theme => ({
@@ -45,136 +35,101 @@ const decorate = withStyles(theme => ({
}
}));
-export const PageUpdateFormComponent = decorate(
- class PageUpdateFormUnstyledComponent extends React.Component<
- PageUpdateFormComponentProps &
- WithStyles<"cardActions" | "addHelperTextPadding">,
- PageUpdateFormComponentState
- > {
- static defaultState = {
- availableOn: "",
- content: "",
- isVisible: false,
- slug: "",
- title: ""
- };
-
- constructor(props) {
- super(props);
- this.state = {
- ...PageUpdateFormUnstyledComponent.defaultState,
- ...props.formInitialValues
- };
- }
-
- handleChange = event => {
- this.setState({ [event.target.name]: event.target.value });
- };
-
- render() {
- const { classes, created, errors, handleSubmit } = this.props;
- const combinedErrors = errors
- ? (errors.reduce((prev, curr) => {
- prev[curr.field] = curr.message;
- return prev;
- }, {}) as CombinedErrorsType)
- : undefined;
- return (
- <>
- <CardContent>
- <Grid container spacing={16}>
- <Grid item xs={12} md={9}>
- <TextField
- name="title"
- label={i18n.t("Title", { context: "object" })}
- defaultValue={this.state.title}
- onChange={this.handleChange}
- className={
- (errors ? !combinedErrors.title : true)
- ? classes.addHelperTextPadding
- : ""
- }
- helperText={
- combinedErrors && combinedErrors.title
- ? combinedErrors.title
- : ""
- }
- fullWidth
- />
- <FormSpacer />
- <RichTextEditor
- defaultValue={this.state.content}
- name="content"
- label={i18n.t("Content", { context: "object" })}
- helperText={
- combinedErrors && combinedErrors.content
- ? combinedErrors.content
- : i18n.t("Select text to enable text-formatting tools.", {
- context: "object"
- })
- }
- onChange={this.handleChange}
- fullWidth
- />
- </Grid>
- <Grid item xs={12} md={3}>
- <TextField
- name="slug"
- label={i18n.t("Slug", { context: "object" })}
- defaultValue={this.state.slug}
- helperText={
- combinedErrors && combinedErrors.slug
- ? combinedErrors.slug
- : i18n.t("Slug is being used to create page URL", {
- context: "object"
- })
- }
- onChange={this.handleChange}
- fullWidth
- />
- <FormSpacer />
- <TextField
- name="availableOn"
- label={i18n.t("Available on", { context: "label" })}
- type="date"
- defaultValue={this.state.availableOn}
- onChange={this.handleChange}
- InputLabelProps={{
- shrink: true
- }}
- helperText={
- combinedErrors && combinedErrors.availableOn
- ? combinedErrors.availableOn
- : ""
- }
- />
- <FormSpacer />
- {created && (
- <Typography variant="body1">Created at: {created}</Typography>
- )}
- <FormSpacer />
- <ControlledCheckbox
- checked={this.state.isVisible}
- label={i18n.t("Published", { context: "label" })}
- name="isVisible"
- onChange={this.handleChange}
- />
- </Grid>
- </Grid>
- </CardContent>
- <Toolbar className={classes.cardActions}>
- <Button
- variant="raised"
- color="primary"
- onClick={() => handleSubmit(this.state)}
- >
- {i18n.t("Submit", { context: "label" })}
- </Button>
- </Toolbar>
- </>
- );
- }
+export const PageBaseForm = decorate<PageBaseFormComponentProps>(
+ ({
+ classes,
+ created,
+ errors,
+ title,
+ content,
+ slug,
+ availableOn,
+ isVisible,
+ onChange
+ }) => {
+ const errorList: { [key: string]: string } = errors.reduce((acc, curr) => {
+ acc[curr.field] = curr.message;
+ return acc;
+ }, {});
+ return (
+ <CardContent>
+ <Grid container spacing={16}>
+ <Grid item xs={12} md={9}>
+ <TextField
+ autoFocus
+ name="title"
+ label={i18n.t("Title", { context: "object" })}
+ value={title}
+ onChange={onChange}
+ className={
+ (errors ? !errorList.title : true)
+ ? classes.addHelperTextPadding
+ : ""
+ }
+ helperText={errorList && errorList.title ? errorList.title : ""}
+ fullWidth
+ />
+ <FormSpacer />
+ <RichTextEditor
+ value={content}
+ name="content"
+ label={i18n.t("Content", { context: "object" })}
+ helperText={
+ errorList && errorList.content
+ ? errorList.content
+ : i18n.t("Select text to enable text-formatting tools.", {
+ context: "object"
+ })
+ }
+ onChange={onChange}
+ fullWidth
+ />
+ </Grid>
+ <Grid item xs={12} md={3}>
+ <TextField
+ name="slug"
+ label={i18n.t("Slug", { context: "object" })}
+ value={slug}
+ helperText={
+ errorList && errorList.slug
+ ? errorList.slug
+ : i18n.t("Slug is being used to create page URL", {
+ context: "object"
+ })
+ }
+ onChange={onChange}
+ fullWidth
+ />
+ <FormSpacer />
+ <TextField
+ name="availableOn"
+ label={i18n.t("Available on", { context: "label" })}
+ type="date"
+ value={availableOn}
+ onChange={onChange}
+ InputLabelProps={{
+ shrink: true
+ }}
+ helperText={
+ errorList && errorList.availableOn ? errorList.availableOn : ""
+ }
+ />
+ <FormSpacer />
+ {created && (
+ <Typography variant="body1">Created at: {created}</Typography>
+ )}
+ <FormSpacer />
+ <ControlledCheckbox
+ checked={isVisible}
+ label={i18n.t("Published", { context: "label" })}
+ name="isVisible"
+ onChange={onChange}
+ />
+ </Grid>
+ </Grid>
+ </CardContent>
+ );
}
);
-export default PageUpdateFormComponent;
+export default PageBaseForm;
diff --git a/saleor/static/dashboard-next/page/index.tsx b/saleor/static/dashboard-next/page/index.tsx
index c8e455c5ca..6d97264a21 100644
--- a/saleor/static/dashboard-next/page/index.tsx
+++ b/saleor/static/dashboard-next/page/index.tsx
@@ -32,7 +32,9 @@ export function pageEditUrl(id: string) {
}
export function pageStorefrontUrl(slug: string) {
- return `/page/${slug}/`;
+ return `/pages/${slug}/`;
}
+export const pageListUrl = "/pages/";
+
export default Component;
diff --git a/saleor/static/dashboard-next/page/views/PageCreateForm.tsx b/saleor/static/dashboard-next/page/views/PageCreateForm.tsx
index 555800f64b..9d0b553984 100644
--- a/saleor/static/dashboard-next/page/views/PageCreateForm.tsx
+++ b/saleor/static/dashboard-next/page/views/PageCreateForm.tsx
@@ -2,12 +2,19 @@ import Card from "material-ui/Card";
import * as React from "react";
import { Redirect } from "react-router";
+import { pageListUrl } from "..";
+import Form, { FormActions, FormProps } from "../../components/Form";
import { NavigatorLink } from "../../components/Navigator";
import PageHeader from "../../components/PageHeader";
+import { PageCreateMutationVariables } from "../../gql-types";
import i18n from "../../i18n";
import PageBaseForm from "../components/PageBaseForm";
import { pageCreateMutation, TypedPageCreateMutation } from "../mutations";
+const PageForm: React.ComponentType<
+ FormProps<PageCreateMutationVariables>
+> = Form;
+
interface PageCreateFormProps {
id: string;
}
@@ -16,29 +23,56 @@ export const PageCreateForm: React.StatelessComponent<PageCreateFormProps> = ({
id
}) => (
<TypedPageCreateMutation mutation={pageCreateMutation}>
- {(createPage, { called, data, error, loading }) => {
- if (called && !loading && !data.pageCreate.errors.length) {
- if (error) {
- console.error(error);
- return;
- }
- return <Redirect to="/pages/" />;
+ {(createPage, { called, data: createResult, error, loading }) => {
+ if (called && !loading && !createResult.pageCreate.errors.length) {
+ return <Redirect to={pageListUrl} push={false} />;
}
return (
- <NavigatorLink to={"/pages/"}>
+ <NavigatorLink to={pageListUrl}>
{handleCancel => (
- <Card>
- <PageHeader
- onBack={handleCancel}
- title={i18n.t("Add page", { context: "title" })}
- />
- <PageBaseForm
- errors={called && data ? data.pageCreate.errors : undefined}
- handleSubmit={data =>
- createPage({ variables: { id, ...data } })
- }
- />
- </Card>
+ <PageForm
+ initial={{
+ availableOn: "",
+ content: "",
+ isVisible: false,
+ slug: "",
+ title: ""
+ }}
+ onSubmit={data =>
+ createPage({
+ variables: data
+ })
+ }
+ >
+ {({ change, data, submit: handleSubmit }) => (
+ <Card>
+ <PageHeader
+ onBack={handleCancel}
+ title={i18n.t("Add page", { context: "title" })}
+ />
+ <PageBaseForm
+ errors={
+ called && createResult
+ ? createResult.pageCreate.errors
+ : []
+ }
+ title={data.title}
+ content={data.content}
+ slug={data.slug}
+ availableOn={data.availableOn}
+ isVisible={data.isVisible}
+ onChange={change}
+ />
+ <FormActions
+ onCancel={handleCancel}
+ onSubmit={handleSubmit}
+ submitLabel={i18n.t("Save", {
+ context: "button"
+ })}
+ />
+ </Card>
+ )}
+ </PageForm>
)}
</NavigatorLink>
);
diff --git a/saleor/static/dashboard-next/page/views/PageUpdateForm.tsx b/saleor/static/dashboard-next/page/views/PageUpdateForm.tsx
index 8e4e3bc0b3..ba1abc5b5a 100644
--- a/saleor/static/dashboard-next/page/views/PageUpdateForm.tsx
+++ b/saleor/static/dashboard-next/page/views/PageUpdateForm.tsx
@@ -7,9 +7,12 @@ import * as React from "react";
import { Redirect } from "react-router";
import { DialogContentText } from "material-ui/Dialog";
+import { pageListUrl } from "..";
import ErrorMessageCard from "../../components/ErrorMessageCard";
+import Form, { FormActions, FormProps } from "../../components/Form";
import { NavigatorLink } from "../../components/Navigator";
import PageHeader from "../../components/PageHeader";
+import { PageUpdateMutationVariables } from "../../gql-types";
import i18n from "../../i18n";
import PageBaseForm from "../components/PageBaseForm";
import PageDeleteDialog from "../components/PageDeleteDialog";
@@ -29,6 +32,10 @@ interface PageUpdateFormState {
opened: boolean;
}
+const PageForm: React.ComponentType<
+ FormProps<PageUpdateMutationVariables>
+> = Form;
+
export class PageUpdateForm extends React.Component<
PageUpdateFormProps,
PageUpdateFormState
@@ -43,7 +50,7 @@ export class PageUpdateForm extends React.Component<
const { id } = this.props;
return (
<TypedPageDetailsQuery query={pageDetailsQuery} variables={{ id }}>
- {({ data, error, loading }) => {
+ {({ data: detailsResult, error, loading }) => {
if (error) {
return (
<Grid container spacing={16}>
@@ -56,9 +63,9 @@ export class PageUpdateForm extends React.Component<
);
}
if (loading) {
- return <>loading</>;
+ return <CircularProgress />;
}
- const { page } = data;
+ const { page } = detailsResult;
const formInitialValues = {
availableOn: page.availableOn,
content: page.content,
@@ -70,90 +77,124 @@ export class PageUpdateForm extends React.Component<
<TypedPageDeleteMutation mutation={pageDeleteMutation}>
{(
deletePage,
- { called, data, error, loading: deleteInProgress }
+ { called, data: deleteResult, error, loading: deleteInProgress }
) => {
if (called && !deleteInProgress) {
if (error) {
console.error(error);
return;
}
- if (data && data.pageDelete.errors.length) {
+ if (
+ deleteResult.pageDelete.errors &&
+ deleteResult.pageDelete.errors.length
+ ) {
+ console.error(deleteResult.pageDelete);
return;
}
- // FIXME: component is loaded with previous state (meaning that delete d page will still be there until table reload)
+ // FIXME: component is loaded with previous state (meaning that deleted page will still be there until table reload)
this.handleRemoveButtonClick();
- return <Redirect to="/pages/" />;
+ return <Redirect to={pageListUrl} />;
}
return (
<TypedPageUpdateMutation mutation={pageUpdateMutation}>
{(
updatePage,
- { called, data, error, loading: updateInProgress }
+ {
+ called,
+ data: updateResult,
+ error,
+ loading: updateInProgress
+ }
) => {
if (
called &&
!updateInProgress &&
- !data.pageUpdate.errors.length
+ !updateResult.pageUpdate.errors.length
) {
if (error) {
console.error(error);
return;
}
- return <Redirect to="/pages/" />;
+ return <Redirect to={pageListUrl} />;
}
return (
- <NavigatorLink to={"/pages/"}>
+ <NavigatorLink to={pageListUrl}>
{handleCancel => (
- <Card>
- <PageHeader
- onBack={handleCancel}
- title={i18n.t("Page details", {
- context: "title"
- })}
- >
- <IconButton
- onClick={this.handleRemoveButtonClick}
+ <>
+ <Card>
+ <PageForm
+ initial={{
+ availableOn: page.availableOn || "",
+ content: page.content || "",
+ id,
+ isVisible: page.isVisible,
+ slug: page.slug || "",
+ title: page.title || ""
+ }}
+ onSubmit={data =>
+ updatePage({
+ variables: data
+ })
+ }
>
- <DeleteIcon />
- </IconButton>
- </PageHeader>
- {loading ? (
- <CircularProgress />
- ) : (
- <>
- <PageBaseForm
- handleSubmit={data =>
- updatePage({
- variables: { id, ...data }
- })
- }
- formInitialValues={formInitialValues}
- created={page.created}
- errors={
- data ? data.pageUpdate.errors : undefined
- }
- />
- {!loading ? (
- <PageDeleteDialog
- onClose={this.handleRemoveButtonClick}
- onConfirm={() =>
- deletePage({ variables: { id } })
- }
- opened={this.state.opened}
- >
- <DialogContentText
- dangerouslySetInnerHTML={{
- __html: i18n.t(
- "Are you sure you want to remove <strong>{{name}}</strong>?",
- { name: page.title }
- )
- }}
+ {({ change, data, submit: handleSubmit }) => (
+ <>
+ <PageHeader
+ onBack={handleCancel}
+ title={i18n.t("Page details", {
+ context: "title"
+ })}
+ >
+ <IconButton
+ onClick={this.handleRemoveButtonClick}
+ >
+ <DeleteIcon />
+ </IconButton>
+ </PageHeader>
+ <PageBaseForm
+ onChange={change}
+ created={page.created}
+ errors={
+ updateResult
+ ? updateResult.pageUpdate.errors
+ : []
+ }
+ title={data.title}
+ content={data.content}
+ slug={data.slug}
+ availableOn={data.availableOn}
+ isVisible={data.isVisible}
/>
- </PageDeleteDialog>
- ) : null}
- </>
+ <FormActions
+ onCancel={handleCancel}
+ onSubmit={handleSubmit}
+ submitLabel={i18n.t("Save", {
+ context: "button"
+ })}
+ />
+ </>
+ )}
+ </PageForm>
+ </Card>
+ {!loading && (
+ <PageDeleteDialog
+ onClose={this.handleRemoveButtonClick}
+ onConfirm={() =>
+ deletePage({ variables: { id } })
+ }
+ opened={this.state.opened}
+ >
+ <DialogContentText
+ dangerouslySetInnerHTML={{
+ __html: i18n.t(
+ "Are you sure you want to remove <strong>{{name}}</strong>?",
+ { name: page.title }
+ )
+ }}
+ />
+ </PageDeleteDialog>
)}
- </Card>
+ </>
)}
</NavigatorLink>
);