|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { Button, Skeleton } from "@zenml-io/react-component-library"; |
| 4 | +import { useId } from "react"; |
| 5 | +import * as Wizard from "@/components/wizard/Wizard"; |
| 6 | +import { Link, useNavigate, useParams } from "react-router-dom"; |
| 7 | +import { componentQueries } from "@/data/components"; |
| 8 | +import { useQuery } from "@tanstack/react-query"; |
| 9 | +import { routes } from "@/router/routes"; |
| 10 | +import { ComponentConfigurationForm } from "@/components/stack-components/create-component/configuration-form"; |
| 11 | + |
| 12 | +export function EditComponentConfig() { |
| 13 | + const navigate = useNavigate(); |
| 14 | + const { componentId } = useParams() as { componentId: string }; |
| 15 | + const component = useQuery({ |
| 16 | + ...componentQueries.componentDetail(componentId), |
| 17 | + throwOnError: true |
| 18 | + }); |
| 19 | + |
| 20 | + function handleSuccess(id: string) { |
| 21 | + navigate(routes.components.detail(id)); |
| 22 | + } |
| 23 | + |
| 24 | + const formId = useId(); |
| 25 | + if (component.isPending) { |
| 26 | + return <Skeleton className="h-[300px] w-full" />; |
| 27 | + } |
| 28 | + |
| 29 | + if (component.isError) { |
| 30 | + return <div>Error</div>; |
| 31 | + } |
| 32 | + |
| 33 | + const flavorId = component.data.resources?.flavor?.id; |
| 34 | + |
| 35 | + if (!flavorId) { |
| 36 | + return <div>No flavor found</div>; |
| 37 | + } |
| 38 | + |
| 39 | + return ( |
| 40 | + <> |
| 41 | + <Wizard.Body className="p-0"> |
| 42 | + <ComponentConfigurationForm |
| 43 | + component={component.data} |
| 44 | + flavorId={flavorId} |
| 45 | + formId={formId} |
| 46 | + isCreate={false} |
| 47 | + successHandler={handleSuccess} |
| 48 | + FooterComponent={FooterComponent} |
| 49 | + /> |
| 50 | + </Wizard.Body> |
| 51 | + </> |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +function FooterComponent({ formId, isPending }: { formId: string; isPending: boolean }) { |
| 56 | + const params = useParams() as { componentId: string }; |
| 57 | + |
| 58 | + return ( |
| 59 | + <Wizard.Footer> |
| 60 | + <Button asChild intent="secondary" size="md"> |
| 61 | + <Link to={routes.components.detail(params.componentId)}>Cancel</Link> |
| 62 | + </Button> |
| 63 | + <Button size="md" disabled={isPending} type="submit" form={formId}> |
| 64 | + {isPending && ( |
| 65 | + <div |
| 66 | + role="alert" |
| 67 | + aria-busy="true" |
| 68 | + className="full h-[20px] w-[20px] animate-spin rounded-rounded border-2 border-theme-text-negative border-b-theme-text-brand" |
| 69 | + ></div> |
| 70 | + )} |
| 71 | + Update Component |
| 72 | + </Button> |
| 73 | + </Wizard.Footer> |
| 74 | + ); |
| 75 | +} |
0 commit comments