Skip to content

Commit d9a202e

Browse files
committed
1. date selection
1 parent 396f02e commit d9a202e

File tree

3 files changed

+170
-57
lines changed

3 files changed

+170
-57
lines changed

src/App.tsx

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import {
77
orderItem,
88
boxSize,
99
boxSizes,
10+
flavor,
1011
cakeFlavors,
12+
deliveryOption,
13+
deliveryOptions,
1114
frostingFlavors,
1215
defaultCakeFlavor,
1316
defaultFrostingFlavor,
@@ -20,6 +23,7 @@ import {
2023
getFlavorFromString,
2124
getCurrentOrder,
2225
getOrderItemFromKey,
26+
getDeliveryOptionFromKey,
2327
} from "./OrderUtils";
2428
import { generateUniqueKey } from "./Helpers";
2529
import "react-datepicker/dist/react-datepicker.css";
@@ -47,8 +51,25 @@ function App() {
4751
};
4852

4953
const updateOrderDetails = (orderDetails: orderItem[]) => {
54+
// calculate price for each orderItem in orderDetails
55+
orderDetails.forEach((i: orderItem) => {
56+
let basePrice = i.basePrice;
57+
let quantity = i.size.count;
58+
let cakeUpcharge = quantity * i.cakeFlavor.upCharge * i.size.flavorMultiplier;
59+
let numberOfFlavors = 0;
60+
let frostingUpcharge = 0;
61+
i.frostingFlavor.forEach((f: flavor) => {
62+
numberOfFlavors += (f.name != "- none -") ? 1 : 0;
63+
frostingUpcharge += (f.name != "- none -") ? f.upCharge : 0;
64+
});
65+
i.totalPrice =
66+
basePrice +
67+
cakeUpcharge +
68+
(((frostingUpcharge * quantity) / numberOfFlavors) * i.size.flavorMultiplier);
69+
});
70+
5071
setState({
51-
orderTotal: calculateTotal(orderDetails),
72+
orderTotal: calculateTotal(orderDetails) + state.deliveryOption.price,
5273
orderDetails: orderDetails
5374
});
5475
};
@@ -60,7 +81,8 @@ function App() {
6081
size: newItem,
6182
cakeFlavor: defaultCakeFlavor,
6283
frostingFlavor: [defaultFrostingFlavor, defaultNullFrostingFlavor],
63-
price: newItem.price
84+
flavorMultiplier: newItem.flavorMultiplier,
85+
basePrice: newItem.price * newItem.count,
6486
});
6587
updateOrderDetails(currentOrder);
6688
};
@@ -107,28 +129,47 @@ function App() {
107129
}
108130
};
109131

