Skip to content

Commit

Permalink
[refactor] Circular in Filter, deps
Browse files Browse the repository at this point in the history
  • Loading branch information
amivanoff committed Oct 18, 2021
1 parent 23fce9e commit d5961f2
Show file tree
Hide file tree
Showing 7 changed files with 432 additions and 342 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@
"@storybook/react": "^6.3.12",
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^12.1.2",
"@testing-library/user-event": "^13.4.1",
"@testing-library/user-event": "^13.4.2",
"@types/jest": "^27.0.2",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"@typescript-eslint/eslint-plugin": "^5.1.0",
"@typescript-eslint/parser": "^5.1.0",
"autoprefixer": "^10.3.7",
"babel-loader": "^8.2.2",
"cross-env": "^7.0.3",
Expand All @@ -144,15 +144,15 @@
"eslint-config-prettier": "^8.3.0",
"eslint-config-react-app": "^6.0.0",
"eslint-plugin-cypress": "^2.12.1",
"eslint-plugin-flowtype": "^6.1.0",
"eslint-plugin-flowtype": "^6.1.1",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-jest": "^25.2.2",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-react": "^7.26.1",
"eslint-plugin-react-hooks": "^4.2.0",
"husky": "^7.0.2",
"jest": "^27.2.5",
"jest": "^27.3.0",
"jest-watch-typeahead": "^1.0.0",
"lint-staged": "^11.2.3",
"npm-run-all": "^4.1.5",
Expand Down
21 changes: 18 additions & 3 deletions src/MstContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
********************************************************************************/
import React, { createContext, PropsWithChildren } from 'react';
import { CellRendererRegistryEntry, RendererRegistryEntry } from './renderers';
import { registerMstViewKindSchema } from './models/MstViewDescr';
import { registerMstViewDescrSchema, registerMstViewKindSchema } from './models/MstViewDescr';

