-
-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
244 additions
and
2 deletions.
There are no files selected for viewing
195 changes: 195 additions & 0 deletions
195
app/ui/src/components/Bot/Integration/IntegrationForm.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,195 @@ | ||
import { Divider, Form, notification } from "antd"; | ||
import { Switch } from "@headlessui/react"; | ||
import React from "react"; | ||
import { useMutation, useQueryClient } from "@tanstack/react-query"; | ||
import api from "../../../services/api"; | ||
import { useParams } from "react-router-dom"; | ||
import axios from "axios"; | ||
|
||
//@ts-ignore | ||
function classNames(...classes) { | ||
return classes.filter(Boolean).join(" "); | ||
} | ||
type Props = { | ||
onClose: () => void; | ||
data: { | ||
name: string; | ||
channel: string; | ||
logo: string; | ||
description: string; | ||
link: string; | ||
fields: { | ||
name: string; | ||
type: string; | ||
title: string; | ||
description: string; | ||
help: string; | ||
requiredMessage: string; | ||
value: string; | ||
}[]; | ||
isPaused: boolean; | ||
status: string; | ||
color: string; | ||
textColor: string; | ||
}; | ||
}; | ||
|
||
export const IntegrationForm: React.FC<Props> = ({ onClose, data }) => { | ||
const [enabled, setEnabled] = React.useState(!data.isPaused); | ||
const params = useParams<{ id: string }>(); | ||
|
||
const onSubmit = async (value: any) => { | ||
const response = await api.post(`/bot/integration/${params.id}`, { | ||
provider: data.channel, | ||
value, | ||
}); | ||
return response.data; | ||
}; | ||
|
||
const client = useQueryClient(); | ||
|
||
const { mutate: updateIntegration, isLoading: isUpdating } = useMutation( | ||
onSubmit, | ||
{ | ||
onSuccess: () => { | ||
client.invalidateQueries(["getBotEIntegration", params.id]); | ||
notification.success({ | ||
message: "Success", | ||
description: "Integration updated successfully", | ||
}); | ||
onClose(); | ||
}, | ||
onError: (e: any) => { | ||
if (axios.isAxiosError(e)) { | ||
const message = | ||
e.response?.data?.message || | ||
e?.response?.data?.error || | ||
e.message || | ||
"Something went wrong"; | ||
notification.error({ | ||
message: "Error", | ||
description: message, | ||
}); | ||
return; | ||
} | ||
|
||
notification.error({ | ||
message: "Error", | ||
description: e.message || "Something went wrong", | ||
}); | ||
}, | ||
} | ||
); | ||
|
||
const { mutate: toggleIntegration, isLoading: isToggling } = useMutation( | ||
async () => { | ||
const response = await api.post(`/bot/integration/${params.id}/toggle`, { | ||
provider: data.channel, | ||
}); | ||
return response.data; | ||
}, | ||
{ | ||
onSuccess: () => { | ||
client.invalidateQueries(["getBotEIntegration", params.id]); | ||
notification.success({ | ||
message: "Success", | ||
description: "Integration toggled successfully", | ||
}); | ||
onClose(); | ||
}, | ||
onError: (e: any) => { | ||
if (axios.isAxiosError(e)) { | ||
const message = | ||
e.response?.data?.message || | ||
e?.response?.data?.error || | ||
e.message || | ||
"Something went wrong"; | ||
notification.error({ | ||
message: "Error", | ||
description: message, | ||
}); | ||
return; | ||
} | ||
|
||
notification.error({ | ||
message: "Error", | ||
description: e.message || "Something went wrong", | ||
}); | ||
}, | ||
} | ||
); | ||
|
||
return ( | ||
<> | ||
<Form | ||
initialValues={{ | ||
...data.fields.reduce((acc, field) => { | ||
//@ts-ignore | ||
acc[field.name] = field.value; | ||
return acc; | ||
}, {}), | ||
}} | ||
layout="vertical" | ||
className="space-y-6" | ||
onFinish={updateIntegration} | ||
> | ||
{data.fields.map((field, index) => { | ||
return ( | ||
<Form.Item | ||
key={index} | ||
label={field.title} | ||
name={field.name} | ||
rules={[{ required: true, message: field.requiredMessage }]} | ||
> | ||
<input | ||
type="password" | ||
placeholder={field.help} | ||
className="border border-gray-300 rounded-md px-4 py-2 w-full focus:outline-none focus:border-blue-500 focus:ring-1 focus:ring-blue-500" | ||
/> | ||
</Form.Item> | ||
); | ||
})} | ||
|
||
<Form.Item> | ||
<button | ||
type="submit" | ||
disabled={isUpdating || isToggling} | ||
className="flex w-full justify-center rounded-md border border-transparent bg-indigo-600 py-2 px-4 text-sm font-medium text-white shadow-sm hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2" | ||
> | ||
{isUpdating ? "Saving..." : "Save"} | ||
</button> | ||
</Form.Item> | ||
</Form> | ||
<Divider /> | ||
<Switch.Group as="div" className="flex items-center justify-between"> | ||
<Switch.Label as="span" className="ml-3"> | ||
<span className="text-sm font-medium text-gray-600"> | ||
Enable integration | ||
</span> | ||
</Switch.Label> | ||
<Switch | ||
disabled={ | ||
data.status.toLowerCase() === "connect" || isToggling || isUpdating | ||
} | ||
checked={enabled} | ||
onChange={(e) => { | ||
setEnabled(e); | ||
toggleIntegration(); | ||
}} | ||
className={classNames( | ||
enabled ? "bg-indigo-600" : "bg-gray-200", | ||
"relative inline-flex h-6 w-11 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 disabled:opacity-50" | ||
)} | ||
> | ||
<span | ||
aria-hidden="true" | ||
className={classNames( | ||
enabled ? "translate-x-5" : "translate-x-0", | ||
"pointer-events-none inline-block h-5 w-5 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out" | ||
)} | ||
/> | ||
</Switch> | ||
</Switch.Group> | ||
</> | ||
); | ||
}; |
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