1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-06 00:07:44 +01:00
unleash.unleash/frontend/src/component/application/ApplicationEdit/ApplicationEdit.tsx

149 lines
5.2 KiB
TypeScript
Raw Normal View History

/* eslint react/no-multi-comp:off */
2022-02-09 16:15:07 +01:00
import { useContext, useState } from 'react';
import {
Avatar,
Link,
Icon,
IconButton,
LinearProgress,
Typography,
} from '@material-ui/core';
import { Link as LinkIcon } from '@material-ui/icons';
2022-02-09 16:15:07 +01:00
import ConditionallyRender from '../../common/ConditionallyRender/ConditionallyRender';
import { UPDATE_APPLICATION } from '../../providers/AccessProvider/permissions';
import { ApplicationView } from '../ApplicationView/ApplicationView';
import { ApplicationUpdate } from '../ApplicationUpdate/ApplicationUpdate';
2022-02-09 16:15:07 +01:00
import TabNav from '../../common/TabNav/TabNav';
import Dialogue from '../../common/Dialogue';
import PageContent from '../../common/PageContent';
import HeaderTitle from '../../common/HeaderTitle';
import AccessContext from '../../../contexts/AccessContext';
import useApplicationsApi from '../../../hooks/api/actions/useApplicationsApi/useApplicationsApi';
import useApplication from '../../../hooks/api/getters/useApplication/useApplication';
import { useHistory, useParams } from 'react-router-dom';
2022-02-09 16:15:07 +01:00
import { useLocationSettings } from '../../../hooks/useLocationSettings';
import useToast from '../../../hooks/useToast';
import PermissionButton from '../../common/PermissionButton/PermissionButton';
2022-02-25 10:40:55 +01:00
import { formatDateYMD } from '../../../utils/format-date';
export const ApplicationEdit = () => {
const history = useHistory();
const { name } = useParams<{ name: string }>();
2022-02-09 16:15:07 +01:00
const { application, loading } = useApplication(name);
const { appName, url, description, icon = 'apps', createdAt } = application;
const { hasAccess } = useContext(AccessContext);
const { deleteApplication } = useApplicationsApi();
const { locationSettings } = useLocationSettings();
2022-02-09 16:15:07 +01:00
const { setToastData, setToastApiError } = useToast();
const [showDialog, setShowDialog] = useState(false);
const toggleModal = () => {
setShowDialog(!showDialog);
};
2022-02-25 10:40:55 +01:00
const formatDate = (v: string) => formatDateYMD(v, locationSettings.locale);
const onDeleteApplication = async (evt: Event) => {
evt.preventDefault();
2022-02-09 16:15:07 +01:00
try {
await deleteApplication(appName);
setToastData({
title: 'Deleted Successfully',
text: 'Application deleted successfully',
type: 'success',
});
history.push('/applications');
} catch (e: any) {
setToastApiError(e.toString());
}
};
const renderModal = () => (
<Dialogue
open={showDialog}
onClose={toggleModal}
onClick={onDeleteApplication}
title="Are you sure you want to delete this application?"
/>
);
const tabData = [
{
label: 'Application overview',
component: <ApplicationView />,
},
{
label: 'Edit application',
component: <ApplicationUpdate application={application} />,
},
];
if (loading) {
return (
<div>
<p>Loading...</p>
<LinearProgress />
</div>
);
} else if (!application) {
return <p>Application ({appName}) not found</p>;
}
return (
<PageContent
headerContent={
<HeaderTitle
title={
<span
style={{
display: 'flex',
alignItems: 'center',
}}
>
<Avatar style={{ marginRight: '8px' }}>
<Icon>{icon || 'apps'}</Icon>
</Avatar>
{appName}
</span>
}
actions={
<>
<ConditionallyRender
condition={Boolean(url)}
show={
<IconButton component={Link} href={url}>
<LinkIcon />
</IconButton>
}
/>
2022-02-09 16:15:07 +01:00
<PermissionButton
title="Delete application"
onClick={toggleModal}
permission={UPDATE_APPLICATION}
>
Delete
</PermissionButton>
</>
}
/>
}
>
<div>
<Typography variant="body1">{description || ''}</Typography>
<Typography variant="body2">
Created: <strong>{formatDate(createdAt)}</strong>
</Typography>
</div>
<ConditionallyRender
condition={hasAccess(UPDATE_APPLICATION)}
show={
<div>
{renderModal()}
<TabNav tabData={tabData} />
</div>
}
/>
</PageContent>
);
};