status fix
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { Switch } from '@astryxdesign/core/Switch';
|
||||
import { Button } from '@astryxdesign/core/Button';
|
||||
@@ -56,17 +56,35 @@ export function StoreOpenControl({
|
||||
const [until, setUntil] = useState('');
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
// Re-seed if the page refetches with a different answer, but never while the
|
||||
// confirmation is up — that would move the switch under the shopkeeper's hand.
|
||||
/*
|
||||
Re-seed only when the PROP ITSELF changes.
|
||||
|
||||
This used to run whenever `confirming` flipped, and that undid its own
|
||||
save: closing the branch set open to false and confirming to false, the
|
||||
change of `confirming` re-ran the effect, and it wrote the prop — still
|
||||
true, because the page behind had not refetched yet — straight back over
|
||||
the answer. The switch snapped open the instant the shop was closed.
|
||||
|
||||
Comparing against the last value seen means a genuinely new answer from the
|
||||
server still lands, and nothing else can reach in and overwrite what the
|
||||
shopkeeper just did.
|
||||
*/
|
||||
const lastSeen = useRef(isOpen);
|
||||
useEffect(() => {
|
||||
if (!confirming) setOpen(isOpen);
|
||||
}, [isOpen, confirming]);
|
||||
if (lastSeen.current !== isOpen) {
|
||||
lastSeen.current = isOpen;
|
||||
setOpen(isOpen);
|
||||
}
|
||||
}, [isOpen]);
|
||||
|
||||
const save = useMutation({
|
||||
mutationFn: (next: { isopen: boolean; closeduntil?: string }) =>
|
||||
tenantsApi.setStoreOpen({ tenantid, locationid, ...next }),
|
||||
onMutate: () => setError(null),
|
||||
onSuccess: (_data, next) => {
|
||||
// Move the reference too. Otherwise the next render carrying the old
|
||||
// prop reads as a change and reverts what was just saved.
|
||||
lastSeen.current = next.isopen;
|
||||
setOpen(next.isopen);
|
||||
setConfirming(false);
|
||||
setUntil('');
|
||||
@@ -75,6 +93,7 @@ export function StoreOpenControl({
|
||||
// A close that fails silently is the worst outcome here: the shopkeeper
|
||||
// walks away believing the shop is shut while it keeps taking orders.
|
||||
onError: (cause) => {
|
||||
lastSeen.current = isOpen;
|
||||
setOpen(isOpen);
|
||||
setError(errorMessage(cause));
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user