1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
| import { useState } from "react";
const initialFriends = [ { id: 118836, name: "Clark", image: "https://i.pravatar.cc/48?u=118836", balance: -7, }, { id: 933372, name: "Sarah", image: "https://i.pravatar.cc/48?u=933372", balance: 20, }, { id: 499476, name: "Anthony", image: "https://i.pravatar.cc/48?u=499476", balance: 0, }, ];
function Button({ children, onClick }) { return ( <button className="button" onClick={onClick}> {children} </button> ); }
export default function App() { const [showAddFriend, setShowAddFriend] = useState(false); const [friends, setFriends] = useState(initialFriends); const [selectedFriend, setSelectFriend] = useState(null);
function handleAddFriend(friend) { setFriends((friends) => [...friends, friend]); setShowAddFriend(false); } function handleSelection(friend) { setSelectFriend((cur) => (cur?.id === friend.id ? null : friend)); setShowAddFriend(false); } function handleSplitBill(value) { setFriends((friends) => friends.map((friend) => friend.id === selectedFriend.id ? { ...friend, balance: friend.balance + value } : friend ) ); setSelectFriend(null); }
return ( <div className="app"> <div className="sidebar"> <FriendList friends={friends} onSelection={handleSelection} selectedFriend={selectedFriend} />
{showAddFriend && <FormAddFriend onAddFriends={handleAddFriend} />}
<Button onClick={() => setShowAddFriend((e) => !e)}> {showAddFriend ? "Close" : "Add friend"} </Button> </div>
{selectedFriend && ( <FormSplitBill selectedFriend={selectedFriend} onSplitBill={handleSplitBill} key={selectedFriend.id} /> )} </div> ); }
function FriendList({ friends, onSelection, selectedFriend }) { return ( <ul> {friends.map((friend) => ( <Friend friend={friend} key={friend.id} onSelection={onSelection} selectedFriend={selectedFriend} /> ))} </ul> ); }
function Friend({ friend, onSelection, selectedFriend }) { const isSelected = selectedFriend?.id === friend.id;
return ( <li className={isSelected ? "selected" : ""}> <img src={friend.image} alt={friend.name} /> <h3>{friend.name}</h3> {friend.balance < 0 && ( <p className="red"> You owe {friend.name} {Math.abs(friend.balance)}$ </p> )} {friend.balance > 0 && ( <p className="green"> {friend.name} owes you {friend.balance}$ </p> )} {friend.balance === 0 && <p>You and your friend are even</p>} <Button onClick={() => onSelection(friend)}> {isSelected ? "Close" : "Select"} </Button> </li> ); }
function FormAddFriend({ onAddFriends }) { const [name, setName] = useState(""); const [image, setImage] = useState("https://i.pravatar.cc/48");
function handleSubmit(e) { e.preventDefault(); if (!name || !image) return; const id = crypto.randomUUID();
const newFriend = { id, name, image: `${image}?=${id}`, balance: 0, };
onAddFriends(newFriend);
setName(""); setImage("https://i.pravatar.cc/48"); }
return ( <form className="form-add-friend" onSubmit={handleSubmit}> <label>👧🏻Friend name</label> <input type="text" value={name} onChange={(e) => setName(e.target.value)} />
<label>🧣Image URL</label> <input type="text" value={image} onChange={(e) => setImage(e.target.value)} /> <Button>Add</Button> </form> ); }
function FormSplitBill({ selectedFriend, onSplitBill }) { const [bill, setBill] = useState(""); const [paidByUser, setPaidByUser] = useState(""); const paidByFriend = bill ? bill - paidByUser : ""; const [whoIsPaying, setWhoIsPaying] = useState("user");
function handleSubmit(e) { e.preventDefault(); if (!bill || !paidByUser) return; onSplitBill(whoIsPaying === "user" ? paidByFriend : -paidByUser); }
return ( <form className="form-split-bill" onSubmit={handleSubmit}> <h2>Split a Bill With {selectedFriend.name}</h2> <label>💜Bill Value</label> <input type="text" value={bill} onChange={(e) => setBill(Number(e.target.value))} />
<label>🔆Your expense</label> <input type="text" value={paidByUser} onChange={(e) => setPaidByUser(Number(e.target.value))} />
<label>☀️{selectedFriend.name}'s expense</label> <input type="text" value={paidByFriend} disabled />
<label>🛎️Who</label> <select value={whoIsPaying} onChange={(e) => setWhoIsPaying(e.target.value)} > <option value="user">You</option> <option value="friend">{selectedFriend.name}</option> </select>
<Button>Split bill</Button> </form> ); }
|