-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
190 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { useMemo } from "react"; | ||
import type { WithId } from "~/lib/typeguard"; | ||
import type { OrderEntity } from "~/models/order"; | ||
|
||
const useLatestOrderId = (orders: WithId<OrderEntity>[] | undefined) => { | ||
const latestOrderId = useMemo( | ||
() => orders?.reduce((acc, cur) => Math.max(acc, cur.orderId), 0) ?? 0, | ||
[orders], | ||
); | ||
const nextOrderId = useMemo(() => latestOrderId + 1, [latestOrderId]); | ||
|
||
return { latestOrderId, nextOrderId }; | ||
}; | ||
export { useLatestOrderId }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { useReducer } from "react"; | ||
import type { WithId } from "~/lib/typeguard"; | ||
import type { ItemEntity } from "~/models/item"; | ||
import { OrderEntity } from "~/models/order"; | ||
|
||
type BaseAction<TypeName extends string> = { type: TypeName }; | ||
type Action< | ||
TypeName extends string, | ||
U = Record<never, never>, | ||
> = BaseAction<TypeName> & U; | ||
|
||
type Clear = Action<"clear", { effectFn?: () => void }>; | ||
type UpdateOrderId = Action<"updateOrderId", { orderId: number }>; | ||
type AddItem = Action<"addItem", { item: WithId<ItemEntity> }>; | ||
type MutateItem = Action< | ||
"mutateItem", | ||
{ idx: number; action: (prev: WithId<ItemEntity>) => WithId<ItemEntity> } | ||
>; | ||
type ApplyDiscount = Action< | ||
"applyDiscount", | ||
{ discountOrder: WithId<OrderEntity> } | ||
>; | ||
type RemoveDiscount = Action<"removeDiscount">; | ||
type SetReceived = Action<"setReceived", { received: string }>; | ||
type SetDescription = Action<"setDescription", { description: string }>; | ||
|
||
export type OrderAction = | ||
| Clear | ||
| UpdateOrderId | ||
| AddItem | ||
| MutateItem | ||
| ApplyDiscount | ||
| RemoveDiscount | ||
| SetReceived | ||
| SetDescription; | ||
|
||
type OrderReducer<T extends OrderAction> = ( | ||
state: OrderEntity, | ||
action: T, | ||
) => OrderEntity; | ||
|
||
const clear: OrderReducer<Clear> = (state, action) => { | ||
const effectFn = action.effectFn; | ||
if (effectFn) { | ||
effectFn(); | ||
} | ||
return OrderEntity.createNew({ orderId: state.orderId }); | ||
}; | ||
|
||
const updateOrderId: OrderReducer<UpdateOrderId> = (state, action) => { | ||
const updated = state.clone(); | ||
updated.orderId = action.orderId; | ||
return updated; | ||
}; | ||
|
||
const addItem: OrderReducer<AddItem> = (state, action) => { | ||
const updated = state.clone(); | ||
updated.items = [...updated.items, action.item]; | ||
return updated; | ||
}; | ||
|
||
const mutateItem: OrderReducer<MutateItem> = (state, action) => { | ||
const updated = state.clone(); | ||
updated.items[action.idx] = action.action(updated.items[action.idx]); | ||
return updated; | ||
}; | ||
|
||
const applyDiscount: OrderReducer<ApplyDiscount> = (state, action) => { | ||
const updated = state.clone(); | ||
updated.applyDiscount(action.discountOrder); | ||
return updated; | ||
}; | ||
|
||
const removeDiscount: OrderReducer<RemoveDiscount> = (state, action) => { | ||
const updated = state.clone(); | ||
updated.removeDiscount(); | ||
return updated; | ||
}; | ||
|
||
const setReceived: OrderReducer<SetReceived> = (state, action) => { | ||
const updated = state.clone(); | ||
updated.received = Number(action.received); | ||
return updated; | ||
}; | ||
|
||
const setDescription: OrderReducer<SetDescription> = (state, action) => { | ||
const updated = state.clone(); | ||
updated.description = action.description; | ||
return updated; | ||
}; | ||
|
||
const reducer: OrderReducer<OrderAction> = (state, action): OrderEntity => { | ||
switch (action.type) { | ||
case "clear": | ||
return clear(state, action); | ||
case "applyDiscount": | ||
return applyDiscount(state, action); | ||
case "removeDiscount": | ||
return removeDiscount(state, action); | ||
case "addItem": | ||
return addItem(state, action); | ||
case "mutateItem": | ||
return mutateItem(state, action); | ||
case "setReceived": | ||
return setReceived(state, action); | ||
case "setDescription": | ||
return setDescription(state, action); | ||
case "updateOrderId": | ||
return updateOrderId(state, action); | ||
} | ||
}; | ||
|
||
const useOrderState = () => | ||
useReducer(reducer, OrderEntity.createNew({ orderId: -1 })); | ||
|
||
export { useOrderState }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useCallback, useMemo, useState } from "react"; | ||
|
||
type UISession = { | ||
date: Date; | ||
key: string; | ||
}; | ||
|
||
const useUISession = (): [UISession, () => void] => { | ||
const [date, setDate] = useState(new Date()); | ||
|
||
const UISession = useMemo(() => { | ||
return { | ||
date, | ||
key: date.toJSON(), | ||
}; | ||
}, [date]); | ||
|
||
const renewUISession = useCallback(() => { | ||
setDate(new Date()); | ||
}, []); | ||
|
||
return [UISession, renewUISession]; | ||
}; | ||
|
||
export { useUISession }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters