Added metadata creation (#2086)
* Reworked metadata creation * Fix from PR * Removed consolelog
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
import { useContext } from 'react';
|
||||
|
||||
import { ApolloClientMetadataContext } from '../context/ApolloClientMetadataContext';
|
||||
import { ApolloMetadataClientContext } from '../context/ApolloClientMetadataContext';
|
||||
|
||||
export const useApolloClientMetadata = () => {
|
||||
return useContext(ApolloClientMetadataContext);
|
||||
export const useApolloMetadataClient = () => {
|
||||
return useContext(ApolloMetadataClientContext);
|
||||
};
|
||||
|
||||
@ -1,8 +0,0 @@
|
||||
// TODO: add zod to validate that we have at least id on each object
|
||||
export const useCreateOneCustomObject = ({
|
||||
_objectName,
|
||||
}: {
|
||||
_objectName: string;
|
||||
}) => {
|
||||
// TODO : code
|
||||
};
|
||||
@ -0,0 +1,41 @@
|
||||
import { ApolloClient, useMutation } from '@apollo/client';
|
||||
import { getOperationName } from '@apollo/client/utilities';
|
||||
|
||||
import {
|
||||
CreateOneFieldMutation,
|
||||
CreateOneFieldMutationVariables,
|
||||
} from '~/generated-metadata/graphql';
|
||||
|
||||
import { CREATE_ONE_FIELD } from '../graphql/mutations';
|
||||
import { GET_ALL_OBJECTS } from '../graphql/queries';
|
||||
|
||||
import { useApolloMetadataClient } from './useApolloClientMetadata';
|
||||
|
||||
export const useCreateOneMetadataField = () => {
|
||||
const apolloMetadataClient = useApolloMetadataClient();
|
||||
|
||||
const [mutate] = useMutation<
|
||||
CreateOneFieldMutation,
|
||||
CreateOneFieldMutationVariables
|
||||
>(CREATE_ONE_FIELD, {
|
||||
client: apolloMetadataClient ?? ({} as ApolloClient<any>),
|
||||
});
|
||||
|
||||
const createOneMetadataField = (
|
||||
input: CreateOneFieldMutationVariables['input']['field'],
|
||||
) =>
|
||||
mutate({
|
||||
variables: {
|
||||
input: {
|
||||
field: {
|
||||
...input,
|
||||
},
|
||||
},
|
||||
},
|
||||
refetchQueries: [getOperationName(GET_ALL_OBJECTS) ?? ''],
|
||||
});
|
||||
|
||||
return {
|
||||
createOneMetadataField,
|
||||
};
|
||||
};
|
||||
@ -0,0 +1,41 @@
|
||||
import { ApolloClient, useMutation } from '@apollo/client';
|
||||
import { getOperationName } from '@apollo/client/utilities';
|
||||
|
||||
import {
|
||||
CreateOneObjectMutation,
|
||||
CreateOneObjectMutationVariables,
|
||||
} from '~/generated-metadata/graphql';
|
||||
|
||||
import { CREATE_ONE_OBJECT } from '../graphql/mutations';
|
||||
import { GET_ALL_OBJECTS } from '../graphql/queries';
|
||||
|
||||
import { useApolloMetadataClient } from './useApolloClientMetadata';
|
||||
|
||||
export const useCreateOneMetadataObject = () => {
|
||||
const apolloMetadataClient = useApolloMetadataClient();
|
||||
|
||||
const [mutate] = useMutation<
|
||||
CreateOneObjectMutation,
|
||||
CreateOneObjectMutationVariables
|
||||
>(CREATE_ONE_OBJECT, {
|
||||
client: apolloMetadataClient ?? ({} as ApolloClient<any>),
|
||||
});
|
||||
|
||||
const createOneMetadataObject = (
|
||||
input: CreateOneObjectMutationVariables['input']['object'],
|
||||
) =>
|
||||
mutate({
|
||||
variables: {
|
||||
input: {
|
||||
object: {
|
||||
...input,
|
||||
},
|
||||
},
|
||||
},
|
||||
refetchQueries: [getOperationName(GET_ALL_OBJECTS) ?? ''],
|
||||
});
|
||||
|
||||
return {
|
||||
createOneMetadataObject,
|
||||
};
|
||||
};
|
||||
46
front/src/modules/metadata/hooks/useFindAllMetadata.ts
Normal file
46
front/src/modules/metadata/hooks/useFindAllMetadata.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import { useMemo } from 'react';
|
||||
import { useQuery } from '@apollo/client';
|
||||
|
||||
import {
|
||||
MetadataObjectsQuery,
|
||||
MetadataObjectsQueryVariables,
|
||||
} from '~/generated-metadata/graphql';
|
||||
|
||||
import { GET_ALL_OBJECTS } from '../graphql/queries';
|
||||
import { formatPagedMetadataObjectsToMetadataObjects } from '../utils/formatPagedMetadataObjectsToMetadataObjects';
|
||||
|
||||
import { useApolloMetadataClient } from './useApolloClientMetadata';
|
||||
|
||||
// TODO: test fetchMore
|
||||
export const useFindAllMetadata = () => {
|
||||
const apolloMetadataClient = useApolloMetadataClient();
|
||||
|
||||
const { data, fetchMore: fetchMoreInternal } = useQuery<
|
||||
MetadataObjectsQuery,
|
||||
MetadataObjectsQueryVariables
|
||||
>(GET_ALL_OBJECTS, {
|
||||
client: apolloMetadataClient ?? ({} as any),
|
||||
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]);
|
||||
|
||||
return {
|
||||
metadataObjects,
|
||||
hasMore,
|
||||
fetchMore,
|
||||
};
|
||||
};
|
||||
@ -1,27 +1,28 @@
|
||||
import { useMemo } from 'react';
|
||||
import { gql, useQuery } from '@apollo/client';
|
||||
import { useRecoilState } from 'recoil';
|
||||
|
||||
import { metadataObjectsState } from '../states/metadataObjectsState';
|
||||
import { PaginatedObjectType } from '../types/PaginatedObjectType';
|
||||
import { formatPagedObjectsToObjects } from '../utils/formatPagedObjectsToObjects';
|
||||
import { generateFindManyCustomObjectsQuery } from '../utils/generateFindManyCustomObjectsQuery';
|
||||
|
||||
// TODO: add zod to validate that we have at least id on each object
|
||||
export const useFindManyCustomObjects = ({
|
||||
objectName,
|
||||
}: {
|
||||
objectName: string;
|
||||
}) => {
|
||||
const [metadataObjects] = useRecoilState(metadataObjectsState);
|
||||
import { useFindAllMetadata } from './useFindAllMetadata';
|
||||
|
||||
const foundObject = metadataObjects.find(
|
||||
(object) => object.nameSingular === objectName,
|
||||
// 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 }>({
|
||||
objectNamePlural,
|
||||
}: {
|
||||
objectNamePlural: string;
|
||||
}) => {
|
||||
const { metadataObjects } = useFindAllMetadata();
|
||||
|
||||
const foundMetadataObject = metadataObjects.find(
|
||||
(object) => object.namePlural === objectNamePlural,
|
||||
);
|
||||
|
||||
// eslint-disable-next-line no-console
|
||||
console.log({ foundObject });
|
||||
|
||||
const generatedQuery = foundObject
|
||||
const generatedQuery = foundMetadataObject
|
||||
? generateFindManyCustomObjectsQuery({
|
||||
metadataObject: foundObject,
|
||||
metadataObject: foundMetadataObject,
|
||||
})
|
||||
: gql`
|
||||
query EmptyQuery {
|
||||
@ -29,31 +30,29 @@ export const useFindManyCustomObjects = ({
|
||||
}
|
||||
`;
|
||||
|
||||
const {
|
||||
fetchMore: fetchMoreBase,
|
||||
data,
|
||||
loading,
|
||||
error,
|
||||
} = useQuery(generatedQuery, {
|
||||
skip: !foundObject,
|
||||
});
|
||||
const { data, loading, error } = useQuery<PaginatedObjectType<ObjectType>>(
|
||||
generatedQuery,
|
||||
{
|
||||
skip: !foundMetadataObject,
|
||||
},
|
||||
);
|
||||
|
||||
// eslint-disable-next-line no-console
|
||||
console.log({ data, loading, error });
|
||||
const objects = useMemo(
|
||||
() =>
|
||||
formatPagedObjectsToObjects({
|
||||
pagedObjects: data,
|
||||
objectNamePlural,
|
||||
}),
|
||||
[data, objectNamePlural],
|
||||
);
|
||||
|
||||
const fetchMore = ({ fromCursor }: { fromCursor: string }) => {
|
||||
fetchMoreBase({
|
||||
variables: { fromCursor },
|
||||
});
|
||||
};
|
||||
|
||||
const objectNotFoundInMetadata = metadataObjects.length > 0 && !foundObject;
|
||||
const objectNotFoundInMetadata =
|
||||
metadataObjects.length > 0 && !foundMetadataObject;
|
||||
|
||||
return {
|
||||
data,
|
||||
objects,
|
||||
loading,
|
||||
error,
|
||||
fetchMore,
|
||||
objectNotFoundInMetadata,
|
||||
};
|
||||
};
|
||||
|
||||
@ -1,77 +1,39 @@
|
||||
import {
|
||||
CreateOneFieldMutation,
|
||||
CreateOneFieldMutationVariables,
|
||||
CreateOneObjectMutation,
|
||||
CreateOneObjectMutationVariables,
|
||||
} from '~/generated-metadata/graphql';
|
||||
|
||||
import { CREATE_ONE_FIELD, CREATE_ONE_OBJECT } from '../graphql/mutations';
|
||||
|
||||
import { useApolloClientMetadata } from './useApolloClientMetadata';
|
||||
import { useCreateOneMetadataField } from './useCreateOneMetadataField';
|
||||
import { useCreateOneMetadataObject } from './useCreateOneMetadataObject';
|
||||
|
||||
export const useSeedCustomObjectsTemp = () => {
|
||||
const client = useApolloClientMetadata();
|
||||
const { createOneMetadataObject } = useCreateOneMetadataObject();
|
||||
const { createOneMetadataField } = useCreateOneMetadataField();
|
||||
|
||||
return async () => {
|
||||
if (!client) return;
|
||||
|
||||
const { data: createSuppliersData } = await client?.mutate<
|
||||
CreateOneObjectMutation,
|
||||
CreateOneObjectMutationVariables
|
||||
>({
|
||||
mutation: CREATE_ONE_OBJECT,
|
||||
variables: {
|
||||
input: {
|
||||
object: {
|
||||
labelPlural: 'Suppliers',
|
||||
labelSingular: 'Supplier',
|
||||
nameSingular: 'supplier',
|
||||
namePlural: 'suppliers',
|
||||
description: 'Suppliers',
|
||||
icon: 'IconBuilding',
|
||||
},
|
||||
},
|
||||
},
|
||||
const createdMetadataObject = await createOneMetadataObject({
|
||||
labelPlural: 'Suppliers',
|
||||
labelSingular: 'Supplier',
|
||||
nameSingular: 'supplier',
|
||||
namePlural: 'suppliers',
|
||||
description: 'Suppliers',
|
||||
icon: 'IconBuilding',
|
||||
});
|
||||
|
||||
const supplierObjectId = createSuppliersData?.createOneObject?.id ?? '';
|
||||
const supplierObjectId =
|
||||
createdMetadataObject.data?.createOneObject?.id ?? '';
|
||||
|
||||
await client?.mutate<
|
||||
CreateOneFieldMutation,
|
||||
CreateOneFieldMutationVariables
|
||||
>({
|
||||
mutation: CREATE_ONE_FIELD,
|
||||
variables: {
|
||||
input: {
|
||||
field: {
|
||||
objectId: supplierObjectId,
|
||||
label: 'Name',
|
||||
name: 'name',
|
||||
type: 'text',
|
||||
description: 'Name',
|
||||
icon: 'IconBuilding',
|
||||
},
|
||||
},
|
||||
},
|
||||
await createOneMetadataField({
|
||||
objectId: supplierObjectId,
|
||||
name: 'name',
|
||||
type: 'text',
|
||||
description: 'Name',
|
||||
label: 'Name',
|
||||
icon: 'IconBuilding',
|
||||
});
|
||||
|
||||
await client?.mutate<
|
||||
CreateOneFieldMutation,
|
||||
CreateOneFieldMutationVariables
|
||||
>({
|
||||
mutation: CREATE_ONE_FIELD,
|
||||
variables: {
|
||||
input: {
|
||||
field: {
|
||||
objectId: supplierObjectId,
|
||||
label: 'City',
|
||||
name: 'city',
|
||||
type: 'text',
|
||||
description: 'City',
|
||||
icon: 'IconMap',
|
||||
},
|
||||
},
|
||||
},
|
||||
await createOneMetadataField({
|
||||
objectId: supplierObjectId,
|
||||
label: 'City',
|
||||
name: 'city',
|
||||
type: 'text',
|
||||
description: 'City',
|
||||
icon: 'IconMap',
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
@ -18,9 +18,7 @@ export const useSetObjectDataTableData = () => {
|
||||
|
||||
return useRecoilCallback(
|
||||
({ set, snapshot }) =>
|
||||
<T extends { node: { id: string } }>(newEntityArrayRaw: T[]) => {
|
||||
const newEntityArray = newEntityArrayRaw.map((entity) => entity.node);
|
||||
|
||||
<T extends { id: string }>(newEntityArray: T[]) => {
|
||||
for (const entity of newEntityArray) {
|
||||
const currentEntity = snapshot
|
||||
.getLoadable(entityFieldsFamilyState(entity.id))
|
||||
@ -33,9 +31,6 @@ export const useSetObjectDataTableData = () => {
|
||||
|
||||
const entityIds = newEntityArray.map((entity) => entity.id);
|
||||
|
||||
// eslint-disable-next-line no-console
|
||||
console.log({ newEntityArray, entityIds });
|
||||
|
||||
set(tableRowIdsState, (currentRowIds) => {
|
||||
if (JSON.stringify(currentRowIds) !== JSON.stringify(entityIds)) {
|
||||
return entityIds;
|
||||
|
||||
Reference in New Issue
Block a user