fix: concurrency
This commit is contained in:
@@ -5,6 +5,8 @@ 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
|
||||
@@ -56,15 +58,25 @@ export const ItemTableListRow = ({item}: ItemTableListRowProps) => {
|
||||
|
||||
const [shouldPushNewItemPrice, setShouldPushNewItemPrice] = useState<boolean>(false)
|
||||
|
||||
const authUserStore = useCurrentAuthenticatedUserStore(useShallow((state) => state))
|
||||
|
||||
useAsyncEffect(async () => {
|
||||
if(shouldPushNewItemPrice)
|
||||
if(shouldPushNewItemPrice && authUserStore.Admin)
|
||||
{
|
||||
const release = await itemTableListRowMutex.acquire()
|
||||
setShouldPushNewItemPrice(false)
|
||||
await itemStore.setItemPrice(item.ItemId, newItemPrice)
|
||||
await release()
|
||||
}
|
||||
}, [shouldPushNewItemPrice])
|
||||
}, [shouldPushNewItemPrice, authUserStore])
|
||||
|
||||
useAsyncEffect(async () => {
|
||||
if (authUserStore.Id === -1) {
|
||||
const release = await itemTableListRowMutex.acquire()
|
||||
await authUserStore.sync()
|
||||
await release()
|
||||
}
|
||||
}, [authUserStore])
|
||||
|
||||
return (
|
||||
<li>
|
||||
@@ -79,7 +91,7 @@ export const ItemTableListRow = ({item}: ItemTableListRowProps) => {
|
||||
price: {Math.trunc(item.ItemPrice * 100) / 100}
|
||||
</ItemFieldContainer>
|
||||
</ItemOverviewContainer>
|
||||
{shouldShowDetails && (
|
||||
{shouldShowDetails && authUserStore.Admin && (
|
||||
<>
|
||||
<ItemDetailsContainer>
|
||||
<h1 className="text-xl">Set Item Price</h1>
|
||||
|
||||
Reference in New Issue
Block a user