Skip to content

Commit

Permalink
source version retire/release
Browse files Browse the repository at this point in the history
  • Loading branch information
snyaggarwal committed Jan 14, 2021
1 parent 3070260 commit ba165b3
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 23 deletions.
75 changes: 57 additions & 18 deletions src/components/common/ConceptContainerVersionList.jsx
Expand Up @@ -4,12 +4,14 @@ import {
Accordion, AccordionSummary, AccordionDetails, Typography, Divider, Tooltip,
IconButton,
} from '@material-ui/core';
import { map, isEmpty, startCase, get } from 'lodash';
import { map, isEmpty, startCase, get, includes, merge } from 'lodash';
import {
ExpandMore as ExpandMoreIcon, Search as SearchIcon, Edit as EditIcon, Delete as DeleteIcon
ExpandMore as ExpandMoreIcon, Search as SearchIcon, Edit as EditIcon,
DeleteForever as DeleteIcon,
NewReleases as ReleaseIcon, Delete as RetireIcon, FileCopy as CopyIcon,
} from '@material-ui/icons';
import APIService from '../../services/APIService';
import { headFirst } from '../../common/utils';
import { headFirst, copyURL, toFullAPIURL } from '../../common/utils';
import LastUpdatedOnLabel from './LastUpdatedOnLabel';
import ResourceVersionLabel from './ResourceVersionLabel';
import ConceptContainerTip from './ConceptContainerTip';
Expand All @@ -27,33 +29,55 @@ const None = () => {
return <div style={{margin: '5px', fontWeight: '300'}}>None</div>
}

