-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
116 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters