Feat/front temp seed custom objects (#2070)
* wip * Fixed bugs * Added flexible backend test
This commit is contained in:
@ -8,11 +8,17 @@ import { supportChatState } from '@/client-config/states/supportChatState';
|
||||
import { telemetryState } from '@/client-config/states/telemetryState';
|
||||
import { useGetClientConfigQuery } from '~/generated/graphql';
|
||||
|
||||
import { isFlexibleBackendEnabledState } from '../states/isFlexibleBackendEnabledState';
|
||||
|
||||
export const ClientConfigProvider: React.FC<React.PropsWithChildren> = ({
|
||||
children,
|
||||
}) => {
|
||||
const [, setAuthProviders] = useRecoilState(authProvidersState);
|
||||
const [, setIsDebugMode] = useRecoilState(isDebugModeState);
|
||||
const [, setIsFlexibleBackendEnabled] = useRecoilState(
|
||||
isFlexibleBackendEnabledState,
|
||||
);
|
||||
|
||||
const [, setIsSignInPrefilled] = useRecoilState(isSignInPrefilledState);
|
||||
const [, setTelemetry] = useRecoilState(telemetryState);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
@ -30,6 +36,7 @@ export const ClientConfigProvider: React.FC<React.PropsWithChildren> = ({
|
||||
password: data?.clientConfig.authProviders.password,
|
||||
magicLink: false,
|
||||
});
|
||||
setIsFlexibleBackendEnabled(data?.clientConfig.flexibleBackendEnabled);
|
||||
setIsDebugMode(data?.clientConfig.debugMode);
|
||||
setIsSignInPrefilled(data?.clientConfig.signInPrefilled);
|
||||
setTelemetry(data?.clientConfig.telemetry);
|
||||
@ -39,6 +46,7 @@ export const ClientConfigProvider: React.FC<React.PropsWithChildren> = ({
|
||||
data,
|
||||
setAuthProviders,
|
||||
setIsDebugMode,
|
||||
setIsFlexibleBackendEnabled,
|
||||
setIsSignInPrefilled,
|
||||
setTelemetry,
|
||||
setIsLoading,
|
||||
|
||||
@ -17,6 +17,7 @@ export const GET_CLIENT_CONFIG = gql`
|
||||
supportDriver
|
||||
supportFrontChatId
|
||||
}
|
||||
flexibleBackendEnabled
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
@ -0,0 +1,6 @@
|
||||
import { atom } from 'recoil';
|
||||
|
||||
export const isFlexibleBackendEnabledState = atom<boolean>({
|
||||
key: 'isFlexibleBackendEnabledState',
|
||||
default: false,
|
||||
});
|
||||
@ -1,20 +1,28 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useRecoilState } from 'recoil';
|
||||
|
||||
import { isFlexibleBackendEnabledState } from '@/client-config/states/isFlexibleBackendEnabledState';
|
||||
import { ObjectsQuery } from '~/generated-metadata/graphql';
|
||||
|
||||
import { GET_ALL_OBJECTS } from '../graphql/queries';
|
||||
import { useApolloClientMetadata } from '../hooks/useApolloClientMetadata';
|
||||
import { useSeedCustomObjectsTemp } from '../hooks/useSeedCustomObjectsTemp';
|
||||
import { metadataObjectsState } from '../states/metadataObjectsState';
|
||||
import { MetadataObject } from '../types/MetadataObject';
|
||||
|
||||
export const FetchMetadataEffect = () => {
|
||||
const [metadataObjects, setMetadataObjects] =
|
||||
useRecoilState(metadataObjectsState);
|
||||
|
||||
const [isFlexibleBackendEnabled] = useRecoilState(
|
||||
isFlexibleBackendEnabledState,
|
||||
);
|
||||
const apolloClientMetadata = useApolloClientMetadata();
|
||||
|
||||
const seedCustomObjectsTemp = useSeedCustomObjectsTemp();
|
||||
|
||||
useEffect(() => {
|
||||
if (!isFlexibleBackendEnabled) return;
|
||||
|
||||
(async () => {
|
||||
if (apolloClientMetadata && metadataObjects.length === 0) {
|
||||
const objects = await apolloClientMetadata.query<ObjectsQuery>({
|
||||
@ -31,10 +39,38 @@ export const FetchMetadataEffect = () => {
|
||||
fields: object.node.fields.edges.map((field) => field.node),
|
||||
}));
|
||||
setMetadataObjects(formattedObjects);
|
||||
} else if (
|
||||
objects.data.objects.edges.length === 0 &&
|
||||
metadataObjects.length === 0
|
||||
) {
|
||||
try {
|
||||
await seedCustomObjectsTemp();
|
||||
|
||||
const objects = await apolloClientMetadata.query<ObjectsQuery>({
|
||||
query: GET_ALL_OBJECTS,
|
||||
});
|
||||
|
||||
const formattedObjects: MetadataObject[] =
|
||||
objects.data.objects.edges.map((object) => ({
|
||||
...object.node,
|
||||
fields: object.node.fields.edges.map((field) => field.node),
|
||||
}));
|
||||
|
||||
setMetadataObjects(formattedObjects);
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
})();
|
||||
}, [metadataObjects, setMetadataObjects, apolloClientMetadata]);
|
||||
}, [
|
||||
isFlexibleBackendEnabled,
|
||||
metadataObjects,
|
||||
setMetadataObjects,
|
||||
apolloClientMetadata,
|
||||
seedCustomObjectsTemp,
|
||||
]);
|
||||
|
||||
return <></>;
|
||||
};
|
||||
|
||||
@ -11,8 +11,7 @@ import { sortsScopedState } from '@/ui/data/view-bar/states/sortsScopedState';
|
||||
import { useRecoilScopeId } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopeId';
|
||||
|
||||
import { useFindManyCustomObjects } from '../hooks/useFindManyCustomObjects';
|
||||
|
||||
import { useSetObjectDataTableData } from './useSetDataTableData';
|
||||
import { useSetObjectDataTableData } from '../hooks/useSetDataTableData';
|
||||
|
||||
export const ObjectDataTableEffect = ({
|
||||
objectName,
|
||||
|
||||
30
front/src/modules/metadata/graphql/mutations.ts
Normal file
30
front/src/modules/metadata/graphql/mutations.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import { gql } from '@apollo/client';
|
||||
|
||||
export const CREATE_ONE_OBJECT = gql`
|
||||
mutation CreateOneObject($input: CreateOneObjectInput!) {
|
||||
createOneObject(input: $input) {
|
||||
id
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
export const CREATE_ONE_FIELD = gql`
|
||||
mutation CreateOneField($input: CreateOneFieldInput!) {
|
||||
createOneField(input: $input) {
|
||||
id
|
||||
type
|
||||
nameSingular
|
||||
namePlural
|
||||
labelSingular
|
||||
labelPlural
|
||||
description
|
||||
icon
|
||||
placeholder
|
||||
isCustom
|
||||
isActive
|
||||
isNullable
|
||||
createdAt
|
||||
updatedAt
|
||||
}
|
||||
}
|
||||
`;
|
||||
83
front/src/modules/metadata/hooks/useSeedCustomObjectsTemp.ts
Normal file
83
front/src/modules/metadata/hooks/useSeedCustomObjectsTemp.ts
Normal file
@ -0,0 +1,83 @@
|
||||
import {
|
||||
CreateOneFieldMutation,
|
||||
CreateOneFieldMutationVariables,
|
||||
CreateOneObjectMutation,
|
||||
CreateOneObjectMutationVariables,
|
||||
} from '~/generated-metadata/graphql';
|
||||
|
||||
import { CREATE_ONE_FIELD, CREATE_ONE_OBJECT } from '../graphql/mutations';
|
||||
|
||||
import { useApolloClientMetadata } from './useApolloClientMetadata';
|
||||
|
||||
export const useSeedCustomObjectsTemp = () => {
|
||||
const client = useApolloClientMetadata();
|
||||
|
||||
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 supplierObjectId = createSuppliersData?.createOneObject?.id ?? '';
|
||||
|
||||
await client?.mutate<
|
||||
CreateOneFieldMutation,
|
||||
CreateOneFieldMutationVariables
|
||||
>({
|
||||
mutation: CREATE_ONE_FIELD,
|
||||
variables: {
|
||||
input: {
|
||||
field: {
|
||||
objectId: supplierObjectId,
|
||||
labelSingular: 'Name',
|
||||
nameSingular: 'name',
|
||||
type: 'text',
|
||||
description: 'Name',
|
||||
labelPlural: 'Names',
|
||||
namePlural: 'names',
|
||||
placeholder: 'Name',
|
||||
icon: 'IconBuilding',
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await client?.mutate<
|
||||
CreateOneFieldMutation,
|
||||
CreateOneFieldMutationVariables
|
||||
>({
|
||||
mutation: CREATE_ONE_FIELD,
|
||||
variables: {
|
||||
input: {
|
||||
field: {
|
||||
objectId: supplierObjectId,
|
||||
labelSingular: 'City',
|
||||
nameSingular: 'city',
|
||||
type: 'text',
|
||||
description: 'City',
|
||||
labelPlural: 'Cities',
|
||||
namePlural: 'cities',
|
||||
placeholder: 'City',
|
||||
icon: 'IconMap',
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
};
|
||||
@ -1,5 +1,8 @@
|
||||
import { Field, Object as GeneratedObject } from '~/generated-metadata/graphql';
|
||||
|
||||
export type MetadataObject = Omit<GeneratedObject, 'fields'> & {
|
||||
export type MetadataObject = Omit<
|
||||
GeneratedObject,
|
||||
'fields' | 'dataSourceId'
|
||||
> & {
|
||||
fields: Field[];
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user