Skip to content

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

Add country assigning

dominik-zeglen5c6a43b90c37
diff --git a/saleor/graphql/shipping/mutations.py b/saleor/graphql/shipping/mutations.py
index 688776a743..d562087496 100644
--- a/saleor/graphql/shipping/mutations.py
+++ b/saleor/graphql/shipping/mutations.py
@@ -51,7 +51,7 @@ class ShippingZoneMixin:
def clean_input(cls, info, instance, input, errors):
cleaned_input = super().clean_input(info, instance, input, errors)
default = cleaned_input.get('default')
- if default is not None:
+ if default:
if default_shipping_zone_exists(instance.pk):
cls.add_error(
errors, 'default', 'Default shipping zone already exists.')
diff --git a/saleor/static/dashboard-next/components/CountryList/CountryList.tsx b/saleor/static/dashboard-next/components/CountryList/CountryList.tsx
index 9639d6e93a..95e94fdab7 100644
--- a/saleor/static/dashboard-next/components/CountryList/CountryList.tsx
+++ b/saleor/static/dashboard-next/components/CountryList/CountryList.tsx
@@ -170,7 +170,9 @@ const CountryList = withStyles(styles, {
),
() => (
<TableRow>
- <TableCell colSpan={2}>{emptyText}</TableCell>
+ <TableCell className={classes.toLeft} colSpan={2}>
+ {emptyText}
+ </TableCell>
</TableRow>
)
)}
diff --git a/saleor/static/dashboard-next/shipping/components/ShippingZoneCountriesAssignDialog/ShippingZoneCountriesAssignDialog.tsx b/saleor/static/dashboard-next/shipping/components/ShippingZoneCountriesAssignDialog/ShippingZoneCountriesAssignDialog.tsx
new file mode 100644
index 0000000000..5530cce065
--- /dev/null
+++ b/saleor/static/dashboard-next/shipping/components/ShippingZoneCountriesAssignDialog/ShippingZoneCountriesAssignDialog.tsx
@@ -0,0 +1,232 @@
+import Button from "@material-ui/core/Button";
+import Checkbox from "@material-ui/core/Checkbox";
+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 * as React from "react";
+
+import ConfirmButton, {
+ ConfirmButtonTransitionState
+} from "../../../components/ConfirmButton/ConfirmButton";
+import Form from "../../../components/Form";
+import FormSpacer from "../../../components/FormSpacer";
+import Hr from "../../../components/Hr";
+import { ShopInfo_shop_countries } from "../../../components/Shop/types/ShopInfo";
+import i18n from "../../../i18n";
+
+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: Theme) =>
+ createStyles({
+ checkboxCell: {
+ paddingLeft: 0
+ },
+ container: {
+ maxHeight: 400
+ },
+ heading: {
+ marginBottom: theme.spacing.unit * 2,
+ marginTop: theme.spacing.unit * 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<typeof styles>) => {
+ const initialForm: FormData = {
+ countries: initial,
+ query: "",
+ restOfTheWorld: isDefault
+ };
+ return (
+ <Dialog open={open} fullWidth maxWidth="sm">
+ <Form initial={initialForm} onSubmit={onConfirm}>
+ {({ data, change }) => {
+ const countrySelectionMap = countries.reduce((acc, country) => {
+ acc[country.code] = !!data.countries.find(
+ selectedCountries => selectedCountries === country.code
+ );
+ return acc;
+ }, {});
+
+ return (
+ <>
+ <DialogTitle>{i18n.t("Assign Countries")}</DialogTitle>
+ <DialogContent>
+ <Typography>
+ {i18n.t(
+ "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={i18n.t("Search Countries", {
+ context: "country search input label"
+ })}
+ placeholder={i18n.t("Search by country name", {
+ context: "country search input placeholder"
+ })}
+ fullWidth
+ />
+ </DialogContent>
+ <Hr />
+
+ <DialogContent className={classes.container}>
+ <Typography className={classes.heading} variant="subheading">
+ {i18n.t("Quick Pick")}
+ </Typography>
+ <Table className={classes.table}>
+ <TableBody>
+ <TableRow>
+ <TableCell className={classes.wideCell}>
+ {i18n.t("Rest of the World")}
+ <Typography variant="caption">
+ {i18n.t(
+ "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>
+ </Table>
+ </DialogContent>
+
+ <DialogContent className={classes.container}>
+ <Typography className={classes.heading} variant="subheading">
+ {i18n.t("Countries A to Z", {
+ context: "country selection"
+ })}
+ </Typography>
+ <Table className={classes.table}>
+ <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>
+ );
+ })}
+ </TableBody>
+ </Table>
+ </DialogContent>
+ <DialogActions>
+ <Button onClick={onClose}>
+ {i18n.t("Cancel", { context: "button" })}
+ </Button>
+ <ConfirmButton
+ transitionState={confirmButtonState}
+ color="primary"
+ variant="contained"
+ type="submit"
+ >
+ {i18n.t("Assign countries", { context: "button" })}
+ </ConfirmButton>
+ </DialogActions>
+ </>
+ );
+ }}
+ </Form>
+ </Dialog>
+ );
+ }
+);
+ShippingZoneCountriesAssignDialog.displayName =
+ "ShippingZoneCountriesAssignDialog";
+export default ShippingZoneCountriesAssignDialog;
diff --git a/saleor/static/dashboard-next/shipping/components/ShippingZoneCountriesAssignDialog/index.ts b/saleor/static/dashboard-next/shipping/components/ShippingZoneCountriesAssignDialog/index.ts
new file mode 100644
index 0000000000..882ec3f994
--- /dev/null
+++ b/saleor/static/dashboard-next/shipping/components/ShippingZoneCountriesAssignDialog/index.ts
@@ -0,0 +1,2 @@
+export { default } from './ShippingZoneCountriesAssignDialog';
+export * from './ShippingZoneCountriesAssignDialog';
\ No newline at end of file
diff --git a/saleor/static/dashboard-next/shipping/components/ShippingZoneDetailsPage/ShippingZoneDetailsPage.tsx b/saleor/static/dashboard-next/shipping/components/ShippingZoneDetailsPage/ShippingZoneDetailsPage.tsx
index 182e55ed99..62f87f768d 100644
--- a/saleor/static/dashboard-next/shipping/components/ShippingZoneDetailsPage/ShippingZoneDetailsPage.tsx
+++ b/saleor/static/dashboard-next/shipping/components/ShippingZoneDetailsPage/ShippingZoneDetailsPage.tsx
@@ -67,7 +67,17 @@ const ShippingZoneDetailsPage: React.StatelessComponent<
<CountryList
countries={maybe(() => shippingZone.countries)}
disabled={disabled}
- emptyText={i18n.t("Placeholder")}
+ emptyText={maybe(
+ () =>
+ shippingZone.default
+ ? i18n.t(
+ "This is default shipping zone, which means that it covers all of the countries which are not assigned to other shipping zones"
+ )
+ : i18n.t(
+ "Currently, there are no countries assigned to this shipping zone"
+ ),
+ "..."
+ )}
onCountryAssign={onCountryAdd}
onCountryUnassign={onCountryRemove}
title={i18n.t("Countries")}
diff --git a/saleor/static/dashboard-next/shipping/views/ShippingZoneDetails/index.tsx b/saleor/static/dashboard-next/shipping/views/ShippingZoneDetails/index.tsx
index 129f2c4d73..46edabcadb 100644
--- a/saleor/static/dashboard-next/shipping/views/ShippingZoneDetails/index.tsx
+++ b/saleor/static/dashboard-next/shipping/views/ShippingZoneDetails/index.tsx
@@ -9,6 +9,7 @@ import Shop from "../../../components/Shop";
import i18n from "../../../i18n";
import { getMutationState, maybe } from "../../../misc";
import { ShippingMethodTypeEnum } from "../../../types/globalTypes";
+import ShippingZoneCountriesAssignDialog from "../../components/ShippingZoneCountriesAssignDialog";
import ShippingZoneDetailsPage from "../../components/ShippingZoneDetailsPage";
import ShippingZoneRateDialog from "../../components/ShippingZoneRateDialog";
import { TypedShippingZone } from "../../queries";
@@ -20,6 +21,8 @@ import { UpdateShippingZone } from "../../types/UpdateShippingZone";
import { shippingZonesListUrl, shippingZoneUrl } from "../../urls";
import ShippingZoneOperations from "./ShippingZoneOperations";
import {
+ shippingZoneAssignCountryPath,
+ shippingZoneAssignCountryUrl,
shippingZoneDeletePath,
shippingZoneDeleteUrl,
shippingZonePriceRateCreatePath,
@@ -28,6 +31,8 @@ import {
shippingZoneRateDeleteUrl,
shippingZoneRatePath,
shippingZoneRateUrl,
+ shippingZoneUnassignCountryPath,
+ shippingZoneUnassignCountryUrl,
shippingZoneWeightRateCreatePath,
shippingZoneWeightRateCreateUrl
} from "./urls";
@@ -94,6 +99,7 @@ const ShippingZoneDetails: React.StatelessComponent<
pushMessage({
text: i18n.t("Successfully updated shipping zone")
});
+ closeModal();
}
};
@@ -166,8 +172,14 @@ const ShippingZoneDetails: React.StatelessComponent<
<ShippingZoneDetailsPage
disabled={loading}
onBack={() => navigate(shippingZonesListUrl)}
- onCountryAdd={() => undefined}
- onCountryRemove={() => undefined}
+ onCountryAdd={() =>
+ navigate(shippingZoneAssignCountryUrl(id))
+ }
+ onCountryRemove={code =>
+ navigate(
+ shippingZoneUnassignCountryUrl(id, code)
+ )
+ }
onDelete={() =>
navigate(shippingZoneDeleteUrl(id))
}
@@ -198,6 +210,38 @@ const ShippingZoneDetails: React.StatelessComponent<
shippingZone={maybe(() => data.shippingZone)}
/>
<Switch>
+ <Route
+ exact
+ path={shippingZoneAssignCountryPath(":id")}
+ render={({ match }) => (
+ <ShippingZoneCountriesAssignDialog
+ confirmButtonState={formTransitionState}
+ countries={maybe(() => shop.countries, [])}
+ initial={maybe(
+ () =>
+ data.shippingZone.countries.map(
+ country => country.code
+ ),
+ []
+ )}
+ isDefault={maybe(
+ () => data.shippingZone.default,
+ false
+ )}
+ onClose={closeModal}
+ onConfirm={formData =>
+ ops.shippingZoneUpdate.mutate({
+ id,
+ input: {
+ countries: formData.countries,
+ default: formData.restOfTheWorld
+ }
+ })
+ }
+ open={!!match}
+ />
+ )}
+ />
<Route
exact
path={shippingZonePriceRateCreatePath(
@@ -269,6 +313,54 @@ const ShippingZoneDetails: React.StatelessComponent<
</ActionDialog>
)}
/>
+ <Route
+ path={shippingZoneUnassignCountryPath(
+ ":id",
+ ":code"
+ )}
+ render={({ match }) => (
+ <ActionDialog
+ confirmButtonState={formTransitionState}
+ onClose={closeModal}
+ onConfirm={() =>
+ ops.shippingZoneUpdate.mutate({
+ id,
+ input: {
+ countries: data.shippingZone.countries
+ .filter(
+ country =>
+ country.code !==
+ match.params.code
+ )
+ .map(country => country.code)
+ }
+ })
+ }
+ open={!!match}
+ title={i18n.t("Remove from shipping zone")}
+ variant="delete"
+ >
+ <DialogContentText
+ dangerouslySetInnerHTML={{
+ __html: i18n.t(
+ "Are you sure you want to remove <strong>{{ name }}</strong> from this shipping zone?",
+ {
+ context: "unassign country",
+ name: maybe(
+ () =>
+ data.shippingZone.countries.find(
+ country =>
+ country.code ===
+ match.params.code
+ ).country
+ )
+ }
+ )
+ }}
+ />
+ </ActionDialog>
+ )}
+ />
<Route
path={shippingZoneRateDeletePath(
":shippingZoneId",
diff --git a/saleor/static/dashboard-next/shipping/views/ShippingZoneDetails/urls.ts b/saleor/static/dashboard-next/shipping/views/ShippingZoneDetails/urls.ts
index c7c325e7ac..78bdfad47c 100644
--- a/saleor/static/dashboard-next/shipping/views/ShippingZoneDetails/urls.ts
+++ b/saleor/static/dashboard-next/shipping/views/ShippingZoneDetails/urls.ts
@@ -37,3 +37,13 @@ export const shippingZoneDeletePath = (id: string) =>
urlJoin(shippingZonePath(id), "delete");
export const shippingZoneDeleteUrl = (id: string) =>
shippingZoneDeletePath(encodeURIComponent(id));
+
+export const shippingZoneAssignCountryPath = (id: string) =>
+ urlJoin(shippingZonePath(id), "assign-countries");
+export const shippingZoneAssignCountryUrl = (id: string) =>
+ shippingZoneAssignCountryPath(encodeURIComponent(id));
+
+export const shippingZoneUnassignCountryPath = (id: string, code: string) =>
+ urlJoin(shippingZonePath(id), "unassign-country", code);
+export const shippingZoneUnassignCountryUrl = (id: string, code: string) =>
+ shippingZoneUnassignCountryPath(encodeURIComponent(id), code);
diff --git a/saleor/static/dashboard-next/storybook/config.js b/saleor/static/dashboard-next/storybook/config.js
index 486f5e75ef..91ec42607f 100644
--- a/saleor/static/dashboard-next/storybook/config.js
+++ b/saleor/static/dashboard-next/storybook/config.js
@@ -115,6 +115,7 @@ function loadStories() {
require("./stories/productTypes/ProductTypeListPage");
// Shipping
+ require("./stories/shipping/ShippingZoneCountriesAssignDialog");
require("./stories/shipping/ShippingZoneDetailsPage");
require("./stories/shipping/ShippingZonesListPage");
require("./stories/shipping/ShippingZoneRateDialog");
diff --git a/saleor/static/dashboard-next/storybook/stories/shipping/ShippingZoneCountriesAssignDialog.tsx b/saleor/static/dashboard-next/storybook/stories/shipping/ShippingZoneCountriesAssignDialog.tsx
new file mode 100644
index 0000000000..89328ccb87
--- /dev/null
+++ b/saleor/static/dashboard-next/storybook/stories/shipping/ShippingZoneCountriesAssignDialog.tsx
@@ -0,0 +1,21 @@
+import { storiesOf } from "@storybook/react";
+import * as React from "react";
+
+import ShippingZoneCountriesAssignDialog, {
+ ShippingZoneCountriesAssignDialogProps
+} from "../../../shipping/components/ShippingZoneCountriesAssignDialog";
+import Decorator from "../../Decorator";
+import { countries } from "../taxes/fixtures";
+
+const props: ShippingZoneCountriesAssignDialogProps = {
+ confirmButtonState: "default",
+ countries,
+ initial: ["PL", "GB", "DE"],
+ onClose: () => undefined,
+ onConfirm: () => undefined,
+ open: true
+};
+
+storiesOf("Shipping / Assign countries", module)
+ .addDecorator(Decorator)
+ .add("default", () => <ShippingZoneCountriesAssignDialog {...props} />);