132+
const updateDeliveryOption = (
133+
e: any,
134+
) => {
135+
let newDeliveryOption = getDeliveryOptionFromKey(e.target.value, deliveryOptions);
136+
if (newDeliveryOption) {
137+
setState({
138+
orderTotal: calculateTotal(state.orderDetails) + newDeliveryOption.price,
139+
deliveryOption: newDeliveryOption,
140+
});
141+
}
142+
};
143+
110144
return (
111145
<div>
112146
<h1>Cupcake Configurator</h1>
113-
<b>When would you like your order?</b>
114-
<br />
115-
You may submit a quote request for delivery 48 hours in advance or more.
116-
Before requesting a quote, please check my{" "}
117-
<a href="https://emoticakes.com/" target="_blank">
118-
availability calendar
119-
</a>
120-
.<br />
147+
<h4>When would you like your order?</h4>
148+
<p>You may submit a quote request for delivery 48 hours in advance or more.</p>
149+
<p>Before requesting a quote, please check my <a href="https://emoticakes.com/" target="_blank">availability calendar</a>.</p>
121150
<DatePicker
122151
selected={state.orderDate}
123152
minDate={addDays(new Date(), 2)}
124153
onChange={date => updateOrderDate(date)}
125154
monthsShown={2}
126155
/>
156+
<h4>Delivery Option</h4>
157+
<select
158+
value={state.deliveryOption.key}
159+
onChange={e => updateDeliveryOption(e)}
160+
>
161+
{Object.keys(deliveryOptions).map((deliveryOption, i) => (
162+
<option key={deliveryOptions[i].key} value={deliveryOptions[i].key}>
163+
{deliveryOptions[i].name}
164+
{deliveryOptions[i].price ? "(+$" + deliveryOptions[i].price + ")" : ""}
165+
</option>
166+
))}
167+
</select>
127168
<h4>Add to Order</h4>
128169
<ul>
129170
{Object.keys(boxSizes).map((item, i) => (
130171
<li onClick={e => addToOrder(boxSizes[i])} key={i}>
131-
{boxSizes[i].name} ({boxSizes[i].count})
172+
{boxSizes[i].name} ({boxSizes[i].count}) <i>per cupcake: ${boxSizes[i].price}</i>
132173
</li>
133174
))}
134175
</ul>
@@ -139,9 +180,12 @@ function App() {
139180
<tr>
140181
<th></th>
141182
<th>Item</th>
183+
<th># of Cupcakes</th>
184+
<th>Base Price</th>
142185
<th>Cake</th>
143186
<th>Frosting</th>
144187
<th>2nd Frosting (optional)</th>
188+
<th>Item Total</th>
145189
</tr>
146190
</thead>
147191
<tbody>
@@ -153,6 +197,8 @@ function App() {
153197
</button>
154198
</td>
155199
<td>{item.size.name}</td>
200+
<td>{item.size.count}</td>
201+
<td>${(item.size.price * item.size.count).toFixed(2)}</td>
156202
<td>
157203
<select
158204
value={item.cakeFlavor && item.cakeFlavor.name}
@@ -161,6 +207,7 @@ function App() {
161207
{Object.keys(cakeFlavors).map((thisFlavor, i) => (
162208
<option key={i} value={cakeFlavors[i].name}>
163209
{cakeFlavors[i].name}
210+
{cakeFlavors[i].upCharge ? "(+" + (cakeFlavors[i].upCharge * item.size.flavorMultiplier).toFixed(2) + " per)" : ""}
164211
</option>
165212
))}
166213
</select>
@@ -173,6 +220,7 @@ function App() {
173220
{Object.keys(frostingFlavors).map((thisFlavor, i) => (
174221
<option key={i} value={frostingFlavors[i].name}>
175222
{frostingFlavors[i].name}
223+
{frostingFlavors[i].upCharge ? "(+" + (frostingFlavors[i].upCharge * item.size.flavorMultiplier).toFixed(2) + " per)" : ""}
176224
</option>
177225
))}
178226
</select>
@@ -183,15 +231,14 @@ function App() {
183231
onChange={e => updateFrostingFlavor(e, item.key, 1)}
184232
>
185233
{Object.keys(frostingFlavors).map((thisFlavor, i) => (
186-
<option
187-
key={i}
188-
value={frostingFlavors[i] && frostingFlavors[i].name}
189-
>
234+
<option key={i} value={frostingFlavors[i].name}>
190235
{frostingFlavors[i].name}
236+
{frostingFlavors[i].upCharge ? "(+" + (frostingFlavors[i].upCharge * item.size.flavorMultiplier).toFixed(2) + " per)" : ""}
191237
</option>
192238
))}
193239
</select>
194240
</td>
241+
<td>${item.totalPrice.toFixed(2)}</td>
195242
</tr>
196243
))}
197244
</tbody>
@@ -244,9 +291,7 @@ function App() {
244291

245292
// todo:
246293
//
247-
// 1. provide a message on cupcakes (custom disc color, letter color)
248-
// 2. delivery (extra charge) or pick-up
249-
// 3. variable pricing based on flavor
294+
// provide a message on cupcakes (custom disc color, letter color)
250295
//
251296

252297
export default App;

0 commit comments

Comments
 (0)