From 1b427ece36588862ae510d02d02c2f91dc7e7777 Mon Sep 17 00:00:00 2001 From: anteqkois Date: Sat, 8 Jun 2024 22:55:16 +0200 Subject: [PATCH] feat(triggers): add ability to use sample data --- .../editor/steps/GenerateTestDataButton.tsx | 66 +++++++++++++++---- .../editor/trigger/TriggerConnectorPanel.tsx | 2 + .../editor/trigger/TriggerTestFunction.tsx | 34 +++++++++- .../trigger/TriggerWebhookSimulation.tsx | 32 ++++++++- .../package-pre-publish-checks.ts | 4 +- 5 files changed, 118 insertions(+), 20 deletions(-) diff --git a/apps/web/modules/editor/steps/GenerateTestDataButton.tsx b/apps/web/modules/editor/steps/GenerateTestDataButton.tsx index 0068f6c2..d8fb08ab 100644 --- a/apps/web/modules/editor/steps/GenerateTestDataButton.tsx +++ b/apps/web/modules/editor/steps/GenerateTestDataButton.tsx @@ -7,24 +7,62 @@ export interface GenerateTestDataButtonProps extends HTMLAttributes void } -export const GenerateTestDataButton = ({ disabled, disabledMessage, text, loading, onClick }: GenerateTestDataButtonProps) => { +export const GenerateTestDataButton = ({ + disabled, + disabledMessage, + text, + loading, + haveSampleData, + onSelectSampleData, + onClick, +}: GenerateTestDataButtonProps) => { return ( - - - - - - {text} - - - {disabled ? ( +
+ + + + + + {text} + + -

{disabledMessage}

+

{disabled ? disabledMessage : 'Test this trigger to generate sample data that can be used in the next steps'}

- ) : null} -
-
+ + + {haveSampleData ? ( + <> + or + + + + + Use Mock Data + + + +

+ {disabled + ? disabledMessage + : 'You can use predefined sample data. In most case data schema is the same, but values can be different.'} +

+
+
+
+ + ) : null} +
) } diff --git a/apps/web/modules/editor/trigger/TriggerConnectorPanel.tsx b/apps/web/modules/editor/trigger/TriggerConnectorPanel.tsx index d3d6ee73..c4c6da40 100644 --- a/apps/web/modules/editor/trigger/TriggerConnectorPanel.tsx +++ b/apps/web/modules/editor/trigger/TriggerConnectorPanel.tsx @@ -231,6 +231,7 @@ export const TriggerConnectorPanel = () => { onResize={(size) => setTestDataPanelHeight(size)} > { className="px-1" > { panelSize: number disabled: boolean disabledMessage: string + sampleData?: any } -export const TriggerTestFunction = ({ panelSize, disabled, disabledMessage }: TriggerTestFunctionProps) => { +export const TriggerTestFunction = ({ panelSize, disabled, disabledMessage, sampleData }: TriggerTestFunctionProps) => { const { toast } = useToast() const { flow, editedTrigger, testPoolTrigger, flowOperationRunning, patchEditedTriggerConnector } = useEditor() assertNotNullOrUndefined(editedTrigger?.name, 'editedTrigger.name') @@ -62,6 +63,24 @@ export const TriggerTestFunction = ({ panelSize, disabled, disabledMessage }: Tr [data], ) + const onSelectSampleData = useCallback(async () => { + if (!sampleData) + return toast({ + title: 'Can not retrive sample data', + description: `We can not retrive sample data. Please inform our team about this incident.`, + variant: 'destructive', + }) + + setRecord(prepareCodeMirrorValue(sampleData)) + await patchEditedTriggerConnector({ + settings: { + inputUiInfo: { + currentSelectedData: sampleData, + }, + }, + }) + }, [sampleData]) + useEffect(() => { // remve trigger events when trigger changed setRecord('') @@ -69,7 +88,14 @@ export const TriggerTestFunction = ({ panelSize, disabled, disabledMessage }: Tr }, [editedTrigger.settings.triggerName]) useEffect(() => { - if (status !== 'success' || !data?.length) return + if (status !== 'success') return + + if (!data?.length) { + // check if user selected sample data + if (!editedTrigger.settings.inputUiInfo.currentSelectedData) return + setRecord(prepareCodeMirrorValue(editedTrigger.settings.inputUiInfo.currentSelectedData)) + } + if (!editedTrigger.settings.inputUiInfo.currentSelectedData || !editedTrigger.settings.inputUiInfo.lastTestDate) return setRecord(prepareCodeMirrorValue(editedTrigger.settings.inputUiInfo.currentSelectedData)) @@ -118,6 +144,8 @@ export const TriggerTestFunction = ({ panelSize, disabled, disabledMessage }: Tr ) : null} { +export const TriggerWebhookSimulation = ({ panelSize, disabled, disabledMessage, triggerDisplayName, sampleData }: TriggerWebhookSimulationProps) => { const { toast } = useToast() const { flow, @@ -72,6 +73,24 @@ export const TriggerWebhookSimulation = ({ panelSize, disabled, disabledMessage, [data], ) + const onSelectSampleData = useCallback(async () => { + if (!sampleData) + return toast({ + title: 'Can not retrive sample data', + description: `We can not retrive sample data. Please inform our team about this incident.`, + variant: 'destructive', + }) + + setRecord(prepareCodeMirrorValue(sampleData)) + await patchEditedTriggerConnector({ + settings: { + inputUiInfo: { + currentSelectedData: sampleData, + }, + }, + }) + }, [sampleData]) + useEffect(() => { // remve trigger events when trigger changed setRecord('') @@ -79,7 +98,14 @@ export const TriggerWebhookSimulation = ({ panelSize, disabled, disabledMessage, }, [editedTrigger.settings.triggerName]) useEffect(() => { - if (status !== 'success' || !data?.length) return + if (status !== 'success') return + + if (!data?.length) { + // check if user selected sample data + if (!editedTrigger.settings.inputUiInfo.currentSelectedData) return + setRecord(prepareCodeMirrorValue(editedTrigger.settings.inputUiInfo.currentSelectedData)) + } + if (!editedTrigger.settings.inputUiInfo.currentSelectedData || !editedTrigger.settings.inputUiInfo.lastTestDate) return setRecord(prepareCodeMirrorValue(editedTrigger.settings.inputUiInfo.currentSelectedData)) @@ -148,6 +174,8 @@ export const TriggerWebhookSimulation = ({ panelSize, disabled, disabledMessage, ) : ( (URL, { + const response = await axios.get(URL, { headers: process.env.NODE_ENV === 'production' ? { @@ -58,7 +58,7 @@ const getLatestPublishedVersionPrivateRegistry = async (packageName: string, max console.info(`[getLatestPublishedVersion] packageName=${packageName}, latestVersion=${version}`) return version } catch (err: any) { - if (!err?.error?.includes('no such package available')) console.dir(err.response.data, { depth: null }) + if (!err?.error?.includes('no such package available')) console.log(err); if (attempt === maxRetries) { throw err // If it's the last attempt, rethrow the error