SALEOR-1658 Add fulfill finalize button display logic (#885)

* Add logic for displaying finalize button in order fulfillment

* Update story
This commit is contained in:
mmarkusik 2020-12-04 13:54:27 +01:00 committed by GitHub
parent c7cd531aeb
commit bdb7837ccc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 6 deletions

View file

@ -8,8 +8,8 @@ import { orderToFulfill } from "./fixtures";
import OrderFulfillPage, { OrderFulfillPageProps } from "./OrderFulfillPage"; import OrderFulfillPage, { OrderFulfillPageProps } from "./OrderFulfillPage";
const props: OrderFulfillPageProps = { const props: OrderFulfillPageProps = {
disabled: false,
errors: [], errors: [],
loading: false,
onBack: () => undefined, onBack: () => undefined,
onSubmit: () => undefined, onSubmit: () => undefined,
order: orderToFulfill, order: orderToFulfill,
@ -23,7 +23,7 @@ storiesOf("Views / Orders / Fulfill order", module)
.add("loading", () => ( .add("loading", () => (
<OrderFulfillPage <OrderFulfillPage
{...props} {...props}
disabled={true} loading={true}
order={undefined} order={undefined}
warehouses={undefined} warehouses={undefined}
/> />

View file

@ -124,7 +124,7 @@ interface OrderFulfillSubmitData extends OrderFulfillFormData {
items: FormsetData<null, OrderFulfillStockInput[]>; items: FormsetData<null, OrderFulfillStockInput[]>;
} }
export interface OrderFulfillPageProps { export interface OrderFulfillPageProps {
disabled: boolean; loading: boolean;
errors: FulfillOrder_orderFulfill_errors[]; errors: FulfillOrder_orderFulfill_errors[];
order: OrderFulfillData_order; order: OrderFulfillData_order;
saveButtonBar: ConfirmButtonTransitionState; saveButtonBar: ConfirmButtonTransitionState;
@ -143,7 +143,7 @@ function getRemainingQuantity(line: OrderFulfillData_order_lines): number {
const OrderFulfillPage: React.FC<OrderFulfillPageProps> = props => { const OrderFulfillPage: React.FC<OrderFulfillPageProps> = props => {
const { const {
disabled, loading,
errors, errors,
order, order,
saveButtonBar, saveButtonBar,
@ -184,6 +184,35 @@ const OrderFulfillPage: React.FC<OrderFulfillPageProps> = props => {
items: formsetData items: formsetData
}); });
const shouldEnableSave = () => {
if (!order || loading) {
return false;
}
const isAtLeastOneFulfilled = formsetData.some(({ value }) =>
value.some(({ quantity }) => quantity > 0)
);
const areProperlyFulfilled = formsetData.every(({ id, value }) => {
const { lines } = order;
const { quantity, quantityFulfilled } = lines.find(
({ id: lineId }) => lineId === id
);
const remainingQuantity = quantity - quantityFulfilled;
const formQuantityFulfilled = value.reduce(
(result, { quantity }) => result + quantity,
0
);
return formQuantityFulfilled <= remainingQuantity;
});
return isAtLeastOneFulfilled && areProperlyFulfilled;
};
return ( return (
<Container> <Container>
<AppHeader onBack={onBack}> <AppHeader onBack={onBack}>
@ -453,7 +482,7 @@ const OrderFulfillPage: React.FC<OrderFulfillPageProps> = props => {
</CardActions> </CardActions>
</Card> </Card>
<SaveButtonBar <SaveButtonBar
disabled={disabled} disabled={!shouldEnableSave()}
labels={{ labels={{
save: intl.formatMessage({ save: intl.formatMessage({
defaultMessage: "Fulfill", defaultMessage: "Fulfill",

View file

@ -23,12 +23,14 @@ const OrderFulfill: React.FC<OrderFulfillProps> = ({ orderId }) => {
orderId orderId
} }
}); });
const { data: warehouseData, loading: warehousesLoading } = useWarehouseList({ const { data: warehouseData, loading: warehousesLoading } = useWarehouseList({
displayLoader: true, displayLoader: true,
variables: { variables: {
first: 20 first: 20
} }
}); });
const [fulfillOrder, fulfillOrderOpts] = useOrderFulfill({ const [fulfillOrder, fulfillOrderOpts] = useOrderFulfill({
onCompleted: data => { onCompleted: data => {
if (data.orderFulfill.errors.length === 0) { if (data.orderFulfill.errors.length === 0) {
@ -65,7 +67,7 @@ const OrderFulfill: React.FC<OrderFulfillProps> = ({ orderId }) => {
} }
/> />
<OrderFulfillPage <OrderFulfillPage
disabled={loading || warehousesLoading || fulfillOrderOpts.loading} loading={loading || warehousesLoading || fulfillOrderOpts.loading}
errors={fulfillOrderOpts.data?.orderFulfill.errors} errors={fulfillOrderOpts.data?.orderFulfill.errors}
onBack={() => navigate(orderUrl(orderId))} onBack={() => navigate(orderUrl(orderId))}
onSubmit={formData => onSubmit={formData =>