Skip to content

Commit

Permalink
moved it outside
Browse files Browse the repository at this point in the history
  • Loading branch information
hagen-danswer committed Nov 22, 2024
1 parent 3e58f9f commit e32809f
Showing 1 changed file with 54 additions and 52 deletions.
106 changes: 54 additions & 52 deletions web/src/app/admin/bots/SlackTokensForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,56 +24,58 @@ export const SlackTokensForm = ({
setPopup: (popup: { message: string; type: "error" | "success" }) => void;
router: any;
onValuesChange?: (values: any) => void;
}) => (
<Formik
initialValues={initialValues}
validationSchema={Yup.object().shape({
bot_token: Yup.string().required(),
app_token: Yup.string().required(),
name: Yup.string().required(),
})}
onSubmit={async (values, formikHelpers) => {
formikHelpers.setSubmitting(true);
}) => {
useEffect(() => {
if (onValuesChange) {
onValuesChange(initialValues);
}
}, [initialValues]);

let response;
if (isUpdate) {
response = await updateSlackBot(existingSlackBotId!, values);
} else {
response = await createSlackBot(values);
}
formikHelpers.setSubmitting(false);
if (response.ok) {
if (refreshSlackBot) {
refreshSlackBot();
}
const responseJson = await response.json();
const botId = isUpdate ? existingSlackBotId : responseJson.id;
setPopup({
message: isUpdate
? "Successfully updated Slack Bot!"
: "Successfully created Slack Bot!",
type: "success",
});
router.push(`/admin/bots/${encodeURIComponent(botId)}`);
} else {
const responseJson = await response.json();
const errorMsg = responseJson.detail || responseJson.message;
setPopup({
message: isUpdate
? `Error updating Slack Bot - ${errorMsg}`
: `Error creating Slack Bot - ${errorMsg}`,
type: "error",
});
}
}}
enableReinitialize={true}
>
{({ isSubmitting, setFieldValue, values }) => {
useEffect(() => {
onValuesChange?.(values);
}, [values, onValuesChange]);
return (
<Formik
initialValues={initialValues}
validationSchema={Yup.object().shape({
bot_token: Yup.string().required(),
app_token: Yup.string().required(),
name: Yup.string().required(),
})}
onSubmit={async (values, formikHelpers) => {
formikHelpers.setSubmitting(true);

return (
let response;
if (isUpdate) {
response = await updateSlackBot(existingSlackBotId!, values);
} else {
response = await createSlackBot(values);
}
formikHelpers.setSubmitting(false);
if (response.ok) {
if (refreshSlackBot) {
refreshSlackBot();
}
const responseJson = await response.json();
const botId = isUpdate ? existingSlackBotId : responseJson.id;
setPopup({
message: isUpdate
? "Successfully updated Slack Bot!"
: "Successfully created Slack Bot!",
type: "success",
});
router.push(`/admin/bots/${encodeURIComponent(botId)}`);
} else {
const responseJson = await response.json();
const errorMsg = responseJson.detail || responseJson.message;
setPopup({
message: isUpdate
? `Error updating Slack Bot - ${errorMsg}`
: `Error creating Slack Bot - ${errorMsg}`,
type: "error",
});
}
}}
enableReinitialize={true}
>
{({ isSubmitting, setFieldValue, values }) => (
<Form className="w-full">
{!isUpdate && (
<div className="">
Expand Down Expand Up @@ -126,7 +128,7 @@ export const SlackTokensForm = ({
</Button>
</div>
</Form>
);
}}
</Formik>
);
)}
</Formik>
);
};

0 comments on commit e32809f

Please sign in to comment.