saleor-dashboard/src/collections/components/CollectionDetailsPage/CollectionDetailsPage.tsx
Wojciech Mista a5ac6bb92e
Exit form fixes (#1889)
* Add onBeforeUnload handler to prevent accidental refresh

* Update button messages

* Fix exit form not working after submit

* Make onBeforeUnload disable if env is development

* Fix onClose

* Remove internal date time field state

* Update messages and dialog

* Prevent navigation on 400 error

* Add submit disabled ref in exit form

* Update exit form dialog for disabled save

* Update confirmLeave forms to set ref if save is disabled

* Remove unused error handling

* Remove explicit ref type

* Remove unused import

* Fix disabled type

* Add disable check function to generic forms

* Add custom isDisabled method to sale and voucher forms

* Add default isDisabled functions to confirmLeave forms

* Update tests

* Remove unused code

* Rebase fixes + update tests

* Refactor form and useform

* Refactor disabling forms

* Change "saveDisabled" name to "isSaveDisabled" for improved readability

* Change "isDisabled" function to "checkIfSaveIsDisabled"

* Update exit form disabling conditions for zone rates forms
2022-03-23 10:13:23 +01:00

166 lines
5.7 KiB
TypeScript

import { ChannelCollectionData } from "@saleor/channels/utils";
import { CardSpacer } from "@saleor/components/CardSpacer";
import ChannelsAvailabilityCard from "@saleor/components/ChannelsAvailabilityCard";
import { Container } from "@saleor/components/Container";
import Grid from "@saleor/components/Grid";
import Metadata from "@saleor/components/Metadata/Metadata";
import PageHeader from "@saleor/components/PageHeader";
import Savebar from "@saleor/components/Savebar";
import SeoForm from "@saleor/components/SeoForm";
import {
CollectionChannelListingErrorFragment,
CollectionDetailsQuery,
CollectionErrorFragment,
PermissionEnum
} from "@saleor/graphql";
import { SubmitPromise } from "@saleor/hooks/useForm";
import { sectionNames } from "@saleor/intl";
import { Backlink, ConfirmButtonTransitionState } from "@saleor/macaw-ui";
import React from "react";
import { useIntl } from "react-intl";
import { ChannelProps, ListActions, PageListProps } from "../../../types";
import CollectionDetails from "../CollectionDetails/CollectionDetails";
import { CollectionImage } from "../CollectionImage/CollectionImage";
import CollectionProducts from "../CollectionProducts/CollectionProducts";
import CollectionUpdateForm, { CollectionUpdateData } from "./form";
export interface CollectionDetailsPageProps
extends PageListProps,
ListActions,
ChannelProps {
channelsCount: number;
channelsErrors: CollectionChannelListingErrorFragment[];
collection: CollectionDetailsQuery["collection"];
currentChannels: ChannelCollectionData[];
errors: CollectionErrorFragment[];
hasChannelChanged: boolean;
saveButtonBarState: ConfirmButtonTransitionState;
onBack: () => void;
onCollectionRemove: () => void;
onImageDelete: () => void;
onImageUpload: (file: File) => void;
onProductUnassign: (id: string, event: React.MouseEvent<any>) => void;
onSubmit: (data: CollectionUpdateData) => SubmitPromise;
onChannelsChange: (data: ChannelCollectionData[]) => void;
openChannelsModal: () => void;
}
const CollectionDetailsPage: React.FC<CollectionDetailsPageProps> = ({
channelsCount,
channelsErrors,
collection,
currentChannels = [],
disabled,
errors,
hasChannelChanged,
saveButtonBarState,
selectedChannelId,
onBack,
onCollectionRemove,
onImageDelete,
onImageUpload,
onSubmit,
onChannelsChange,
openChannelsModal,
...collectionProductsProps
}: CollectionDetailsPageProps) => {
const intl = useIntl();
return (
<CollectionUpdateForm
collection={collection}
currentChannels={currentChannels}
setChannels={onChannelsChange}
onSubmit={onSubmit}
disabled={disabled}
hasChannelChanged={hasChannelChanged}
>
{({ change, data, handlers, submit, isSaveDisabled }) => (
<Container>
<Backlink onClick={onBack}>
{intl.formatMessage(sectionNames.collections)}
</Backlink>
<PageHeader title={collection?.name} />
<Grid>
<div>
<CollectionDetails
data={data}
disabled={disabled}
errors={errors}
onChange={change}
onDescriptionChange={handlers.changeDescription}
/>
<CardSpacer />
<CollectionImage
data={data}
image={collection?.backgroundImage}
onImageDelete={onImageDelete}
onImageUpload={onImageUpload}
onChange={change}
/>
<CardSpacer />
<Metadata data={data} onChange={handlers.changeMetadata} />
<CardSpacer />
<CollectionProducts
disabled={disabled}
collection={collection}
{...collectionProductsProps}
/>
<CardSpacer />
<SeoForm
description={data.seoDescription}
disabled={disabled}
descriptionPlaceholder=""
helperText={intl.formatMessage({
defaultMessage:
"Add search engine title and description to make this collection easier to find"
})}
errors={errors}
slug={data.slug}
slugPlaceholder={data.name}
title={data.seoTitle}
titlePlaceholder={collection?.name}
onChange={change}
/>
</div>
<div>
<div>
<ChannelsAvailabilityCard
managePermissions={[PermissionEnum.MANAGE_PRODUCTS]}
messages={{
hiddenLabel: intl.formatMessage({
defaultMessage: "Hidden",
description: "collection label"
}),
visibleLabel: intl.formatMessage({
defaultMessage: "Visible",
description: "collection label"
})
}}
errors={channelsErrors}
selectedChannelsCount={data.channelListings.length}
allChannelsCount={channelsCount}
channels={data.channelListings}
disabled={disabled}
onChange={handlers.changeChannels}
openModal={openChannelsModal}
/>
</div>
</div>
</Grid>
<Savebar
state={saveButtonBarState}
disabled={isSaveDisabled}
onCancel={onBack}
onDelete={onCollectionRemove}
onSubmit={submit}
/>
</Container>
)}
</CollectionUpdateForm>
);
};
CollectionDetailsPage.displayName = "CollectionDetailsPage";
export default CollectionDetailsPage;