2024-03-18 13:58:05 +01:00
|
|
|
import { type ChangeEvent, useMemo, useState } from 'react';
|
2023-12-27 18:35:08 +01:00
|
|
|
import { Grid, TextField, styled } from '@mui/material';
|
2022-05-02 15:52:41 +02:00
|
|
|
import { useThemeStyles } from 'themes/themeStyles';
|
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';
|
2024-03-18 13:58:05 +01:00
|
|
|
import type { IApplication } from 'interfaces/application';
|
2022-03-28 10:49:59 +02:00
|
|
|
import useApplication from 'hooks/api/getters/useApplication/useApplication';
|
2022-03-25 12:34:20 +01:00
|
|
|
import { formatUnknownError } from 'utils/formatUnknownError';
|
2023-12-27 18:35:08 +01:00
|
|
|
import { HelpIcon } from 'component/common/HelpIcon/HelpIcon';
|
2022-02-10 09:33:09 +01:00
|
|
|
|
2022-02-08 16:38:08 +01:00
|
|
|
interface IApplicationUpdateProps {
|
|
|
|
application: IApplication;
|
|
|
|
}
|
|
|
|
|
2023-12-27 18:35:08 +01:00
|
|
|
const StyledSelectContainer = styled('div')(({ theme }) => ({
|
|
|
|
display: 'flex',
|
|
|
|
alignItems: 'center',
|
|
|
|
gap: theme.spacing(1),
|
|
|
|
}));
|
|
|
|
|
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-05-02 15:52:41 +02:00
|
|
|
const { classes: themeStyles } = useThemeStyles();
|
2022-02-04 12:45:08 +01:00
|
|
|
|
2022-04-20 11:47:17 +02:00
|
|
|
const onChange = async (
|
2022-02-10 11:02:53 +01:00
|
|
|
field: string,
|
2022-04-20 11:47:17 +02:00
|
|
|
value: string,
|
2023-10-02 14:25:46 +02:00
|
|
|
event?: ChangeEvent,
|
2022-02-09 19:09:02 +01:00
|
|
|
) => {
|
2022-04-20 11:47:17 +02:00
|
|
|
event?.preventDefault();
|
2022-02-09 19:09:02 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-12-27 18:35:08 +01:00
|
|
|
const options = useMemo(() => icons.map((v) => ({ key: v, label: v })), []);
|
|
|
|
|
2022-02-04 12:45:08 +01:00
|
|
|
return (
|
|
|
|
<Grid container style={{ marginTop: '1rem' }}>
|
2022-05-02 15:52:41 +02:00
|
|
|
<Grid item sm={12} xs={12} className={themeStyles.contentSpacingY}>
|
2022-02-04 12:45:08 +01:00
|
|
|
<Grid item>
|
2023-12-27 18:35:08 +01:00
|
|
|
<StyledSelectContainer>
|
|
|
|
<GeneralSelect
|
|
|
|
name='iconSelect'
|
|
|
|
id='selectIcon'
|
|
|
|
label='Icon'
|
|
|
|
options={options}
|
|
|
|
value={icon || 'apps'}
|
|
|
|
onChange={(key) => onChange('icon', key)}
|
|
|
|
/>
|
|
|
|
<HelpIcon
|
|
|
|
htmlTooltip
|
|
|
|
tooltip={
|
|
|
|
<>
|
|
|
|
<p>Unleash is using Material Icons</p>
|
|
|
|
<br />
|
|
|
|
<a
|
|
|
|
href='https://mui.com/material-ui/material-icons/'
|
|
|
|
target='_blank'
|
|
|
|
rel='noreferrer'
|
|
|
|
>
|
|
|
|
Preview icons on MUI.com
|
|
|
|
</a>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</StyledSelectContainer>
|
2022-02-04 12:45:08 +01:00
|
|
|
</Grid>
|
|
|
|
<Grid item>
|
|
|
|
<TextField
|
|
|
|
value={localUrl}
|
2023-10-02 14:25:46 +02:00
|
|
|
onChange={(e) => setLocalUrl(e.target.value)}
|
|
|
|
label='Application URL'
|
|
|
|
placeholder='https://example.com'
|
|
|
|
type='url'
|
|
|
|
variant='outlined'
|
|
|
|
size='small'
|
|
|
|
onBlur={(e) => onChange('url', localUrl, e)}
|
2022-02-04 12:45:08 +01:00
|
|
|
/>
|
|
|
|
</Grid>
|
|
|
|
<Grid item>
|
|
|
|
<TextField
|
|
|
|
value={localDescription}
|
2023-10-02 14:25:46 +02:00
|
|
|
label='Description'
|
|
|
|
variant='outlined'
|
|
|
|
size='small'
|
2022-02-04 12:45:08 +01:00
|
|
|
rows={2}
|
2023-10-02 14:25:46 +02:00
|
|
|
onChange={(e) => setLocalDescription(e.target.value)}
|
|
|
|
onBlur={(e) =>
|
2022-04-20 11:47:17 +02:00
|
|
|
onChange('description', localDescription, e)
|
2022-02-04 12:45:08 +01:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
</Grid>
|
|
|
|
</Grid>
|
|
|
|
</Grid>
|
|
|
|
);
|
|
|
|
};
|