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:
parent
a6e1e60e2c
commit
c10525108e
@ -1,9 +1,10 @@
|
|||||||
import { useState } from 'react';
|
import { ChangeEvent, useState } from 'react';
|
||||||
import { TextField, Grid } from '@material-ui/core';
|
import { TextField, Grid } from '@material-ui/core';
|
||||||
import { useCommonStyles } from '../../../common.styles';
|
import { useCommonStyles } from '../../../common.styles';
|
||||||
import icons from '../icon-names';
|
import icons from '../icon-names';
|
||||||
import GeneralSelect from '../../common/GeneralSelect/GeneralSelect';
|
import GeneralSelect from '../../common/GeneralSelect/GeneralSelect';
|
||||||
import useApplicationsApi from '../../../hooks/api/actions/useApplicationsApi/useApplicationsApi';
|
import useApplicationsApi from '../../../hooks/api/actions/useApplicationsApi/useApplicationsApi';
|
||||||
|
import useToast from '../../../hooks/useToast';
|
||||||
|
|
||||||
interface IApplication {
|
interface IApplication {
|
||||||
appName: string;
|
appName: string;
|
||||||
@ -26,8 +27,24 @@ const ApplicationUpdate = ({ application }: IApplicationUpdateProps) => {
|
|||||||
const { appName, icon, url, description } = application;
|
const { appName, icon, url, description } = application;
|
||||||
const [localUrl, setLocalUrl] = useState(url || '');
|
const [localUrl, setLocalUrl] = useState(url || '');
|
||||||
const [localDescription, setLocalDescription] = useState(description || '');
|
const [localDescription, setLocalDescription] = useState(description || '');
|
||||||
|
const { setToastApiError } = useToast();
|
||||||
const commonStyles = useCommonStyles();
|
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 (
|
return (
|
||||||
<Grid container style={{ marginTop: '1rem' }}>
|
<Grid container style={{ marginTop: '1rem' }}>
|
||||||
<Grid item sm={12} xs={12} className={commonStyles.contentSpacingY}>
|
<Grid item sm={12} xs={12} className={commonStyles.contentSpacingY}>
|
||||||
@ -38,13 +55,7 @@ const ApplicationUpdate = ({ application }: IApplicationUpdateProps) => {
|
|||||||
label="Icon"
|
label="Icon"
|
||||||
options={icons.map(v => ({ key: v, label: v }))}
|
options={icons.map(v => ({ key: v, label: v }))}
|
||||||
value={icon || 'apps'}
|
value={icon || 'apps'}
|
||||||
onChange={e =>
|
onChange={e => handleChange(e)}
|
||||||
storeApplicationMetaData(
|
|
||||||
appName,
|
|
||||||
'icon',
|
|
||||||
e.target.value as string
|
|
||||||
)
|
|
||||||
}
|
|
||||||
/>
|
/>
|
||||||
</Grid>
|
</Grid>
|
||||||
<Grid item>
|
<Grid item>
|
||||||
|
@ -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;
|
|
Loading…
Reference in New Issue
Block a user