import { useState } from "react" import styled from "styled-components" import { ItemPriceResponse } from "../client/response" import { ItemHistoryTable } from "./ItemHistoryTable" import useAsyncEffect from "use-async-effect" import { useItemStore } from "../providers/ItemsProvider" import { Mutex } from "async-mutex" import { useCurrentAuthenticatedUserStore } from "../providers" import { useShallow } from "zustand/shallow" type ItemTableListRowProps = { item: ItemPriceResponse } const ItemRowContainer = styled.div` margin-bottom: 10px; padding-top: 5px; padding-bottom: 5px; width: 100%; ` const ItemFieldContainer = styled.div` display: inline-block; padding-left: 5px; padding-right: 5px; ` const ItemOverviewContainer = styled.div` display: inline-block; background-color: #410041ff; width: 100%; &:hover { background-color: #920592ff; cursor: pointer; } ` const ItemDetailsContainer = styled.div` display: inline-block; background-color: #6e296eff; width: 100%; &:hover { background-color: #da51daff; color: #000; cursor: pointer; } ` const itemTableListRowMutex = new Mutex() export const ItemTableListRow = ({item}: ItemTableListRowProps) => { const [shouldShowDetails, setShouldShowDetails] = useState(false) const itemStore = useItemStore((state) => state) const [newItemPrice, setNewItemPrice] = useState(item.ItemPrice) const [shouldPushNewItemPrice, setShouldPushNewItemPrice] = useState(false) const authUserStore = useCurrentAuthenticatedUserStore(useShallow((state) => state)) useAsyncEffect(async () => { if(shouldPushNewItemPrice && authUserStore.Admin) { const release = await itemTableListRowMutex.acquire() setShouldPushNewItemPrice(false) await itemStore.setItemPrice(item.ItemId, newItemPrice) await release() } }, [shouldPushNewItemPrice, authUserStore]) useAsyncEffect(async () => { if (authUserStore.Id === -1) { const release = await itemTableListRowMutex.acquire() await authUserStore.sync() await release() } }, [authUserStore]) return (
  • { setShouldShowDetails(!shouldShowDetails) }}> item: {item.ItemName} price: {Math.trunc(item.ItemPrice * 100) / 100} {shouldShowDetails && authUserStore.Admin && ( <>

    Set Item Price


    { const newPrice = parseInt(e.currentTarget.value) if(!Number.isNaN(newPrice)) setNewItemPrice(newPrice) }}/>
    )}
  • ) }