-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add menu option to submit playbook run for component
Build on top of #1352 PR Closes #1353 fix: close modal on save fix: fix submit relaoding full page and wrong endpoint fix: fix playbook submission params
- Loading branch information
1 parent
1961111
commit e801159
Showing
8 changed files
with
263 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { useFormikContext } from "formik"; | ||
import { SubmitPlaybookRunFormValues } from "./SubmitPlaybookRunForm"; | ||
import { useMemo } from "react"; | ||
import FormikTextInput from "../../Forms/Formik/FormikTextInput"; | ||
|
||
export default function AddPlaybookToRunParams() { | ||
const { values } = useFormikContext<SubmitPlaybookRunFormValues>(); | ||
|
||
const playbookParams = useMemo( | ||
() => | ||
(values.playbook_spec?.parameters as { name: string; label: string }[]) ?? | ||
[], | ||
[values.playbook_spec] | ||
); | ||
|
||
return ( | ||
<div className="flex flex-col gap-4"> | ||
<div className="font-bold">Playbook Run Params</div> | ||
<div className="flex flex-col gap-2"> | ||
{playbookParams.length > 0 ? ( | ||
playbookParams.map(({ name, label }) => ( | ||
<FormikTextInput name={`params.${name}`} label={label} /> | ||
)) | ||
) : ( | ||
<div className="text-gray-400">No parameters for this playbook.</div> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useFormikContext } from "formik"; | ||
import { useGetAllPlaybookSpecs } from "../../../api/query-hooks/playbooks"; | ||
import { Loading } from "../../Loading"; | ||
import { SubmitPlaybookRunFormValues } from "./SubmitPlaybookRunForm"; | ||
|
||
export default function SelectPlaybookToRun() { | ||
const { data: playbooks, isLoading, error } = useGetAllPlaybookSpecs(); | ||
|
||
// access formik state here | ||
const { setFieldValue } = useFormikContext<SubmitPlaybookRunFormValues>(); | ||
|
||
return ( | ||
<div className="flex flex-col gap-2"> | ||
<div className="font-bold">Select a Playbook</div> | ||
<div className="flex flex-col gap-2"> | ||
{error && !playbooks && ( | ||
<div className="text-red-500 text-center">{error.message}</div> | ||
)} | ||
{isLoading && !playbooks && <Loading />} | ||
{playbooks && ( | ||
<div className="flex flex-col gap-2"> | ||
{playbooks?.map(({ id, name, spec }) => ( | ||
<div | ||
role="button" | ||
onClick={() => { | ||
// update formik state here | ||
setFieldValue("id", id); | ||
setFieldValue("playbook_spec", spec); | ||
}} | ||
className="flex flex-row rounded-md border border-gray-200 hover:border-gray-400 hover:bg-gray-200 hover:font-semibold cursor py-2 px-2" | ||
> | ||
{name} | ||
</div> | ||
))} | ||
</div> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
} |
106 changes: 106 additions & 0 deletions
106
src/components/Playbooks/Runs/SubmitPlaybookRunForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { Form, Formik } from "formik"; | ||
import SelectPlaybookToRun from "./SelectPlaybookToRun"; | ||
import { useMemo } from "react"; | ||
import AddPlaybookToRunParams from "./AddPlaybookToRunParams"; | ||
import { Modal } from "../../Modal"; | ||
import { Button } from "../../Button"; | ||
import { useSubmitPlaybookRunMutation } from "../../../api/query-hooks/playbooks"; | ||
import { toastError } from "../../Toast/toast"; | ||
|
||
export type SubmitPlaybookRunFormValues = { | ||
// if this is present in the form, we show step to add params | ||
id: string; | ||
component_id?: string; | ||
config_id?: string; | ||
params?: Record<string, any>; | ||
// do not send this to the backend | ||
playbook_spec?: Record<string, any>; | ||
}; | ||
|
||
type Props = { | ||
isOpen: boolean; | ||
onClose: () => void; | ||
}; | ||
|
||
type SubmitPlaybookRunFormForComponentProps = { | ||
type: "component"; | ||
componentId: string; | ||
} & Props; | ||
|
||
type SubmitPlaybookRunFormForConfigProps = { | ||
type: "config"; | ||
configId: string; | ||
} & Props; | ||
|
||
type SubmitPlaybookRunFormProps = | ||
| SubmitPlaybookRunFormForComponentProps | ||
| SubmitPlaybookRunFormForConfigProps; | ||
|
||
export default function SubmitPlaybookRunForm({ | ||
isOpen, | ||
onClose, | ||
...props | ||
}: SubmitPlaybookRunFormProps) { | ||
const initialValues: Partial<SubmitPlaybookRunFormValues> = useMemo( | ||
() => ({ | ||
playbook_id: undefined, | ||
params: undefined, | ||
...(props.type === "component" | ||
? { component_id: props.componentId } | ||
: { | ||
config_id: props.configId | ||
}) | ||
}), | ||
[props] | ||
); | ||
|
||
const { mutate: submitPlaybookRun } = useSubmitPlaybookRunMutation({ | ||
onSuccess: () => { | ||
toastError("Playbook run submitted successfully"); | ||
onClose(); | ||
}, | ||
onError: (error) => { | ||
toastError(error.message); | ||
} | ||
}); | ||
|
||
return ( | ||
<Modal | ||
title="Submit a Playbook Run" | ||
open={isOpen} | ||
onClose={onClose} | ||
size="slightly-small" | ||
bodyClass="" | ||
containerClassName="" | ||
> | ||
<Formik | ||
initialValues={initialValues} | ||
onSubmit={(values) => { | ||
const { playbook_spec, ...rest } = values; | ||
submitPlaybookRun(rest as SubmitPlaybookRunFormValues); | ||
}} | ||
> | ||
{({ values, handleSubmit }) => { | ||
return ( | ||
<Form onSubmit={handleSubmit} className="flex flex-col gap-2"> | ||
<div className="flex p-4 flex-col gap-2"> | ||
{!values.id ? ( | ||
<SelectPlaybookToRun /> | ||
) : ( | ||
<AddPlaybookToRunParams /> | ||
)} | ||
</div> | ||
<div className="flex flex-row p-4 justify-end bg-gray-200 rounded-b rounded-md"> | ||
<Button | ||
disabled={values.id === undefined} | ||
text="Submit" | ||
type="submit" | ||
/> | ||
</div> | ||
</Form> | ||
); | ||
}} | ||
</Formik> | ||
</Modal> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters