-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* allow shipping cost refund * add tests * extract hook to separate file * fixes
- Loading branch information
Showing
6 changed files
with
199 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"saleor-dashboard": patch | ||
--- | ||
|
||
You can now make a refund with shipping costs or custom shipping amount. This means you can make a refund without selecting line items. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/orders/components/OrderReturnPage/useFulfillmentFormset.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
|
||
import { useFulfillmentFormset } from "./useFulfillmentFormset"; | ||
|
||
describe("useFulfillmentFormset", () => { | ||
it("should be disabled when no items are returned, no shipping and no amount", () => { | ||
// Arrange & Act | ||
const { result } = renderHook(() => | ||
useFulfillmentFormset({ | ||
order: { | ||
fulfillments: [], | ||
lines: [], | ||
} as any, | ||
formData: { | ||
amount: 0, | ||
refundShipmentCosts: false, | ||
}, | ||
}), | ||
); | ||
|
||
// Assert | ||
expect(result.current.disabled).toBe(true); | ||
}); | ||
|
||
it("should not disabled when no items are returned, but with shipping and no amount", () => { | ||
// Arrange & Act | ||
const { result } = renderHook(() => | ||
useFulfillmentFormset({ | ||
order: { | ||
fulfillments: [], | ||
lines: [], | ||
} as any, | ||
formData: { | ||
amount: 0, | ||
refundShipmentCosts: true, | ||
}, | ||
}), | ||
); | ||
|
||
// Assert | ||
expect(result.current.disabled).toBe(false); | ||
}); | ||
|
||
it("should not disabled when no items are returned, but with shipping and amount", () => { | ||
// Arrange & Act | ||
const { result } = renderHook(() => | ||
useFulfillmentFormset({ | ||
order: { | ||
fulfillments: [], | ||
lines: [], | ||
} as any, | ||
formData: { | ||
amount: 21.37, | ||
refundShipmentCosts: true, | ||
}, | ||
}), | ||
); | ||
|
||
// Assert | ||
expect(result.current.disabled).toBe(false); | ||
}); | ||
|
||
it("should not disabled when there are items selected with value, but with no shipping and no amount", () => { | ||
// Arrange & Act | ||
const { result } = renderHook(() => | ||
useFulfillmentFormset({ | ||
order: { | ||
fulfillments: [ | ||
{ | ||
status: "FULFILLED", | ||
lines: [ | ||
{ | ||
id: "id", | ||
quantity: 1, | ||
orderLine: { | ||
id: "id", | ||
}, | ||
}, | ||
], | ||
} as any, | ||
], | ||
lines: [ | ||
{ | ||
id: "id", | ||
quantity: 1, | ||
} as any, | ||
], | ||
} as any, | ||
formData: { | ||
amount: 0, | ||
refundShipmentCosts: false, | ||
}, | ||
}), | ||
); | ||
|
||
result.current.fulfiledItemsQuatities.change("id", 21.37); | ||
|
||
// Assert | ||
expect(result.current.fulfiledItemsQuatities.data.length).toBe(1); | ||
expect(result.current.disabled).toBe(false); | ||
}); | ||
}); |
46 changes: 46 additions & 0 deletions
46
src/orders/components/OrderReturnPage/useFulfillmentFormset.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { OrderDetailsFragment } from "@dashboard/graphql"; | ||
import useFormset from "@dashboard/hooks/useFormset"; | ||
|
||
import { LineItemData } from "./form"; | ||
import { | ||
getItemsFulfilled, | ||
getItemsWaiting, | ||
getOrderUnfulfilledLines, | ||
getParsedLineData, | ||
LineItem, | ||
} from "./utils"; | ||
|
||
const mapWithLabel = (line: LineItem<number>) => ({ ...line, label: line.label ?? "" }); | ||
|
||
export const useFulfillmentFormset = ({ | ||
order, | ||
formData, | ||
}: { | ||
order: OrderDetailsFragment; | ||
formData: { refundShipmentCosts: boolean; amount: number }; | ||
}) => { | ||
const fulfiledItemsQuatities = useFormset<LineItemData, number>( | ||
getItemsFulfilled(order).map(mapWithLabel), | ||
); | ||
const waitingItemsQuantities = useFormset<LineItemData, number>( | ||
getItemsWaiting(order).map(mapWithLabel), | ||
); | ||
const unfulfiledItemsQuantites = useFormset<LineItemData, number>( | ||
getOrderUnfulfilledLines(order) | ||
.map(getParsedLineData({ initialValue: 0 })) | ||
.map(mapWithLabel), | ||
); | ||
|
||
const hasAnyItemsSelected = | ||
fulfiledItemsQuatities.data.some(({ value }) => !!value) || | ||
waitingItemsQuantities.data.some(({ value }) => !!value) || | ||
unfulfiledItemsQuantites.data.some(({ value }) => !!value); | ||
const disabled = !hasAnyItemsSelected && !formData.refundShipmentCosts && !formData.amount; | ||
|
||
return { | ||
fulfiledItemsQuatities, | ||
waitingItemsQuantities, | ||
unfulfiledItemsQuantites, | ||
disabled, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters