Skip to content

Commit

Permalink
NEOS-1628: Optimizes Bulk Set and Apply Default (#2968)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickzelei authored Nov 21, 2024
1 parent e6a65dc commit 0be8c6c
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import {
import SchemaPageSkeleton from './SchemaPageSkeleton';
import { useOnApplyDefaultClick } from './useOnApplyDefaultClick';
import { useOnImportMappings } from './useOnImportMappings';
import { useOnTransformerBulkUpdateClick } from './useOnTransformerBulkUpdateClick';
import {
getFilteredTransformersForBulkSet,
getOnSelectedTableToggle,
Expand Down Expand Up @@ -290,24 +291,34 @@ export default function DataGenConnectionCard({ jobId }: Props): ReactElement {
getMappings() {
return form.getValues('mappings');
},
setTransformer: onTransformerUpdate,
setMappings(mappings) {
form.setValue('mappings', mappings, {
shouldDirty: true,
shouldTouch: true,
shouldValidate: false,
});
},
constraintHandler: schemaConstraintHandler,
triggerUpdate() {
form.trigger('mappings');
},
});

function onTransformerBulkUpdate(
indices: number[],
config: JobMappingTransformerForm
): void {
indices.forEach((idx) => {
onTransformerUpdate(idx, config);
});
setTimeout(() => {
const { onClick: onTransformerBulkUpdate } = useOnTransformerBulkUpdateClick({
getMappings() {
return form.getValues('mappings');
},
setMappings(mappings) {
form.setValue('mappings', mappings, {
shouldDirty: true,
shouldTouch: true,
shouldValidate: false,
});
},
triggerUpdate() {
form.trigger('mappings');
}, 0);
}
},
});

if (isJobLoading || isSchemaDataMapLoading || isGetTransformersLoading) {
return <SchemaPageSkeleton />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ import {
import SchemaPageSkeleton from './SchemaPageSkeleton';
import { useOnApplyDefaultClick } from './useOnApplyDefaultClick';
import { useOnImportMappings } from './useOnImportMappings';
import { useOnTransformerBulkUpdateClick } from './useOnTransformerBulkUpdateClick';
import {
getConnectionIdFromSource,
getDestinationDetailsRecord,
Expand Down Expand Up @@ -520,13 +521,35 @@ export default function DataSyncConnectionCard({ jobId }: Props): ReactElement {
getMappings() {
return form.getValues('mappings');
},
setTransformer: onTransformerUpdate,
setMappings(mappings) {
form.setValue('mappings', mappings, {
shouldDirty: true,
shouldTouch: true,
shouldValidate: false,
});
},
constraintHandler: schemaConstraintHandler,
triggerUpdate() {
form.trigger('mappings');
},
});

const { onClick: onTransformerBulkUpdate } = useOnTransformerBulkUpdateClick({
getMappings() {
return form.getValues('mappings');
},
setMappings(mappings) {
form.setValue('mappings', mappings, {
shouldDirty: true,
shouldTouch: true,
shouldValidate: false,
});
},
triggerUpdate() {
form.trigger('mappings');
},
});

if (
isConnectionsLoading ||
isSchemaDataMapLoading ||
Expand Down Expand Up @@ -569,18 +592,6 @@ export default function DataSyncConnectionCard({ jobId }: Props): ReactElement {
);
}

function onTransformerBulkUpdate(
indices: number[],
config: JobMappingTransformerForm
): void {
indices.forEach((idx) => {
onTransformerUpdate(idx, config);
});
setTimeout(() => {
form.trigger('mappings');
}, 0);
}

return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { SchemaConstraintHandler } from '@/components/jobs/SchemaTable/schema-co
import {
convertJobMappingTransformerToForm,
JobMappingFormValues,
JobMappingTransformerForm,
} from '@/yup-validations/jobs';
import {
GenerateDefault,
Expand All @@ -13,7 +12,7 @@ import {

interface Props {
getMappings(): JobMappingFormValues[];
setTransformer(idx: number, transformer: JobMappingTransformerForm): void;
setMappings(mappings: JobMappingFormValues[]): void;
triggerUpdate(): void;
constraintHandler: SchemaConstraintHandler;
}
Expand All @@ -26,16 +25,15 @@ interface UseOnApplyDefaultClickResponse {
export function useOnApplyDefaultClick(
props: Props
): UseOnApplyDefaultClickResponse {
const { getMappings, setTransformer, triggerUpdate, constraintHandler } =
props;
const { getMappings, triggerUpdate, constraintHandler, setMappings } = props;

return {
onClick(override) {
const formMappings = getMappings();
formMappings.forEach((fm, idx) => {
const updatedMappings = formMappings.map((fm) => {
// skips setting the default transformer if the user has already set the transformer
if (fm.transformer.config.case && !override) {
return;
return fm;
} else {
const colkey = {
schema: fm.schema,
Expand All @@ -45,9 +43,11 @@ export function useOnApplyDefaultClick(
const isGenerated = constraintHandler.getIsGenerated(colkey);
const identityType = constraintHandler.getIdentityType(colkey);
const newJm = getDefaultJMTransformer(isGenerated && !identityType);
setTransformer(idx, convertJobMappingTransformerToForm(newJm));
fm.transformer = convertJobMappingTransformerToForm(newJm);
}
return fm;
});
setMappings(updatedMappings);
setTimeout(() => {
triggerUpdate();
}, 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
JobMappingFormValues,
JobMappingTransformerForm,
} from '@/yup-validations/jobs';

interface Props {
getMappings(): JobMappingFormValues[];
setMappings(mappings: JobMappingFormValues[]): void;
triggerUpdate(): void;
}

interface UseOnTransformerBulkUpdateClickResponse {
onClick(indices: number[], transformer: JobMappingTransformerForm): void;
}

// Hook that provides an onClick handler that will handle setting specified job mappings to a the provided transformer
export function useOnTransformerBulkUpdateClick(
props: Props
): UseOnTransformerBulkUpdateClickResponse {
const { getMappings, triggerUpdate, setMappings } = props;

return {
onClick(indices, transformer) {
if (indices.length === 0) {
return;
}

const formMappings = getMappings();

indices.forEach((idx) => {
if (formMappings[idx]) {
formMappings[idx].transformer = transformer;
}
});

setMappings(formMappings);
setTimeout(() => {
triggerUpdate();
}, 0);
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import FormPersist from '@/app/(mgmt)/FormPersist';
import { useOnApplyDefaultClick } from '@/app/(mgmt)/[account]/jobs/[id]/source/components/useOnApplyDefaultClick';
import { useOnImportMappings } from '@/app/(mgmt)/[account]/jobs/[id]/source/components/useOnImportMappings';
import { useOnTransformerBulkUpdateClick } from '@/app/(mgmt)/[account]/jobs/[id]/source/components/useOnTransformerBulkUpdateClick';
import {
getFilteredTransformersForBulkSet,
getOnSelectedTableToggle,
Expand Down Expand Up @@ -323,24 +324,34 @@ export default function Page({ searchParams }: PageProps): ReactElement {
getMappings() {
return form.getValues('mappings');
},
setTransformer: onTransformerUpdate,
setMappings(mappings) {
form.setValue('mappings', mappings, {
shouldDirty: true,
shouldTouch: true,
shouldValidate: false,
});
},
constraintHandler: schemaConstraintHandler,
triggerUpdate() {
form.trigger('mappings');
},
});

function onTransformerBulkUpdate(
indices: number[],
config: JobMappingTransformerForm
): void {
indices.forEach((idx) => {
onTransformerUpdate(idx, config);
});
setTimeout(() => {
const { onClick: onTransformerBulkUpdate } = useOnTransformerBulkUpdateClick({
getMappings() {
return form.getValues('mappings');
},
setMappings(mappings) {
form.setValue('mappings', mappings, {
shouldDirty: true,
shouldTouch: true,
shouldValidate: false,
});
},
triggerUpdate() {
form.trigger('mappings');
}, 0);
}
},
});

return (
<div className="flex flex-col gap-5">
Expand Down
37 changes: 24 additions & 13 deletions frontend/apps/web/app/(mgmt)/[account]/new/job/schema/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import { toast } from 'sonner';
import { useSessionStorage } from 'usehooks-ts';
import { useOnApplyDefaultClick } from '../../../jobs/[id]/source/components/useOnApplyDefaultClick';
import { useOnImportMappings } from '../../../jobs/[id]/source/components/useOnImportMappings';
import { useOnTransformerBulkUpdateClick } from '../../../jobs/[id]/source/components/useOnTransformerBulkUpdateClick';
import {
getDestinationDetailsRecord,
getFilteredTransformersForBulkSet,
Expand Down Expand Up @@ -419,13 +420,35 @@ export default function Page({ searchParams }: PageProps): ReactElement {
getMappings() {
return form.getValues('mappings');
},
setTransformer: onTransformerUpdate,
setMappings(mappings) {
form.setValue('mappings', mappings, {
shouldDirty: true,
shouldTouch: true,
shouldValidate: false,
});
},
constraintHandler: schemaConstraintHandler,
triggerUpdate() {
form.trigger('mappings');
},
});

const { onClick: onTransformerBulkUpdate } = useOnTransformerBulkUpdateClick({
getMappings() {
return form.getValues('mappings');
},
setMappings(mappings) {
form.setValue('mappings', mappings, {
shouldDirty: true,
shouldTouch: true,
shouldValidate: false,
});
},
triggerUpdate() {
form.trigger('mappings');
},
});

if (isConnectionLoading || isSchemaMapLoading || isGetTransformersLoading) {
return <SkeletonForm />;
}
Expand Down Expand Up @@ -475,18 +498,6 @@ export default function Page({ searchParams }: PageProps): ReactElement {
});
}

function onTransformerBulkUpdate(
indices: number[],
config: JobMappingTransformerForm
): void {
indices.forEach((idx) => {
onTransformerUpdate(idx, config);
});
setTimeout(() => {
form.trigger('mappings');
}, 0);
}

return (
<div className="flex flex-col gap-5">
<FormPersist formKey={schemaFormKey} form={form} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function FormBody(props: FormBodyProps): ReactElement {
isChecked={field.value}
onCheckedChange={field.onChange}
title="Override Mapped Transformers"
description="Do you want to overwrite the Transformers you have already mapped."
description="Do you want to overwrite the Transformers you have already mapped?"
/>
</FormControl>
<FormMessage />
Expand Down

0 comments on commit 0be8c6c

Please sign in to comment.