From c10525108e4442822636ed2ce0880cde9aa60d76 Mon Sep 17 00:00:00 2001 From: Youssef Date: Wed, 9 Feb 2022 19:09:02 +0100 Subject: [PATCH] refactor: add handleChange --- .../ApplicationUpdate/ApplicationUpdate.tsx | 27 +++++++++---- .../useApplicationsForStrategy.ts | 40 ------------------- 2 files changed, 19 insertions(+), 48 deletions(-) delete mode 100644 frontend/src/hooks/api/getters/useApplicationsForStrategy/useApplicationsForStrategy.ts diff --git a/frontend/src/component/application/ApplicationUpdate/ApplicationUpdate.tsx b/frontend/src/component/application/ApplicationUpdate/ApplicationUpdate.tsx index eed4150074..a295c84212 100644 --- a/frontend/src/component/application/ApplicationUpdate/ApplicationUpdate.tsx +++ b/frontend/src/component/application/ApplicationUpdate/ApplicationUpdate.tsx @@ -1,9 +1,10 @@ -import { useState } from 'react'; +import { ChangeEvent, useState } from 'react'; import { TextField, Grid } from '@material-ui/core'; import { useCommonStyles } from '../../../common.styles'; import icons from '../icon-names'; import GeneralSelect from '../../common/GeneralSelect/GeneralSelect'; import useApplicationsApi from '../../../hooks/api/actions/useApplicationsApi/useApplicationsApi'; +import useToast from '../../../hooks/useToast'; interface IApplication { appName: string; @@ -26,8 +27,24 @@ const ApplicationUpdate = ({ application }: IApplicationUpdateProps) => { const { appName, icon, url, description } = application; const [localUrl, setLocalUrl] = useState(url || ''); const [localDescription, setLocalDescription] = useState(description || ''); + const { setToastApiError } = useToast(); const commonStyles = useCommonStyles(); + const handleChange = ( + evt: ChangeEvent<{ name?: string | undefined; value: unknown }> + ) => { + evt.preventDefault(); + try { + storeApplicationMetaData( + appName, + 'icon', + evt.target.value as string + ); + } catch (e: any) { + setToastApiError(e.toString()); + } + }; + return ( @@ -38,13 +55,7 @@ const ApplicationUpdate = ({ application }: IApplicationUpdateProps) => { label="Icon" options={icons.map(v => ({ key: v, label: v }))} value={icon || 'apps'} - onChange={e => - storeApplicationMetaData( - appName, - 'icon', - e.target.value as string - ) - } + onChange={e => handleChange(e)} /> diff --git a/frontend/src/hooks/api/getters/useApplicationsForStrategy/useApplicationsForStrategy.ts b/frontend/src/hooks/api/getters/useApplicationsForStrategy/useApplicationsForStrategy.ts deleted file mode 100644 index 8a537e910f..0000000000 --- a/frontend/src/hooks/api/getters/useApplicationsForStrategy/useApplicationsForStrategy.ts +++ /dev/null @@ -1,40 +0,0 @@ -import useSWR, { mutate, SWRConfiguration } from 'swr'; -import { useState, useEffect } from 'react'; -import { formatApiPath } from '../../../../utils/format-path'; -import handleErrorResponses from '../httpErrorResponseHandler'; - -const path = formatApiPath(`api/admin/metrics/applications`); -const KEY = `api/admin/metrics/applications`; - -const useApplicationsForStrategy = ( - strategyName: string, - options: SWRConfiguration = {} -) => { - const fetcher = async () => { - const res = await fetch(`${path}?strategyName=${strategyName}`, { - method: 'GET', - }).then(handleErrorResponses('Application')); - return res.json(); - }; - - const { data, error } = useSWR(KEY, fetcher, options); - const [loading, setLoading] = useState(!error && !data); - - const refetchAddons = () => { - mutate(KEY); - }; - - useEffect(() => { - setLoading(!error && !data); - }, [data, error]); - - return { - addons: data?.addons || [], - providers: data?.providers || [], - error, - loading, - refetchAddons, - }; -}; - -export default useApplicationsForStrategy;