Skip to content

Commit

Permalink
[refactor] GH-5 Move ViewDescr here, add aldkg ns
Browse files Browse the repository at this point in the history
  • Loading branch information
amivanoff committed Aug 8, 2021
1 parent 2dd796c commit 81e421d
Show file tree
Hide file tree
Showing 11 changed files with 470 additions and 124 deletions.
7 changes: 4 additions & 3 deletions src/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,13 @@ export const Form: React.FC<FormsInitStateProps> = observer<FormsInitStateProps>
//const collSS = getSnapshot(coll);
const views = coll?.data;
const viewObs: any = coll?.dataByIri(viewIri);

if (!viewObs) {
return <Spin />;
}
const view = getSnapshot(viewObs);
const viewElement = view;
return (
<ErrorBoundary FallbackComponent={ErrorFallback} onReset={() => {}}>
<FormsDispatch {...props} view={view} viewElement={viewElement} />
<FormsDispatch {...props} view={view} viewElement={view} />
</ErrorBoundary>
);
});
28 changes: 28 additions & 0 deletions src/stores/ViewCollConstrs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/********************************************************************************
* Copyright (c) 2021 Agentlab and others.
*
* This program and the accompanying materials are made available under the
* terms of the GNU General Public License v. 3.0 which is available at
* https://www.gnu.org/licenses/gpl-3.0.html.
*
* SPDX-License-Identifier: GPL-3.0-only
********************************************************************************/
export const viewDescrCollConstr = {
'@id': 'rm:Views_Coll',
entConstrs: [
{
'@id': 'rm:Views_EntConstr0',
schema: 'rm:ViewShape',
},
],
};

export const viewKindCollConstr = {
'@id': 'aldkg:ViewKinds_Coll',
entConstrs: [
{
'@id': 'aldkg:ViewKinds_EntConstr0',
schema: 'aldkg:ViewKindShape',
},
],
};
88 changes: 88 additions & 0 deletions src/stores/ViewDescr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/********************************************************************************
* Copyright (c) 2020 Agentlab and others.
*
* This program and the accompanying materials are made available under the
* terms of the GNU General Public License v. 3.0 which is available at
* https://www.gnu.org/licenses/gpl-3.0.html.
*
* SPDX-License-Identifier: GPL-3.0-only
********************************************************************************/
import { reaction } from 'mobx';
import { getParent, getRoot, IAnyModelType, IAnyStateTreeNode, SnapshotIn, types } from 'mobx-state-tree';

import { arrDiff, CollConstr, JsObject } from '@agentlab/sparql-jsld-client';

export const ViewElement = types.model('ViewElement', {
'@id': types.maybe(types.string), // JSON-LD object id of a view
'@type': types.maybe(types.string), // JSON-LD class id of a View
title: types.maybe(types.string),
description: types.maybe(types.string),
viewKind: types.maybe(types.string),

type: types.string,
scope: types.maybe(types.string),
resultsScope: types.maybe(types.string),
options: types.maybe(types.frozen<JsObject>()),

// Container-specific (e.g. Layout, type: 'xxxLayout')
elements: types.maybe(types.array(types.late((): IAnyModelType => ViewElement))),
});

