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]
|
## [Unreleased]
|
||||||
|
|
||||||
- Add weight field and fix warehouse country selection - #597 by @dominik-zeglen
|
- 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.10.0
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,13 @@ import { ShippingZoneUrlQueryParams } from "@saleor/shipping/urls";
|
||||||
import { ShippingMethodTypeEnum } from "@saleor/types/globalTypes";
|
import { ShippingMethodTypeEnum } from "@saleor/types/globalTypes";
|
||||||
import { UpdateShippingRateVariables } from "@saleor/shipping/types/UpdateShippingRate";
|
import { UpdateShippingRateVariables } from "@saleor/shipping/types/UpdateShippingRate";
|
||||||
import { CreateShippingRateVariables } from "@saleor/shipping/types/CreateShippingRate";
|
import { CreateShippingRateVariables } from "@saleor/shipping/types/CreateShippingRate";
|
||||||
|
import { ShippingZone_shippingZone_shippingMethods } from "@saleor/shipping/types/ShippingZone";
|
||||||
import { FormData as ShippingZoneRateDialogFormData } from "../../components/ShippingZoneRateDialog";
|
import { FormData as ShippingZoneRateDialogFormData } from "../../components/ShippingZoneRateDialog";
|
||||||
|
|
||||||
|
function getValue(value: string, hasLimits: boolean): number | null {
|
||||||
|
return hasLimits ? null : parseFloat(value);
|
||||||
|
}
|
||||||
|
|
||||||
export function getCreateShippingRateVariables(
|
export function getCreateShippingRateVariables(
|
||||||
data: ShippingZoneRateDialogFormData,
|
data: ShippingZoneRateDialogFormData,
|
||||||
params: ShippingZoneUrlQueryParams,
|
params: ShippingZoneUrlQueryParams,
|
||||||
|
@ -13,28 +18,20 @@ export function getCreateShippingRateVariables(
|
||||||
input: {
|
input: {
|
||||||
maximumOrderPrice:
|
maximumOrderPrice:
|
||||||
params.type === ShippingMethodTypeEnum.PRICE
|
params.type === ShippingMethodTypeEnum.PRICE
|
||||||
? data.noLimits
|
? getValue(data.maxValue, data.noLimits)
|
||||||
? null
|
|
||||||
: parseFloat(data.maxValue)
|
|
||||||
: null,
|
: null,
|
||||||
maximumOrderWeight:
|
maximumOrderWeight:
|
||||||
params.type === ShippingMethodTypeEnum.WEIGHT
|
params.type === ShippingMethodTypeEnum.WEIGHT
|
||||||
? data.noLimits
|
? getValue(data.maxValue, data.noLimits)
|
||||||
? null
|
|
||||||
: parseFloat(data.maxValue)
|
|
||||||
: null,
|
: null,
|
||||||
|
|
||||||
minimumOrderPrice:
|
minimumOrderPrice:
|
||||||
params.type === ShippingMethodTypeEnum.PRICE
|
params.type === ShippingMethodTypeEnum.PRICE
|
||||||
? data.noLimits
|
? getValue(data.maxValue, data.noLimits)
|
||||||
? null
|
|
||||||
: parseFloat(data.minValue)
|
|
||||||
: null,
|
: null,
|
||||||
minimumOrderWeight:
|
minimumOrderWeight:
|
||||||
params.type === ShippingMethodTypeEnum.WEIGHT
|
params.type === ShippingMethodTypeEnum.WEIGHT
|
||||||
? data.noLimits
|
? getValue(data.minValue, data.noLimits)
|
||||||
? null
|
|
||||||
: parseFloat(data.minValue)
|
|
||||||
: null,
|
: null,
|
||||||
name: data.name,
|
name: data.name,
|
||||||
price: data.isFree ? 0 : parseFloat(data.price),
|
price: data.isFree ? 0 : parseFloat(data.price),
|
||||||
|
@ -46,17 +43,36 @@ export function getCreateShippingRateVariables(
|
||||||
|
|
||||||
export function getUpdateShippingRateVariables(
|
export function getUpdateShippingRateVariables(
|
||||||
data: ShippingZoneRateDialogFormData,
|
data: ShippingZoneRateDialogFormData,
|
||||||
params: ShippingZoneUrlQueryParams,
|
shippingRate: ShippingZone_shippingZone_shippingMethods,
|
||||||
id: string
|
shippingZoneId: string
|
||||||
): UpdateShippingRateVariables {
|
): UpdateShippingRateVariables {
|
||||||
return {
|
const base: UpdateShippingRateVariables = {
|
||||||
id: params.id,
|
id: shippingRate.id,
|
||||||
input: {
|
input: {
|
||||||
maximumOrderPrice: data.noLimits ? null : parseFloat(data.maxValue),
|
|
||||||
minimumOrderPrice: data.noLimits ? null : parseFloat(data.minValue),
|
|
||||||
name: data.name,
|
name: data.name,
|
||||||
price: data.isFree ? 0 : parseFloat(data.price),
|
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}
|
disabled={updateShippingRateOpts.loading}
|
||||||
errors={updateShippingRateOpts.data?.shippingPriceUpdate.errors || []}
|
errors={updateShippingRateOpts.data?.shippingPriceUpdate.errors || []}
|
||||||
onClose={closeModal}
|
onClose={closeModal}
|
||||||
onSubmit={data =>
|
onSubmit={submitData =>
|
||||||
updateShippingRate({
|
updateShippingRate({
|
||||||
variables: getUpdateShippingRateVariables(data, params, id)
|
variables: getUpdateShippingRateVariables(
|
||||||
|
submitData,
|
||||||
|
data?.shippingZone?.shippingMethods.find(
|
||||||
|
shippingMethod => shippingMethod.id === params.id
|
||||||
|
),
|
||||||
|
id
|
||||||
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
open={params.action === "edit-rate"}
|
open={params.action === "edit-rate"}
|
||||||
|
|
Loading…
Reference in a new issue