const ConceptContainerVersionList = ({ versions, resource, canEdit }) => {
let sortedVersions = headFirst(versions);
const onCopyClick = version => copyURL(toFullAPIURL(version.version_url));
const handleResponse = (response, resource, action, successCallback) => {
if(includes([200, 204], get(response, 'status')))
alertifyjs.success(`${resource} Version ${action}`, 1, () => {
if(successCallback)
successCallback(response.data)
else
window.location.reload()
})
else
alertifyjs.error('Something bad happened!')
}
const getService = version => APIService.new().overrideURL(version.version_url)
const deleteVersion = version => getService(version).delete().then(response => handleResponse(response, version.type, 'Deleted'))
const updateVersion = (version, data, verb, successCallback) => getService(version).put(data).then(response => handleResponse(response, version.type, verb, updatedVersion => successCallback(merge(version, updatedVersion))))

const ConceptContainerVersionList = ({ versions, resource, canEdit, onUpdate }) => {
const sortedVersions = headFirst(versions);
const [versionForm, setVersionForm] = React.useState(false);
const [selectedVersion, setSelectedVersion] = React.useState();
const onEditClick = version => {
setSelectedVersion(version)
setVersionForm(true)
}
const deleteVersion = version => {
APIService.new().overrideURL(version.version_url).delete().then(response => {
if(get(response, 'status') === 204)
alertifyjs.success('Source Version Deleted', 1, () => window.location.reload())
else
alertifyjs.error('Something bad happened!')
})
}
const onEditCancel = () => setVersionForm(false);
const onReleaseClick = version => onVersionUpdate(version, 'released', 'release', 'Released')
const onRetireClick = version => onVersionUpdate(version, 'retired', 'retire', 'Retired')
const onDeleteClick = version => {
const title = `Delete Source Version : ${version.short_code} [${version.id}]`;
const message = `Are you sure you want to permanently delete this source version ${version.id}? This action cannot be undone! This will delete the version and all of its details. Concepts and mappings in this source version will not be affected.`
const title = `Delete ${startCase(resource)} Version : ${version.short_code} [${version.id}]`;
const message = `Are you sure you want to permanently delete this ${resource} version ${version.id}? This action cannot be undone! This will delete the version and all of its details. Concepts and mappings in this ${resource} version will not be affected.`

handleOnClick(title, message, () => deleteVersion(version))
}
const handleOnClick = (title, message, onOk) => {
const confirm = alertifyjs.confirm()
confirm.setHeader(title);
confirm.setMessage(message);
confirm.set('onok', () => deleteVersion(version));
confirm.set('onok', onOk);
confirm.show();
}
const onVersionUpdate = (version, attr, label, successLabel) => {
const newValue = !get(version, attr)
label = newValue ? label : `un-${label}`;
const resLabel = newValue ? successLabel : `Un${successLabel}`
const title = `Update ${startCase(resource)} Version : ${version.short_code} [${version.id}]`;
const message = `Are you sure you want to ${label} this ${resource} version ${version.id}?`

handleOnClick(title, message, () => updateVersion(version, {[attr]: newValue}, resLabel, onUpdate))
}

return (
<div className='col-md-12'>
Expand All @@ -73,7 +97,7 @@ const ConceptContainerVersionList = ({ versions, resource, canEdit }) => {
map(sortedVersions, (version, index) => (
<div className='col-md-12 no-side-padding' key={index}>
<div className='col-md-12 no-side-padding flex-vertical-center' style={{margin: '10px 0'}}>
<div className='col-md-9 no-left-padding'>
<div className='col-md-6 no-left-padding'>
<div className='col-md-12 no-side-padding' style={{marginBottom: '5px'}}>
<ResourceVersionLabel {...version} />
</div>
Expand All @@ -88,7 +112,7 @@ const ConceptContainerVersionList = ({ versions, resource, canEdit }) => {
/>
</div>
</div>
<div className='col-md-3 no-right-padding' style={{textAlign: 'right'}}>
<div className='col-md-6 no-right-padding' style={{textAlign: 'right'}}>
{
canEdit && version.id.toLowerCase() !== 'head' &&
<React.Fragment>
Expand All @@ -97,6 +121,16 @@ const ConceptContainerVersionList = ({ versions, resource, canEdit }) => {
<EditIcon fontSize='inherit' />
</IconButton>
</Tooltip>
<Tooltip title={version.released ? 'UnRelease Version' : 'Release Version'}>
<IconButton color={version.released ? 'primary' : 'default' } onClick={() => onReleaseClick(version)}>
<ReleaseIcon fontSize='inherit' />
</IconButton>
</Tooltip>
<Tooltip title={version.retired ? 'UnRetire Version' : 'Retire Version'}>
<IconButton color={version.retired ? 'primary' : 'default' } onClick={() => onRetireClick(version)}>
<RetireIcon fontSize='inherit' />
</IconButton>
</Tooltip>
<Tooltip title='Delete Version'>
<IconButton onClick={() => onDeleteClick(version)}>
<DeleteIcon fontSize='inherit' />
Expand All @@ -109,6 +143,11 @@ const ConceptContainerVersionList = ({ versions, resource, canEdit }) => {
<SearchIcon fontSize='inherit' />
</IconButton>
</Tooltip>
<Tooltip title='Copy URL'>
<IconButton onClick={() => onCopyClick(version)}>
<CopyIcon fontSize='inherit' />
</IconButton>
</Tooltip>
</div>
</div>
{
Expand Down
7 changes: 7 additions & 0 deletions src/components/common/ResourceVersionLabel.jsx
Expand Up @@ -3,6 +3,7 @@ import {
AccountTreeRounded as TreeIcon
} from '@material-ui/icons';
import ReleasedChip from './ReleasedChip';
import RetiredChip from './RetiredChip';

const SEPARATOR = '/'
const ResourceVersionLabel = props => {
Expand All @@ -24,6 +25,12 @@ const ResourceVersionLabel = props => {
<ReleasedChip size='small' />
</span>
}
{
props.retired &&
<span style={{marginLeft: '10px'}}>
<RetiredChip size='small' />
</span>
}
</div>
)
}
Expand Down
16 changes: 16 additions & 0 deletions src/components/common/RetiredChip.jsx
@@ -0,0 +1,16 @@
import React from 'react';
import { Chip, Tooltip } from '@material-ui/core';
import {Delete as RetireIcon} from '@material-ui/icons';
import { RED } from '../../common/constants';

const RetiredChip = props => {
const icon = <RetireIcon fontSize='inherit' style={{color: RED}} />;

return (
<Tooltip placement='top-start' title='Retired'>
<Chip icon={icon} label='Retired' variant='outlined' style={{color: RED, border: `1px solid ${RED}`}} size={props.size || 'medium'}/>
</Tooltip>
)
}

export default RetiredChip;
10 changes: 9 additions & 1 deletion src/components/sources/SourceHome.jsx
@@ -1,6 +1,6 @@
import React from 'react';
import { CircularProgress } from '@material-ui/core';
import { includes, isEmpty, get } from 'lodash';
import { includes, isEmpty, get, findIndex } from 'lodash';
import APIService from '../../services/APIService';
import SourceHomeHeader from './SourceHomeHeader';
import SourceHomeTabs from './SourceHomeTabs';
Expand Down Expand Up @@ -117,6 +117,13 @@ class SourceHome extends React.Component {
return !isEmpty(get(this, 'state.source.extras.about'));
}

onVersionUpdate = updatedVersion => {
const newState = {...this.state}
const index = findIndex(newState.versions, {uuid: updatedVersion.uuid})
newState.versions.splice(index, 1, updatedVersion)
this.setState(newState)
}

render() {
const { source, versions, isLoading, tab } = this.state;
const currentURL = this.getURLFromPath()
Expand All @@ -143,6 +150,7 @@ class SourceHome extends React.Component {
versionedObjectURL={versionedObjectURL}
currentVersion={this.getCurrentVersion()}
aboutTab={showAboutTab}
onVersionUpdate={this.onVersionUpdate}
/>
</div>
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/sources/SourceHomeTabs.jsx
Expand Up @@ -14,7 +14,7 @@ import SourceVersionForm from './SourceVersionForm';
const SourceHomeTabs = props => {
const {
tab, onChange, source, versions, location, versionedObjectURL, currentVersion,
aboutTab
aboutTab, onVersionUpdate
} = props;
const about = get(source, 'extras.about')
const [conceptForm, setConceptForm] = React.useState(false);
Expand Down Expand Up @@ -70,7 +70,7 @@ const SourceHomeTabs = props => {
}
{
tab === 2 &&
<ConceptContainerVersionList versions={versions} resource='source' canEdit={hasAccess} />
<ConceptContainerVersionList versions={versions} resource='source' canEdit={hasAccess} onUpdate={onVersionUpdate} />
}
{
aboutTab && tab === 3 &&
Expand Down
4 changes: 2 additions & 2 deletions src/components/sources/SourceVersionForm.jsx
Expand Up @@ -23,10 +23,10 @@ class SourceVersionForm extends React.Component {
}

setFieldsForEdit() {
const { source } = this.props;
const { version } = this.props;
const attrs = ['id', 'description', 'external_id']
const newState = {...this.state}
attrs.forEach(attr => set(newState.fields, attr, get(source, attr, '') || ''))
attrs.forEach(attr => set(newState.fields, attr, get(version, attr, '') || ''))
this.setState(newState);
}

Expand Down

0 comments on commit ba165b3

Please sign in to comment.