From 4ccff9b4ce579cc585f16cf540002d37349997e2 Mon Sep 17 00:00:00 2001 From: Seogeurim Date: Wed, 29 Dec 2021 20:37:45 +0900 Subject: [PATCH] =?UTF-8?q?[#65]=20feat:=20=EC=84=9C=EB=B9=84=EC=8A=A4=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20API=20=EC=97=B0=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - useServiceEdit hook 수정 - API 연동, 로딩, 에러, 수정 완료 처리 --- src/apis/serviceApi/index.ts | 4 +++ .../service/ServiceEditForm/index.tsx | 29 ++++++++++++++----- src/hooks/useServiceEdit.ts | 28 ++++++++++++++++++ src/pages/ServiceEditPage/index.tsx | 15 ++++++---- 4 files changed, 64 insertions(+), 12 deletions(-) diff --git a/src/apis/serviceApi/index.ts b/src/apis/serviceApi/index.ts index fb2e610..402ea31 100644 --- a/src/apis/serviceApi/index.ts +++ b/src/apis/serviceApi/index.ts @@ -23,6 +23,10 @@ class ServiceApi extends Api { return response.data; } + + async updateInfo(id: string, formData: FormData): Promise { + await this.patch(formData, { additionalUri: `/${id}` }); + } } export default ServiceApi; diff --git a/src/components/service/ServiceEditForm/index.tsx b/src/components/service/ServiceEditForm/index.tsx index c5ddafd..beb4763 100644 --- a/src/components/service/ServiceEditForm/index.tsx +++ b/src/components/service/ServiceEditForm/index.tsx @@ -14,6 +14,7 @@ import { type ServiceEditFormProps = { serviceInfo: ServiceInfo; + updateServiceInfo: (formData: FormData) => void; }; type ServiceEditFormInput = { @@ -29,7 +30,10 @@ type ImageInput = { raw: File | null; }; -const ServiceEditForm: FC = ({ serviceInfo }) => { +const ServiceEditForm: FC = ({ + serviceInfo, + updateServiceInfo, +}) => { const { name, clientId, @@ -74,10 +78,6 @@ const ServiceEditForm: FC = ({ serviceInfo }) => { [inputs], ); - const onSubmit = () => { - // TODO: 서비스 수정 API 연결 - }; - const onChangeImgFile = (e) => { if (e.target.files.length) { setImage({ @@ -87,8 +87,23 @@ const ServiceEditForm: FC = ({ serviceInfo }) => { } }; + const onClickSaveBtn = () => { + const formData = new FormData(); + if (name !== inputs.name) formData.append('name', inputs.name); + if (description !== inputs.description) + formData.append('description', inputs.description); + if (domain !== inputs.domain) formData.append('domain', inputs.domain); + if (firstQuestion !== inputs.firstQuestion) + formData.append('question', inputs.firstQuestion); + if (theme.toString() !== inputs.theme) + formData.append('themeId', inputs.theme); + if (image.raw !== null) formData.append('image', image.raw); + + updateServiceInfo(formData); + }; + return ( -
+
- diff --git a/src/hooks/useServiceEdit.ts b/src/hooks/useServiceEdit.ts index 469f6bf..f777133 100644 --- a/src/hooks/useServiceEdit.ts +++ b/src/hooks/useServiceEdit.ts @@ -6,19 +6,25 @@ import { NOT_FOUND_ERROR, UNEXPECTED_ERROR, } from '@/constants/error'; +import styleTheme from '@/styles/theme'; import { ServiceInfo } from '@/types'; +import useSnackbar from './useSnackbar'; + const useServiceEdit = ( serviceId: string, ): { isLoading: boolean; error: string; serviceInfo: ServiceInfo; + updateServiceInfo: (formData: FormData) => void; } => { const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const [serviceInfo, setServiceInfo] = useState(null); + const snackbar = useSnackbar({}); + const getServiceInfo = async () => { try { setIsLoading(true); @@ -58,6 +64,27 @@ const useServiceEdit = ( } }; + const updateServiceInfo = async (formData: FormData) => { + try { + setIsLoading(true); + + await serviceApi.updateInfo(serviceId, formData); + + snackbar.showMessage('Service Update Success!!', { + duration: 1500, + backgroundColor: styleTheme.colorSuccess, + }); + } catch (e) { + if (e.response) { + setError(UNEXPECTED_ERROR); + } else { + setError(NETWORK_ERROR); + } + } finally { + setIsLoading(false); + } + }; + useEffect(() => { getServiceInfo(); }, []); @@ -66,6 +93,7 @@ const useServiceEdit = ( isLoading, error, serviceInfo, + updateServiceInfo, }; }; diff --git a/src/pages/ServiceEditPage/index.tsx b/src/pages/ServiceEditPage/index.tsx index 81b4ab6..409db53 100644 --- a/src/pages/ServiceEditPage/index.tsx +++ b/src/pages/ServiceEditPage/index.tsx @@ -11,15 +11,15 @@ import theme from '@/styles/theme'; const ServiceEditPage: FC = () => { const { serviceId } = useParams<{ serviceId: string }>(); - const { isLoading, error, serviceInfo } = useServiceEdit(serviceId); - const snackbar = useSnackbar({ - backgroundColor: theme.colorPrimary, - }); + const { isLoading, error, serviceInfo, updateServiceInfo } = + useServiceEdit(serviceId); + const snackbar = useSnackbar({}); useEffect(() => { if (error) { snackbar.showMessage(error, { duration: 1500, + backgroundColor: theme.colorPrimary, }); } }, [error]); @@ -27,7 +27,12 @@ const ServiceEditPage: FC = () => { return ( {isLoading && } - {serviceInfo && } + {serviceInfo && ( + + )} {error && } );