Skip to content

Commit

Permalink
schema use fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
elewis2 committed Apr 30, 2024
1 parent 8848625 commit 0214b23
Show file tree
Hide file tree
Showing 20 changed files with 120 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const FilteredRecordAutocomplete = ({
label={`Filter (${name}) Search by Class`}
name="search-class"
onChange={handleClassChange}
options={filterOptions || [...model.descendantTree(false).map((m) => m.name)]}
options={filterOptions || [...schemaDefn.descendants(model.name, { excludeAbstract: false, includeSelf: true })]}
value={selectedClassName}
/>
)}
Expand Down
8 changes: 3 additions & 5 deletions src/components/FormField/PermissionsTable/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import './index.scss';

Check failure on line 1 in src/components/FormField/PermissionsTable/index.tsx

View workflow job for this annotation

GitHub Actions / build

Run autofix to sort these imports!

import kbSchema, { schema as schemaDefn } from '@bcgsc-pori/graphkb-schema';
import { schema as schemaDefn, PERMISSIONS } from '@bcgsc-pori/graphkb-schema';
import {
Checkbox,
Table,
Expand All @@ -11,8 +11,6 @@ import {
} from '@material-ui/core';
import React, { useCallback, useState } from 'react';

const { constants: { PERMISSIONS } } = kbSchema;

/* eslint-disable no-bitwise */

/**
Expand All @@ -22,7 +20,7 @@ const { constants: { PERMISSIONS } } = kbSchema;
const splitPermissionsByOperation = (permissions) => {
const permByModelName = {};

Object.keys(schemaDefn.schema).forEach((modelName) => {
Object.keys(schemaDefn.models).forEach((modelName) => {
const model = schemaDefn.get(modelName);

if (!model.embedded) {
Expand Down Expand Up @@ -58,7 +56,7 @@ interface PermissionsTableProps {
/** flag to indicate this field cannot be edited */
disabled?: boolean;
/** the current permissions set */
value?:Record<string, unknown>;
value?: Record<string, unknown>;
}

