
* Clean up stories * Add missing props * Add zip codes section (#861) * Add zip code listing * Add list wrapping * Update snapshots * Set up API data * Fix lgtm warning * Update snapshots * Run Actions on all PR * Checks on PR * Test envs on PR * Cleanup action on PR * Update messages Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> * Add zip code range dialog * Fix path management * Use query params to handle modal actions * Allow zip codes to be assigned to shipping method * Make params optional * Fix types * Add zip code deletion (#871) * Add zip code range dialog * Fix path management * Use query params to handle modal actions * Allow zip codes to be assigned to shipping method * Make params optional * Fix types * Clean up urls * Add zip code range delete action * Update snapshots and messages * Update testing and changelog * Update schema * Simplify code * Refresh zip code list after assigning them * Update view after zip code deletion * Update types and snapshots * Update snapshots * Fix error message, checkbox default value (#880) * Fix error message, checkbox default value * Update snapshots * Use price instead of weight variant * Update schema and types * Hide exclude/include zip codes section * Update stories Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> Co-authored-by: Tomasz Szymański <lime129@gmail.com>
157 lines
5 KiB
TypeScript
157 lines
5 KiB
TypeScript
import { useChannelsList } from "@saleor/channels/queries";
|
|
import {
|
|
createShippingChannels,
|
|
createSortedShippingChannels
|
|
} from "@saleor/channels/utils";
|
|
import ChannelsAvailabilityDialog from "@saleor/components/ChannelsAvailabilityDialog";
|
|
import { WindowTitle } from "@saleor/components/WindowTitle";
|
|
import { ShippingMethodFragment_zipCodeRules } from "@saleor/fragments/types/ShippingMethodFragment";
|
|
import useChannels from "@saleor/hooks/useChannels";
|
|
import useNavigator from "@saleor/hooks/useNavigator";
|
|
import { sectionNames } from "@saleor/intl";
|
|
import ShippingRateZipCodeRangeRemoveDialog from "@saleor/shipping/components/ShippingRateZipCodeRangeRemoveDialog";
|
|
import ShippingZoneRatesPage from "@saleor/shipping/components/ShippingZoneRatesPage";
|
|
import ShippingZoneZipCodeRangeDialog from "@saleor/shipping/components/ShippingZoneZipCodeRangeDialog";
|
|
import { useShippingRateCreator } from "@saleor/shipping/handlers";
|
|
import {
|
|
ShippingRateCreateUrlDialog,
|
|
ShippingRateCreateUrlQueryParams,
|
|
shippingWeightRatesUrl,
|
|
shippingZoneUrl
|
|
} from "@saleor/shipping/urls";
|
|
import { MinMax } from "@saleor/types";
|
|
import { ShippingMethodTypeEnum } from "@saleor/types/globalTypes";
|
|
import createDialogActionHandlers from "@saleor/utils/handlers/dialogActionHandlers";
|
|
import { remove } from "@saleor/utils/lists";
|
|
import React from "react";
|
|
import { useIntl } from "react-intl";
|
|
|
|
export interface WeightRatesCreateProps {
|
|
id: string;
|
|
params: ShippingRateCreateUrlQueryParams;
|
|
}
|
|
|
|
export const WeightRatesCreate: React.FC<WeightRatesCreateProps> = ({
|
|
id,
|
|
params
|
|
}) => {
|
|
const navigate = useNavigator();
|
|
const intl = useIntl();
|
|
|
|
const [zipCodes, setZipCodes] = React.useState<
|
|
ShippingMethodFragment_zipCodeRules[]
|
|
>([]);
|
|
|
|
const { data: channelsData, loading: channelsLoading } = useChannelsList({});
|
|
|
|
const [openModal, closeModal] = createDialogActionHandlers<
|
|
ShippingRateCreateUrlDialog,
|
|
ShippingRateCreateUrlQueryParams
|
|
>(navigate, params => shippingWeightRatesUrl(id, params), params);
|
|
|
|
const shippingChannels = createShippingChannels(channelsData?.channels);
|
|
const allChannels = createSortedShippingChannels(channelsData?.channels);
|
|
|
|
const {
|
|
channelListElements,
|
|
channelsToggle,
|
|
currentChannels,
|
|
handleChannelsConfirm,
|
|
handleChannelsModalClose,
|
|
handleChannelsModalOpen,
|
|
isChannelSelected,
|
|
isChannelsModalOpen,
|
|
setCurrentChannels,
|
|
toggleAllChannels
|
|
} = useChannels(shippingChannels);
|
|
|
|
const {
|
|
channelErrors,
|
|
createShippingRate,
|
|
errors,
|
|
status
|
|
} = useShippingRateCreator(id, ShippingMethodTypeEnum.WEIGHT, zipCodes);
|
|
|
|
const handleBack = () => navigate(shippingZoneUrl(id));
|
|
|
|
const handleZipCodeRangeAdd = (data: MinMax) => {
|
|
setZipCodes(zipCodes => [
|
|
...zipCodes,
|
|
{
|
|
__typename: "ShippingMethodZipCodeRule",
|
|
end: data.max,
|
|
id: zipCodes.length.toString(),
|
|
start: data.min
|
|
}
|
|
]);
|
|
closeModal();
|
|
};
|
|
const handleZipCodeRangeDelete = (id: string) => {
|
|
setZipCodes(zipCodes =>
|
|
remove(
|
|
zipCodes.find(zipCode => zipCode.id === id),
|
|
zipCodes,
|
|
(a, b) => a.id === b.id
|
|
)
|
|
);
|
|
closeModal();
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<WindowTitle title={intl.formatMessage(sectionNames.shipping)} />
|
|
{!!allChannels?.length && (
|
|
<ChannelsAvailabilityDialog
|
|
isSelected={isChannelSelected}
|
|
disabled={!channelListElements.length}
|
|
channels={allChannels}
|
|
onChange={channelsToggle}
|
|
onClose={handleChannelsModalClose}
|
|
open={isChannelsModalOpen}
|
|
title={intl.formatMessage({
|
|
defaultMessage: "Manage Channels Availability"
|
|
})}
|
|
confirmButtonState="default"
|
|
selected={channelListElements.length}
|
|
onConfirm={handleChannelsConfirm}
|
|
toggleAll={toggleAllChannels}
|
|
/>
|
|
)}
|
|
<ShippingZoneRatesPage
|
|
allChannelsCount={allChannels?.length}
|
|
shippingChannels={currentChannels}
|
|
disabled={channelsLoading || status === "loading"}
|
|
saveButtonBarState={status}
|
|
onSubmit={createShippingRate}
|
|
onBack={handleBack}
|
|
errors={errors}
|
|
channelErrors={channelErrors}
|
|
rate={null}
|
|
zipCodes={zipCodes}
|
|
openChannelsModal={handleChannelsModalOpen}
|
|
onChannelsChange={setCurrentChannels}
|
|
onZipCodeAssign={() => openModal("add-range")}
|
|
onZipCodeUnassign={id =>
|
|
openModal("remove-range", {
|
|
id
|
|
})
|
|
}
|
|
variant={ShippingMethodTypeEnum.WEIGHT}
|
|
/>
|
|
<ShippingZoneZipCodeRangeDialog
|
|
confirmButtonState="default"
|
|
onClose={closeModal}
|
|
onSubmit={handleZipCodeRangeAdd}
|
|
open={params.action === "add-range"}
|
|
/>
|
|
<ShippingRateZipCodeRangeRemoveDialog
|
|
confirmButtonState="default"
|
|
onClose={closeModal}
|
|
onConfirm={() => handleZipCodeRangeDelete(params.id)}
|
|
open={params.action === "remove-range"}
|
|
/>
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default WeightRatesCreate;
|