Merge pull request #604 from mirumee/fix/weight-rate-update
Fix weight based rate update
This commit is contained in:
commit
973add1fe6
3 changed files with 44 additions and 21 deletions
|
@ -5,6 +5,7 @@ All notable, unreleased changes to this project will be documented in this file.
|
|||
## [Unreleased]
|
||||
|
||||
- Add weight field and fix warehouse country selection - #597 by @dominik-zeglen
|
||||
- Fix weight based rate update - #604 by @dominik-zeglen
|
||||
|
||||
## 2.10.0
|
||||
|
||||
|
|
|
@ -2,8 +2,13 @@ import { ShippingZoneUrlQueryParams } from "@saleor/shipping/urls";
|
|||
import { ShippingMethodTypeEnum } from "@saleor/types/globalTypes";
|
||||
import { UpdateShippingRateVariables } from "@saleor/shipping/types/UpdateShippingRate";
|
||||
import { CreateShippingRateVariables } from "@saleor/shipping/types/CreateShippingRate";
|
||||
import { ShippingZone_shippingZone_shippingMethods } from "@saleor/shipping/types/ShippingZone";
|
||||
import { FormData as ShippingZoneRateDialogFormData } from "../../components/ShippingZoneRateDialog";
|
||||
|
||||
function getValue(value: string, hasLimits: boolean): number | null {
|
||||
return hasLimits ? null : parseFloat(value);
|
||||
}
|
||||
|
||||
export function getCreateShippingRateVariables(
|
||||
data: ShippingZoneRateDialogFormData,
|
||||
params: ShippingZoneUrlQueryParams,
|
||||
|
@ -13,28 +18,20 @@ export function getCreateShippingRateVariables(
|
|||
input: {
|
||||
maximumOrderPrice:
|
||||
params.type === ShippingMethodTypeEnum.PRICE
|
||||
? data.noLimits
|
||||
? null
|
||||
: parseFloat(data.maxValue)
|
||||
? getValue(data.maxValue, data.noLimits)
|
||||
: null,
|
||||
maximumOrderWeight:
|
||||
params.type === ShippingMethodTypeEnum.WEIGHT
|
||||
? data.noLimits
|
||||
? null
|
||||
: parseFloat(data.maxValue)
|
||||
? getValue(data.maxValue, data.noLimits)
|
||||
: null,
|
||||
|
||||
minimumOrderPrice:
|
||||
params.type === ShippingMethodTypeEnum.PRICE
|
||||
? data.noLimits
|
||||
? null
|
||||
: parseFloat(data.minValue)
|
||||
? getValue(data.maxValue, data.noLimits)
|
||||
: null,
|
||||
minimumOrderWeight:
|
||||
params.type === ShippingMethodTypeEnum.WEIGHT
|
||||
? data.noLimits
|
||||
? null
|
||||
: parseFloat(data.minValue)
|
||||
? getValue(data.minValue, data.noLimits)
|
||||
: null,
|
||||
name: data.name,
|
||||
price: data.isFree ? 0 : parseFloat(data.price),
|
||||
|
@ -46,17 +43,36 @@ export function getCreateShippingRateVariables(
|
|||
|
||||
export function getUpdateShippingRateVariables(
|
||||
data: ShippingZoneRateDialogFormData,
|
||||
params: ShippingZoneUrlQueryParams,
|
||||
id: string
|
||||
shippingRate: ShippingZone_shippingZone_shippingMethods,
|
||||
shippingZoneId: string
|
||||
): UpdateShippingRateVariables {
|
||||
return {
|
||||
id: params.id,
|
||||
const base: UpdateShippingRateVariables = {
|
||||
id: shippingRate.id,
|
||||
input: {
|
||||
maximumOrderPrice: data.noLimits ? null : parseFloat(data.maxValue),
|
||||
minimumOrderPrice: data.noLimits ? null : parseFloat(data.minValue),
|
||||
name: data.name,
|
||||
price: data.isFree ? 0 : parseFloat(data.price),
|
||||
shippingZone: id
|
||||
shippingZone: shippingZoneId,
|
||||
type: shippingRate.type
|
||||
}
|
||||
};
|
||||
|
||||
if (shippingRate.type === ShippingMethodTypeEnum.PRICE) {
|
||||
return {
|
||||
...base,
|
||||
input: {
|
||||
...base.input,
|
||||
maximumOrderPrice: getValue(data.maxValue, data.noLimits),
|
||||
minimumOrderPrice: getValue(data.minValue, data.noLimits)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
...base,
|
||||
input: {
|
||||
...base.input,
|
||||
maximumOrderWeight: getValue(data.maxValue, data.noLimits),
|
||||
minimumOrderWeight: getValue(data.minValue, data.noLimits)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -221,9 +221,15 @@ const ShippingZoneDetails: React.FC<ShippingZoneDetailsProps> = ({
|
|||
disabled={updateShippingRateOpts.loading}
|
||||
errors={updateShippingRateOpts.data?.shippingPriceUpdate.errors || []}
|
||||
onClose={closeModal}
|
||||
onSubmit={data =>
|
||||
onSubmit={submitData =>
|
||||
updateShippingRate({
|
||||
variables: getUpdateShippingRateVariables(data, params, id)
|
||||
variables: getUpdateShippingRateVariables(
|
||||
submitData,
|
||||
data?.shippingZone?.shippingMethods.find(
|
||||
shippingMethod => shippingMethod.id === params.id
|
||||
),
|
||||
id
|
||||
)
|
||||
})
|
||||
}
|
||||
open={params.action === "edit-rate"}
|
||||
|
|
Loading…
Reference in a new issue