Use dedicated mutations to assign warehouses
This commit is contained in:
parent
37b43a064c
commit
d69c1fe916
4 changed files with 15 additions and 190 deletions
|
@ -1,7 +1,6 @@
|
||||||
import gql from "graphql-tag";
|
import gql from "graphql-tag";
|
||||||
|
|
||||||
import makeMutation from "@saleor/hooks/makeMutation";
|
import makeMutation from "@saleor/hooks/makeMutation";
|
||||||
import { warehouseErrorFragment } from "@saleor/warehouses/mutations";
|
|
||||||
import { countryFragment } from "../taxes/queries";
|
import { countryFragment } from "../taxes/queries";
|
||||||
import { shippingMethodFragment, shippingZoneDetailsFragment } from "./queries";
|
import { shippingMethodFragment, shippingZoneDetailsFragment } from "./queries";
|
||||||
import {
|
import {
|
||||||
|
@ -40,14 +39,6 @@ import {
|
||||||
UpdateShippingZone,
|
UpdateShippingZone,
|
||||||
UpdateShippingZoneVariables
|
UpdateShippingZoneVariables
|
||||||
} from "./types/UpdateShippingZone";
|
} from "./types/UpdateShippingZone";
|
||||||
import {
|
|
||||||
AssignShippingZoneToWarehouse,
|
|
||||||
AssignShippingZoneToWarehouseVariables
|
|
||||||
} from "./types/AssignShippingZoneToWarehouse";
|
|
||||||
import {
|
|
||||||
UnassignShippingZoneToWarehouse,
|
|
||||||
UnassignShippingZoneToWarehouseVariables
|
|
||||||
} from "./types/UnassignShippingZoneToWarehouse";
|
|
||||||
|
|
||||||
export const shippingErrorFragment = gql`
|
export const shippingErrorFragment = gql`
|
||||||
fragment ShippingErrorFragment on ShippingError {
|
fragment ShippingErrorFragment on ShippingError {
|
||||||
|
@ -223,45 +214,3 @@ export const useShippingRateBulkDelete = makeMutation<
|
||||||
BulkDeleteShippingRate,
|
BulkDeleteShippingRate,
|
||||||
BulkDeleteShippingRateVariables
|
BulkDeleteShippingRateVariables
|
||||||
>(bulkDeleteShippingRate);
|
>(bulkDeleteShippingRate);
|
||||||
|
|
||||||
const assignShippingZoneToWarehouse = gql`
|
|
||||||
${warehouseErrorFragment}
|
|
||||||
mutation AssignShippingZoneToWarehouse(
|
|
||||||
$warehouseId: ID!
|
|
||||||
$shippingZoneId: ID!
|
|
||||||
) {
|
|
||||||
assignWarehouseShippingZone(
|
|
||||||
id: $warehouseId
|
|
||||||
shippingZoneIds: [$shippingZoneId]
|
|
||||||
) {
|
|
||||||
errors: warehouseErrors {
|
|
||||||
...WarehouseErrorFragment
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
export const useAssignShippingZoneToWarehouse = makeMutation<
|
|
||||||
AssignShippingZoneToWarehouse,
|
|
||||||
AssignShippingZoneToWarehouseVariables
|
|
||||||
>(assignShippingZoneToWarehouse);
|
|
||||||
|
|
||||||
const unassignShippingZoneToWarehouse = gql`
|
|
||||||
${warehouseErrorFragment}
|
|
||||||
mutation UnassignShippingZoneToWarehouse(
|
|
||||||
$warehouseId: ID!
|
|
||||||
$shippingZoneId: ID!
|
|
||||||
) {
|
|
||||||
unassignWarehouseShippingZone(
|
|
||||||
id: $warehouseId
|
|
||||||
shippingZoneIds: [$shippingZoneId]
|
|
||||||
) {
|
|
||||||
errors: warehouseErrors {
|
|
||||||
...WarehouseErrorFragment
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
export const useUnassignShippingZoneToWarehouse = makeMutation<
|
|
||||||
UnassignShippingZoneToWarehouse,
|
|
||||||
UnassignShippingZoneToWarehouseVariables
|
|
||||||
>(unassignShippingZoneToWarehouse);
|
|
||||||
|
|
|
@ -1,29 +0,0 @@
|
||||||
/* tslint:disable */
|
|
||||||
/* eslint-disable */
|
|
||||||
// This file was automatically generated and should not be edited.
|
|
||||||
|
|
||||||
import { WarehouseErrorCode } from "./../../types/globalTypes";
|
|
||||||
|
|
||||||
// ====================================================
|
|
||||||
// GraphQL mutation operation: AssignShippingZoneToWarehouse
|
|
||||||
// ====================================================
|
|
||||||
|
|
||||||
export interface AssignShippingZoneToWarehouse_assignWarehouseShippingZone_errors {
|
|
||||||
__typename: "WarehouseError";
|
|
||||||
code: WarehouseErrorCode;
|
|
||||||
field: string | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface AssignShippingZoneToWarehouse_assignWarehouseShippingZone {
|
|
||||||
__typename: "WarehouseShippingZoneAssign";
|
|
||||||
errors: AssignShippingZoneToWarehouse_assignWarehouseShippingZone_errors[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface AssignShippingZoneToWarehouse {
|
|
||||||
assignWarehouseShippingZone: AssignShippingZoneToWarehouse_assignWarehouseShippingZone | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface AssignShippingZoneToWarehouseVariables {
|
|
||||||
warehouseId: string;
|
|
||||||
shippingZoneId: string;
|
|
||||||
}
|
|
|
@ -1,29 +0,0 @@
|
||||||
/* tslint:disable */
|
|
||||||
/* eslint-disable */
|
|
||||||
// This file was automatically generated and should not be edited.
|
|
||||||
|
|
||||||
import { WarehouseErrorCode } from "./../../types/globalTypes";
|
|
||||||
|
|
||||||
// ====================================================
|
|
||||||
// GraphQL mutation operation: UnassignShippingZoneToWarehouse
|
|
||||||
// ====================================================
|
|
||||||
|
|
||||||
export interface UnassignShippingZoneToWarehouse_unassignWarehouseShippingZone_errors {
|
|
||||||
__typename: "WarehouseError";
|
|
||||||
code: WarehouseErrorCode;
|
|
||||||
field: string | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface UnassignShippingZoneToWarehouse_unassignWarehouseShippingZone {
|
|
||||||
__typename: "WarehouseShippingZoneUnassign";
|
|
||||||
errors: UnassignShippingZoneToWarehouse_unassignWarehouseShippingZone_errors[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface UnassignShippingZoneToWarehouse {
|
|
||||||
unassignWarehouseShippingZone: UnassignShippingZoneToWarehouse_unassignWarehouseShippingZone | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface UnassignShippingZoneToWarehouseVariables {
|
|
||||||
warehouseId: string;
|
|
||||||
shippingZoneId: string;
|
|
||||||
}
|
|
|
@ -14,9 +14,7 @@ import {
|
||||||
useShippingRateUpdate,
|
useShippingRateUpdate,
|
||||||
useShippingRateDelete,
|
useShippingRateDelete,
|
||||||
useShippingZoneDelete,
|
useShippingZoneDelete,
|
||||||
useShippingZoneUpdate,
|
useShippingZoneUpdate
|
||||||
useAssignShippingZoneToWarehouse,
|
|
||||||
useUnassignShippingZoneToWarehouse
|
|
||||||
} from "@saleor/shipping/mutations";
|
} from "@saleor/shipping/mutations";
|
||||||
import createDialogActionHandlers from "@saleor/utils/handlers/dialogActionHandlers";
|
import createDialogActionHandlers from "@saleor/utils/handlers/dialogActionHandlers";
|
||||||
import ShippingZoneRateDialog from "@saleor/shipping/components/ShippingZoneRateDialog";
|
import ShippingZoneRateDialog from "@saleor/shipping/components/ShippingZoneRateDialog";
|
||||||
|
@ -65,9 +63,6 @@ const ShippingZoneDetails: React.FC<ShippingZoneDetailsProps> = ({
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
const [assignToWarehouse] = useAssignShippingZoneToWarehouse({});
|
|
||||||
const [unassignToWarehouse] = useUnassignShippingZoneToWarehouse({});
|
|
||||||
|
|
||||||
const { data, loading } = useShippingZone({
|
const { data, loading } = useShippingZone({
|
||||||
displayLoader: true,
|
displayLoader: true,
|
||||||
variables: { id }
|
variables: { id }
|
||||||
|
@ -147,83 +142,22 @@ const ShippingZoneDetails: React.FC<ShippingZoneDetailsProps> = ({
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const handleSubmit = async (submitData: FormData) => {
|
const handleSubmit = (submitData: FormData) => {
|
||||||
try {
|
const warehouseDiff = diff(
|
||||||
const updateResult = await updateShippingZone({
|
data.shippingZone.warehouses.map(warehouse => warehouse.id),
|
||||||
variables: {
|
submitData.warehouses
|
||||||
id,
|
);
|
||||||
input: {
|
|
||||||
name: submitData.name
|
updateShippingZone({
|
||||||
}
|
variables: {
|
||||||
|
id,
|
||||||
|
input: {
|
||||||
|
addWarehouses: warehouseDiff.added,
|
||||||
|
name: submitData.name,
|
||||||
|
removeWarehouses: warehouseDiff.removed
|
||||||
}
|
}
|
||||||
});
|
|
||||||
const updateErrors = updateResult.data.shippingZoneUpdate.errors;
|
|
||||||
|
|
||||||
if (updateErrors.length === 0) {
|
|
||||||
const warehouseDiff = diff(
|
|
||||||
data.shippingZone.warehouses.map(warehouse => warehouse.id),
|
|
||||||
submitData.warehouses
|
|
||||||
);
|
|
||||||
const assignResults = await Promise.all(
|
|
||||||
warehouseDiff.added.map(warehouseId =>
|
|
||||||
assignToWarehouse({
|
|
||||||
variables: {
|
|
||||||
shippingZoneId: id,
|
|
||||||
warehouseId
|
|
||||||
}
|
|
||||||
})
|
|
||||||
)
|
|
||||||
);
|
|
||||||
const assignErrors = assignResults
|
|
||||||
.map(
|
|
||||||
assignResult => assignResult.data.assignWarehouseShippingZone.errors
|
|
||||||
)
|
|
||||||
.reduce((acc, errors) => [...acc, ...errors], []);
|
|
||||||
|
|
||||||
if (assignErrors.length === 0) {
|
|
||||||
notify({
|
|
||||||
text: intl.formatMessage(commonMessages.savedChanges)
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
throw new Error(
|
|
||||||
`Assigning to warehouse failed: ${assignErrors[0].code}`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const unassignResults = await Promise.all(
|
|
||||||
warehouseDiff.removed.map(warehouseId =>
|
|
||||||
unassignToWarehouse({
|
|
||||||
variables: {
|
|
||||||
shippingZoneId: id,
|
|
||||||
warehouseId
|
|
||||||
}
|
|
||||||
})
|
|
||||||
)
|
|
||||||
);
|
|
||||||
const unassignErrors = unassignResults
|
|
||||||
.map(
|
|
||||||
unassignResult =>
|
|
||||||
unassignResult.data.unassignWarehouseShippingZone.errors
|
|
||||||
)
|
|
||||||
.reduce((acc, errors) => [...acc, ...errors], []);
|
|
||||||
|
|
||||||
if (unassignErrors.length === 0) {
|
|
||||||
notify({
|
|
||||||
text: intl.formatMessage(commonMessages.savedChanges)
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
throw new Error(
|
|
||||||
`Assigning to warehouse failed: ${unassignErrors[0].code}`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new Error(`Updating failed: ${updateErrors[0].code}`);
|
|
||||||
}
|
}
|
||||||
} catch (err) {
|
});
|
||||||
notify({
|
|
||||||
text: intl.formatMessage(commonMessages.somethingWentWrong)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (data?.shippingZone === null) {
|
if (data?.shippingZone === null) {
|
||||||
|
|
Loading…
Reference in a new issue