Skip to content

Commit abe7084

Browse files
committed
refactor: convert privacy update flag from ref to state
- Changed isUpdatingUserPrivacy from useRef to useState for better state management - Updated Switch component to use state value directly for disabled prop - Modified handleSwitchChange to use setState instead of mutating ref - Ensures UI properly reflects loading state during privacy updates
1 parent 5a91215 commit abe7084

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

app/pages/users.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export default function Users() {
5656
const { users, setUsers, isPublic, setIsPublic, transactions } = useData();
5757
const [messageApi, contextHolder] = message.useMessage();
5858
const [myProfile, setMyProfile] = useState<DBUser>();
59+
const [isUpdatingUserPrivacy, setIsUpdatingUserPrivacy] = useState(false);
5960

6061
const [selectedUsers, setSelectedUsers] = useState<string[]>([]);
6162

@@ -99,11 +100,10 @@ export default function Users() {
99100
.finally(() => (isLoading.current = false));
100101
}, [needRefresh, setNeedRefresh, page, processUsers]);
101102

102-
const isUpdatingUserPrivacy = useRef(false);
103103
const handleSwitchChange = (value: boolean) => {
104-
if (!currentUser || isUpdatingUserPrivacy.current) return;
104+
if (!currentUser || isUpdatingUserPrivacy) return;
105105

106-
isUpdatingUserPrivacy.current = true;
106+
setIsUpdatingUserPrivacy(true);
107107

108108
fetch('/api/database/updatePrivacy', {
109109
method: 'POST',
@@ -112,7 +112,7 @@ export default function Users() {
112112
.then(result => (result.ok ? setIsPublic(value) : undefined))
113113
.catch(console.error)
114114
.finally(() => {
115-
isUpdatingUserPrivacy.current = false;
115+
setIsUpdatingUserPrivacy(false);
116116
});
117117
};
118118

@@ -152,7 +152,7 @@ export default function Users() {
152152
{isPublic !== undefined ? (
153153
<Flex justify="end" align="center">
154154
<Text className="mx-2 whitespace-nowrap">{isPublic ? t.yes : t.no}</Text>
155-
<Switch disabled={isUpdatingUserPrivacy.current} checked={isPublic} onChange={handleSwitchChange} />
155+
<Switch disabled={isUpdatingUserPrivacy} checked={isPublic} onChange={handleSwitchChange} />
156156
</Flex>
157157
) : (
158158
<div className="bg-theme-border rounded-md w-[70px] h-5 mb-1" />

0 commit comments

Comments
 (0)