Skip to content

Commit

Permalink
Make see fields generate draft (#810)
Browse files Browse the repository at this point in the history
* Making a hook to share the generate functionality

* Allowing a presave function to be called

* Wiring up refresh button so it can handle generating the draft

* Updating to use the new mutate context

* Getting captures using the next context

* Better way to check if the call worked

* No longer need to pass disabled to refresh
Better handling of errors for refresh button

* Adding a hook to scroll elements into view

* Having the general error scroll into view

* Having validation errors scroll into view

* Cleaning up logging

* Adding in disabled so it can be disabled while the form is active

* POST MERGE - fixing imports

* Cleaning up more of the post generate mutate being passed around

* PR : Commenting

* PR : Commenting

* Switching back to null so things do not fail silently

* PR - cleaning up code a bit

* This did nothing - reverting the changes

* Allowing for options to be passed

* PR Cleaning up comments and code
  • Loading branch information
travjenkins authored Nov 3, 2023
1 parent 8c10f80 commit a271364
Show file tree
Hide file tree
Showing 20 changed files with 948 additions and 726 deletions.
83 changes: 43 additions & 40 deletions src/components/capture/Create/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import EntitySaveButton from 'components/shared/Entity/Actions/SaveButton';
import EntityTestButton from 'components/shared/Entity/Actions/TestButton';
import EntityCreate from 'components/shared/Entity/Create';
import EntityToolbar from 'components/shared/Entity/Header';
import { MutateDraftSpecProvider } from 'components/shared/Entity/MutateDraftSpecContext';
import useConnectorWithTagDetail from 'hooks/useConnectorWithTagDetail';
import useDraftSpecs from 'hooks/useDraftSpecs';
import usePageTitle from 'hooks/usePageTitle';
Expand Down Expand Up @@ -86,46 +87,48 @@ function CaptureCreate() {
<DetailsFormHydrator>
<EndpointConfigHydrator>
<ResourceConfigHydrator>
<EntityCreate
entityType={entityType}
draftSpecMetadata={draftSpecsMetadata}
toolbar={
<EntityToolbar
waitTimes={{ generate: MAX_DISCOVER_TIME }}
GenerateButton={
<CaptureGenerateButton
entityType={entityType}
disabled={!hasConnectors}
postGenerateMutate={updateDraftSpecs}
createWorkflowMetadata={{
initiateDiscovery,
setInitiateDiscovery,
}}
/>
}
TestButton={
<EntityTestButton
disabled={!hasConnectors}
logEvent={CustomEvents.CAPTURE_TEST}
/>
}
SaveButton={
<EntitySaveButton
disabled={!draftId}
taskNames={tasks}
logEvent={CustomEvents.CAPTURE_CREATE}
/>
}
/>
}
RediscoverButton={
<RediscoverButton
entityType={entityType}
disabled={!hasConnectors}
postGenerateMutate={updateDraftSpecs}
/>
}
/>
<MutateDraftSpecProvider value={updateDraftSpecs}>
<EntityCreate
entityType={entityType}
draftSpecMetadata={draftSpecsMetadata}
toolbar={
<EntityToolbar
waitTimes={{ generate: MAX_DISCOVER_TIME }}
GenerateButton={
<CaptureGenerateButton
entityType={entityType}
disabled={!hasConnectors}
createWorkflowMetadata={{
initiateDiscovery,
setInitiateDiscovery,
}}
/>
}
TestButton={
<EntityTestButton
disabled={!hasConnectors}
logEvent={CustomEvents.CAPTURE_TEST}
/>
}
SaveButton={
<EntitySaveButton
disabled={!draftId}
taskNames={tasks}
logEvent={
CustomEvents.CAPTURE_CREATE
}
/>
}
/>
}
RediscoverButton={
<RediscoverButton
entityType={entityType}
disabled={!hasConnectors}
/>
}
/>
</MutateDraftSpecProvider>
</ResourceConfigHydrator>
</EndpointConfigHydrator>
</DetailsFormHydrator>
Expand Down
85 changes: 45 additions & 40 deletions src/components/capture/Edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import EntityTestButton from 'components/shared/Entity/Actions/TestButton';
import EntityEdit from 'components/shared/Entity/Edit';
import DraftInitializer from 'components/shared/Entity/Edit/DraftInitializer';
import EntityToolbar from 'components/shared/Entity/Header';
import { MutateDraftSpecProvider } from 'components/shared/Entity/MutateDraftSpecContext';
import useGlobalSearchParams, {
GlobalSearchParams,
} from 'hooks/searchParams/useGlobalSearchParams';
Expand Down Expand Up @@ -68,46 +69,50 @@ function CaptureEdit() {
<DetailsFormHydrator>
<EndpointConfigHydrator>
<ResourceConfigHydrator>
<EntityEdit
title="routeTitle.captureEdit"
entityType={entityType}
readOnly={{ detailsForm: true }}
draftSpecMetadata={draftSpecsMetadata}
toolbar={
<EntityToolbar
waitTimes={{ generate: MAX_DISCOVER_TIME }}
GenerateButton={
<CaptureGenerateButton
entityType={entityType}
disabled={!hasConnectors}
postGenerateMutate={
updateDraftSpecs
}
/>
}
TestButton={
<EntityTestButton
disabled={!hasConnectors}
logEvent={CustomEvents.CAPTURE_TEST}
/>
}
SaveButton={
<EntitySaveButton
disabled={!draftId}
taskNames={taskNames}
logEvent={CustomEvents.CAPTURE_EDIT}
/>
}
/>
}
RediscoverButton={
<RediscoverButton
entityType={entityType}
disabled={!hasConnectors}
postGenerateMutate={updateDraftSpecs}
/>
}
/>
<MutateDraftSpecProvider value={updateDraftSpecs}>
<EntityEdit
title="routeTitle.captureEdit"
entityType={entityType}
readOnly={{ detailsForm: true }}
draftSpecMetadata={draftSpecsMetadata}
toolbar={
<EntityToolbar
waitTimes={{
generate: MAX_DISCOVER_TIME,
}}
GenerateButton={
<CaptureGenerateButton
entityType={entityType}
disabled={!hasConnectors}
/>
}
TestButton={
<EntityTestButton
disabled={!hasConnectors}
logEvent={
CustomEvents.CAPTURE_TEST
}
/>
}
SaveButton={
<EntitySaveButton
disabled={!draftId}
taskNames={taskNames}
logEvent={
CustomEvents.CAPTURE_EDIT
}
/>
}
/>
}
RediscoverButton={
<RediscoverButton
entityType={entityType}
disabled={!hasConnectors}
/>
}
/>
</MutateDraftSpecProvider>
</ResourceConfigHydrator>
</EndpointConfigHydrator>
</DetailsFormHydrator>
Expand Down
4 changes: 0 additions & 4 deletions src/components/capture/GenerateButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import useDiscoverCapture from './useDiscoverCapture';
interface Props {
entityType: Entity;
disabled: boolean;
postGenerateMutate: Function;
createWorkflowMetadata?: {
initiateDiscovery: boolean;
setInitiateDiscovery: Dispatch<SetStateAction<boolean>>;
Expand All @@ -26,14 +25,11 @@ interface Props {
function CaptureGenerateButton({
entityType,
disabled,
postGenerateMutate,
createWorkflowMetadata,
}: Props) {
const rediscoveryRequired = useResourceConfig_rediscoveryRequired();

const { generateCatalog, isSaving, formActive } = useDiscoverCapture(
entityType,
postGenerateMutate,
{
initiateDiscovery: createWorkflowMetadata?.initiateDiscovery,
initiateRediscovery: rediscoveryRequired,
Expand Down
4 changes: 1 addition & 3 deletions src/components/capture/RediscoverButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ import { Entity } from 'types';
interface Props {
entityType: Entity;
disabled: boolean;
postGenerateMutate: Function;
}

function RediscoverButton({ entityType, disabled, postGenerateMutate }: Props) {
function RediscoverButton({ entityType, disabled }: Props) {
const { generateCatalog, isSaving, formActive } = useDiscoverCapture(
entityType,
postGenerateMutate,
{ initiateRediscovery: true }
);

Expand Down
10 changes: 6 additions & 4 deletions src/components/capture/useCaptureDraftUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
useEditorStore_setId,
} from 'components/editor/Store/hooks';
import useEntityWorkflowHelpers from 'components/shared/Entity/hooks/useEntityWorkflowHelpers';
import { useMutateDraftSpec } from 'components/shared/Entity/MutateDraftSpecContext';
import { useEntityWorkflow_Editing } from 'context/Workflow';
import useEntityNameSuffix from 'hooks/useEntityNameSuffix';
import { useCallback } from 'react';
Expand All @@ -21,10 +22,11 @@ import {
import { useResourceConfig_resourceConfig } from 'stores/ResourceConfig/hooks';
import { modifyExistingCaptureDraftSpec } from 'utils/workflow-utils';

function useDiscoverDraftUpdate(
postGenerateMutate: Function,
options?: { initiateRediscovery?: boolean; initiateDiscovery?: boolean }
) {
function useDiscoverDraftUpdate(options?: {
initiateRediscovery?: boolean;
initiateDiscovery?: boolean;
}) {
const postGenerateMutate = useMutateDraftSpec();
const isEdit = useEntityWorkflow_Editing();
const { callFailed } = useEntityWorkflowHelpers();

Expand Down
8 changes: 2 additions & 6 deletions src/components/capture/useDiscoverCapture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,11 @@ import useDiscoverStartDiscovery from './useDiscoverStartDiscovery';

function useDiscoverCapture(
entityType: Entity,
postGenerateMutate: Function,
options?: { initiateRediscovery?: boolean; initiateDiscovery?: boolean }
) {
const draftUpdate = useDiscoverDraftUpdate(postGenerateMutate, options);
const draftUpdate = useDiscoverDraftUpdate(options);
const configEncrypt = useDiscoverConfigEncrypt();
const startDiscovery = useDiscoverStartDiscovery(
entityType,
postGenerateMutate
);
const startDiscovery = useDiscoverStartDiscovery(entityType);

const isEdit = useEntityWorkflow_Editing();

Expand Down
11 changes: 3 additions & 8 deletions src/components/capture/useDiscoverStartDiscovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@ import { useFormStateStore_setFormState } from 'stores/FormState/hooks';
import { Entity } from 'types';
import useDiscoverStartSubscription from './useDiscoverStartSubscription';

function useDiscoverStartDiscovery(
entityType: Entity,
postGenerateMutate: Function
) {
const createDiscoversSubscription = useDiscoverStartSubscription(
entityType,
postGenerateMutate
);
function useDiscoverStartDiscovery(entityType: Entity) {
const createDiscoversSubscription =
useDiscoverStartSubscription(entityType);
const { callFailed } = useEntityWorkflowHelpers();

const persistedDraftId = useEditorStore_persistedDraftId();
Expand Down
8 changes: 4 additions & 4 deletions src/components/capture/useDiscoverStartSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
useEditorStore_setId,
} from 'components/editor/Store/hooks';
import useEntityWorkflowHelpers from 'components/shared/Entity/hooks/useEntityWorkflowHelpers';
import { useMutateDraftSpec } from 'components/shared/Entity/MutateDraftSpecContext';
import { useClient } from 'hooks/supabase-swr';
import useStoreDiscoveredCaptures from 'hooks/useStoreDiscoveredCaptures';
import { useCallback } from 'react';
Expand Down Expand Up @@ -33,10 +34,9 @@ const trackEvent = (payload: any) => {
});
};

function useDiscoverStartSubscription(
entityType: Entity,
postGenerateMutate: Function
) {
function useDiscoverStartSubscription(entityType: Entity) {
const postGenerateMutate = useMutateDraftSpec();

const supabaseClient = useClient();

const { callFailed } = useEntityWorkflowHelpers();
Expand Down
55 changes: 41 additions & 14 deletions src/components/editor/Bindings/FieldSelection/RefreshButton.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,56 @@
import { useEditorStore_id } from 'components/editor/Store/hooks';
import EntityCreateSave from 'components/shared/Entity/Actions/Save';
import { Button } from '@mui/material';
import useGenerateCatalog from 'components/materialization/useGenerateCatalog';
import useSave from 'components/shared/Entity/Actions/useSave';
import useEntityWorkflowHelpers from 'components/shared/Entity/hooks/useEntityWorkflowHelpers';
import { useMutateDraftSpec } from 'components/shared/Entity/MutateDraftSpecContext';
import { useState } from 'react';
import { FormattedMessage } from 'react-intl';
import { CustomEvents } from 'services/logrocket';

interface Props {
disabled: boolean;
logEvent: CustomEvents.MATERIALIZATION_TEST;
buttonLabelId: string;
logEvent: CustomEvents.MATERIALIZATION_TEST;
disabled?: boolean;
}

function RefreshButton({ disabled, logEvent, buttonLabelId }: Props) {
const { callFailed } = useEntityWorkflowHelpers();

// Draft Editor Store
const draftId = useEditorStore_id();
const [updating, setUpdating] = useState(false);

const generateCatalog = useGenerateCatalog();
const mutateDraftSpec = useMutateDraftSpec();
const saveCatalog = useSave(logEvent, callFailed, true, true);

return (
<EntityCreateSave
dryRun
disabled={disabled || !draftId}
onFailure={callFailed}
logEvent={logEvent}
buttonLabelId={buttonLabelId}
forceLogsClosed
/>
<Button
disabled={Boolean(updating || disabled)}
onClick={async () => {
setUpdating(true);

let evaluatedDraftId;
try {
evaluatedDraftId = await generateCatalog(mutateDraftSpec);
} catch (_error: unknown) {
setUpdating(false);
}

// Make sure we have a draft id so we know the generate worked
// if this is not returned then the function itself handled showing an error
if (evaluatedDraftId) {
try {
await saveCatalog(evaluatedDraftId);
} catch (_error: unknown) {
setUpdating(false);
}
}

// I do not think this is truly needed but being safe so the user is not stuck with a disabled button
setUpdating(false);
}}
>
<FormattedMessage id={buttonLabelId} />
</Button>
);
}
export default RefreshButton;
4 changes: 2 additions & 2 deletions src/components/editor/Bindings/FieldSelection/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,9 @@ function FieldSelectionViewer({ collectionName }: Props) {
{/* The shared test and save button component is disabled when the form is active.
No additional disabled conditions are needed. */}
<RefreshButton
disabled={false}
logEvent={CustomEvents.MATERIALIZATION_TEST}
buttonLabelId="fieldSelection.cta.populateTable"
disabled={formActive}
logEvent={CustomEvents.MATERIALIZATION_TEST}
/>
</Box>
</Stack>
Expand Down
Loading

0 comments on commit a271364

Please sign in to comment.