A few react-admin upgrades

as per https://marmelab.com/react-admin/Upgrade.html
This commit is contained in:
Abel Luck 2023-06-07 12:14:12 +00:00
parent cdccc7f062
commit 49650795df
6 changed files with 41 additions and 34 deletions

View file

@ -138,7 +138,7 @@ const handleRequestCode = async ({
verifyMode,
id,
onSuccess,
onFailure,
onError,
captchaCode = undefined,
}: any) => {
if (verifyMode === MODE.SMS) console.log("REQUESTING sms");
@ -160,7 +160,7 @@ const handleRequestCode = async ({
if (response && response.ok) {
onSuccess();
} else {
onFailure(response.status || 400);
onError(response.status || 400);
}
} catch (error: any) {
console.error("Failed to request verification code:", error);
@ -171,7 +171,7 @@ const VerificationCodeRequest = ({
verifyMode,
data,
onSuccess,
onFailure,
onError,
}: any) => {
React.useEffect(() => {
(async () => {
@ -179,10 +179,10 @@ const VerificationCodeRequest = ({
verifyMode,
id: data.id,
onSuccess,
onFailure,
onError,
});
})();
}, [data.id, onFailure, onSuccess, verifyMode]);
}, [data.id, onError, onSuccess, verifyMode]);
return (
<>
@ -204,7 +204,7 @@ const VerificationCaptcha = ({
verifyMode,
data,
onSuccess,
onFailure,
onError,
handleClose,
}: any) => {
const [code, setCode] = React.useState(undefined);
@ -216,7 +216,7 @@ const VerificationCaptcha = ({
verifyMode,
id: data.id,
onSuccess,
onFailure,
onError,
captchaCode: code,
});
setSubmitting(false);
@ -367,7 +367,7 @@ const VerificationCodeDialog = (props: any) => {
props.handleClose();
};
const onFailure = (code: number) => {
const onError = (code: number) => {
if (code === 402 || code === 500) {
setStage("captcha");
} else {
@ -385,7 +385,7 @@ const VerificationCodeDialog = (props: any) => {
<VerificationCodeRequest
mode={props.verifyMode}
onSuccess={onRequestSuccess}
onFailure={onFailure}
onError={onError}
{...props}
/>
)}
@ -400,7 +400,7 @@ const VerificationCodeDialog = (props: any) => {
<VerificationCaptcha
mode={props.verifyMode}
onSuccess={onRequestSuccess}
onFailure={onRestartVerification}
onError={onRestartVerification}
handleClose={handleClose}
{...props}
/>

View file

@ -9,10 +9,10 @@ import {
import { useSession } from "next-auth/react";
import { UserRoleInput } from "./shared";
const UserCreate: FC<CreateProps> = (props: any) => {
const UserCreate: FC<CreateProps> = () => {
const { data: session } = useSession();
return (
<Create {...props} title="Create Users">
<Create title="Create Users">
<SimpleForm>
<TextInput source="email" />
<TextInput source="name" />

View file

@ -8,8 +8,8 @@ import {
Toolbar,
SaveButton,
DeleteButton,
EditProps,
useRedirect,
useRecordContext,
} from "react-admin";
import { useSession } from "next-auth/react";
import { UserRoleInput } from "./shared";
@ -23,16 +23,20 @@ const useStyles = makeStyles((_theme: any) => ({
}));
const UserEditToolbar = (props: any) => {
const classes = useStyles(props);
const classes = useStyles();
const redirect = useRedirect();
const record = useRecordContext();
const {session} = props;
const shouldDisableDelete = !session || !session.user || session.user.id === record.id;
return (
<Toolbar className={classes.defaultToolbar} {...props}>
<Toolbar className={classes.defaultToolbar}>
<SaveButton
label="save"
mutationOptions={{ onSuccess: () => redirect("/users") }}
/>
<DeleteButton disabled={props.session.user.id === props.record.id} />
<DeleteButton disabled={shouldDisableDelete} />
</Toolbar>
);
};
@ -43,11 +47,11 @@ const UserTitle = ({ record }: { record?: any }) => {
return <span>User {title}</span>;
};
const UserEdit = (props: EditProps) => {
const UserEdit = () => {
const { data: session } = useSession();
return (
<Edit title={<UserTitle />} {...props}>
<Edit title={<UserTitle />}>
<SimpleForm toolbar={<UserEditToolbar session={session} />}>
<TextInput disabled source="id" />
<TextInput source="email" />

View file

@ -9,8 +9,8 @@ import {
ListProps,
} from "react-admin";
const UserList = (props: ListProps) => (
<List {...props} exporter={false}>
const UserList = () => (
<List exporter={false}>
<Datagrid rowClick="edit">
<EmailField source="email" />
<DateField source="emailVerified" />

View file

@ -1,6 +1,8 @@
import { SelectInput } from "react-admin";
import { SelectInput, useRecordContext } from "react-admin";
export const UserRoleInput = (props: any) => (
export const UserRoleInput = (props: any) => {
const record = useRecordContext();
return (
<SelectInput
source="userRole"
choices={[
@ -8,7 +10,8 @@ export const UserRoleInput = (props: any) => (
{ id: "USER", name: "User" },
{ id: "ADMIN", name: "Admin" },
]}
disabled={props.session.user.id === props.record.id}
disabled={props.session.user.id === record.id}
{...props}
/>
);
};

View file

@ -117,7 +117,7 @@ const Sidebar = ({ record }: any) => {
const WhatsappBotShow = (props: ShowProps) => {
const refresh = useRefresh();
const { data } = useGetOne("whatsappBots", props.id as any);
const { data } = useGetOne("whatsappBots", {id: props.id});
const { data: registerData, error: registerError } = useSWR(
data && !data?.isVerified