Skip to content

Commit c4875fa

Browse files
committed
fix(lint): resolve set-state-in-effect errors in user admin dialog
1 parent 5a4d943 commit c4875fa

1 file changed

Lines changed: 41 additions & 26 deletions

File tree

app/(authenticated)/users/_components/user-admin-dialog.tsx

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,33 @@ function FieldError({ errors }: { errors?: string[] }) {
4242
return <p className="text-sm text-destructive">{errors[0]}</p>;
4343
}
4444

45+
function useActionConfirm(
46+
state: UserAdminActionState | null,
47+
pending: boolean,
48+
onSuccess: () => void,
49+
) {
50+
const [confirmOpen, setConfirmOpen] = useState(false);
51+
const prevStateRef = useRef(state);
52+
53+
useEffect(() => {
54+
if (state === prevStateRef.current) return;
55+
prevStateRef.current = state;
56+
if (state?.message) onSuccess();
57+
}, [state, onSuccess]);
58+
59+
const confirmVisible =
60+
confirmOpen &&
61+
!state?.message &&
62+
!(state?.errors && !pending);
63+
64+
function openConfirm() {
65+
setConfirmOpen(false);
66+
queueMicrotask(() => setConfirmOpen(true));
67+
}
68+
69+
return { confirmVisible, openConfirm, setConfirmOpen };
70+
}
71+
4572
type ConfirmAlertProps = {
4673
open: boolean;
4774
onOpenChange: (open: boolean) => void;
@@ -104,7 +131,6 @@ function CreateUserFormContent({
104131
onSuccess,
105132
onCancel,
106133
}: CreateUserFormContentProps) {
107-
const [confirmOpen, setConfirmOpen] = useState(false);
108134
const [firstName, setFirstName] = useState(CREATE_DEFAULTS.firstName);
109135
const [lastName, setLastName] = useState(CREATE_DEFAULTS.lastName);
110136
const [email, setEmail] = useState(CREATE_DEFAULTS.email);
@@ -123,16 +149,11 @@ function CreateUserFormContent({
123149
FormData
124150
>(createUserAdmin, null);
125151

126-
useEffect(() => {
127-
if (state?.message) {
128-
setConfirmOpen(false);
129-
onSuccess();
130-
}
131-
}, [state?.message, onSuccess]);
132-
133-
useEffect(() => {
134-
if (state?.errors) setConfirmOpen(false);
135-
}, [state?.errors]);
152+
const { confirmVisible, openConfirm, setConfirmOpen } = useActionConfirm(
153+
state,
154+
pending,
155+
onSuccess,
156+
);
136157

137158
return (
138159
<>
@@ -274,7 +295,7 @@ function CreateUserFormContent({
274295
</Button>
275296
<Button
276297
type="button"
277-
onClick={() => setConfirmOpen(true)}
298+
onClick={openConfirm}
278299
disabled={pending}
279300
>
280301
Create user
@@ -283,7 +304,7 @@ function CreateUserFormContent({
283304
</form>
284305

285306
<ConfirmAlert
286-
open={confirmOpen}
307+
open={confirmVisible}
287308
onOpenChange={setConfirmOpen}
288309
title="Create user?"
289310
description="Are you sure you want to create this user?"
@@ -342,7 +363,6 @@ function EditUserFormContent({
342363
onSuccess,
343364
onCancel,
344365
}: EditUserFormContentProps) {
345-
const [confirmOpen, setConfirmOpen] = useState(false);
346366
const [email, setEmail] = useState(user.email);
347367
const [role, setRole] = useState(user.role);
348368

@@ -352,16 +372,11 @@ function EditUserFormContent({
352372
FormData
353373
>(updateUserAdmin, null);
354374

355-
useEffect(() => {
356-
if (state?.message) {
357-
setConfirmOpen(false);
358-
onSuccess();
359-
}
360-
}, [state?.message, onSuccess]);
361-
362-
useEffect(() => {
363-
if (state?.errors) setConfirmOpen(false);
364-
}, [state?.errors]);
375+
const { confirmVisible, openConfirm, setConfirmOpen } = useActionConfirm(
376+
state,
377+
pending,
378+
onSuccess,
379+
);
365380

366381
return (
367382
<>
@@ -430,7 +445,7 @@ function EditUserFormContent({
430445
</Button>
431446
<Button
432447
type="button"
433-
onClick={() => setConfirmOpen(true)}
448+
onClick={openConfirm}
434449
disabled={pending}
435450
>
436451
Save changes
@@ -439,7 +454,7 @@ function EditUserFormContent({
439454
</form>
440455

441456
<ConfirmAlert
442-
open={confirmOpen}
457+
open={confirmVisible}
443458
onOpenChange={setConfirmOpen}
444459
title="Save changes?"
445460
description="Are you sure you want to save these changes?"

0 commit comments

Comments
 (0)