From a918dce05f32541b735a3d80941cf41d6976f07c Mon Sep 17 00:00:00 2001 From: Xiao Yijun Date: Thu, 24 Mar 2022 11:55:13 +0800 Subject: [PATCH] feat(console): save changes in application detail page (#439) --- .../src/pages/ApplicationDetails/index.tsx | 27 ++++++++++++++++--- packages/phrases/src/locales/en.ts | 1 + packages/phrases/src/locales/zh-cn.ts | 1 + 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/packages/console/src/pages/ApplicationDetails/index.tsx b/packages/console/src/pages/ApplicationDetails/index.tsx index b61e35c84..2c1af99b0 100644 --- a/packages/console/src/pages/ApplicationDetails/index.tsx +++ b/packages/console/src/pages/ApplicationDetails/index.tsx @@ -1,6 +1,7 @@ import { Application } from '@logto/schemas'; import React, { useEffect, useMemo, useState } from 'react'; import { useController, useForm } from 'react-hook-form'; +import { toast } from 'react-hot-toast'; import { useTranslation } from 'react-i18next'; import Modal from 'react-modal'; import { useLocation, useParams } from 'react-router-dom'; @@ -17,7 +18,7 @@ import ImagePlaceholder from '@/components/ImagePlaceholder'; import MultilineInput from '@/components/MultilineInput'; import TabNav, { TabNavLink } from '@/components/TabNav'; import TextInput from '@/components/TextInput'; -import { RequestError } from '@/hooks/use-api'; +import useApi, { RequestError } from '@/hooks/use-api'; import Delete from '@/icons/Delete'; import More from '@/icons/More'; import * as modalStyles from '@/scss/modal.module.scss'; @@ -38,7 +39,9 @@ const ApplicationDetails = () => { const location = useLocation(); const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); - const { data, error } = useSWR(id && `/api/applications/${id}`); + const { data, error, mutate } = useSWR( + id && `/api/applications/${id}` + ); // TODO LOG-1908: OidcConfig in Application Details const { data: oidcConfig, error: fetchOidcConfigError } = useSWR( '/oidc/.well-known/openid-configuration' @@ -48,6 +51,8 @@ const ApplicationDetails = () => { const [isReadmeOpen, setIsReadmeOpen] = useState(false); const [isDeleteFormOpen, setIsDeleteFormOpen] = useState(false); + const api = useApi(); + const [submitting, setSubmitting] = useState(false); const { control, handleSubmit, register, reset } = useForm(); @@ -75,8 +80,21 @@ const ApplicationDetails = () => { defaultValue: [], }); - const onSubmit = handleSubmit((formData) => { - console.log(formData); + const onSubmit = handleSubmit(async (formData) => { + if (!data || submitting) { + return; + } + setSubmitting(true); + + try { + const updatedApplication = await api + .patch(`/api/applications/${data.id}`, { json: formData }) + .json(); + void mutate(updatedApplication); + toast.success(t('application_details.save_success')); + } finally { + setSubmitting(false); + } }); const isAdvancedSettings = location.pathname.includes('advanced-settings'); @@ -218,6 +236,7 @@ const ApplicationDetails = () => {