Skip to content

Commit

Permalink
move order row to separate component
Browse files Browse the repository at this point in the history
  • Loading branch information
SheepTester committed Apr 30, 2024
1 parent 4aa2379 commit 7629d92
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 88 deletions.
69 changes: 69 additions & 0 deletions src/components/admin/store/PickupOrder/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Button, Typography } from '@/components/common';
import { OrderStatusIndicator } from '@/components/store';
import { StoreAPI } from '@/lib/api';
import { PublicOrderItemWithQuantity, PublicOrderWithItems } from '@/lib/types/apiResponses';
import { OrderStatus } from '@/lib/types/enums';
import { getOrderItemQuantities, reportError } from '@/lib/utils';
import styles from './style.module.scss';

interface PickupOrderProps {
token: string;
canFulfill: boolean;
order: PublicOrderWithItems;
onOrderUpdate: (orders: PublicOrderWithItems) => void;
}

const itemToString = (item: PublicOrderItemWithQuantity): string => {
if (item.option.metadata !== null)
return `${item.option.item.itemName} (${item.option.metadata.type}: ${item.option.metadata.value})`;
return item.option.item.itemName;
};

const PickupOrder = ({ token, canFulfill, order, onOrderUpdate }: PickupOrderProps) => {
const showFulfill = canFulfill && order.status === OrderStatus.PLACED;
const itemQuantities = getOrderItemQuantities(order.items);
return (
<tr className={styles.row}>
<td>
<Typography variant="h5/regular">{`${order.user.firstName} ${order.user.lastName}`}</Typography>
<OrderStatusIndicator orderStatus={order.status} />
{showFulfill && itemQuantities.length > 1 ? (
<Button
size="small"
onClick={async () => {
try {
const newOrder = await StoreAPI.fulfillOrderPickup(token, order.uuid, order.items);
const itemUuids = order.items.map(item => item.uuid);
onOrderUpdate({
...newOrder,
items: order.items.map(item =>
itemUuids.includes(item.uuid) ? { ...item, fulfilled: true } : item
),
});
} catch (error: unknown) {
reportError('Failed to fulfill order', error);
}
}}
>
Fulfill
</Button>
) : null}
</td>
<td>
<ul className={styles.itemList}>
{itemQuantities.map(item => {
return (
<li key={item.uuid}>
<Typography variant="h5/regular">{`${item.quantity} x ${itemToString(
item
)}`}</Typography>
</li>
);
})}
</ul>
</td>
</tr>
);
};

export default PickupOrder;
18 changes: 18 additions & 0 deletions src/components/admin/store/PickupOrder/style.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.row {
border-top: 1px solid var(--theme-accent-line-2);

td {
padding: 1.5rem;
}

.itemList {
list-style-type: disc;

li {
align-items: center;
display: flex;
flex-wrap: wrap;
gap: 10px;
}
}
}
10 changes: 10 additions & 0 deletions src/components/admin/store/PickupOrder/style.module.scss.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export type Styles = {
itemList: string;
row: string;
};

export type ClassNames = keyof Styles;

declare const styles: Styles;

export default styles;
83 changes: 19 additions & 64 deletions src/components/admin/store/PickupOrdersPrepareDisplay/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Button, Typography } from '@/components/common';
import { OrderStatusIndicator } from '@/components/store';
import { StoreAPI } from '@/lib/api';
import PickupOrder from '@/components/admin/store/PickupOrder';
import { Typography } from '@/components/common';
import { PublicOrderItemWithQuantity, PublicOrderWithItems } from '@/lib/types/apiResponses';
import { OrderStatus } from '@/lib/types/enums';
import { getOrderItemQuantities, reportError } from '@/lib/utils';
import { getOrderItemQuantities } from '@/lib/utils';
import { useMemo } from 'react';
import styles from './style.module.scss';

Expand Down Expand Up @@ -77,65 +75,22 @@ const PickupOrdersPrepareDisplay = ({
</tr>
</thead>
<tbody>
{orders.map(order => {
const showFulfill = canFulfill && order.status === OrderStatus.PLACED;
const itemQuantities = getOrderItemQuantities(order.items);
return (
<tr key={order.uuid}>
<td>
<Typography variant="h5/regular">{`${order.user.firstName} ${order.user.lastName}`}</Typography>
<OrderStatusIndicator orderStatus={order.status} />
{showFulfill && itemQuantities.length > 1 ? (
<Button
size="small"
onClick={async () => {
try {
const newOrder = await StoreAPI.fulfillOrderPickup(
token,
order.uuid,
order.items
);
const itemUuids = order.items.map(item => item.uuid);
onOrderUpdate(
orders.map(
(order): PublicOrderWithItems =>
order.uuid === newOrder.uuid
? {
...newOrder,
items: order.items.map(item =>
itemUuids.includes(item.uuid)
? { ...item, fulfilled: true }
: item
),
}
: order
)
);
} catch (error: unknown) {
reportError('Failed to fulfill order', error);
}
}}
>
Fulfill
</Button>
) : null}
</td>
<td>
<ul className={styles.itemList}>
{itemQuantities.map(item => {
return (
<li key={item.uuid}>
<Typography variant="h5/regular">{`${item.quantity} x ${itemToString(
item
)}`}</Typography>
</li>
);
})}
</ul>
</td>
</tr>
);
})}
{orders.map(order => (
<PickupOrder
canFulfill={canFulfill}
order={order}
onOrderUpdate={newOrder =>
onOrderUpdate(
orders.map(
(order): PublicOrderWithItems =>
order.uuid === newOrder.uuid ? newOrder : order
)
)
}
token={token}
key={order.uuid}
/>
))}
</tbody>
</table>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,4 @@
padding: 1rem 1.5rem;
text-align: left;
}

td {
padding: 1.5rem;
}

tbody tr {
border-top: 1px solid var(--theme-accent-line-2);
}

tr:last-child td:first-child {
border-bottom-left-radius: 1rem;
}
}

.itemList {
list-style-type: disc;

li {
align-items: center;
display: flex;
flex-wrap: wrap;
gap: 10px;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export type Styles = {
breakdown: string;
container: string;
itemList: string;
table: string;
};

Expand Down
1 change: 1 addition & 0 deletions src/components/admin/store/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import { PickupEventStatus } from '@/components/admin/store/PickupEventCard';
export { default as CollectionDetailsForm } from './DetailsForm/CollectionDetailsForm';
export { default as ItemDetailsForm } from './DetailsForm/ItemDetailsForm';
export { default as PickupEventCard } from './PickupEventCard';
export { default as PickupOrder } from './PickupOrder';
export { default as PickupOrdersPrepareDisplay } from './PickupOrdersPrepareDisplay';
export { PickupEventStatus };

0 comments on commit 7629d92

Please sign in to comment.