diff --git a/src/components/UserModal/UserModal.tsx b/src/components/UserModal/UserModal.tsx index ced538a..0993b94 100644 --- a/src/components/UserModal/UserModal.tsx +++ b/src/components/UserModal/UserModal.tsx @@ -11,8 +11,19 @@ import { UserModalProps } from './types'; export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps) => { const [deleteModal, setDeleteModal] = useState(false); - const { user, loadUser, editUserById, createNewUser, userModalLoading, deleteUserById, clearSelectedUser } = - useUsers(); + const [isAdminRoleSelected, setAdminRoleSelected] = useState(true); + const [isPersonalModal, setPersonalModal] = useState(false); + const { + user, + loadUser, + loadPersonalInfo, + editUserById, + editPersonalInfo, + createNewUser, + userModalLoading, + deleteUserById, + clearSelectedUser, + } = useUsers(); const { currentUser, isAdmin } = useAuth(); const { control, reset, handleSubmit } = useForm({ @@ -26,7 +37,13 @@ export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps) useEffect(() => { if (userId) { - loadUser(userId); + const currentUserId = currentUser?.id; + if (currentUserId === userId) { + setPersonalModal(true); + loadPersonalInfo(); + } else { + loadUser(userId); + } } // eslint-disable-next-line react-hooks/exhaustive-deps }, [userId, open]); @@ -47,7 +64,9 @@ export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps) }); useEffect(() => { - if (dashboardRole === UserRole.Admin) { + const isAdminDashboardRoleSelected = dashboardRole === UserRole.Admin; + setAdminRoleSelected(isAdminDashboardRoleSelected); + if (isAdminDashboardRoleSelected) { fields.forEach((field, index) => update(index, { name: field.name, role: UserRole.Admin })); } // eslint-disable-next-line react-hooks/exhaustive-deps @@ -55,7 +74,9 @@ export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps) const handleSave = async () => { try { - if (userId) { + if (isPersonalModal) { + await handleSubmit((data) => editPersonalInfo(data))(); + } else if (userId) { await handleSubmit((data) => editUserById(data))(); } else { await handleSubmit((data) => createNewUser(data))(); @@ -178,13 +199,13 @@ export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps) - {isAdmin && ( + {isAdmin && !userModalLoading && (

App Access

- {dashboardRole === UserRole.Admin && ( + {isAdminRoleSelected && (
)} -
-
-
    - {fields.map((item, index) => { - if (item.name === 'dashboard') { - return null; - } + {!isAdminRoleSelected && ( +
    +
    +
      + {fields.map((item, index) => { + if (item.name === 'dashboard') { + return null; + } - return ( -
    • -
      -
      - {item.name -

      - {_.find(appAccessList, ['name', item.name!])?.label} -

      + return ( +
    • +
      +
      + {item.name +

      + {_.find(appAccessList, ['name', item.name!])?.label} +

      +
      +
      + -
      -
      -
    • - ); - })} -
    + + ); + })} +
+
-
+ )}
)} diff --git a/src/services/users/hooks/use-users.ts b/src/services/users/hooks/use-users.ts index 0e5dd35..75febdc 100644 --- a/src/services/users/hooks/use-users.ts +++ b/src/services/users/hooks/use-users.ts @@ -3,7 +3,9 @@ import { getUsers, fetchUsers, fetchUserById, + fetchPersonalInfo, updateUserById, + updatePersonalInfo, createUser, deleteUser, clearCurrentUser, @@ -25,6 +27,10 @@ export function useUsers() { return dispatch(fetchUserById(id)); } + function loadPersonalInfo() { + return dispatch(fetchPersonalInfo()); + } + function clearSelectedUser() { return dispatch(clearCurrentUser()); } @@ -33,6 +39,10 @@ export function useUsers() { return dispatch(updateUserById(data)); } + function editPersonalInfo(data: any) { + return dispatch(updatePersonalInfo(data)); + } + function createNewUser(data: any) { return dispatch(createUser(data)); } @@ -46,7 +56,9 @@ export function useUsers() { user, loadUser, loadUsers, + loadPersonalInfo, editUserById, + editPersonalInfo, userModalLoading, userTableLoading, createNewUser, diff --git a/src/services/users/redux/actions.ts b/src/services/users/redux/actions.ts index 7c35034..1b59cc2 100644 --- a/src/services/users/redux/actions.ts +++ b/src/services/users/redux/actions.ts @@ -69,6 +69,26 @@ export const fetchUserById = (id: string) => async (dispatch: Dispatch) => dispatch(setUserModalLoading(false)); }; +export const fetchPersonalInfo = () => async (dispatch: Dispatch) => { + dispatch(setUserModalLoading(true)); + + try { + const { data } = await performApiCall({ + path: '/me', + method: 'GET', + }); + + dispatch({ + type: UserActionTypes.FETCH_USER, + payload: transformUser(data), + }); + } catch (err) { + console.error(err); + } + + dispatch(setUserModalLoading(false)); +}; + export const updateUserById = (user: any) => async (dispatch: Dispatch, getState: any) => { dispatch(setUserModalLoading(true)); @@ -103,6 +123,34 @@ export const updateUserById = (user: any) => async (dispatch: Dispatch, get dispatch(setUserModalLoading(false)); }; +export const updatePersonalInfo = (user: any) => async (dispatch: Dispatch) => { + dispatch(setUserModalLoading(true)); + + try { + const { data } = await performApiCall({ + path: '/me', + method: 'PUT', + body: transformRequestUser(user), + }); + + dispatch({ + type: UserActionTypes.UPDATE_USER, + payload: transformUser(data), + }); + + dispatch({ + type: AuthActionTypes.UPDATE_AUTH_USER, + payload: transformUser(data), + }); + + showToast('Personal information updated successfully.', ToastType.Success); + } catch (err) { + console.error(err); + } + + dispatch(setUserModalLoading(false)); +}; + export const createUser = (user: any) => async (dispatch: Dispatch) => { dispatch(setUserModalLoading(true));