-
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 mechanism to create agents
- Loading branch information
1 parent
ef320eb
commit 6162be8
Showing
6 changed files
with
114 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { UseMutationOptions, useMutation } from "@tanstack/react-query"; | ||
import { Agent } from "../../../components/Agents/AgentPage"; | ||
import { addAgent } from "../../services/agents"; | ||
|
||
export default function useUpsertAgentMutations( | ||
options?: UseMutationOptions<Agent, Error, Agent> | ||
) { | ||
return useMutation({ | ||
...options, | ||
mutationFn: async (agent: Agent) => addAgent(agent) | ||
}); | ||
} |
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,22 @@ | ||
import { useState } from "react"; | ||
import { AiFillPlusCircle } from "react-icons/ai"; | ||
import AgentForm from "./AddAgentForm"; | ||
|
||
export default function AddAgent() { | ||
const [isModalOpen, setIsModalOpen] = useState(false); | ||
|
||
return ( | ||
<> | ||
<button type="button" className="" onClick={() => setIsModalOpen(true)}> | ||
<AiFillPlusCircle size={32} className="text-blue-600" /> | ||
</button> | ||
<AgentForm | ||
isOpen={isModalOpen} | ||
onClose={() => { | ||
// todo: show modal with helm install instructions | ||
return setIsModalOpen(false); | ||
}} | ||
/> | ||
</> | ||
); | ||
} |
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,65 @@ | ||
import { Form, Formik } from "formik"; | ||
import { Modal } from "../../Modal"; | ||
import { Agent } from "../AgentPage"; | ||
import useUpsertAgentMutations from "../../../api/query-hooks/mutations/useUpsertAgentMutations"; | ||
import { toastError } from "../../Toast/toast"; | ||
import FormikTextInput from "../../Forms/Formik/FormikTextInput"; | ||
import clsx from "clsx"; | ||
import { Button } from "../../Button"; | ||
|
||
type Props = { | ||
agent?: Agent; | ||
isOpen: boolean; | ||
onClose: () => void; | ||
}; | ||
|
||
export default function AgentForm({ agent, isOpen, onClose }: Props) { | ||
const { mutate: upsertAgent } = useUpsertAgentMutations({ | ||
onSuccess: () => { | ||
onClose(); | ||
}, | ||
onError: (error) => { | ||
toastError(error.message); | ||
} | ||
}); | ||
|
||
return ( | ||
<Modal | ||
title={agent?.id ? agent.name : "Add Agent"} | ||
onClose={onClose} | ||
open={isOpen} | ||
bodyClass="flex flex-col w-full flex-1 h-full overflow-y-auto" | ||
> | ||
<Formik | ||
initialValues={{ | ||
...agent | ||
}} | ||
onSubmit={(value) => { | ||
upsertAgent(value as Agent); | ||
}} | ||
> | ||
{({ handleSubmit }) => ( | ||
<Form | ||
className="flex flex-col flex-1 overflow-y-auto" | ||
onSubmit={handleSubmit} | ||
> | ||
<div className={clsx("flex flex-col h-full my-2 overflow-y-auto")}> | ||
<div className={clsx("flex flex-col px-2 mb-2 overflow-y-auto")}> | ||
<div className="flex flex-col space-y-4 overflow-y-auto p-4"> | ||
<FormikTextInput name="name" label="Name" required /> | ||
</div> | ||
</div> | ||
</div> | ||
<div className="flex items-center justify-between py-4 px-5 bg-gray-100"> | ||
<Button | ||
type="submit" | ||
text={agent?.id ? "Update" : "Save"} | ||
className="btn-primary" | ||
/> | ||
</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