saleor-dashboard/src/shipping/components/ShippingZoneCountriesAssignDialog/ShippingZoneCountriesAssignDialog.tsx

220 lines
7.8 KiB
TypeScript
Raw Normal View History

import {
Button,
Dialog,
DialogActions,
DialogContent,
DialogTitle,
TableBody,
TableCell,
TableRow,
TextField,
Typography
} from "@material-ui/core";
2019-06-19 14:40:52 +00:00
import Checkbox from "@saleor/components/Checkbox";
import ConfirmButton, {
ConfirmButtonTransitionState
} from "@saleor/components/ConfirmButton";
import Form from "@saleor/components/Form";
import FormSpacer from "@saleor/components/FormSpacer";
import Hr from "@saleor/components/Hr";
2019-11-04 14:25:23 +00:00
import ResponsiveTable from "@saleor/components/ResponsiveTable";
2019-06-19 14:40:52 +00:00
import { ShopInfo_shop_countries } from "@saleor/components/Shop/types/ShopInfo";
import { buttonMessages } from "@saleor/intl";
2021-11-23 09:03:07 +00:00
import useScrollableDialogStyle from "@saleor/styles/useScrollableDialogStyle";
import { filter } from "fuzzaldrin";
import React from "react";
import { FormattedMessage, useIntl } from "react-intl";
2019-06-19 14:40:52 +00:00
2021-11-23 09:03:07 +00:00
import { useStyles } from "./styles";
2019-06-19 14:40:52 +00:00
interface FormData {
countries: string[];
query: string;
restOfTheWorld: boolean;
}
export interface ShippingZoneCountriesAssignDialogProps {
confirmButtonState: ConfirmButtonTransitionState;
countries: ShopInfo_shop_countries[];
initial: string[];
isDefault: boolean;
open: boolean;
onClose: () => void;
onConfirm: (data: FormData) => void;
}
const ShippingZoneCountriesAssignDialog: React.FC<ShippingZoneCountriesAssignDialogProps> = props => {
2019-10-30 14:34:24 +00:00
const {
2019-06-19 14:40:52 +00:00
confirmButtonState,
isDefault,
onClose,
countries,
open,
initial,
onConfirm
2019-10-30 14:34:24 +00:00
} = props;
const classes = useStyles(props);
2021-11-23 09:03:07 +00:00
const scrollableDialogClasses = useScrollableDialogStyle();
2019-10-30 14:34:24 +00:00
const intl = useIntl();
const initialForm: FormData = {
countries: initial,
query: "",
restOfTheWorld: isDefault
};
return (
<Dialog onClose={onClose} open={open} fullWidth maxWidth="sm">
2021-11-23 09:03:07 +00:00
<Form
initial={initialForm}
onSubmit={onConfirm}
className={scrollableDialogClasses.form}
>
2019-10-30 14:34:24 +00:00
{({ data, change }) => {
const countrySelectionMap = countries.reduce((acc, country) => {
acc[country.code] = !!data.countries.find(
selectedCountries => selectedCountries === country.code
);
return acc;
}, {});
2019-10-30 14:34:24 +00:00
return (
<>
<DialogTitle>
<FormattedMessage
defaultMessage="Assign Countries"
description="dialog header"
/>
</DialogTitle>
<DialogContent>
<Typography>
<FormattedMessage defaultMessage="Choose countries you want to add to shipping zone from list below" />
</Typography>
<FormSpacer />
<TextField
name="query"
value={data.query}
onChange={event => change(event, () => fetch(data.query))}
label={intl.formatMessage({
defaultMessage: "Search Countries"
})}
placeholder={intl.formatMessage({
defaultMessage: "Search by country name"
})}
fullWidth
/>
2021-11-23 09:03:07 +00:00
<FormSpacer />
<Hr />
<FormSpacer />
<Typography variant="subtitle1">
2019-10-30 14:34:24 +00:00
<FormattedMessage defaultMessage="Quick Pick" />
</Typography>
2021-11-23 09:03:07 +00:00
<FormSpacer />
2019-11-04 14:25:23 +00:00
<ResponsiveTable className={classes.table}>
2019-10-30 14:34:24 +00:00
<TableBody>
<TableRow>
<TableCell className={classes.wideCell}>
<FormattedMessage defaultMessage="Rest of the World" />
<Typography variant="caption">
<FormattedMessage defaultMessage="If selected, this will add all of the countries not selected to other shipping zones" />
</Typography>
</TableCell>
<TableCell
padding="checkbox"
className={classes.checkboxCell}
>
<Checkbox
checked={data.restOfTheWorld}
onChange={() =>
change({
target: {
name: "restOfTheWorld" as keyof FormData,
value: !data.restOfTheWorld
}
} as any)
}
/>
</TableCell>
</TableRow>
</TableBody>
2019-11-04 14:25:23 +00:00
</ResponsiveTable>
2021-11-23 09:03:07 +00:00
<FormSpacer />
<Typography variant="subtitle1">
<FormattedMessage
2019-10-30 14:34:24 +00:00
defaultMessage="Countries A to Z"
description="country selection"
/>
2019-10-30 14:34:24 +00:00
</Typography>
</DialogContent>
2021-11-23 09:03:07 +00:00
<DialogContent className={scrollableDialogClasses.scrollArea}>
2019-11-04 14:25:23 +00:00
<ResponsiveTable className={classes.table}>
2019-10-30 14:34:24 +00:00
<TableBody>
{filter(countries, data.query, {
key: "country"
}).map(country => {
const isChecked = countrySelectionMap[country.code];
return (
<TableRow key={country.code}>
<TableCell className={classes.wideCell}>
{country.country}
</TableCell>
<TableCell
padding="checkbox"
className={classes.checkboxCell}
>
<Checkbox
checked={isChecked}
onChange={() =>
isChecked
? change({
target: {
name: "countries" as keyof FormData,
value: data.countries.filter(
selectedCountries =>
selectedCountries !== country.code
)
}
} as any)
: change({
target: {
name: "countries" as keyof FormData,
value: [...data.countries, country.code]
}
} as any)
}
/>
</TableCell>
</TableRow>
);
2019-06-19 14:40:52 +00:00
})}
2019-10-30 14:34:24 +00:00
</TableBody>
2019-11-04 14:25:23 +00:00
</ResponsiveTable>
2019-10-30 14:34:24 +00:00
</DialogContent>
<DialogActions>
<Button onClick={onClose}>
<FormattedMessage {...buttonMessages.back} />
</Button>
<ConfirmButton
transitionState={confirmButtonState}
color="primary"
variant="contained"
type="submit"
>
<FormattedMessage
defaultMessage="Assign countries"
description="button"
2019-06-19 14:40:52 +00:00
/>
2019-10-30 14:34:24 +00:00
</ConfirmButton>
</DialogActions>
</>
);
}}
</Form>
</Dialog>
);
};
2019-06-19 14:40:52 +00:00
ShippingZoneCountriesAssignDialog.displayName =
"ShippingZoneCountriesAssignDialog";
export default ShippingZoneCountriesAssignDialog;