Skip to content

Commit

Permalink
Merged develop into feature/shibui-2394
Browse files Browse the repository at this point in the history
  • Loading branch information
chasegawa committed Oct 27, 2022
2 parents 4ef8d52 + dc09d13 commit 4ae88e1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
10 changes: 10 additions & 0 deletions ui/src/app/metadata/copy/CopySource.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ import { EntityTypeahead } from './EntityTypeahead';
import kebabCase from 'lodash/kebabCase';
import { useMetadataSources } from '../hooks/api';
import { useMetadataSourceSections } from '../domain/source/definition/sections';
import { useMetadataSchemaLoader, useMetadataSchemaType } from '../hoc/MetadataSchema';

export function CopySource({ copy, onNext }) {

const schemaLoader = useMetadataSchemaLoader();
const kind = useMetadataSchemaType();

const { data = [] } = useMetadataSources({ cachePolicy: 'no-cache' }, []);

const [selected, setSelected] = React.useState(copy.properties);
Expand Down Expand Up @@ -57,6 +61,12 @@ export function CopySource({ copy, onNext }) {

const sections = useMetadataSourceSections();

React.useEffect(() => {
if (target && target.protocol !== kind) {
schemaLoader(target.protocol);
}
}, [target, schemaLoader, kind]);

return (
<>
<div className="row">
Expand Down
38 changes: 28 additions & 10 deletions ui/src/app/metadata/hoc/MetadataSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,50 @@ import { useTranslator } from '../../i18n/hooks';
export const MetadataSchemaContext = React.createContext();
export const MetadataDefinitionContext = React.createContext();
export const MetadataSchemaLoading = React.createContext();
export const MetadataSchemaLoader = React.createContext();
export const MetadataSchemaType = React.createContext();

export function MetadataSchema({ type, children, wizard = false }) {

const definition = React.useMemo(() => wizard ? getWizard(type) : getDefinition(type), [type, wizard]);
const [kind, setKind] = React.useState(type);

const [loading, setLoading] = React.useState(false);

const [definition, setDefinition] = React.useState(wizard ? getWizard(kind) : getDefinition(kind));

const { get, response } = useFetch(``, {
cachePolicy: 'no-cache'
});

const [schema, setSchema] = React.useState();

async function loadSchema(d) {
const source = await get(`/${d.schema}`)
async function loadSchema(type) {
const definition = wizard ? getWizard(type) : getDefinition(type);
setDefinition(definition);
setKind(type);
setLoading(true);

const source = await get(`/${definition.schema}`)
if (response.ok) {
setSchema(source);
}
setLoading(false);
}

/*eslint-disable react-hooks/exhaustive-deps*/
React.useEffect(() => {
setSchema(null);
loadSchema(definition);
setLoading(true);
}, [definition]);
React.useState(() => {
loadSchema(type);
}, [type]);

return (
<MetadataDefinitionContext.Provider value={definition}>
{type && definition && schema &&
<MetadataSchemaContext.Provider value={ schema }>
<MetadataSchemaLoading.Provider value={ loading }>
{children}
<MetadataSchemaLoader.Provider value={ loadSchema }>
<MetadataSchemaType.Provider value={ kind }>
{children}
</MetadataSchemaType.Provider>
</MetadataSchemaLoader.Provider>
</MetadataSchemaLoading.Provider>
</MetadataSchemaContext.Provider>
}
Expand All @@ -55,10 +65,18 @@ export function useMetadataSchemaLoading () {
return React.useContext(MetadataSchemaLoading);
}

export function useMetadataSchemaLoader () {
return React.useContext(MetadataSchemaLoader);
}

export function useMetadataDefinitionContext() {
return React.useContext(MetadataDefinitionContext);
}

export function useMetadataSchemaType() {
return React.useContext(MetadataSchemaType);
}

export function useMetadataDefinitionValidator(data, current, group) {
const definition = useMetadataDefinitionContext();
const translator = useTranslator();
Expand Down

0 comments on commit 4ae88e1

Please sign in to comment.