fix: item history page now live updates
This commit is contained in:
BIN
api/__debug_bin2336935653
Executable file
BIN
api/__debug_bin2336935653
Executable file
Binary file not shown.
BIN
api/ordr-api
BIN
api/ordr-api
Binary file not shown.
@@ -25,16 +25,15 @@ export const CreateItem = async (query: CreateItemQuery): Promise<ItemPriceRespo
|
|||||||
return res.data
|
return res.data
|
||||||
}
|
}
|
||||||
|
|
||||||
export const SetItemPrice = async (query: SetItemPriceQuery) => {
|
export const SetItemPrice = async (query: SetItemPriceQuery): Promise<ItemPriceResponse> => {
|
||||||
const queryParams = new URLSearchParams({
|
const queryParams = new URLSearchParams({
|
||||||
item_id: query.item_id,
|
item_id: query.item_id,
|
||||||
item_price: query.item_price
|
item_price: query.item_price
|
||||||
})
|
})
|
||||||
|
|
||||||
const res = await axios.put(process.env.NEXT_PUBLIC_API_URL + `/item/price?${queryParams.toString()}`, {}, {withCredentials: true})
|
const res = await axios.put(process.env.NEXT_PUBLIC_API_URL + `/item/price?${queryParams.toString()}`, {}, {withCredentials: true})
|
||||||
if (res.data.Location) {
|
|
||||||
window.location.href = res.data.Location
|
return res.data as ItemPriceResponse
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
import { Mutex } from "async-mutex"
|
import { Mutex } from "async-mutex"
|
||||||
import { useState } from "react"
|
|
||||||
import styled from "styled-components"
|
import styled from "styled-components"
|
||||||
import useAsyncEffect from "use-async-effect"
|
import useAsyncEffect from "use-async-effect"
|
||||||
import { GetItemHistory } from "../client/controllers"
|
import { useItemStore } from "../providers/ItemsProvider"
|
||||||
import { ItemHistoryResponse } from "../client/response"
|
|
||||||
|
|
||||||
type ItemHistoryTableProps = {
|
type ItemHistoryTableProps = {
|
||||||
itemId: number
|
itemId: number
|
||||||
@@ -39,17 +37,15 @@ const ItemHistoryTableRow = styled.tr`
|
|||||||
const itemHistoryMutex = new Mutex()
|
const itemHistoryMutex = new Mutex()
|
||||||
|
|
||||||
export const ItemHistoryTable = ({itemId}: ItemHistoryTableProps) => {
|
export const ItemHistoryTable = ({itemId}: ItemHistoryTableProps) => {
|
||||||
const [itemPriceHistory, setItemPriceHistory] = useState<ItemHistoryResponse[]>([])
|
const itemStore = useItemStore((state) => state)
|
||||||
|
|
||||||
useAsyncEffect(async () => {
|
useAsyncEffect(async () => {
|
||||||
if(itemPriceHistory.length === 0) {
|
if(itemStore.itemHistories.filter((ih) => parseInt(ih.ItemId) === itemId).length === 0) {
|
||||||
const release = await itemHistoryMutex.acquire()
|
const release = await itemHistoryMutex.acquire()
|
||||||
setItemPriceHistory(await GetItemHistory(itemId))
|
await itemStore.getItemHistory(itemId)
|
||||||
await release()
|
await release()
|
||||||
}
|
}
|
||||||
}, [])
|
}, [itemStore.itemHistories])
|
||||||
|
|
||||||
console.log(itemPriceHistory)
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ItemHistoryTableStyle>
|
<ItemHistoryTableStyle>
|
||||||
@@ -70,7 +66,7 @@ export const ItemHistoryTable = ({itemId}: ItemHistoryTableProps) => {
|
|||||||
</tr>
|
</tr>
|
||||||
</ItemHistoryTableHead>
|
</ItemHistoryTableHead>
|
||||||
<ItemHistoryTableBody>
|
<ItemHistoryTableBody>
|
||||||
{itemPriceHistory.map((iph) => (
|
{itemStore.itemHistories.filter((ih) => parseInt(ih.ItemId) === itemId).map((iph) => (
|
||||||
<ItemHistoryTableRow key = {iph.ItemId + new Date(iph.ValidFrom).getMilliseconds()}>
|
<ItemHistoryTableRow key = {iph.ItemId + new Date(iph.ValidFrom).getMilliseconds()}>
|
||||||
<ItemHistoryTableItem>
|
<ItemHistoryTableItem>
|
||||||
{iph.ItemName}
|
{iph.ItemName}
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
'use client'
|
'use client'
|
||||||
import Image from "next/image";
|
import { useRouter } from "next/navigation"
|
||||||
|
import { useEffect } from "react"
|
||||||
import { useAsyncEffect } from 'use-async-effect'
|
import { useAsyncEffect } from 'use-async-effect'
|
||||||
import { useShallow } from 'zustand/react/shallow'
|
import { useShallow } from 'zustand/react/shallow'
|
||||||
import { UserResponse } from './client/response'
|
import { UserResponse } from './client/response'
|
||||||
import { useCurrentAuthenticatedUserStore, UserActions } from './providers'
|
import { useCurrentAuthenticatedUserStore, UserActions } from './providers'
|
||||||
import { useRouter } from "next/navigation";
|
|
||||||
import { useEffect } from "react";
|
|
||||||
|
|
||||||
export default function Home() {
|
export default function Home() {
|
||||||
const authenticatedUserStore: UserResponse & UserActions = useCurrentAuthenticatedUserStore(useShallow((state) => ({
|
const authenticatedUserStore: UserResponse & UserActions = useCurrentAuthenticatedUserStore(useShallow((state) => ({
|
||||||
|
|||||||
@@ -2,11 +2,12 @@ import { create } from 'zustand'
|
|||||||
import * as ItemController from '../client/controllers/ItemController'
|
import * as ItemController from '../client/controllers/ItemController'
|
||||||
import { CreateItemQuery } from '../client/queries/CreateItemQuery'
|
import { CreateItemQuery } from '../client/queries/CreateItemQuery'
|
||||||
import { SetItemPriceQuery } from '../client/queries/SetItemPriceQuery'
|
import { SetItemPriceQuery } from '../client/queries/SetItemPriceQuery'
|
||||||
import { ItemPriceResponse, OrderFilledResponse, OrderItemPriceResponse } from '../client/response'
|
import { ItemHistoryResponse, ItemPriceResponse, OrderFilledResponse, OrderItemPriceResponse } from '../client/response'
|
||||||
|
|
||||||
export type ItemData = {
|
export type ItemData = {
|
||||||
items: ItemPriceResponse[],
|
items: ItemPriceResponse[],
|
||||||
orderItems: OrderItemPriceResponse[]
|
orderItems: OrderItemPriceResponse[]
|
||||||
|
itemHistories: ItemHistoryResponse[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ItemActions = {
|
export type ItemActions = {
|
||||||
@@ -20,11 +21,13 @@ export type ItemActions = {
|
|||||||
setItemQuantity: (orderId: number, itemId: number, quantity: number) => Promise<OrderFilledResponse>
|
setItemQuantity: (orderId: number, itemId: number, quantity: number) => Promise<OrderFilledResponse>
|
||||||
deleteOrderItem: (orderId: number, itemId: number) => Promise<void>
|
deleteOrderItem: (orderId: number, itemId: number) => Promise<void>
|
||||||
deleteItem: (itemId: number) => Promise<void>
|
deleteItem: (itemId: number) => Promise<void>
|
||||||
|
getItemHistory: (itemId: number) => Promise<ItemHistoryResponse[]>
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useItemStore = create<ItemData & ItemActions>((set, get) => ({
|
export const useItemStore = create<ItemData & ItemActions>((set, get) => ({
|
||||||
items: [],
|
items: [],
|
||||||
orderItems: [],
|
orderItems: [],
|
||||||
|
itemHistories: [],
|
||||||
sync: async (): Promise<void> => {
|
sync: async (): Promise<void> => {
|
||||||
const itemPrices = await ItemController.GetItems()
|
const itemPrices = await ItemController.GetItems()
|
||||||
set((state) => ({
|
set((state) => ({
|
||||||
@@ -52,7 +55,7 @@ export const useItemStore = create<ItemData & ItemActions>((set, get) => ({
|
|||||||
item_price: price.toString()
|
item_price: price.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
await ItemController.SetItemPrice(itemPriceQuery)
|
const itemPrice = await ItemController.SetItemPrice(itemPriceQuery)
|
||||||
|
|
||||||
set((state) => {
|
set((state) => {
|
||||||
const item = state.items.filter((i) => i.ItemId === itemId)[0]
|
const item = state.items.filter((i) => i.ItemId === itemId)[0]
|
||||||
@@ -68,7 +71,14 @@ export const useItemStore = create<ItemData & ItemActions>((set, get) => ({
|
|||||||
if(!a.InSeason && b.InSeason)
|
if(!a.InSeason && b.InSeason)
|
||||||
return -1
|
return -1
|
||||||
return a.ItemId - b.ItemId
|
return a.ItemId - b.ItemId
|
||||||
})
|
}),
|
||||||
|
itemHistories: [...state.itemHistories, {
|
||||||
|
ItemId: itemPrice.ItemId.toString(),
|
||||||
|
ItemName: itemPrice.ItemName,
|
||||||
|
ItemPrice: (Math.trunc(itemPrice.ItemPrice * 100) / 100).toString(),
|
||||||
|
ValidFrom: new Date().toLocaleDateString(),
|
||||||
|
ValidTo: new Date().toLocaleDateString()
|
||||||
|
} as ItemHistoryResponse]
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
@@ -151,7 +161,7 @@ export const useItemStore = create<ItemData & ItemActions>((set, get) => ({
|
|||||||
const orderItem = state.orderItems.filter((oi) => oi.ItemId === itemId && oi.OrderId === orderId)[0]
|
const orderItem = state.orderItems.filter((oi) => oi.ItemId === itemId && oi.OrderId === orderId)[0]
|
||||||
const orderItemsWithoutOrderItem = state.orderItems.filter((oi) => oi.ItemId !== itemId || oi.OrderId !== orderId)
|
const orderItemsWithoutOrderItem = state.orderItems.filter((oi) => oi.ItemId !== itemId || oi.OrderId !== orderId)
|
||||||
|
|
||||||
orderItem.Quantity = quantity
|
orderItem.Quantity= quantity
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
@@ -183,5 +193,18 @@ export const useItemStore = create<ItemData & ItemActions>((set, get) => ({
|
|||||||
orderItems: orderItemsWithoutItem
|
orderItems: orderItemsWithoutItem
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
},
|
||||||
|
getItemHistory: async (itemId: number): Promise<ItemHistoryResponse[]> => {
|
||||||
|
const currHist = get().itemHistories.filter((ih) => parseInt(ih.ItemId) === itemId)
|
||||||
|
if(currHist.length > 0)
|
||||||
|
return currHist
|
||||||
|
const itemHistory = await ItemController.GetItemHistory(itemId)
|
||||||
|
|
||||||
|
set((state) => ({
|
||||||
|
...state,
|
||||||
|
itemHistories: [...state.itemHistories, ...itemHistory]
|
||||||
|
}))
|
||||||
|
|
||||||
|
return itemHistory
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import type { NextConfig } from "next";
|
import type { NextConfig } from "next";
|
||||||
|
|
||||||
const nextConfig: NextConfig = {
|
const nextConfig: NextConfig = {
|
||||||
/* config options here */
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default nextConfig;
|
export default nextConfig;
|
||||||
|
|||||||
Reference in New Issue
Block a user