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:
@ -1,11 +1,13 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useRecoilState } from 'recoil';
|
||||
|
||||
import { isFlexibleBackendEnabledState } from '@/client-config/states/isFlexibleBackendEnabledState';
|
||||
import { MetadataObjectsQuery } from '~/generated-metadata/graphql';
|
||||
|
||||
import { GET_ALL_OBJECTS } from '../graphql/queries';
|
||||
import { useApolloMetadataClient } from '../hooks/useApolloClientMetadata';
|
||||
import { FIND_MANY_METADATA_OBJECTS } from '../graphql/queries';
|
||||
import { useApolloMetadataClient } from '../hooks/useApolloMetadataClient';
|
||||
import { useCreateOneObject } from '../hooks/useCreateOneObject';
|
||||
import { useFindManyObjects } from '../hooks/useFindManyObjects';
|
||||
import { useSeedCustomObjectsTemp } from '../hooks/useSeedCustomObjectsTemp';
|
||||
import { metadataObjectsState } from '../states/metadataObjectsState';
|
||||
import { MetadataObject } from '../types/MetadataObject';
|
||||
@ -20,13 +22,44 @@ export const FetchMetadataEffect = () => {
|
||||
|
||||
const seedCustomObjectsTemp = useSeedCustomObjectsTemp();
|
||||
|
||||
const { createOneObject } = useCreateOneObject({
|
||||
objectNamePlural: 'suppliers',
|
||||
});
|
||||
|
||||
const { objects: suppliers, loading } = useFindManyObjects({
|
||||
objectNamePlural: 'suppliers',
|
||||
});
|
||||
|
||||
const [created, setCreated] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!created && !loading && suppliers.length === 0 && createOneObject) {
|
||||
createOneObject({
|
||||
name: 'Supplier 1',
|
||||
city: 'City 1',
|
||||
});
|
||||
|
||||
createOneObject({
|
||||
name: 'Supplier 2',
|
||||
city: 'City 2',
|
||||
});
|
||||
|
||||
createOneObject({
|
||||
name: 'Supplier 3',
|
||||
city: 'City 3',
|
||||
});
|
||||
|
||||
setCreated(true);
|
||||
}
|
||||
}, [suppliers, createOneObject, loading, created]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isFlexibleBackendEnabled) return;
|
||||
|
||||
(async () => {
|
||||
if (apolloMetadataClient && metadataObjects.length === 0) {
|
||||
const objects = await apolloMetadataClient.query<MetadataObjectsQuery>({
|
||||
query: GET_ALL_OBJECTS,
|
||||
query: FIND_MANY_METADATA_OBJECTS,
|
||||
});
|
||||
|
||||
if (
|
||||
@ -48,7 +81,7 @@ export const FetchMetadataEffect = () => {
|
||||
|
||||
const objects =
|
||||
await apolloMetadataClient.query<MetadataObjectsQuery>({
|
||||
query: GET_ALL_OBJECTS,
|
||||
query: FIND_MANY_METADATA_OBJECTS,
|
||||
});
|
||||
|
||||
const formattedObjects: MetadataObject[] =
|
||||
|
||||
@ -10,7 +10,7 @@ import { savedSortsFamilyState } from '@/ui/data/view-bar/states/savedSortsFamil
|
||||
import { sortsScopedState } from '@/ui/data/view-bar/states/sortsScopedState';
|
||||
import { useRecoilScopeId } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopeId';
|
||||
|
||||
import { useFindManyCustomObjects } from '../hooks/useFindManyCustomObjects';
|
||||
import { useFindManyObjects } from '../hooks/useFindManyObjects';
|
||||
import { useSetObjectDataTableData } from '../hooks/useSetDataTableData';
|
||||
|
||||
export const ObjectDataTableEffect = ({
|
||||
@ -22,7 +22,7 @@ export const ObjectDataTableEffect = ({
|
||||
}) => {
|
||||
const setDataTableData = useSetObjectDataTableData();
|
||||
|
||||
const { objects } = useFindManyCustomObjects({
|
||||
const { objects } = useFindManyObjects({
|
||||
objectNamePlural: objectNamePlural,
|
||||
});
|
||||
|
||||
|
||||
@ -37,7 +37,7 @@ export const ObjectTable = ({
|
||||
/>
|
||||
<ViewBarContext.Provider
|
||||
value={{
|
||||
defaultViewName: 'All Suppliers',
|
||||
defaultViewName: '???',
|
||||
onCurrentViewSubmit: submitCurrentView,
|
||||
onViewCreate: createView,
|
||||
onViewEdit: updateView,
|
||||
|
||||
@ -1,15 +1,15 @@
|
||||
import { gql } from '@apollo/client';
|
||||
|
||||
export const CREATE_ONE_OBJECT = gql`
|
||||
mutation CreateOneObject($input: CreateOneObjectInput!) {
|
||||
export const CREATE_ONE_METADATA_OBJECT = gql`
|
||||
mutation CreateOneMetadataObject($input: CreateOneObjectInput!) {
|
||||
createOneObject(input: $input) {
|
||||
id
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
export const CREATE_ONE_FIELD = gql`
|
||||
mutation CreateOneField($input: CreateOneFieldInput!) {
|
||||
export const CREATE_ONE_METADATA_FIELD = gql`
|
||||
mutation CreateOneMetadataField($input: CreateOneFieldInput!) {
|
||||
createOneField(input: $input) {
|
||||
id
|
||||
type
|
||||
@ -26,3 +26,25 @@ export const CREATE_ONE_FIELD = gql`
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
export const UPDATE_ONE_METADATA_FIELD = gql`
|
||||
mutation UpdateOneMetadataField(
|
||||
$idToUpdate: ID!
|
||||
$updatePayload: UpdateFieldInput!
|
||||
) {
|
||||
updateOneField(input: { id: $idToUpdate, update: $updatePayload }) {
|
||||
id
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
export const UPDATE_ONE_METADATA_OBJECT = gql`
|
||||
mutation UpdateOneMetadataObject(
|
||||
$idToUpdate: ID!
|
||||
$updatePayload: UpdateObjectInput!
|
||||
) {
|
||||
updateOneObject(input: { id: $idToUpdate, update: $updatePayload }) {
|
||||
id
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { gql } from '@apollo/client';
|
||||
|
||||
export const GET_ALL_OBJECTS = gql`
|
||||
export const FIND_MANY_METADATA_OBJECTS = gql`
|
||||
query MetadataObjects {
|
||||
objects {
|
||||
edges {
|
||||
|
||||
@ -3,5 +3,7 @@ import { useContext } from 'react';
|
||||
import { ApolloMetadataClientContext } from '../context/ApolloClientMetadataContext';
|
||||
|
||||
export const useApolloMetadataClient = () => {
|
||||
return useContext(ApolloMetadataClientContext);
|
||||
const apolloMetadataClient = useContext(ApolloMetadataClientContext);
|
||||
|
||||
return apolloMetadataClient;
|
||||
};
|
||||
@ -2,27 +2,27 @@ import { ApolloClient, useMutation } from '@apollo/client';
|
||||
import { getOperationName } from '@apollo/client/utilities';
|
||||
|
||||
import {
|
||||
CreateOneFieldMutation,
|
||||
CreateOneFieldMutationVariables,
|
||||
CreateOneMetadataFieldMutation,
|
||||
CreateOneMetadataFieldMutationVariables,
|
||||
} from '~/generated-metadata/graphql';
|
||||
|
||||
import { CREATE_ONE_FIELD } from '../graphql/mutations';
|
||||
import { GET_ALL_OBJECTS } from '../graphql/queries';
|
||||
import { CREATE_ONE_METADATA_FIELD } from '../graphql/mutations';
|
||||
import { FIND_MANY_METADATA_OBJECTS } from '../graphql/queries';
|
||||
|
||||
import { useApolloMetadataClient } from './useApolloClientMetadata';
|
||||
import { useApolloMetadataClient } from './useApolloMetadataClient';
|
||||
|
||||
export const useCreateOneMetadataField = () => {
|
||||
const apolloMetadataClient = useApolloMetadataClient();
|
||||
|
||||
const [mutate] = useMutation<
|
||||
CreateOneFieldMutation,
|
||||
CreateOneFieldMutationVariables
|
||||
>(CREATE_ONE_FIELD, {
|
||||
CreateOneMetadataFieldMutation,
|
||||
CreateOneMetadataFieldMutationVariables
|
||||
>(CREATE_ONE_METADATA_FIELD, {
|
||||
client: apolloMetadataClient ?? ({} as ApolloClient<any>),
|
||||
});
|
||||
|
||||
const createOneMetadataField = (
|
||||
input: CreateOneFieldMutationVariables['input']['field'],
|
||||
input: CreateOneMetadataFieldMutationVariables['input']['field'],
|
||||
) =>
|
||||
mutate({
|
||||
variables: {
|
||||
@ -32,7 +32,7 @@ export const useCreateOneMetadataField = () => {
|
||||
},
|
||||
},
|
||||
},
|
||||
refetchQueries: [getOperationName(GET_ALL_OBJECTS) ?? ''],
|
||||
refetchQueries: [getOperationName(FIND_MANY_METADATA_OBJECTS) ?? ''],
|
||||
});
|
||||
|
||||
return {
|
||||
|
||||
@ -2,27 +2,27 @@ import { ApolloClient, useMutation } from '@apollo/client';
|
||||
import { getOperationName } from '@apollo/client/utilities';
|
||||
|
||||
import {
|
||||
CreateOneObjectMutation,
|
||||
CreateOneObjectMutationVariables,
|
||||
CreateOneMetadataObjectMutation,
|
||||
CreateOneMetadataObjectMutationVariables,
|
||||
} from '~/generated-metadata/graphql';
|
||||
|
||||
import { CREATE_ONE_OBJECT } from '../graphql/mutations';
|
||||
import { GET_ALL_OBJECTS } from '../graphql/queries';
|
||||
import { CREATE_ONE_METADATA_OBJECT } from '../graphql/mutations';
|
||||
import { FIND_MANY_METADATA_OBJECTS } from '../graphql/queries';
|
||||
|
||||
import { useApolloMetadataClient } from './useApolloClientMetadata';
|
||||
import { useApolloMetadataClient } from './useApolloMetadataClient';
|
||||
|
||||
export const useCreateOneMetadataObject = () => {
|
||||
const apolloMetadataClient = useApolloMetadataClient();
|
||||
|
||||
const [mutate] = useMutation<
|
||||
CreateOneObjectMutation,
|
||||
CreateOneObjectMutationVariables
|
||||
>(CREATE_ONE_OBJECT, {
|
||||
CreateOneMetadataObjectMutation,
|
||||
CreateOneMetadataObjectMutationVariables
|
||||
>(CREATE_ONE_METADATA_OBJECT, {
|
||||
client: apolloMetadataClient ?? ({} as ApolloClient<any>),
|
||||
});
|
||||
|
||||
const createOneMetadataObject = (
|
||||
input: CreateOneObjectMutationVariables['input']['object'],
|
||||
input: CreateOneMetadataObjectMutationVariables['input']['object'],
|
||||
) =>
|
||||
mutate({
|
||||
variables: {
|
||||
@ -32,7 +32,7 @@ export const useCreateOneMetadataObject = () => {
|
||||
},
|
||||
},
|
||||
},
|
||||
refetchQueries: [getOperationName(GET_ALL_OBJECTS) ?? ''],
|
||||
refetchQueries: [getOperationName(FIND_MANY_METADATA_OBJECTS) ?? ''],
|
||||
});
|
||||
|
||||
return {
|
||||
|
||||
49
front/src/modules/metadata/hooks/useCreateOneObject.ts
Normal file
49
front/src/modules/metadata/hooks/useCreateOneObject.ts
Normal file
@ -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,
|
||||
};
|
||||
};
|
||||
@ -0,0 +1,60 @@
|
||||
import { useMemo } from 'react';
|
||||
import { useQuery } from '@apollo/client';
|
||||
|
||||
import {
|
||||
MetadataObjectsQuery,
|
||||
MetadataObjectsQueryVariables,
|
||||
} from '~/generated-metadata/graphql';
|
||||
|
||||
import { FIND_MANY_METADATA_OBJECTS } from '../graphql/queries';
|
||||
import { formatPagedMetadataObjectsToMetadataObjects } from '../utils/formatPagedMetadataObjectsToMetadataObjects';
|
||||
|
||||
import { useApolloMetadataClient } from './useApolloMetadataClient';
|
||||
|
||||
// TODO: test fetchMore
|
||||
export const useFindManyMetadataObjects = () => {
|
||||
const apolloMetadataClient = useApolloMetadataClient();
|
||||
|
||||
const { data, fetchMore: fetchMoreInternal } = useQuery<
|
||||
MetadataObjectsQuery,
|
||||
MetadataObjectsQueryVariables
|
||||
>(FIND_MANY_METADATA_OBJECTS, {
|
||||
client: apolloMetadataClient ?? undefined,
|
||||
skip: !apolloMetadataClient,
|
||||
});
|
||||
|
||||
const hasMore = data?.objects?.pageInfo?.hasNextPage;
|
||||
|
||||
const fetchMore = () =>
|
||||
fetchMoreInternal({
|
||||
variables: {
|
||||
afterCursor: data?.objects?.pageInfo?.endCursor,
|
||||
},
|
||||
});
|
||||
|
||||
const metadataObjects = useMemo(() => {
|
||||
return formatPagedMetadataObjectsToMetadataObjects({
|
||||
pagedMetadataObjects: data,
|
||||
});
|
||||
}, [data]);
|
||||
|
||||
const getMetadataObjectsFromCache = () => {
|
||||
const queryResult = apolloMetadataClient?.readQuery<
|
||||
MetadataObjectsQuery,
|
||||
MetadataObjectsQueryVariables
|
||||
>({
|
||||
query: FIND_MANY_METADATA_OBJECTS,
|
||||
});
|
||||
|
||||
return formatPagedMetadataObjectsToMetadataObjects({
|
||||
pagedMetadataObjects: queryResult ?? undefined,
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
metadataObjects,
|
||||
hasMore,
|
||||
fetchMore,
|
||||
getMetadataObjectsFromCache,
|
||||
};
|
||||
};
|
||||
@ -5,16 +5,18 @@ import { PaginatedObjectType } from '../types/PaginatedObjectType';
|
||||
import { formatPagedObjectsToObjects } from '../utils/formatPagedObjectsToObjects';
|
||||
import { generateFindManyCustomObjectsQuery } from '../utils/generateFindManyCustomObjectsQuery';
|
||||
|
||||
import { useFindAllMetadata } from './useFindAllMetadata';
|
||||
import { useFindManyMetadataObjects } from './useFindManyMetadataObjects';
|
||||
|
||||
// TODO: test with a wrong name
|
||||
// TODO: add zod to validate that we have at least id on each object
|
||||
export const useFindManyCustomObjects = <ObjectType extends { id: string }>({
|
||||
export const useFindManyObjects = <
|
||||
ObjectType extends { id: string } & Record<string, any>,
|
||||
>({
|
||||
objectNamePlural,
|
||||
}: {
|
||||
objectNamePlural: string;
|
||||
}) => {
|
||||
const { metadataObjects } = useFindAllMetadata();
|
||||
const { metadataObjects } = useFindManyMetadataObjects();
|
||||
|
||||
const foundMetadataObject = metadataObjects.find(
|
||||
(object) => object.namePlural === objectNamePlural,
|
||||
@ -1,39 +1,75 @@
|
||||
import { isNonEmptyArray } from '~/utils/isNonEmptyArray';
|
||||
|
||||
import { useCreateOneMetadataField } from './useCreateOneMetadataField';
|
||||
import { useCreateOneMetadataObject } from './useCreateOneMetadataObject';
|
||||
import { useUpdateOneMetadataField } from './useUpdateOneMetadataField';
|
||||
import { useUpdateOneMetadataObject } from './useUpdateOneMetadataObject';
|
||||
|
||||
export const useSeedCustomObjectsTemp = () => {
|
||||
const { createOneMetadataObject } = useCreateOneMetadataObject();
|
||||
const { createOneMetadataField } = useCreateOneMetadataField();
|
||||
|
||||
const { updateOneMetadataObject } = useUpdateOneMetadataObject();
|
||||
const { updateOneMetadataField } = useUpdateOneMetadataField();
|
||||
|
||||
return async () => {
|
||||
const createdMetadataObject = await createOneMetadataObject({
|
||||
labelPlural: 'Suppliers',
|
||||
labelSingular: 'Supplier',
|
||||
nameSingular: 'supplier',
|
||||
namePlural: 'suppliers',
|
||||
description: 'Suppliers',
|
||||
icon: 'IconBuilding',
|
||||
});
|
||||
const { data: createdMetadataObject, errors } =
|
||||
await createOneMetadataObject({
|
||||
labelPlural: 'Suppliers',
|
||||
labelSingular: 'Supplier',
|
||||
nameSingular: 'supplier',
|
||||
namePlural: 'suppliers',
|
||||
description: 'Suppliers',
|
||||
icon: 'IconBuilding',
|
||||
});
|
||||
|
||||
const supplierObjectId =
|
||||
createdMetadataObject.data?.createOneObject?.id ?? '';
|
||||
if (!isNonEmptyArray(errors)) {
|
||||
const supplierObjectId = createdMetadataObject?.createOneObject?.id ?? '';
|
||||
|
||||
await createOneMetadataField({
|
||||
objectId: supplierObjectId,
|
||||
name: 'name',
|
||||
type: 'text',
|
||||
description: 'Name',
|
||||
label: 'Name',
|
||||
icon: 'IconBuilding',
|
||||
});
|
||||
const { data: createNameFieldData } = await createOneMetadataField({
|
||||
objectId: supplierObjectId,
|
||||
name: 'name',
|
||||
type: 'text',
|
||||
description: 'Name',
|
||||
label: 'Name',
|
||||
icon: 'IconBuilding',
|
||||
});
|
||||
|
||||
await createOneMetadataField({
|
||||
objectId: supplierObjectId,
|
||||
label: 'City',
|
||||
name: 'city',
|
||||
type: 'text',
|
||||
description: 'City',
|
||||
icon: 'IconMap',
|
||||
});
|
||||
const nameFieldId = createNameFieldData?.createOneField.id ?? '';
|
||||
|
||||
const { data: createCityFieldData } = await createOneMetadataField({
|
||||
objectId: supplierObjectId,
|
||||
label: 'City',
|
||||
name: 'city',
|
||||
type: 'text',
|
||||
description: 'City',
|
||||
icon: 'IconMap',
|
||||
});
|
||||
|
||||
const cityFieldId = createCityFieldData?.createOneField.id ?? '';
|
||||
|
||||
await updateOneMetadataObject({
|
||||
idToUpdate: supplierObjectId,
|
||||
updatePayload: {
|
||||
labelPlural: 'Suppliers 2',
|
||||
},
|
||||
});
|
||||
|
||||
await updateOneMetadataField({
|
||||
objectIdToUpdate: supplierObjectId,
|
||||
fieldIdToUpdate: cityFieldId,
|
||||
updatePayload: {
|
||||
label: 'City 2',
|
||||
},
|
||||
});
|
||||
|
||||
await updateOneMetadataField({
|
||||
objectIdToUpdate: supplierObjectId,
|
||||
fieldIdToUpdate: nameFieldId,
|
||||
updatePayload: {
|
||||
label: 'Name 2',
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
@ -0,0 +1,76 @@
|
||||
import { useMutation } from '@apollo/client';
|
||||
import { getOperationName } from '@apollo/client/utilities';
|
||||
|
||||
import {
|
||||
UpdateOneMetadataFieldMutation,
|
||||
UpdateOneMetadataFieldMutationVariables,
|
||||
} from '~/generated-metadata/graphql';
|
||||
|
||||
import { UPDATE_ONE_METADATA_FIELD } from '../graphql/mutations';
|
||||
import { FIND_MANY_METADATA_OBJECTS } from '../graphql/queries';
|
||||
|
||||
import { useApolloMetadataClient } from './useApolloMetadataClient';
|
||||
import { useFindManyMetadataObjects } from './useFindManyMetadataObjects';
|
||||
|
||||
export const useUpdateOneMetadataField = () => {
|
||||
const apolloMetadataClient = useApolloMetadataClient();
|
||||
|
||||
const { getMetadataObjectsFromCache } = useFindManyMetadataObjects();
|
||||
|
||||
const [mutate] = useMutation<
|
||||
UpdateOneMetadataFieldMutation,
|
||||
UpdateOneMetadataFieldMutationVariables
|
||||
>(UPDATE_ONE_METADATA_FIELD, {
|
||||
client: apolloMetadataClient ?? undefined,
|
||||
});
|
||||
|
||||
const updateOneMetadataField = ({
|
||||
objectIdToUpdate,
|
||||
fieldIdToUpdate,
|
||||
updatePayload,
|
||||
}: {
|
||||
objectIdToUpdate: string;
|
||||
fieldIdToUpdate: UpdateOneMetadataFieldMutationVariables['idToUpdate'];
|
||||
updatePayload: Partial<
|
||||
Pick<
|
||||
UpdateOneMetadataFieldMutationVariables['updatePayload'],
|
||||
'description' | 'icon' | 'isActive' | 'label'
|
||||
>
|
||||
>;
|
||||
}) => {
|
||||
const metadataObjects = getMetadataObjectsFromCache();
|
||||
|
||||
const foundMetadataObject = metadataObjects.find(
|
||||
(metadataObject) => metadataObject.id === objectIdToUpdate,
|
||||
);
|
||||
|
||||
if (!foundMetadataObject)
|
||||
throw new Error(`Metadata object with id ${objectIdToUpdate} not found`);
|
||||
|
||||
const foundMetadataField = foundMetadataObject.fields.find(
|
||||
(metadataField) => metadataField.id === fieldIdToUpdate,
|
||||
);
|
||||
|
||||
if (!foundMetadataField)
|
||||
throw new Error(`Metadata field with id ${fieldIdToUpdate} not found`);
|
||||
|
||||
return mutate({
|
||||
variables: {
|
||||
idToUpdate: fieldIdToUpdate,
|
||||
updatePayload: {
|
||||
name: foundMetadataField.name,
|
||||
description: foundMetadataField.description,
|
||||
icon: foundMetadataField.icon,
|
||||
isActive: foundMetadataField.isActive,
|
||||
label: foundMetadataField.label,
|
||||
...updatePayload,
|
||||
},
|
||||
},
|
||||
refetchQueries: [getOperationName(FIND_MANY_METADATA_OBJECTS) ?? ''],
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
updateOneMetadataField,
|
||||
};
|
||||
};
|
||||
@ -0,0 +1,71 @@
|
||||
import { useMutation } from '@apollo/client';
|
||||
import { getOperationName } from '@apollo/client/utilities';
|
||||
|
||||
import {
|
||||
UpdateOneMetadataObjectMutation,
|
||||
UpdateOneMetadataObjectMutationVariables,
|
||||
} from '~/generated-metadata/graphql';
|
||||
|
||||
import { UPDATE_ONE_METADATA_OBJECT } from '../graphql/mutations';
|
||||
import { FIND_MANY_METADATA_OBJECTS } from '../graphql/queries';
|
||||
|
||||
import { useApolloMetadataClient } from './useApolloMetadataClient';
|
||||
import { useFindManyMetadataObjects } from './useFindManyMetadataObjects';
|
||||
|
||||
// TODO: Slice the Apollo store synchronously in the update function instead of subscribing, so we can use update after read in the same function call
|
||||
export const useUpdateOneMetadataObject = () => {
|
||||
const apolloClientMetadata = useApolloMetadataClient();
|
||||
|
||||
const { getMetadataObjectsFromCache: queryMetadataObjects } =
|
||||
useFindManyMetadataObjects();
|
||||
|
||||
const [mutate] = useMutation<
|
||||
UpdateOneMetadataObjectMutation,
|
||||
UpdateOneMetadataObjectMutationVariables
|
||||
>(UPDATE_ONE_METADATA_OBJECT, {
|
||||
client: apolloClientMetadata ?? undefined,
|
||||
});
|
||||
|
||||
const updateOneMetadataObject = ({
|
||||
idToUpdate,
|
||||
updatePayload,
|
||||
}: {
|
||||
idToUpdate: UpdateOneMetadataObjectMutationVariables['idToUpdate'];
|
||||
updatePayload: Partial<
|
||||
Pick<
|
||||
UpdateOneMetadataObjectMutationVariables['updatePayload'],
|
||||
'description' | 'icon' | 'isActive' | 'labelPlural' | 'labelSingular'
|
||||
>
|
||||
>;
|
||||
}) => {
|
||||
const metadataObjects = queryMetadataObjects();
|
||||
|
||||
const foundMetadataObject = metadataObjects.find(
|
||||
(metadataObject) => metadataObject.id === idToUpdate,
|
||||
);
|
||||
|
||||
if (!foundMetadataObject)
|
||||
throw new Error(`Metadata object with id ${idToUpdate} not found`);
|
||||
|
||||
return mutate({
|
||||
variables: {
|
||||
idToUpdate,
|
||||
updatePayload: {
|
||||
namePlural: foundMetadataObject.namePlural,
|
||||
nameSingular: foundMetadataObject.nameSingular,
|
||||
description: foundMetadataObject.description,
|
||||
icon: foundMetadataObject.icon,
|
||||
isActive: foundMetadataObject.isActive,
|
||||
labelPlural: foundMetadataObject.labelPlural,
|
||||
labelSingular: foundMetadataObject.labelSingular,
|
||||
...updatePayload,
|
||||
},
|
||||
},
|
||||
refetchQueries: [getOperationName(FIND_MANY_METADATA_OBJECTS) ?? ''],
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
updateOneMetadataObject,
|
||||
};
|
||||
};
|
||||
@ -1,5 +1,5 @@
|
||||
export const formatPagedObjectsToObjects = <
|
||||
ObjectType extends { id: string },
|
||||
ObjectType extends { id: string } & Record<string, any>,
|
||||
ObjectTypeQuery extends {
|
||||
[objectNamePlural: string]: {
|
||||
edges: ObjectEdge[];
|
||||
|
||||
@ -0,0 +1,21 @@
|
||||
import { gql } from '@apollo/client';
|
||||
|
||||
import { capitalize } from '~/utils/string/capitalize';
|
||||
|
||||
import { MetadataObject } from '../types/MetadataObject';
|
||||
|
||||
export const generateCreateOneObjectMutation = ({
|
||||
metadataObject,
|
||||
}: {
|
||||
metadataObject: MetadataObject;
|
||||
}) => {
|
||||
const capitalizedObjectName = capitalize(metadataObject.nameSingular);
|
||||
|
||||
return gql`
|
||||
mutation CreateOne${capitalizedObjectName}($input: ${capitalizedObjectName}CreateInput!) {
|
||||
createOne${capitalizedObjectName}(data: $input) {
|
||||
id
|
||||
}
|
||||
}
|
||||
`;
|
||||
};
|
||||
Reference in New Issue
Block a user