-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Chenqin Nee
authored and
Chenqin Nee
committed
Dec 13, 2022
1 parent
ce33dd9
commit b6f0f51
Showing
4 changed files
with
15 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,26 @@ | ||
import type { Table } from '@egodb/core' | ||
import { Alert, Button, Divider, Group, IconAlertCircle, NumberInput, TextInput } from '@egodb/ui' | ||
import { trpc } from '../../trpc' | ||
import { FieldInputLabel } from '../fields/field-input-label' | ||
import { useCreateRecordFormContext } from '../create-record-form/create-record-form-context' | ||
import { Button, Divider, Group } from '@egodb/ui' | ||
|
||
interface IProps { | ||
table: Table | ||
onCancel: () => void | ||
onSuccess?: () => void | ||
rowId?: string | ||
} | ||
|
||
export const EditRecordForm: React.FC<IProps> = ({ table, onCancel, onSuccess, rowId }) => { | ||
const form = useCreateRecordFormContext() | ||
const utils = trpc.useContext() | ||
|
||
const createRecord = trpc.record.create.useMutation({ | ||
onSuccess: () => { | ||
reset() | ||
utils.record.list.refetch() | ||
onSuccess?.() | ||
}, | ||
}) | ||
|
||
const onSubmit = form.onSubmit((values) => { | ||
createRecord.mutate(values) | ||
}) | ||
|
||
const reset = () => { | ||
onCancel() | ||
createRecord.reset() | ||
form.reset() | ||
form.resetDirty() | ||
form.resetTouched() | ||
} | ||
|
||
export const EditRecordForm: React.FC<IProps> = ({ table, onCancel, onSuccess }) => { | ||
return ( | ||
<form onSubmit={onSubmit}> | ||
{table.schema.fields.map((field, index) => { | ||
const props = form.getInputProps(`value.${index}.value`) | ||
const label = <FieldInputLabel>{field.name.value}</FieldInputLabel> | ||
if (field.type === 'number') { | ||
return <NumberInput {...props} label={label} /> | ||
} | ||
return <TextInput {...props} label={label} /> | ||
})} | ||
|
||
<form> | ||
<Divider my="lg" /> | ||
|
||
<Group position="right"> | ||
<Button variant="subtle" onClick={() => onCancel()}> | ||
Cancel | ||
</Button> | ||
|
||
<Button loading={createRecord.isLoading} miw={200} disabled={!form.isValid()} type="submit"> | ||
<Button miw={200} type="submit"> | ||
Confirm | ||
</Button> | ||
</Group> | ||
|
||
{createRecord.isError && ( | ||
<Alert color="red" icon={<IconAlertCircle size={16} />} title="Oops! Create Record Error!" mt="lg"> | ||
{createRecord.error.message} | ||
</Alert> | ||
)} | ||
</form> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters