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'; import { IApplication } from '../../../interfaces/application'; import useApplication from '../../../hooks/api/getters/useApplication/useApplication'; interface IApplicationUpdateProps { application: IApplication; } export const ApplicationUpdate = ({ application }: IApplicationUpdateProps) => { const { storeApplicationMetaData } = useApplicationsApi(); const { appName, icon, url, description } = application; const { refetchApplication } = useApplication(appName); const [localUrl, setLocalUrl] = useState(url || ''); const [localDescription, setLocalDescription] = useState(description || ''); const { setToastData, setToastApiError } = useToast(); const commonStyles = useCommonStyles(); const handleChange = async ( evt: ChangeEvent<{ name?: string | undefined; value: unknown }>, field: string, value: string ) => { evt.preventDefault(); try { await storeApplicationMetaData(appName, field, value); refetchApplication(); setToastData({ type: 'success', title: 'Updated Successfully', text: `${field} successfully updated`, }); } catch (e: any) { setToastApiError(e.toString()); } }; return ( ({ key: v, label: v }))} value={icon || 'apps'} onChange={e => handleChange(e, 'icon', e.target.value as string) } /> setLocalUrl(e.target.value)} label="Application URL" placeholder="https://example.com" type="url" variant="outlined" size="small" onBlur={e => handleChange(e, 'url', localUrl)} /> setLocalDescription(e.target.value)} onBlur={e => handleChange(e, 'description', localDescription) } /> ); };