export interface MstContextProps {
store: any;
Expand All @@ -34,8 +34,23 @@ export const MstContextProvider = ({
cells?: CellRendererRegistryEntry[];
}>): JSX.Element => {
renderers.forEach((r) => {
if ((r as any).mstVkeType) {
registerMstViewKindSchema((r as any).mstVkeType);
const mstVkeType = (r as any).mstVkeType;
if (mstVkeType) {
registerMstViewKindSchema(mstVkeType, true);
}
const mstVdeType = (r as any).mstVdeType;
if (mstVdeType) {
registerMstViewDescrSchema(mstVdeType, true);
}
});
cells.forEach((с) => {
const mstVkeType = (с as any).mstVkeType;
if (mstVkeType) {
registerMstViewKindSchema(mstVkeType, true);
}
const mstVdeType = (с as any).mstVdeType;
if (mstVdeType) {
registerMstViewDescrSchema(mstVdeType, true);
}
});
return <MstContext.Provider value={{ store, renderers, cells }}>{children}</MstContext.Provider>;
Expand Down
15 changes: 2 additions & 13 deletions src/controls/query/Filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { FilterType, Relation, ValueOfFilter } from './type';
import { JSONSchema6DefinitionForRdfProperty, JsObject } from '@agentlab/sparql-jsld-client';
import moment from 'moment';
import { JSONSchema6, JSONSchema6Definition } from 'json-schema';

import { BodyOfListFilter } from './filtersBody/BodyOfListFilter';
import { BodyOfIntegerFilter } from './filtersBody/BodyOfIntegerFilter';
import { BodyOfIntegerFilter, equalTypesOfRelations } from './filtersBody/BodyOfIntegerFilter';
import { BodyOfDateTimeFilter } from './filtersBody/BodyOfDateTimeFilter';
import { BodyOfStringFilter } from './filtersBody/BodyOfStringFilter';

Expand Down Expand Up @@ -65,18 +66,6 @@ export const genFormattedTypeOfFilter = (
return formattedType;
};

export const equalTypesOfRelations = (typeA?: string, typeB?: string): boolean => {
const keyWord = 'hidden';

let indexOfKeyword = typeA && typeA.indexOf(keyWord) !== -1 ? typeA.indexOf(keyWord) + keyWord.length : 0;
const firstType = typeA && typeA.substring(indexOfKeyword);

indexOfKeyword = typeB && typeB.indexOf(keyWord) !== -1 ? typeB.indexOf(keyWord) + keyWord.length : 0;
const secondType = typeB && typeB.substring(indexOfKeyword);

return firstType === secondType;
};

export const getTypeOfRelation = (currentRelations: Relation[], nameOfSelectRelation: string): string | undefined => {
const foundRelation = currentRelations.find((relation: Relation) => relation.predicate === nameOfSelectRelation);

Expand Down
13 changes: 12 additions & 1 deletion src/controls/query/filtersBody/BodyOfIntegerFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { ChangeEvent, useEffect, useState } from 'react';
import { Input, Tooltip } from 'antd';
import { equalTypesOfRelations } from '../Filter';
import { ValueOfFilter } from '../type';

const localeRus = {
Expand All @@ -15,6 +14,18 @@ interface BodyOfIntegerFilterProps {
type?: string;
}

export const equalTypesOfRelations = (typeA?: string, typeB?: string): boolean => {
const keyWord = 'hidden';

let indexOfKeyword = typeA && typeA.indexOf(keyWord) !== -1 ? typeA.indexOf(keyWord) + keyWord.length : 0;
const firstType = typeA && typeA.substring(indexOfKeyword);

indexOfKeyword = typeB && typeB.indexOf(keyWord) !== -1 ? typeB.indexOf(keyWord) + keyWord.length : 0;
const secondType = typeB && typeB.substring(indexOfKeyword);

return firstType === secondType;
};

export const BodyOfIntegerFilter: React.FC<BodyOfIntegerFilterProps> = ({ handleChange, defaultValues, type }) => {
const relationForOneNumber = { title: localeRus.equal, predicate: 'equal', type: 'singleNumber' };
const relationForSomeNumbers = { title: localeRus.any, predicate: 'any', type: 'hiddenSingleNumber' };
Expand Down
43 changes: 34 additions & 9 deletions src/models/MstViewDescr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@
* SPDX-License-Identifier: GPL-3.0-only
********************************************************************************/
import { reaction } from 'mobx';
import { getParent, getRoot, IAnyComplexType, IAnyStateTreeNode, IAnyType, SnapshotIn, types } from 'mobx-state-tree';
import {
getParent,
getRoot,
IAnyComplexType,
IAnyStateTreeNode,
IAnyType,
IArrayType,
IMSTArray,
IStateTreeNode,
SnapshotIn,
types,
} from 'mobx-state-tree';

import {
arrDiff,
Expand Down Expand Up @@ -45,11 +56,15 @@ export const MstViewKindElement = types.model('MstViewKindElement', {

const mstViewKindSchemas: MstModels = {};

export function registerMstViewKindSchema(mstModel: IAnyComplexType): void {
export function registerMstViewKindSchema(mstModel: IAnyComplexType, noOverride = false): void {
const id = getMstLiteralPropValue(mstModel as any, '@type');
if (id) {
console.log('register mstViewKindSchema', { id, mstModel });
mstViewKindSchemas[id] = mstModel;
if (!noOverride || mstViewKindSchemas[id] === undefined) {
console.log('register mstViewKindSchema', { id, mstModel });
mstViewKindSchemas[id] = mstModel;
} else {
console.log('cannot register mstViewKindSchema: noOverride and existed schema', { mstModel });
}
} else {
console.log('cannot register mstViewKindSchema', { mstModel });
}
Expand Down Expand Up @@ -109,7 +124,10 @@ export const MstViewKind = types
if (coll.resolveCollConstrs) {
dispose = reaction(
() => self.collsConstrs,
(newArr: any[], oldArr: any[]) => {
(
newArr: (IMSTArray<any> & IStateTreeNode<IArrayType<any>>) | undefined,
oldArr: (IMSTArray<any> & IStateTreeNode<IArrayType<any>>) | undefined,
) => {
console.log('ViewKind reaction, add coll ref, @id=', self['@id']);
const { deleted, added } = arrDiff(newArr, oldArr);
console.log('ViewKind reaction, add coll ref, {deleted,added}=', { deleted, added });
Expand Down Expand Up @@ -161,11 +179,15 @@ export const MstViewDescrElement = types.model('MstViewDescrElement', {

const mstViewDescrSchemas: MstModels = {};

export function registerMstViewDescrSchema(mstModel: IAnyComplexType): void {
export function registerMstViewDescrSchema(mstModel: IAnyComplexType, noOverride = false): void {
const id = getMstLiteralPropValue(mstModel as any, '@type');
if (id) {
console.log('register mstViewDescrSchema', { id, mstModel });
mstViewDescrSchemas[id] = mstModel;
if (!noOverride || mstViewDescrSchemas[id] === undefined) {
console.log('register mstViewDescrSchema', { id, mstModel });
mstViewDescrSchemas[id] = mstModel;
} else {
console.log('cannot register mstViewDescrSchema: noOverride and existed schema', { mstModel });
}
} else {
console.log('cannot register mstViewDescrSchema', { mstModel });
}
Expand Down Expand Up @@ -226,7 +248,10 @@ export const MstViewDescr = types
if (coll.resolveCollConstrs) {
dispose = reaction(
() => self.collsConstrs,
(newArr: any[], oldArr: any[]) => {
(
newArr: (IMSTArray<any> & IStateTreeNode<IArrayType<any>>) | undefined,
oldArr: (IMSTArray<any> & IStateTreeNode<IArrayType<any>>) | undefined,
) => {
console.log('MstViewDescr reaction, add coll ref, @id=', self['@id']);
const { deleted, added } = arrDiff(newArr, oldArr);
console.log('MstViewDescr reaction, add coll ref, {deleted,added}=', { deleted, added });
Expand Down
2 changes: 1 addition & 1 deletion stories/Form.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* SPDX-License-Identifier: GPL-3.0-only
********************************************************************************/
import { cloneDeep } from 'lodash';
import { cloneDeep } from 'lodash-es';
import moment from 'moment';
import React from 'react';
import { Meta, Story } from '@storybook/react';
Expand Down
Loading

0 comments on commit d5961f2

Please sign in to comment.