|
| 1 | +import { Form, Switch, Input, notification } from "antd"; |
| 2 | +import { useParams } from "react-router-dom"; |
| 3 | +import api from "../../../services/api"; |
| 4 | +import { useMutation, useQueryClient } from "@tanstack/react-query"; |
| 5 | +import axios from "axios"; |
| 6 | + |
| 7 | +type Props = { |
| 8 | + publicBotPwdProtected: boolean; |
| 9 | + publicBotPwd: string; |
| 10 | +}; |
| 11 | + |
| 12 | +export const SettingsPwdP: React.FC<Props> = ({ |
| 13 | + publicBotPwd, |
| 14 | + publicBotPwdProtected, |
| 15 | +}) => { |
| 16 | + const params = useParams<{ id: string }>(); |
| 17 | + const [form] = Form.useForm(); |
| 18 | + const isEnabled = Form.useWatch("publicBotPwdProtected", form); |
| 19 | + const client = useQueryClient(); |
| 20 | + const onFinish = async (values: any) => { |
| 21 | + const response = await api.put(`/bot/${params.id}/password`, values); |
| 22 | + return response.data; |
| 23 | + }; |
| 24 | + |
| 25 | + const { mutate, isLoading } = useMutation(onFinish, { |
| 26 | + onSuccess: () => { |
| 27 | + client.invalidateQueries(["getBotSettings", params.id]); |
| 28 | + |
| 29 | + notification.success({ |
| 30 | + message: "Bot settings updated successfully", |
| 31 | + }); |
| 32 | + }, |
| 33 | + onError: (error: any) => { |
| 34 | + if (axios.isAxiosError(error)) { |
| 35 | + const message = error.response?.data?.message || "Something went wrong"; |
| 36 | + notification.error({ |
| 37 | + message, |
| 38 | + }); |
| 39 | + return; |
| 40 | + } |
| 41 | + notification.error({ |
| 42 | + message: "Something went wrong", |
| 43 | + }); |
| 44 | + }, |
| 45 | + }); |
| 46 | + return ( |
| 47 | + <Form |
| 48 | + form={form} |
| 49 | + initialValues={{ |
| 50 | + publicBotPwdProtected, |
| 51 | + publicBotPwd, |
| 52 | + }} |
| 53 | + layout="vertical" |
| 54 | + onFinish={mutate} |
| 55 | + > |
| 56 | + <div className="px-4 py-5 bg-white border sm:rounded-lg sm:p-6 dark:bg-[#1e1e1e] dark:border-gray-700"> |
| 57 | + <div className="md:grid md:grid-cols-3 md:gap-6"> |
| 58 | + <div className="md:col-span-1"> |
| 59 | + <h3 className="text-lg font-medium leading-6 text-gray-900 dark:text-white"> |
| 60 | + Password Protection |
| 61 | + </h3> |
| 62 | + <p className="mt-1 text-sm text-gray-500 dark:text-gray-400"> |
| 63 | + Proctect bot's public access with a password. |
| 64 | + </p> |
| 65 | + </div> |
| 66 | + <div className="mt-5 space-y-6 md:col-span-2 md:mt-0"> |
| 67 | + <Form.Item |
| 68 | + name="publicBotPwdProtected" |
| 69 | + valuePropName="checked" |
| 70 | + label="Enable Password Protection" |
| 71 | + > |
| 72 | + <Switch /> |
| 73 | + </Form.Item> |
| 74 | + |
| 75 | + <Form.Item |
| 76 | + name="publicBotPwd" |
| 77 | + label="Password" |
| 78 | + rules={[ |
| 79 | + { |
| 80 | + required: isEnabled, |
| 81 | + message: "Please input your password!", |
| 82 | + }, |
| 83 | + ]} |
| 84 | + > |
| 85 | + <Input.Password |
| 86 | + placeholder="Password" |
| 87 | + disabled={!isEnabled} |
| 88 | + size="large" |
| 89 | + /> |
| 90 | + </Form.Item> |
| 91 | + |
| 92 | + <div className="text-sm text-gray-500 dark:text-gray-400"> |
| 93 | + This feature is in preview and only works with web interface for |
| 94 | + now |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + |
| 99 | + <div className="mt-3 text-right"> |
| 100 | + <button |
| 101 | + type="submit" |
| 102 | + className="inline-flex justify-center rounded-md border border-transparent bg-indigo-600 py-2 px-4 text-sm font-medium text-white hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2" |
| 103 | + > |
| 104 | + {isLoading ? "Saving..." : "Save"} |
| 105 | + </button> |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + </Form> |
| 109 | + ); |
| 110 | +}; |
0 commit comments