From b1dd7bfd252278649622c99b4153e9cfa563cd9f Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 22 Jun 2022 06:56:51 -0700 Subject: Avoid loading current user --- modern/src/settings/UserPage.js | 8 +++++--- modern/src/settings/components/EditItemView.js | 19 +++++++++++-------- 2 files changed, 16 insertions(+), 11 deletions(-) (limited to 'modern/src/settings') diff --git a/modern/src/settings/UserPage.js b/modern/src/settings/UserPage.js index 964291e8..6e637d49 100644 --- a/modern/src/settings/UserPage.js +++ b/modern/src/settings/UserPage.js @@ -1,4 +1,5 @@ import React, { useEffect, useState } from 'react'; +import { useParams } from 'react-router-dom'; import { Accordion, AccordionSummary, @@ -51,12 +52,13 @@ const UserPage = () => { const admin = useAdministrator(); const manager = useManager(); - const currentUserId = useSelector((state) => state.session.user.id); + const currentUser = useSelector((state) => state.session.user); const commonUserAttributes = useCommonUserAttributes(t); const userAttributes = useUserAttributes(t); - const [item, setItem] = useState(); + const { id } = useParams(); + const [item, setItem] = useState(id === currentUser.id.toString() ? currentUser : null); const query = useQuery(); const [queryHandled, setQueryHandled] = useState(false); @@ -74,7 +76,7 @@ const UserPage = () => { }, [item, queryHandled, setQueryHandled, attribute]); const onItemSaved = (result) => { - if (result.id === currentUserId) { + if (result.id === currentUser.id) { dispatch(sessionActions.updateUser(result)); } }; diff --git a/modern/src/settings/components/EditItemView.js b/modern/src/settings/components/EditItemView.js index 8c3df663..a5ac31b1 100644 --- a/modern/src/settings/components/EditItemView.js +++ b/modern/src/settings/components/EditItemView.js @@ -37,17 +37,20 @@ const EditItemView = ({ const { id } = useParams(); useEffectAsync(async () => { - if (id) { - const response = await fetch(`/api/${endpoint}/${id}`); - if (response.ok) { - setItem(await response.json()); + console.log(item); + if (!item) { + if (id) { + const response = await fetch(`/api/${endpoint}/${id}`); + if (response.ok) { + setItem(await response.json()); + } else { + throw Error(await response.text()); + } } else { - throw Error(await response.text()); + setItem(defaultItem || {}); } - } else { - setItem(defaultItem || {}); } - }, [id]); + }, [id, item, defaultItem]); const handleSave = useCatch(async () => { let url = `/api/${endpoint}`; -- cgit v1.2.3