From d970f742f936ae6737cc2c3dfb0b34008f41afbd Mon Sep 17 00:00:00 2001 From: cesarLima1 Date: Mon, 25 Nov 2024 09:16:28 -0400 Subject: [PATCH 1/2] [TM-1385] add button and functionality to create user for super admin --- .../dataProviders/userDataProvider.ts | 15 ++++ src/admin/components/App.tsx | 1 + src/admin/modules/index.tsx | 4 +- .../modules/user/components/UserCreate.tsx | 76 +++++++++++++++++++ .../modules/user/components/UserShowAside.tsx | 24 +++++- src/generated/apiComponents.ts | 65 ++++++++++++++++ src/generated/apiSchemas.ts | 13 ++++ 7 files changed, 196 insertions(+), 2 deletions(-) create mode 100644 src/admin/modules/user/components/UserCreate.tsx diff --git a/src/admin/apiProvider/dataProviders/userDataProvider.ts b/src/admin/apiProvider/dataProviders/userDataProvider.ts index 1e2dcffd6..4cf136351 100644 --- a/src/admin/apiProvider/dataProviders/userDataProvider.ts +++ b/src/admin/apiProvider/dataProviders/userDataProvider.ts @@ -7,11 +7,13 @@ import { fetchGetV2AdminUsersExport, fetchGetV2AdminUsersMulti, fetchGetV2AdminUsersUUID, + fetchPostV2AdminUsersCreate, fetchPutV2AdminUsersUUID, GetV2AdminUsersError, GetV2AdminUsersExportError, GetV2AdminUsersMultiError, GetV2AdminUsersUUIDError, + PostV2AdminUsersCreateError, PutV2AdminUsersUUIDError } from "@/generated/apiComponents"; import { V2AdminUserRead } from "@/generated/apiSchemas"; @@ -34,6 +36,19 @@ const normalizeUserObject = (item: V2AdminUserRead) => ({ }); export const userDataProvider: UserDataProvider = { + //@ts-ignore + async create(__, params) { + try { + const response = await fetchPostV2AdminUsersCreate({ + body: params.data + }); + + // @ts-expect-error + return { data: { ...response.data, id: response.id } }; + } catch (err) { + throw getFormattedErrorForRA(err as PostV2AdminUsersCreateError); + } + }, //@ts-ignore async getList(_, params) { try { diff --git a/src/admin/components/App.tsx b/src/admin/components/App.tsx index a3b4e8db5..2a07ea914 100644 --- a/src/admin/components/App.tsx +++ b/src/admin/components/App.tsx @@ -34,6 +34,7 @@ const App = () => { list={modules.user.List} show={modules.user.Show} edit={modules.user.Edit} + create={modules.user.Create} icon={() => } /> { + const { isSuperAdmin } = useGetUserRole(); + + const schemaObject: any = { + first_name: yup.string().nullable().required("First Name is required"), + last_name: yup.string().nullable().required("Last Name is required"), + email_address: yup.string().nullable().required("Email Address is required").email("Invalid email format"), + phone_number: yup.string().nullable(), + job_role: yup.string().nullable(), + organisation: yup.object().nullable(), + program: yup.string().nullable(), + country: yup.string().nullable() + }; + + if (isSuperAdmin) { + schemaObject.role = yup.string().required("Role is required"); + } + + return ( + + + + + + + + + + + + + {isSuperAdmin && } + + + + + + + + + ); +}; + +export default UserCreate; diff --git a/src/admin/modules/user/components/UserShowAside.tsx b/src/admin/modules/user/components/UserShowAside.tsx index 575191f03..e0b8947ab 100644 --- a/src/admin/modules/user/components/UserShowAside.tsx +++ b/src/admin/modules/user/components/UserShowAside.tsx @@ -1,9 +1,20 @@ import { Box, Button, Divider, Grid, Stack, Typography } from "@mui/material"; import { useState } from "react"; -import { BooleanField, RaRecord, SelectField, TextField, useNotify, useRefresh, useShowContext } from "react-admin"; +import { + BooleanField, + RaRecord, + SelectField, + TextField, + useNotify, + useRedirect, + useRefresh, + useShowContext +} from "react-admin"; +import { When } from "react-if"; import Aside from "@/admin/components/Aside/Aside"; import { ConfirmationDialog } from "@/admin/components/Dialogs/ConfirmationDialog"; +import { useGetUserRole } from "@/admin/hooks/useGetUserRole"; import { ResetPasswordDialog } from "@/admin/modules/user/components/ResetPasswordDialog"; import { usePatchV2AdminUsersVerifyUUID, usePostAuthReset, usePostV2UsersResend } from "@/generated/apiComponents"; import { V2AdminUserRead } from "@/generated/apiSchemas"; @@ -13,6 +24,8 @@ import { userPrimaryRoleChoices } from "../const"; export const UserShowAside = () => { const notify = useNotify(); const refresh = useRefresh(); + const redirect = useRedirect(); + const { isSuperAdmin } = useGetUserRole(); const [showResetPasswordDialog, setShowResetPasswordDialog] = useState(false); const [showVerifyEmailDialog, setShowVerifyEmailDialog] = useState(false); @@ -41,6 +54,10 @@ export const UserShowAside = () => { } }); + const handleCreateUser = () => { + redirect("create", "user"); + }; + return ( <>