refactor: use react-hook-form for Field type config forms (#5326)
Closes #4295 Note: for the sake of an easier code review, I did not rename/move some files and added "todo" comments instead so Github is able to match those files with their previous version.
This commit is contained in:
@ -1,7 +1,3 @@
|
||||
import { v4 } from 'uuid';
|
||||
|
||||
import { FieldMetadataOption } from '@/object-metadata/types/FieldMetadataOption';
|
||||
import { getDefaultValueForBackend } from '@/object-metadata/utils/getDefaultValueForBackend';
|
||||
import { Field } from '~/generated/graphql';
|
||||
|
||||
import { FieldMetadataItem } from '../types/FieldMetadataItem';
|
||||
@ -26,49 +22,12 @@ export const useFieldMetadataItem = () => {
|
||||
) => {
|
||||
const formattedInput = formatFieldMetadataItemInput(input);
|
||||
|
||||
const defaultValue = getDefaultValueForBackend(
|
||||
input.defaultValue ?? formattedInput.defaultValue,
|
||||
input.type,
|
||||
);
|
||||
|
||||
return createOneFieldMetadataItem({
|
||||
...formattedInput,
|
||||
defaultValue,
|
||||
objectMetadataId: input.objectMetadataId,
|
||||
type: input.type,
|
||||
});
|
||||
};
|
||||
|
||||
const editMetadataField = (
|
||||
input: Pick<
|
||||
Field,
|
||||
| 'id'
|
||||
| 'label'
|
||||
| 'icon'
|
||||
| 'description'
|
||||
| 'defaultValue'
|
||||
| 'type'
|
||||
| 'options'
|
||||
>,
|
||||
) => {
|
||||
// In Edit mode, all options need an id,
|
||||
// so we generate an id for newly created options.
|
||||
const inputOptions = input.options?.map((option: FieldMetadataOption) =>
|
||||
option.id ? option : { ...option, id: v4() },
|
||||
);
|
||||
const formattedInput = formatFieldMetadataItemInput({
|
||||
...input,
|
||||
options: inputOptions,
|
||||
});
|
||||
|
||||
const defaultValue = input.defaultValue ?? formattedInput.defaultValue;
|
||||
|
||||
return updateOneFieldMetadataItem({
|
||||
fieldMetadataIdToUpdate: input.id,
|
||||
updatePayload: {
|
||||
...formattedInput,
|
||||
defaultValue,
|
||||
},
|
||||
label: formattedInput.label ?? '',
|
||||
name: formattedInput.name ?? '',
|
||||
});
|
||||
};
|
||||
|
||||
@ -92,6 +51,5 @@ export const useFieldMetadataItem = () => {
|
||||
createMetadataField,
|
||||
disableMetadataField,
|
||||
eraseMetadataField,
|
||||
editMetadataField,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user