|
| 1 | +import { ReactElement } from "react"; |
| 2 | +import { |
| 3 | + Create, |
| 4 | + DateInput, |
| 5 | + ListButton, |
| 6 | + maxLength, |
| 7 | + minValue, |
| 8 | + required, |
| 9 | + SimpleForm, |
| 10 | + TextInput, |
| 11 | + TopToolbar, |
| 12 | + useNotify, |
| 13 | + useRedirect, |
| 14 | +} from "react-admin"; |
| 15 | +import PersonalTokenRaScopesCheckbox from "./PersonalTokenRaScopesCheckbox"; |
| 16 | +import { PersonalTokenCreateDetailedResponseV1 } from "@/dataProvider/types"; |
| 17 | + |
| 18 | +const PersonalTokenRaCreateActions = () => { |
| 19 | + return ( |
| 20 | + <TopToolbar> |
| 21 | + <ListButton /> |
| 22 | + </TopToolbar> |
| 23 | + ); |
| 24 | +}; |
| 25 | + |
| 26 | +const PersonalTokenRaCreate = (): ReactElement => { |
| 27 | + const notify = useNotify(); |
| 28 | + const redirect = useRedirect(); |
| 29 | + |
| 30 | + const onSuccess = (data: PersonalTokenCreateDetailedResponseV1) => { |
| 31 | + navigator.clipboard.writeText(data.content); |
| 32 | + notify("resources.personalTokens.notification.tokenCreated", { |
| 33 | + messageArgs: { smart_count: 1 }, |
| 34 | + type: "warning", |
| 35 | + autoHideDuration: null, |
| 36 | + undoable: false, |
| 37 | + }); |
| 38 | + redirect("show", "personalTokens", data.id, data); |
| 39 | + }; |
| 40 | + |
| 41 | + const today = new Date(); |
| 42 | + today.setHours(0); |
| 43 | + today.setMinutes(0); |
| 44 | + today.setSeconds(0); |
| 45 | + today.setMilliseconds(0); |
| 46 | + const todayString = today.toISOString().slice(0, 10); |
| 47 | + |
| 48 | + const transform = (payload: { |
| 49 | + data: { name: string; until?: string }; |
| 50 | + }) => ({ |
| 51 | + name: payload.data.name, |
| 52 | + until: payload.data.until, |
| 53 | + // Other fields cannot be set |
| 54 | + }); |
| 55 | + |
| 56 | + return ( |
| 57 | + <Create |
| 58 | + resource="personalTokens" |
| 59 | + transform={transform} |
| 60 | + mutationMode="pessimistic" |
| 61 | + mutationOptions={{ onSuccess }} |
| 62 | + actions={<PersonalTokenRaCreateActions />} |
| 63 | + > |
| 64 | + <SimpleForm sanitizeEmptyValues> |
| 65 | + <TextInput |
| 66 | + source="data.name" |
| 67 | + label="resources.personalTokens.fields.name" |
| 68 | + helperText="resources.personalTokens.helperText.name" |
| 69 | + validate={[required(), maxLength(64)]} |
| 70 | + /> |
| 71 | + <PersonalTokenRaScopesCheckbox source="data.scopes" disabled /> |
| 72 | + <DateInput |
| 73 | + source="data.since" |
| 74 | + label="resources.personalTokens.fields.since" |
| 75 | + helperText="resources.personalTokens.helperText.since" |
| 76 | + defaultValue={todayString} |
| 77 | + disabled |
| 78 | + /> |
| 79 | + <DateInput |
| 80 | + source="data.until" |
| 81 | + label="resources.personalTokens.fields.until" |
| 82 | + helperText="resources.personalTokens.helperText.until" |
| 83 | + validate={minValue(todayString)} |
| 84 | + /> |
| 85 | + </SimpleForm> |
| 86 | + </Create> |
| 87 | + ); |
| 88 | +}; |
| 89 | + |
| 90 | +export default PersonalTokenRaCreate; |
0 commit comments