Skip to content

Commit

Permalink
Implemented restore version view
Browse files Browse the repository at this point in the history
  • Loading branch information
rmathis committed May 21, 2021
1 parent 5796c10 commit a63a562
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 79 deletions.
4 changes: 4 additions & 0 deletions ui/src/app/metadata/Metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { MetadataXml } from './view/MetadataXml';
import { MetadataComparison } from './view/MetadataComparison';
import { MetadataVersion } from './view/MetadataVersion';
import { MetadataEdit } from './view/MetadataEdit';
import { MetadataRestore } from './view/MetadataRestore';

export function Metadata () {

Expand Down Expand Up @@ -50,6 +51,9 @@ export function Metadata () {
<Route path={`${path}/edit/:section`} render={ () =>
<MetadataEdit />
} />
<Route path={`${path}/restore/:versionId/:section`} render={ () =>
<MetadataRestore />
} />
<Redirect exact path={`${path}`} to={`${path}/configuration/options`} />
</Switch>
</MetadataSchema>
Expand Down
5 changes: 1 addition & 4 deletions ui/src/app/metadata/editor/MetadataEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,18 @@ import { NavLink } from 'react-router-dom';
import { useTranslator } from '../../i18n/hooks';
import API_BASE_PATH from '../../App.constant';

export function MetadataEditor () {
export function MetadataEditor ({ current }) {

const translator = useTranslator();

const { type, id, section } = useParams();

const current = useMetadataObject();

const { update, loading } = useMetadataUpdater(`${ API_BASE_PATH }${getMetadataPath(type)}`, current);

const { data } = useMetadataEntities(type, {}, []);
const history = useHistory();
const definition = React.useContext(MetadataDefinitionContext);
const schema = React.useContext(MetadataSchemaContext);


const { state, dispatch } = React.useContext(MetadataFormContext);
const { metadata, errors } = state;
Expand Down
9 changes: 0 additions & 9 deletions ui/src/app/metadata/hoc/MetadataSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { useMetadataEntity } from '../hooks/api';

export const MetadataTypeContext = React.createContext();
export const MetadataObjectContext = React.createContext();
export const MetadataLoaderContext = React.createContext();

/*eslint-disable react-hooks/exhaustive-deps*/
export function MetadataSelector({ children, ...props }) {
Expand Down Expand Up @@ -34,16 +33,12 @@ export function MetadataSelector({ children, ...props }) {
}
React.useEffect(() => { loadMetadata(id) }, [id]);

const update = () => loadMetadata(id);

return (
<>
{type &&
<MetadataTypeContext.Provider value={type}>
{metadata && metadata.version &&
<MetadataLoaderContext.Provider value={{ load: update }}>
<MetadataObjectContext.Provider value={metadata}>{children(metadata)}</MetadataObjectContext.Provider>
</MetadataLoaderContext.Provider>
}
</MetadataTypeContext.Provider>
}
Expand All @@ -55,8 +50,4 @@ export function useMetadataObject () {
return React.useContext(MetadataObjectContext);
}

export function useMetadataLoader () {
return React.useContext(MetadataLoaderContext);
}

export default MetadataSelector;
55 changes: 22 additions & 33 deletions ui/src/app/metadata/hoc/MetadataVersionLoader.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,38 @@
import React from 'react';
import { useParams } from 'react-router';
import { getMetadataPath } from '../hooks/api';
import API_BASE_PATH from '../../App.constant';
import useFetch from 'use-http';
import { last } from 'lodash';
import { useParams } from 'react-router-dom';
import { useMetadataEntity } from '../hooks/api';

export function MetadataVersionLoader ({versions, children}) {
export const MetadataVersionContext = React.createContext();

const ref = React.useRef({});
const [list, setList] = React.useState({});
const { Provider } = MetadataVersionContext;

const { type, id } = useParams();
export function MetadataVersionLoader({children}) {

const { get, response } = useFetch(`/${API_BASE_PATH}${getMetadataPath(type)}/${id}/Versions`, {
const { type, id, versionId } = useParams();

const [metadata, setMetadata] = React.useState();

const { get, response } = useMetadataEntity(type, {
cachePolicy: 'no-cache',
}, []);

async function loadVersion(v) {
const l = await get(`/${v}`);
const l = await get(`/${id}/Versions/${v}`);
if (response.ok) {
addToList(v, l);
if (last(versions) !== v) {
loadNext(versions[versions.indexOf(v) + 1]);
}
setMetadata(l);
}
}

function addToList(version, item) {
ref.current = {
...ref.current,
[version]: item
};
setList(ref.current);
}

function loadNext (v) {
loadVersion(v);
}

/*eslint-disable react-hooks/exhaustive-deps*/
React.useEffect(() => {
loadNext(versions[0]);
}, [versions]);
loadVersion(versionId);
}, [versionId]);

return (<React.Fragment>
{children(versions.map(v => list[v]).filter(v => !!v))}
</React.Fragment>);
}
return (
<>
{metadata &&
<Provider value={metadata}>{children(metadata)}</Provider>
}
</>
);
}
49 changes: 49 additions & 0 deletions ui/src/app/metadata/hoc/MetadataVersionsLoader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React from 'react';
import { useParams } from 'react-router';
import { getMetadataPath } from '../hooks/api';
import API_BASE_PATH from '../../App.constant';
import useFetch from 'use-http';
import { last } from 'lodash';

export function MetadataVersionsLoader ({versions, children}) {

const ref = React.useRef({});
const [list, setList] = React.useState({});

const { type, id } = useParams();

const { get, response } = useFetch(`/${API_BASE_PATH}${getMetadataPath(type)}/${id}/Versions`, {
cachePolicy: 'no-cache',
}, []);

async function loadVersion(v) {
const l = await get(`/${v}`);
if (response.ok) {
addToList(v, l);
if (last(versions) !== v) {
loadNext(versions[versions.indexOf(v) + 1]);
}
}
}

function addToList(version, item) {
ref.current = {
...ref.current,
[version]: item
};
setList(ref.current);
}

function loadNext (v) {
loadVersion(v);
}

/*eslint-disable react-hooks/exhaustive-deps*/
React.useEffect(() => {
loadNext(versions[0]);
}, [versions]);

return (<React.Fragment>
{children(versions.map(v => list[v]).filter(v => !!v))}
</React.Fragment>);
}
6 changes: 3 additions & 3 deletions ui/src/app/metadata/view/MetadataComparison.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
withDefault
} from 'use-query-params';
import { MetadataDefinitionContext, MetadataSchemaContext } from '../hoc/MetadataSchema';
import { MetadataVersionLoader } from '../hoc/MetadataVersionLoader';
import { MetadataVersionsLoader } from '../hoc/MetadataVersionsLoader';
import { Configuration } from '../hoc/Configuration';
import { MetadataConfiguration } from '../component/MetadataConfiguration';
import { Link, useParams } from 'react-router-dom';
Expand Down Expand Up @@ -40,7 +40,7 @@ export function MetadataComparison () {
&nbsp;Configuration
</h2>
{versions &&
<MetadataVersionLoader versions={versions}>
<MetadataVersionsLoader versions={versions}>
{(v) =>
<Configuration entities={v} schema={processed} definition={definition} limited={limited}>
{(config) =>
Expand Down Expand Up @@ -77,7 +77,7 @@ export function MetadataComparison () {
}
</Configuration>
}
</MetadataVersionLoader>
</MetadataVersionsLoader>
}
</>
);
Expand Down
2 changes: 1 addition & 1 deletion ui/src/app/metadata/view/MetadataEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function MetadataEdit() {

return (
<MetadataForm initial={base}>
<MetadataEditor />
<MetadataEditor current={base} />
</MetadataForm>
);
}
9 changes: 5 additions & 4 deletions ui/src/app/metadata/view/MetadataHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export function MetadataHistory () {

const history = useHistory();

const { data, loading } = useMetadataHistory(type, id, {}, []);
const { data, loading } = useMetadataHistory(type, id, {
cachePolicy: 'no-cache'
}, []);

const toggleVersionSelected = (version) => {
let s = [...selected];
Expand All @@ -34,7 +36,6 @@ export function MetadataHistory () {
}
setSelected(s);
};
const restore = () => {};
const compare = (versions) => {
const s = sortVersionsByDate(versions);
const path = `/metadata/${type}/${id}/configuration/compare?${queryString.stringify({versions: s.map(s => s.id)}, {
Expand Down Expand Up @@ -96,10 +97,10 @@ export function MetadataHistory () {
<td>{ version.creator }</td>
<td>
{i > 0 &&
<button className="btn btn-text btn-link" onClick={ () => restore(version) }>
<Link className="btn btn-text btn-link" to={`/metadata/${type}/${id}/restore/${version.id}/common`}>
<FontAwesomeIcon icon={faUndo} />&nbsp;
<Translate value="action.restore">Restore</Translate>
</button>
</Link>
}
</td>
</tr>
Expand Down
26 changes: 26 additions & 0 deletions ui/src/app/metadata/view/MetadataRestore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';
import { MetadataEditor } from '../editor/MetadataEditor';
import { MetadataForm } from '../hoc/MetadataFormContext';
import { useMetadataObject } from '../hoc/MetadataSelector';
import { MetadataVersionLoader } from '../hoc/MetadataVersionLoader';

export function MetadataRestore() {

const latest = useMetadataObject();

return (
<MetadataVersionLoader>
{(metadata) =>
<MetadataForm initial={{
...metadata,
version: latest.version
}}>
<MetadataEditor current={{
...metadata,
version: latest.version
}} />
</MetadataForm>
}
</MetadataVersionLoader>
);
}
30 changes: 5 additions & 25 deletions ui/src/app/metadata/view/MetadataVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,21 @@ import { faArrowUp, faHistory, faPlus } from '@fortawesome/free-solid-svg-icons'
import { scroller } from 'react-scroll';

import Translate from '../../i18n/components/translate';
import { useMetadataEntity } from '../hooks/api';
import { MetadataHeader } from '../component/MetadataHeader';
import { MetadataFilters } from '../domain/filter/component/MetadataFilters';
import { MetadataFilterConfigurationList } from '../domain/filter/component/MetadataFilterConfigurationList';
import { MetadataFilterTypes } from '../domain/filter';
import { useMetadataSchema } from '../hooks/schema';

import { MetadataVersionLoader } from '../hoc/MetadataVersionLoader';

export function MetadataVersion() {

const { type, id, versionId } = useParams();

const [metadata, setMetadata] = React.useState();
const { type, id } = useParams();

const schema = React.useContext(MetadataSchemaContext);
const definition = React.useContext(MetadataDefinitionContext);

const processed = useMetadataSchema(definition, schema);

const { get, response } = useMetadataEntity(type, {
cachePolicy: 'no-cache',
}, []);

async function loadVersion(v) {
const l = await get(`/${id}/Versions/${v}`);
if (response.ok) {
setMetadata(l);
}
}

/*eslint-disable react-hooks/exhaustive-deps*/
React.useEffect(() => {
loadVersion(versionId);
}, [versionId]);

const onScrollTo = (element, offset = 0) => {
scroller.scrollTo(element, {
duration: 500,
Expand All @@ -54,8 +34,8 @@ export function MetadataVersion() {
};

return (
<>
{metadata &&
<MetadataVersionLoader>
{(metadata) =>
<Configuration entities={[metadata]} schema={processed} definition={definition}>
{(config) =>
<>
Expand Down Expand Up @@ -108,6 +88,6 @@ export function MetadataVersion() {
}
</Configuration>
}
</>
</MetadataVersionLoader>
);
}

0 comments on commit a63a562

Please sign in to comment.