Skip to content

Commit f6f0190

Browse files
committed
Fix dish order checkboxes
1 parent d9abcd6 commit f6f0190

File tree

5 files changed

+40
-13
lines changed

5 files changed

+40
-13
lines changed

apps/food-order/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"private": true,
44
"version": "0.0.0",
55
"type": "module",
6+
"dependencies": {
7+
"effector-action": "^1.1.0"
8+
},
69
"devDependencies": {
710
"@vitejs/plugin-react": "^3.1.0",
811
"vite": "^4.2.1"

apps/food-order/src/model/dishOrder.model.ts

+24-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
calculateAdditiveOrderPriceFromEntity,
99
calculateDishOrderPrice,
1010
} from './calculateUtils';
11+
import { createAction } from 'effector-action';
1112

1213
const restaurantLens = lens(restaurantsList, $restaurantName);
1314

@@ -51,7 +52,23 @@ export const additivesList = keyval(() => {
5152
});
5253

5354
export const addAdditive = createEvent<{ additive: string; choice: string }>();
54-
export const removeAdditive = createEvent<string>();
55+
export const removeAdditive = createAction({
56+
source: additivesList.$items,
57+
target: {
58+
map: additivesList.edit.map,
59+
remove: additivesList.edit.remove,
60+
},
61+
fn(target, items, additiveToRemove: string) {
62+
if (items.some((e) => e.additive === additiveToRemove && e.amount === 1)) {
63+
target.remove(additiveToRemove);
64+
} else {
65+
target.map({
66+
keys: [additiveToRemove],
67+
map: ({ amount }) => ({ amount: amount - 1 }),
68+
});
69+
}
70+
},
71+
});
5572

5673
export const $currentDishTotalPrice = combine(
5774
$dish,
@@ -63,19 +80,17 @@ export const $currentDishTotalPrice = combine(
6380
sample({
6481
clock: addAdditive,
6582
target: additivesList.edit.map.prepend(
66-
(e: UnitValue<typeof addAdditive>) => ({
67-
keys: [e.additive],
68-
map: ({ amount, ...rest }) => ({ ...rest, amount: amount + 1 }),
83+
({ additive, choice }: UnitValue<typeof addAdditive>) => ({
84+
keys: [additive],
85+
map: ({ amount, choice: currentChoice }) => ({
86+
choice,
87+
amount: currentChoice === choice ? amount + 1 : 1,
88+
}),
6989
upsert: true,
7090
}),
7191
),
7292
});
7393

74-
sample({
75-
clock: removeAdditive,
76-
target: additivesList.edit.remove,
77-
});
78-
7994
sample({
8095
clock: addToOrder,
8196
source: { restaurant: $restaurantName },

apps/food-order/src/view/DishView.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,9 @@ const AdditiveSimpleItem = ({
5959
price: number;
6060
}) => {
6161
const item = useEntityItem(additivesList, additive);
62-
const orderedAmount = useEntityItem(additivesList, additive).amount;
62+
const orderedAmount = item.choice === choice ? item.amount : 0;
6363
const [add, remove] = useUnit([addAdditive, removeAdditive]);
64-
const selected = orderedAmount !== 0;
65-
console.log('AdditiveSimpleItem', item, selected);
64+
const selected = item.choice === choice && orderedAmount !== 0;
6665
return (
6766
<div className="flex justify-between items-center mb-2">
6867
<AdditiveWithPrice text={choice} price={price} />

apps/food-order/src/view/components.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ export const Radio = ({
1010
checked: boolean;
1111
onClick: () => void;
1212
}) => (
13-
<label className="flex items-center space-x-3 cursor-pointer">
13+
<label
14+
className="flex items-center space-x-3 cursor-pointer"
15+
onClick={(e) => {
16+
e.preventDefault();
17+
onClick();
18+
}}
19+
>
1420
<input
1521
type="radio"
1622
name={name}

pnpm-lock.yaml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)