-
Notifications
You must be signed in to change notification settings - Fork 79
fix(orchestrator): Disable Next button when active widgets are fetching and processing data #1672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| --- | ||
| '@red-hat-developer-hub/backstage-plugin-orchestrator-form-react': minor | ||
| '@red-hat-developer-hub/backstage-plugin-orchestrator-form-api': minor | ||
| '@red-hat-developer-hub/backstage-plugin-orchestrator-form-widgets': minor | ||
| --- | ||
|
|
||
| Disable Next button when active widgets are fetching and processing data | ||
|
|
||
| - Add isFetching state tracking to StepperContext using a counter to monitor multiple concurrent async operations | ||
| - Update OrchestratorFormToolbar to disable Next button when isFetching is true (in addition to existing isValidating check) | ||
| - Add handleFetchStarted and handleFetchEnded callbacks to OrchestratorFormContextProps to allow widgets to report their loading status | ||
| - Update useFetchAndEvaluate to track complete loading state (fetch + template evaluation) and notify context | ||
| - Create useProcessingState custom hook to reduce code duplication across widgets, providing a reusable pattern for tracking both fetch and processing states | ||
| - Refactor SchemaUpdater, ActiveTextInput, ActiveDropdown, and ActiveMultiSelect to use useProcessingState hook | ||
| - Track the complete loading lifecycle: fetch → process → ready, ensuring Next button is disabled until all async work completes | ||
| - Prevents race conditions where Next button becomes enabled before widgets finish processing data |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -147,13 +147,23 @@ const FormComponent = (decoratorProps: FormDecoratorProps) => { | |
|
|
||
| const OrchestratorFormWrapper = (props: OrchestratorFormContextProps) => { | ||
| const formApi = useOrchestratorFormApiOrDefault(); | ||
| const { handleFetchStarted, handleFetchEnded } = useStepperContext(); | ||
|
|
||
| const NewComponent = useMemo(() => { | ||
| const formDecorator = formApi.getFormDecorator(); | ||
| return formDecorator(FormComponent); | ||
| }, [formApi]); | ||
|
|
||
| return <NewComponent {...props} />; | ||
| const propsWithFetchHandlers = useMemo( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be left to the |
||
| () => ({ | ||
| ...props, | ||
| handleFetchStarted, | ||
| handleFetchEnded, | ||
| }), | ||
| [props, handleFetchStarted, handleFetchEnded], | ||
| ); | ||
|
|
||
| return <NewComponent {...propsWithFetchHandlers} />; | ||
| }; | ||
|
|
||
| export default OrchestratorFormWrapper; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * Copyright Red Hat, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { useCallback, useEffect, useState } from 'react'; | ||
|
|
||
| /** | ||
| * Custom hook to manage processing state and notify parent context about loading status. | ||
| * This hook tracks both fetch loading and post-fetch processing, ensuring that the | ||
| * parent form knows when async operations are complete. | ||
| * | ||
| * @param fetchLoading - Whether data is currently being fetched | ||
| * @param handleFetchStarted - Optional callback to notify when processing starts | ||
| * @param handleFetchEnded - Optional callback to notify when processing ends | ||
| * @returns Object containing processing state management | ||
| */ | ||
| export const useProcessingState = ( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure we need this additional hook. All the data is in the form context, including |
||
| fetchLoading: boolean, | ||
| handleFetchStarted?: () => void, | ||
| handleFetchEnded?: () => void, | ||
| ) => { | ||
| const [isProcessing, setIsProcessing] = useState(false); | ||
|
|
||
| // Complete loading = fetch loading OR post-fetch processing | ||
| const completeLoading = fetchLoading || isProcessing; | ||
|
|
||
| // Notify parent context about the complete loading state | ||
| useEffect(() => { | ||
| if (completeLoading && handleFetchStarted) { | ||
| handleFetchStarted(); | ||
| return () => { | ||
| if (handleFetchEnded) { | ||
| handleFetchEnded(); | ||
| } | ||
| }; | ||
| } | ||
| return undefined; | ||
| }, [completeLoading, handleFetchStarted, handleFetchEnded]); | ||
|
|
||
| // Helper to wrap async processing with setIsProcessing | ||
| const wrapProcessing = useCallback( | ||
| async <T>(fn: () => Promise<T>): Promise<T> => { | ||
| setIsProcessing(true); | ||
| try { | ||
| return await fn(); | ||
| } finally { | ||
| setIsProcessing(false); | ||
| } | ||
| }, | ||
| [], | ||
| ); | ||
|
|
||
| return { | ||
| isProcessing, | ||
| setIsProcessing, | ||
| completeLoading, | ||
| wrapProcessing, | ||
| }; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ import { | |
| useRetriggerEvaluate, | ||
| useTemplateUnitEvaluator, | ||
| applySelectorArray, | ||
| useProcessingState, | ||
| } from '../utils'; | ||
| import { UiProps } from '../uiPropTypes'; | ||
| import { ErrorText } from './ErrorText'; | ||
|
|
@@ -82,6 +83,9 @@ export const ActiveDropdown: Widget< | |
| const [labels, setLabels] = useState<string[]>(); | ||
| const [values, setValues] = useState<string[]>(); | ||
|
|
||
| const handleFetchStarted = formContext?.handleFetchStarted; | ||
| const handleFetchEnded = formContext?.handleFetchEnded; | ||
|
|
||
| const retrigger = useRetriggerEvaluate( | ||
| templateUnitEvaluator, | ||
| formData, | ||
|
|
@@ -91,28 +95,37 @@ export const ActiveDropdown: Widget< | |
|
|
||
| const { data, error, loading } = useFetch(formData ?? {}, uiProps, retrigger); | ||
|
|
||
| // Track the complete loading state (fetch + processing) | ||
| const { completeLoading, wrapProcessing } = useProcessingState( | ||
| loading, | ||
| handleFetchStarted, | ||
| handleFetchEnded, | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| if (!data || !labelSelector || !valueSelector) { | ||
| return; | ||
| } | ||
|
|
||
| const doItAsync = async () => { | ||
| const selectedLabels = await applySelectorArray(data, labelSelector); | ||
| const selectedValues = await applySelectorArray(data, valueSelector); | ||
|
|
||
| if (selectedLabels.length !== selectedValues.length) { | ||
| setLocalError( | ||
| `Selected labels and values have different count (${selectedLabels.length} and ${selectedValues.length}) for ${props.id}`, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| setLabels(selectedLabels); | ||
| setValues(selectedValues); | ||
| await wrapProcessing(async () => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is that? Isn't tracking of the |
||
| const selectedLabels = await applySelectorArray(data, labelSelector); | ||
| const selectedValues = await applySelectorArray(data, valueSelector); | ||
|
|
||
| if (selectedLabels.length !== selectedValues.length) { | ||
| setLocalError( | ||
| `Selected labels and values have different count (${selectedLabels.length} and ${selectedValues.length}) for ${props.id}`, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| setLabels(selectedLabels); | ||
| setValues(selectedValues); | ||
| }); | ||
| }; | ||
|
|
||
| doItAsync(); | ||
| }, [labelSelector, valueSelector, data, props.id]); | ||
| }, [labelSelector, valueSelector, data, props.id, wrapProcessing]); | ||
|
|
||
| const handleChange = useCallback( | ||
| (changed: string, isByUser: boolean) => { | ||
|
|
@@ -136,7 +149,7 @@ export const ActiveDropdown: Widget< | |
| return <ErrorText text={localError ?? error ?? ''} id={id} />; | ||
| } | ||
|
|
||
| if (loading || !labels || !values) { | ||
| if (completeLoading || !labels || !values) { | ||
| return <CircularProgress size={20} />; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a need to have those as optional?