2022-02-09 19:09:02 +01:00
|
|
|
import { ChangeEvent, useState } from 'react';
|
2022-02-25 10:55:39 +01:00
|
|
|
import { Grid, TextField } from '@material-ui/core';
|
2022-03-28 10:49:59 +02:00
|
|
|
import { useCommonStyles } from 'common.styles';
|
2022-03-25 12:34:20 +01:00
|
|
|
import icons from 'component/application/iconNames';
|
2022-03-28 10:49:59 +02:00
|
|
|
import GeneralSelect from 'component/common/GeneralSelect/GeneralSelect';
|
|
|
|
import useApplicationsApi from 'hooks/api/actions/useApplicationsApi/useApplicationsApi';
|
|
|
|
import useToast from 'hooks/useToast';
|
|
|
|
import { IApplication } from 'interfaces/application';
|
|
|
|
import useApplication from 'hooks/api/getters/useApplication/useApplication';
|
2022-03-25 12:34:20 +01:00
|
|
|
import { formatUnknownError } from 'utils/formatUnknownError';
|
2022-02-10 09:33:09 +01:00
|
|
|
|
2022-02-08 16:38:08 +01:00
|
|
|
interface IApplicationUpdateProps {
|
|
|
|
application: IApplication;
|
|
|
|
}
|
|
|
|
|
2022-02-10 10:36:53 +01:00
|
|
|
export const ApplicationUpdate = ({ application }: IApplicationUpdateProps) => {
|
2022-02-04 12:45:08 +01:00
|
|
|
const { storeApplicationMetaData } = useApplicationsApi();
|
|
|
|
const { appName, icon, url, description } = application;
|
2022-02-11 15:07:58 +01:00
|
|
|
const { refetchApplication } = useApplication(appName);
|
2022-02-04 16:26:51 +01:00
|
|
|
const [localUrl, setLocalUrl] = useState(url || '');
|
|
|
|
const [localDescription, setLocalDescription] = useState(description || '');
|
2022-02-10 11:02:53 +01:00
|
|
|
const { setToastData, setToastApiError } = useToast();
|
2022-02-04 12:45:08 +01:00
|
|
|
const commonStyles = useCommonStyles();
|
|
|
|
|
2022-02-11 15:07:58 +01:00
|
|
|
const handleChange = async (
|
2022-02-10 11:02:53 +01:00
|
|
|
evt: ChangeEvent<{ name?: string | undefined; value: unknown }>,
|
|
|
|
field: string,
|
|
|
|
value: string
|
2022-02-09 19:09:02 +01:00
|
|
|
) => {
|
|
|
|
evt.preventDefault();
|
|
|
|
try {
|
2022-02-11 15:07:58 +01:00
|
|
|
await storeApplicationMetaData(appName, field, value);
|
|
|
|
refetchApplication();
|
2022-02-10 11:02:53 +01:00
|
|
|
setToastData({
|
|
|
|
type: 'success',
|
|
|
|
title: 'Updated Successfully',
|
|
|
|
text: `${field} successfully updated`,
|
|
|
|
});
|
2022-02-25 10:55:39 +01:00
|
|
|
} catch (error: unknown) {
|
|
|
|
setToastApiError(formatUnknownError(error));
|
2022-02-09 19:09:02 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-04 12:45:08 +01:00
|
|
|
return (
|
|
|
|
<Grid container style={{ marginTop: '1rem' }}>
|
|
|
|
<Grid item sm={12} xs={12} className={commonStyles.contentSpacingY}>
|
|
|
|
<Grid item>
|
|
|
|
<GeneralSelect
|
|
|
|
name="iconSelect"
|
|
|
|
id="selectIcon"
|
|
|
|
label="Icon"
|
|
|
|
options={icons.map(v => ({ key: v, label: v }))}
|
|
|
|
value={icon || 'apps'}
|
2022-02-10 11:02:53 +01:00
|
|
|
onChange={e =>
|
|
|
|
handleChange(e, 'icon', e.target.value as string)
|
|
|
|
}
|
2022-02-04 12:45:08 +01:00
|
|
|
/>
|
|
|
|
</Grid>
|
|
|
|
<Grid item>
|
|
|
|
<TextField
|
|
|
|
value={localUrl}
|
|
|
|
onChange={e => setLocalUrl(e.target.value)}
|
|
|
|
label="Application URL"
|
|
|
|
placeholder="https://example.com"
|
|
|
|
type="url"
|
|
|
|
variant="outlined"
|
|
|
|
size="small"
|
2022-02-10 11:02:53 +01:00
|
|
|
onBlur={e => handleChange(e, 'url', localUrl)}
|
2022-02-04 12:45:08 +01:00
|
|
|
/>
|
|
|
|
</Grid>
|
|
|
|
<Grid item>
|
|
|
|
<TextField
|
|
|
|
value={localDescription}
|
|
|
|
label="Description"
|
|
|
|
variant="outlined"
|
|
|
|
size="small"
|
|
|
|
rows={2}
|
|
|
|
onChange={e => setLocalDescription(e.target.value)}
|
2022-02-10 11:02:53 +01:00
|
|
|
onBlur={e =>
|
|
|
|
handleChange(e, 'description', localDescription)
|
2022-02-04 12:45:08 +01:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
</Grid>
|
|
|
|
</Grid>
|
|
|
|
</Grid>
|
|
|
|
);
|
|
|
|
};
|