import Button from "@material-ui/core/Button"; import Dialog from "@material-ui/core/Dialog"; import DialogActions from "@material-ui/core/DialogActions"; import DialogContent from "@material-ui/core/DialogContent"; import DialogTitle from "@material-ui/core/DialogTitle"; import { createStyles, Theme, withStyles, WithStyles } from "@material-ui/core/styles"; import Table from "@material-ui/core/Table"; import TableBody from "@material-ui/core/TableBody"; import TableCell from "@material-ui/core/TableCell"; import TableRow from "@material-ui/core/TableRow"; import TextField from "@material-ui/core/TextField"; import Typography from "@material-ui/core/Typography"; import { filter } from "fuzzaldrin"; import React from "react"; import { FormattedMessage, useIntl } from "react-intl"; 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"; // tslint:disable no-submodule-imports import { ShopInfo_shop_countries } from "@saleor/components/Shop/types/ShopInfo"; import { buttonMessages } from "@saleor/intl"; 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 styles = theme => createStyles({ checkboxCell: { paddingLeft: 0 }, container: { maxHeight: 400 }, heading: { marginBottom: theme.spacing(2), marginTop: theme.spacing(2) }, table: { border: "1px solid " + theme.palette.grey[200] }, wideCell: { width: "100%" } }); const ShippingZoneCountriesAssignDialog = withStyles(styles, { name: "ShippingZoneCountriesAssignDialog" })( ({ classes, confirmButtonState, isDefault, onClose, countries, open, initial, onConfirm }: ShippingZoneCountriesAssignDialogProps & WithStyles) => { const intl = useIntl(); const initialForm: FormData = { countries: initial, query: "", restOfTheWorld: isDefault }; return (
{({ data, change }) => { const countrySelectionMap = countries.reduce((acc, country) => { acc[country.code] = !!data.countries.find( selectedCountries => selectedCountries === country.code ); return acc; }, {}); return ( <> change(event, () => fetch(data.query))} label={intl.formatMessage({ defaultMessage: "Search Countries" })} placeholder={intl.formatMessage({ defaultMessage: "Search by country name" })} fullWidth />
change({ target: { name: "restOfTheWorld" as keyof FormData, value: !data.restOfTheWorld } } as any) } />
{filter(countries, data.query, { key: "country" }).map(country => { const isChecked = countrySelectionMap[country.code]; return ( {country.country} 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) } /> ); })}
); }}
); } ); ShippingZoneCountriesAssignDialog.displayName = "ShippingZoneCountriesAssignDialog"; export default ShippingZoneCountriesAssignDialog;