1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-23 00:22:19 +01:00

refactor: add handleChange

This commit is contained in:
Youssef 2022-02-09 19:09:02 +01:00
parent a6e1e60e2c
commit c10525108e
2 changed files with 19 additions and 48 deletions

View File

@ -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 (
<Grid container style={{ marginTop: '1rem' }}>
<Grid item sm={12} xs={12} className={commonStyles.contentSpacingY}>
@ -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)}
/>
</Grid>
<Grid item>

View File

@ -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;