Feat/metadata add update and delete on frontend (#2102)
* Reworked metadata creation * Wip * Fix from PR * Removed consolelog * Post merge * Fixed seeds
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import { gql, useMutation } from '@apollo/client';
|
||||
|
||||
import { generateCreateOneObjectMutation } from '../utils/generateCreateOneObjectMutation';
|
||||
|
||||
import { useFindManyMetadataObjects } from './useFindManyMetadataObjects';
|
||||
|
||||
export const useCreateOneObject = ({
|
||||
objectNamePlural,
|
||||
}: {
|
||||
objectNamePlural: string;
|
||||
}) => {
|
||||
const { metadataObjects } = useFindManyMetadataObjects();
|
||||
|
||||
const foundMetadataObject = metadataObjects.find(
|
||||
(object) => object.namePlural === objectNamePlural,
|
||||
);
|
||||
|
||||
const generatedMutation = foundMetadataObject
|
||||
? generateCreateOneObjectMutation({
|
||||
metadataObject: foundMetadataObject,
|
||||
})
|
||||
: gql`
|
||||
mutation EmptyMutation {
|
||||
empty
|
||||
}
|
||||
`;
|
||||
|
||||
const [mutate] = useMutation(generatedMutation);
|
||||
|
||||
const createOneObject = foundMetadataObject
|
||||
? (input: Record<string, any>) => {
|
||||
return mutate({
|
||||
variables: {
|
||||
input: {
|
||||
...input,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
: undefined;
|
||||
|
||||
const objectNotFoundInMetadata =
|
||||
metadataObjects.length > 0 && !foundMetadataObject;
|
||||
|
||||
return {
|
||||
createOneObject,
|
||||
objectNotFoundInMetadata,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user