|
| 1 | +import React from "react"; |
| 2 | + |
| 3 | +interface IOrderItem { |
| 4 | + id: string; |
| 5 | + quantity: number; |
| 6 | +} |
| 7 | + |
| 8 | +interface IOrderContent { |
| 9 | + items: IOrderItem[]; |
| 10 | +} |
| 11 | + |
| 12 | +const MyItemAudi = ({ |
| 13 | + orderContent, |
| 14 | + setOrderContent |
| 15 | +}: { |
| 16 | + orderContent: IOrderContent; |
| 17 | + setOrderContent: (orderContent: IOrderContent) => void; |
| 18 | +}) => { |
| 19 | + const quantity = orderContent.items.reduce( |
| 20 | + (acc, item) => acc + (item.id === "audi" ? item.quantity : 0), |
| 21 | + 0 |
| 22 | + ); |
| 23 | + |
| 24 | + const more = () => { |
| 25 | + setOrderContent({ |
| 26 | + items: orderContent.items |
| 27 | + .filter(item => item.id !== "audi") |
| 28 | + .concat({ id: "audi", quantity: quantity + 1 }) |
| 29 | + }); |
| 30 | + }; |
| 31 | + |
| 32 | + return ( |
| 33 | + <> |
| 34 | + Audi cars are very nice [...more text...]. Quantity: {quantity}.{" "} |
| 35 | + <button onClick={more}>Add more</button> |
| 36 | + </> |
| 37 | + ); |
| 38 | +}; |
| 39 | + |
| 40 | +const MyItemBmw = ({ |
| 41 | + orderContent, |
| 42 | + setOrderContent |
| 43 | +}: { |
| 44 | + orderContent: IOrderContent; |
| 45 | + setOrderContent: (orderContent: IOrderContent) => void; |
| 46 | +}) => { |
| 47 | + const quantity = orderContent.items.reduce( |
| 48 | + (acc, item) => acc + (item.id === "bmw" ? item.quantity : 0), |
| 49 | + 0 |
| 50 | + ); |
| 51 | + |
| 52 | + const more = () => { |
| 53 | + setOrderContent({ |
| 54 | + items: orderContent.items |
| 55 | + .filter(item => item.id !== "bmw") |
| 56 | + .concat({ id: "bmw", quantity: quantity + 1 }) |
| 57 | + }); |
| 58 | + }; |
| 59 | + |
| 60 | + return ( |
| 61 | + <> |
| 62 | + BMW are nice too, although a little bit tacky [...more text...]. Quantity:{" "} |
| 63 | + {quantity}. <button onClick={more}>Add more</button> |
| 64 | + </> |
| 65 | + ); |
| 66 | +}; |
| 67 | + |
| 68 | +const AllItems = ({ |
| 69 | + orderContent, |
| 70 | + setOrderContent |
| 71 | +}: { |
| 72 | + orderContent: IOrderContent; |
| 73 | + setOrderContent: (orderContent: IOrderContent) => void; |
| 74 | +}) => { |
| 75 | + const [selected, setSelected] = React.useState<string>("audi"); |
| 76 | + |
| 77 | + return ( |
| 78 | + <div> |
| 79 | + <div> |
| 80 | + Select car:{" "} |
| 81 | + <select |
| 82 | + onChange={e => { |
| 83 | + setSelected(e.currentTarget.value); |
| 84 | + }} |
| 85 | + > |
| 86 | + <option value="audi">Audi</option> |
| 87 | + <option value="bmw">BMW</option> |
| 88 | + </select> |
| 89 | + </div> |
| 90 | + |
| 91 | + <div> |
| 92 | + Car description: |
| 93 | + <div> |
| 94 | + {selected === "audi" && ( |
| 95 | + <MyItemAudi |
| 96 | + orderContent={orderContent} |
| 97 | + setOrderContent={setOrderContent} |
| 98 | + /> |
| 99 | + )} |
| 100 | + {selected === "bmw" && ( |
| 101 | + <MyItemBmw |
| 102 | + orderContent={orderContent} |
| 103 | + setOrderContent={setOrderContent} |
| 104 | + /> |
| 105 | + )} |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + |
| 109 | + <button onClick={() => setOrderContent({ items: [] })}>reset</button> |
| 110 | + </div> |
| 111 | + ); |
| 112 | +}; |
| 113 | + |
| 114 | +const OrderManager = () => { |
| 115 | + const [orderContent, setOrderContent] = React.useState<IOrderContent>({ |
| 116 | + items: [] |
| 117 | + }); |
| 118 | + |
| 119 | + return ( |
| 120 | + <> |
| 121 | + <pre>{JSON.stringify(orderContent, null, 2)}</pre> |
| 122 | + <AllItems orderContent={orderContent} setOrderContent={setOrderContent} /> |
| 123 | + </> |
| 124 | + ); |
| 125 | +}; |
| 126 | + |
| 127 | +export default OrderManager; |
0 commit comments