/**
Expand Down
14 changes: 5 additions & 9 deletions src/components/FormField/PositionForm/BasicPosition.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import './index.scss';

import { schema as schemaDefn } from '@bcgsc-pori/graphkb-schema';
import { schema as schemaDefn, validateProperty } from '@bcgsc-pori/graphkb-schema';
import {
TextField,
} from '@material-ui/core';
import React, { useCallback, useEffect, useState } from 'react';

import FieldWrapper from '../FieldWrapper';

const {
schema: {
GenomicPosition: { properties: { pos: posProperty } },
CdsPosition: { properties: { offset: offsetProperty } },
},
} = schemaDefn;
const { pos: posProperty } = schemaDefn.getProperties('GenomicPosition');
const { offset: offsetProperty } = schemaDefn.getProperties('CdsPosition');

interface BasicPositionFormProps {
/** change handler */
Expand Down Expand Up @@ -56,7 +52,7 @@ const BasicPositionForm = ({
setPositionError('missing required property');
} else {
try {
posProperty.validate(position);
validateProperty(posProperty, position);
setPositionError('');
} catch (err) {
setPositionError(err.toString());
Expand All @@ -74,7 +70,7 @@ const BasicPositionForm = ({
}
} else {
try {
offsetProperty.validate(offset);
validateProperty(offsetProperty, offset);
setOffsetError('');
} catch (err) {
setOffsetError(err.toString());
Expand Down
10 changes: 3 additions & 7 deletions src/components/FormField/PositionForm/CytobandPosition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@ import useSchemaForm from '@/components/hooks/useSchemaForm';
import FieldWrapper from '../FieldWrapper';

const {
schema: {
CytobandPosition: {
properties,
properties: { arm, majorBand, minorBand }, name: VARIANT,
},
},
} = schema;
properties,
properties: { arm, majorBand, minorBand }, name: VARIANT,
} = schema.get('CytobandPosition');

interface CytobandPositionProps {
/** change handler */
Expand Down
12 changes: 5 additions & 7 deletions src/components/FormField/PositionForm/ProteinPosition.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import './index.scss';

import { schema } from '@bcgsc-pori/graphkb-schema';
import { schema, validateProperty } from '@bcgsc-pori/graphkb-schema';
import {
TextField,
} from '@material-ui/core';
Expand All @@ -9,10 +9,8 @@ import React, { useCallback, useEffect, useState } from 'react';
import FieldWrapper from '../FieldWrapper';

const {
schema: {
ProteinPosition: { properties: { pos: posProperty, refAA: refAAProperty }, name: VARIANT },
},
} = schema;
properties: { pos: posProperty, refAA: refAAProperty }, name: VARIANT

Check failure on line 12 in src/components/FormField/PositionForm/ProteinPosition.tsx

View workflow job for this annotation

GitHub Actions / build

Missing trailing comma
} = schema.get('ProteinPosition');

interface Value {
'@class'?: string;
Expand Down Expand Up @@ -55,7 +53,7 @@ const ProteinPosition = ({
setPositionError('missing required field');
} else {
try {
posProperty.validate(position || null);
validateProperty(posProperty, position || null);
setPositionError('');
} catch (err) {
setPositionError(err.toString());
Expand All @@ -69,7 +67,7 @@ const ProteinPosition = ({
setPositionError('missing required field');
} else {
try {
refAAProperty.validate(refAA || null);
validateProperty(refAAProperty, refAA || null);
setRefAAError('');
} catch (err) {
setRefAAError(err.toString());
Expand Down
4 changes: 2 additions & 2 deletions src/components/FormField/PositionForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ const PositionForm = ({
variant: initialVariant,
...props
}: PositionFormProps) => {
const positionVariants = schemaDefn.schema[baseVariant || DEFAULT_BASE_VARIANT]
.descendantTree(true).map((m) => m.name);
const positionVariants = schemaDefn.descendants(baseVariant || DEFAULT_BASE_VARIANT, { excludeAbstract: true, includeSelf: true });

const [variant, setVariant] = useState(initialVariant);

let PositionComponent;
Expand Down
7 changes: 4 additions & 3 deletions src/components/FormField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import Timestamp from './Timestamp';

const POSITION_CLASSES = [
'Position',
...schemaDefn.schema.Position.descendantTree(false).map((m) => m.name),
...schemaDefn.descendants('Position', { excludeAbstract: false, includeSelf: true }),
];

interface FormFieldProps {
Expand Down Expand Up @@ -70,14 +70,15 @@ const FormField = ({
description,
example,
generateDefault,
linkedClass,
linkedClass: linkedClassName,
linkedType,
name,
type,
nullable,
iterable,
format,
} = model;
const linkedClass = schemaDefn.get(linkedClassName, false);

const inputValue = formContent[name];
const generated = Boolean(model.generated && formVariant !== FORM_VARIANT.SEARCH);
Expand Down Expand Up @@ -219,7 +220,7 @@ const FormField = ({
// special case (KBDEV-790) to improve user inputs
if (name === 'conditions' && linkedClass.name === 'Biomarker') {
autoProps.filterOptions = [
...schemaDefn.schema.Variant.descendantTree(false).map((m) => m.name),
...schemaDefn.descendants('Variant', { excludeAbstract: false, includeSelf: true }),
'Disease',
'CatalogueVariant',
];
Expand Down
4 changes: 2 additions & 2 deletions src/components/ModelSelect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ const ModelSelect = ({
const model = value || defaultValue;

useEffect(() => {
const models = schemaDefn.get(baseModel).descendantTree(!includeAbstract).map((m) => ({
label: m.name, value: m.name, caption: m.description, key: m.name,
const models = schemaDefn.descendants(baseModel || '', { excludeAbstract: !includeAbstract, includeSelf: true }).map((m) => ({
label: m, value: m, caption: schemaDefn.get(m).description, key: m,
})).sort((m1, m2) => m1.label.localeCompare(m2.label));
setChoices(models);
}, [baseModel, includeAbstract, name, onChange, value]);
Expand Down
6 changes: 4 additions & 2 deletions src/components/SentencePreview/StatementSentence.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { schema as schemaDefn, sentenceTemplates } from '@bcgsc-pori/graphkb-schema';

Check failure on line 1 in src/components/SentencePreview/StatementSentence.tsx

View workflow job for this annotation

GitHub Actions / build

Run autofix to sort these imports!
import { StatementRecord } from '@bcgsc-pori/graphkb-schema/dist/types';
import React from 'react';


Check failure on line 5 in src/components/SentencePreview/StatementSentence.tsx

View workflow job for this annotation

GitHub Actions / build

More than 1 blank line not allowed
import { StatementType } from '@/components/types';

import SentencePreview from '.';

interface StatementSentenceProps {
content?: StatementType;
content?: StatementRecord;
}

const StatementSentence = ({ content: record }: StatementSentenceProps) => {
const { content, highlighted } = sentenceTemplates.generateStatementSentence(schemaDefn, record);
const { content, highlighted } = sentenceTemplates.generateStatementSentence(schemaDefn.getPreview, record);

return (
<SentencePreview
Expand Down
10 changes: 5 additions & 5 deletions src/components/StatementForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const StatementForm = ({

const snackbar = useSnackbar();
const auth = useAuth();
const model = schemaDefn.schema.Statement;
const model = schemaDefn.get('Statement');
const fieldDefs = model.properties;

const [reviewDialogOpen, setReviewDialogOpen] = useState(false);
Expand Down Expand Up @@ -134,7 +134,7 @@ const StatementForm = ({
return 'prognostic statements should have the Vocabulary record "patient" for the subject';
}
}
} catch (err) {} // eslint-disable-line no-empty
} catch (err) { } // eslint-disable-line no-empty
return '';
}, [diagnosticData, prognosticData, therapeuticData]);

Expand Down Expand Up @@ -370,7 +370,7 @@ const StatementForm = ({
// for spacing issues only
: (<div />)}
{actionInProgress && (
<CircularProgress size={50} />
<CircularProgress size={50} />
)}
{additionalValidationError && (
<Alert severity="error">{additionalValidationError}</Alert>
Expand Down Expand Up @@ -400,8 +400,8 @@ const StatementForm = ({
};

StatementForm.defaultProps = {
onError: () => {},
onSubmit: () => {},
onError: () => { },
onSubmit: () => { },
onToggleState: undefined,
rid: null,
variant: FORM_VARIANT.VIEW,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('BreakpointForm', () => {
<BreakpointForm
coordinateType="GenomicPosition"
end="break1End"
model={schemaDefn.schema.PositionalVariant}
model={schemaDefn.get('PositionalVariant')}
reference="reference1"
start="break1Start"
/>
Expand All @@ -35,7 +35,7 @@ describe('BreakpointForm', () => {
<BreakpointForm
coordinateType="GenomicPosition"
end="break1End"
model={schemaDefn.schema.PositionalVariant}
model={schemaDefn.get('PositionalVariant')}
reference="reference1"
start="break1Start"
/>
Expand All @@ -55,7 +55,7 @@ describe('BreakpointForm', () => {
<BreakpointForm
coordinateType="GenomicPosition"
end="break1End"
model={schemaDefn.schema.PositionalVariant}
model={schemaDefn.get('PositionalVariant')}
reference="reference1"
start="break1Start"
/>
Expand All @@ -75,7 +75,7 @@ describe('BreakpointForm', () => {
<QueryClientProvider client={api.queryClient}>
<BreakpointForm
coordinateType="GenomicPosition"
model={schemaDefn.schema.PositionalVariant}
model={schemaDefn.get('PositionalVariant')}
reference="reference1"
/>
</QueryClientProvider>,
Expand Down
24 changes: 13 additions & 11 deletions src/components/VariantForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import BreakpointForm from './BreakpointForm';
import FormStepWrapper from './FormStepWrapper';
import SteppedForm from './SteppedForm';

const { schema: { PositionalVariant, CategoryVariant, Position } } = schemaDefn;
const PositionalVariant = schemaDefn.get('PositionalVariant');
const CategoryVariant = schemaDefn.get('CategoryVariant');
const Position = schemaDefn.get('Position');

const leftoverPositionalProps = omit(
PositionalVariant.properties,
Expand Down Expand Up @@ -59,8 +61,8 @@ const { fields: categoryFields } = sortAndGroupFields(
{ collapseExtra: false, variant: FORM_VARIANT.NEW },
);

const coordinateOptions = Position.descendantTree(true).map((m) => ({
label: m.name, value: m.name, key: m.name, caption: m.description,
const coordinateOptions = schemaDefn.descendants('Position', { excludeAbstract: true, includeSelf: true }).map((m) => ({
label: m, value: m, key: m, caption: schemaDefn.get(m).description,
}));

const MAJOR_FORM_TYPES = {
Expand Down Expand Up @@ -294,15 +296,15 @@ const VariantForm = ({
<FieldGroup
disabled={false}
model={
hasPositions
? PositionalVariant
: CategoryVariant
}
hasPositions
? PositionalVariant
: CategoryVariant
}
ordering={
hasPositions
? positionalFields
: categoryFields
}
hasPositions
? positionalFields
: categoryFields
}
/>
</List>
</FormStepWrapper>
Expand Down
17 changes: 9 additions & 8 deletions src/services/__tests__/schema.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import testSchema from '../schema';
describe('schema service', () => {
describe('Retrieving classmodels and properties', () => {
test('gets classes properly', () => {
Object.keys(SCHEMA_DEFN).forEach((key) => {
Object.keys(SCHEMA_DEFN.models).forEach((key) => {
expect(SCHEMA_DEFN.get(key)).toBeDefined();
});
});
Expand All @@ -29,8 +29,8 @@ describe('schema service', () => {
});

test('Returns edges', () => {
const edges = Object.values(SCHEMA_DEFN.schema)
.filter((model) => model.inherits && model.inherits.includes('E'))
const edges = Object.values(SCHEMA_DEFN.models)
.filter((model) => SCHEMA_DEFN.ancestors(model.name) && SCHEMA_DEFN.ancestors(model.name).includes('E'))
.map((model) => model.name);
expect(testSchema.getEdges()).toEqual(edges);

Expand Down Expand Up @@ -65,10 +65,10 @@ describe('schema service', () => {
'@class': 'Statement',
'@rid': '22:0',
displayNameTemplate: 'Given {conditions} {relevance} applies to {subject} ({evidence})',
relevance: { displayName: 'Mood Swings' },
conditions: [{ displayName: 'Low blood sugar' }],
subject: { displayName: 'hungertitis' },
evidence: [{ displayName: 'A reputable source' }],
relevance: { displayName: 'Mood Swings', '@rid': '1' },
conditions: [{ displayName: 'Low blood sugar', '@class': 'Disease', '@rid': '2' }],
subject: { displayName: 'hungertitis', '@rid': '3', '@class': 'Disease' },
evidence: [{ displayName: 'A reputable source', '@rid': '4' }]

Check failure on line 71 in src/services/__tests__/schema.test.tsx

View workflow job for this annotation

GitHub Actions / build

Missing trailing comma
};

const statementLabel = SCHEMA_DEFN.getPreview(mockStatementRecord);
Expand Down Expand Up @@ -101,7 +101,8 @@ describe('schema service', () => {
expect(cutOffLabel).toEqual('super long display name that is going to go ove...');
});

test('returns classModel name', () => {
// TODO: no longer capitalizes; do we want this
xtest('returns classModel name', () => {

Check failure on line 105 in src/services/__tests__/schema.test.tsx

View workflow job for this annotation

GitHub Actions / build

Use "test.skip" instead
const classModel = {
'@class': 'disease',
};
Expand Down
Loading

0 comments on commit 0214b23

Please sign in to comment.