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/application-edit-component.js

163 lines
6.0 KiB
JavaScript
Raw Normal View History

2016-12-09 14:02:36 +01:00
/* eslint react/no-multi-comp:off */
2020-09-24 20:32:38 +02:00
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
2016-12-05 15:15:01 +01:00
import { Avatar, Link, Icon, IconButton, Button, LinearProgress, Typography } from '@material-ui/core';
import ConditionallyRender from '../common/ConditionallyRender/ConditionallyRender';
2020-09-26 22:14:56 +02:00
import { formatFullDateTimeWithLocale, formatDateWithLocale } from '../common/util';
2020-09-24 20:32:38 +02:00
import { UPDATE_APPLICATION } from '../../permissions';
import ApplicationView from './application-view';
import ApplicationUpdate from './application-update';
import TabNav from '../common/TabNav/TabNav';
import Dialogue from '../common/Dialogue';
import PageContent from '../common/PageContent';
import HeaderTitle from '../common/HeaderTitle';
2016-12-09 14:02:36 +01:00
2016-12-13 20:54:53 +01:00
class ClientApplications extends PureComponent {
static propTypes = {
fetchApplication: PropTypes.func.isRequired,
appName: PropTypes.string,
application: PropTypes.object,
2018-02-08 11:23:07 +01:00
location: PropTypes.object,
storeApplicationMetaData: PropTypes.func.isRequired,
deleteApplication: PropTypes.func.isRequired,
hasPermission: PropTypes.func.isRequired,
history: PropTypes.object.isRequired,
};
constructor(props) {
2020-09-24 20:32:38 +02:00
super();
this.state = {
activeTab: 0,
loading: !props.application,
prompt: false,
};
2016-12-13 20:54:53 +01:00
}
componentDidMount() {
this.props.fetchApplication(this.props.appName).finally(() => this.setState({ loading: false }));
2016-12-05 15:15:01 +01:00
}
2020-09-24 20:32:38 +02:00
formatFullDateTime = v => formatFullDateTimeWithLocale(v, this.props.location.locale);
2020-09-26 22:14:56 +02:00
formatDate = v => formatDateWithLocale(v, this.props.location.locale);
deleteApplication = async evt => {
evt.preventDefault();
// if (window.confirm('Are you sure you want to remove this application?')) {
const { deleteApplication, appName } = this.props;
await deleteApplication(appName);
this.props.history.push('/applications');
// }
};
render() {
if (this.state.loading) {
return (
<div>
<p>Loading...</p>
<LinearProgress />
</div>
);
} else if (!this.props.application) {
return <p>Application ({this.props.appName}) not found</p>;
2016-12-05 15:15:01 +01:00
}
const { application, storeApplicationMetaData, hasPermission } = this.props;
2020-09-26 22:14:56 +02:00
const { appName, instances, strategies, seenToggles, url, description, icon = 'apps', createdAt } = application;
const toggleModal = () => {
this.setState(prev => ({ ...prev, prompt: !prev.prompt }));
};
const renderModal = () => (
<Dialogue
open={this.state.prompt}
onClose={toggleModal}
onClick={this.deleteApplication}
title="Are you sure you want to delete this application?"
/>
);
const tabData = [
{
label: 'Application overview',
component: (
<ApplicationView
strategies={strategies}
instances={instances}
seenToggles={seenToggles}
hasPermission={hasPermission}
formatFullDateTime={this.formatFullDateTime}
/>
),
},
{
label: 'Edit application',
component: (
<ApplicationUpdate application={application} storeApplicationMetaData={storeApplicationMetaData} />
),
},
];
2016-12-13 20:54:53 +01:00
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={url}
show={
<IconButton component={Link} href={url}>
<Icon>link</Icon>
</IconButton>
}
/>
<ConditionallyRender
condition={hasPermission(UPDATE_APPLICATION)}
show={
<Button color="secondary" title="Delete application" onClick={toggleModal}>
Delete
</Button>
}
/>
</>
}
/>
}
>
<div>
<Typography variant="body1">{description || ''}</Typography>
<Typography variant="body2">
2020-09-26 22:14:56 +02:00
Created: <strong>{this.formatDate(createdAt)}</strong>
</Typography>
</div>
<ConditionallyRender
condition={hasPermission(UPDATE_APPLICATION)}
show={
<div>
{renderModal()}
2016-12-09 14:02:36 +01:00
<TabNav tabData={tabData} />
</div>
}
/>
</PageContent>
2016-12-05 15:15:01 +01:00
);
}
}
2016-12-13 20:54:53 +01:00
export default ClientApplications;