From 8b24553d38f55f23b70150eded52c7b1db455f29 Mon Sep 17 00:00:00 2001 From: Grant Pierce Date: Thu, 4 Apr 2024 20:14:08 -0700 Subject: [PATCH] Implement take step UI for query builder --- .../query/query-builder/query-builder.tsx | 18 +- .../query/query-builder/steps/take-step.tsx | 292 ++++++++++++++++++ .../utils/pipeline/validate-pipeline-step.tsx | 31 +- 3 files changed, 337 insertions(+), 4 deletions(-) create mode 100644 frontend/src/components/query/query-builder/steps/take-step.tsx diff --git a/frontend/src/components/query/query-builder/query-builder.tsx b/frontend/src/components/query/query-builder/query-builder.tsx index 0c349f0..891cf38 100644 --- a/frontend/src/components/query/query-builder/query-builder.tsx +++ b/frontend/src/components/query/query-builder/query-builder.tsx @@ -3,10 +3,11 @@ import { Pipeline, SelectStep, AggregateStep, + RelateStep, + TakeStep, Step, StepIdentifier, StepIdentifierEnum, - RelateStep, } from "@/definitions/pipeline"; import { Button, NonIdealState, Section } from "@blueprintjs/core"; import Loading from "@/app/loading"; @@ -16,6 +17,7 @@ import FromStepComponent from "./steps/from-step"; import SelectStepComponent from "./steps/select-step"; import AggregateStepComponent from "./steps/aggregate-step"; import RelateStepComponent from "./steps/relate/relate-step"; +import TakeStepComponent from "./steps/take-step"; import { usePipelineSchema } from "@/data/use-user-query"; import { useState, useEffect } from "react"; import * as _ from "lodash"; @@ -130,10 +132,22 @@ export default function QueryBuilder({ create={create} /> ); + case StepIdentifierEnum.Take: + return ( + + ); case StepIdentifierEnum.Derive: case StepIdentifierEnum.Filter: case StepIdentifierEnum.Order: - case StepIdentifierEnum.Take: return (
void; + edit: boolean; + setEditStepIndex: (value: number | null) => void; + setNewStepType: (value: NewStepSelection | null) => void; + create?: boolean; +} + +export default function TakeStepComponent({ + index, + step, + pipeline, + setPipeline, + edit, + setEditStepIndex, + setNewStepType, + create, +}: TakeStepProps) { + const offset = useField(undefined); + const limit = useField(undefined, { + valueTransformer: (value) => { + return value === 0 ? 1 : value; + }, + }); + + const { + data: inputSchema, + isLoading: isLoadingInputSchema, + error: inputSchemaError, + } = usePipelineSchema({ + ...pipeline, + steps: _.slice(pipeline.steps, 0, index), + }); + + const { + data: schema, + isLoading: isLoadingSchema, + error: schemaError, + } = usePipelineSchema({ + ...pipeline, + steps: _.slice(pipeline.steps, 0, index + 1), + }); + + useEffect(() => { + resetFields(); + }, [step]); + + function resetFields() { + if (step) { + offset.onValueChange(step.offset); + limit.onValueChange(step.limit); + } else { + offset.onValueChange(undefined); + limit.onValueChange(undefined); + } + } + + function getAdditionalClasses() { + if (inputSchema && !inputSchema.success) { + return "border-2 border-gold"; + } else if (schema && !schema.success) { + return "border-2 border-error"; + } + } + + function renderTitle() { + // If we are creating a new step, editing a step, or the step is not defined, show the default title + // Also show the default title if the schemas are loading or errored + if ( + edit || + create || + !step || + isLoadingSchema || + isLoadingInputSchema || + inputSchemaError || + schemaError + ) { + return Take:; + } else if (inputSchema && !inputSchema.success) { + return ( +
+ Take: + +
+ limit + {step.limit} +
+ +
+ offset + {step.offset} +
+
+
+ ); + } else if (schema && !schema.success) { + return ( +
+ Take: + +
+ ); + } else { + return ( +
+ Take: + +
+ limit + {step.limit} +
+ +
+ offset + {step.offset} +
+
+
+ ); + } + } + + function renderRightElement() { + if (create) { + return ( + <> +