/**
* View Description, which could be persisted in DB
*/
export const ViewDescr = types
.model('ViewDescr', {
'@id': types.identifier, // JSON-LD object id of a view
'@type': types.string, // JSON-LD class id of a View
viewKind: types.maybe(types.string),

title: types.maybe(types.string), // mandatory title
description: types.maybe(types.string),

type: types.string,
scope: types.maybe(types.string),
resultsScope: types.maybe(types.string),
options: types.maybe(types.frozen<JsObject>()),

// Container-specific (e.g. Layout, type: 'xxxLayout')
elements: types.array(ViewElement),

collsConstrs: types.array(CollConstr), // former 'queries'
})
.actions((self) => {
const rep: IAnyStateTreeNode = getRoot(self);
const coll: IAnyStateTreeNode = getParent(self, 2);
let disp: any;
return {
afterAttach() {
console.log('ViewDescr afterAttach, @id=', self['@id']);
if (coll.resolveCollConstrs) {
disp = reaction(
() => self.collsConstrs,
(newArr: any[], oldArr: any[]) => {
console.log('ViewDescr reaction, add coll ref, @id=', self['@id']);
const { deleted, added } = arrDiff(newArr, oldArr);
console.log('ViewDescr reaction, add coll ref, {deleted,added}=', { deleted, added });
deleted.forEach((e: any) => rep.colls.delete(e['@id']));
added.forEach((e: any) => rep.addCollByConstrRef(e));
},
{ fireImmediately: true },
);
}
},
beforeDetach() {
console.log('ViewDescr beforeDetach, @id=', self['@id']);
if (coll.resolveCollConstrs) {
if (disp) disp();
self.collsConstrs.forEach((e) => rep.colls.delete(e['@id']));
}
},
setCollConstrs(collsConstrs: any[]) {
const ccso = collsConstrs.map((cc) => CollConstr.create(cc));
self.collsConstrs.push(...ccso);
},
};
});

export type IViewDescrSnapshotIn = SnapshotIn<typeof ViewDescr>;
62 changes: 62 additions & 0 deletions src/stores/ViewShapeSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/********************************************************************************
* Copyright (c) 2020 Agentlab and others.
*
* This program and the accompanying materials are made available under the
* terms of the GNU General Public License v. 3.0 which is available at
* https://www.gnu.org/licenses/gpl-3.0.html.
*
* SPDX-License-Identifier: GPL-3.0-only
********************************************************************************/
import { JSONSchema6forRdf } from '@agentlab/sparql-jsld-client';

export const ViewShapeSchema: JSONSchema6forRdf = {
$schema: 'http://json-schema.org/draft-07/schema#',
'@id': 'rm:ViewShape',
'@type': 'sh:NodeShape',
title: 'View Shape',
description: 'Artifact Shape',
targetClass: 'aldkg:ViewDescr',
type: 'object',
'@context': {
'@type': 'rdf:type',
},
properties: {
'@id': {
title: 'URI',
type: 'string',
format: 'iri',
},
'@type': {
title: 'Тип',
type: 'string',
format: 'iri',
},
},
required: ['@id', '@type'],
};

export const ViewKindShapeSchema: JSONSchema6forRdf = {
$schema: 'http://json-schema.org/draft-07/schema#',
'@id': 'rm:ViewShape',
'@type': 'sh:NodeShape',
title: 'View Shape',
description: 'Artifact Shape',
targetClass: 'aldkg:ViewDescr',
type: 'object',
'@context': {
'@type': 'rdf:type',
},
properties: {
'@id': {
title: 'URI',
type: 'string',
format: 'iri',
},
'@type': {
title: 'Тип',
type: 'string',
format: 'iri',
},
},
required: ['@id', '@type'],
};
8 changes: 4 additions & 4 deletions src/util/ContextToProps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ export interface ButtonComponent {

export const withStoreToControlProps = (Component: React.FC<ControlComponent>): React.FC<ToControlProps> =>
observer<ToControlProps>((props) => {
const succesValidation = {
validateStatus: 'succes',
const successValidation = {
validateStatus: 'success',
};
const { form, viewElement } = props;
const id = viewElement.resultsScope;
const [validateObj, setValidateObj] = useState<{
validateStatus: string;
help?: string;
}>(succesValidation);
}>(successValidation);
const [req] = id?.split('/') || [];
const [testReq, testUri] = viewElement.resultsScope?.split('/') || [];
const { store } = useContext(MstContext);
Expand All @@ -86,7 +86,7 @@ export const withStoreToControlProps = (Component: React.FC<ControlComponent>):
help,
});
} else {
setValidateObj(succesValidation);
setValidateObj(successValidation);
store.setOnValidate(form, viewElement.resultsScope, true);
}
}
Expand Down
Loading

0 comments on commit 81e421d

Please sign in to comment.