Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cbi 773 #28

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/components/Card/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function input(
schema,
dropdowns,
parentValue,
filterSchema,
loading: string,
getValues: (name: string) => unknown,
setValue: (name: string, value: unknown) => void,
Expand Down Expand Up @@ -155,6 +156,7 @@ function input(
<Json
{...field}
value={field.value || ''}
schema={filterSchema || ''}
{...parentField && {previous: parentValue}}
{...props}
/>
Expand Down
3 changes: 2 additions & 1 deletion src/components/Json/Json.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Meta } from '@storybook/react';
// import page from './README.mdx';
import Json from '.';
import decorators from '../test/decorator';
import {previous, current} from '../test/diff';
import {previous, current, schema} from '../test/diff';

const meta: Meta = {
title: 'Json',
Expand All @@ -21,6 +21,7 @@ export const Diff: React.FC = () =>
<Json
value={current}
previous={previous}
schema={schema}
/>;

export const View: React.FC = () =>
Expand Down
1 change: 1 addition & 0 deletions src/components/Json/Json.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface Props extends React.HTMLAttributes<HTMLDivElement> {
showUnchangedValues?: boolean,
value: unknown,
previous?: unknown,
schema?: unknown,
keyValue?: boolean
}

Expand Down
69 changes: 69 additions & 0 deletions src/components/Json/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,85 @@ const convert = keyValue => {
};
};

const flatten = (properties, root = '') => Object.entries(properties || {}).reduce(
(map, [name, property]) => {
return ('properties' in property) ? {
...map,
[root + name]: property,
...flatten(property.properties, root + name + '.')
} : ('items' in property) ? {
...map,
[root + name]: property,
...flatten(property.items.properties, root + name + '.')
} : {
...map,
[root + name]: property
};
},
{}
);

const filterBySchema = (obj, schema) => {
if (!schema) {
return obj;
}
const schemaMap = flatten(schema);
const convertType = (value, {type = 'string'}) => {
switch (Array.isArray(type) ? type.shift() : type) {
case 'number':
return parseInt(value);
case 'string':
return value?.toString();
default:
return value;
}
};
const filter = (obj, objPath = '') => {
return obj && Object.entries(obj).reduce(
(map, [name, property]) => {
const properties = schemaMap[objPath ? `${objPath}.${name}` : name];
if (!properties) {
return map;
}
if (Array.isArray(property)) {
return {
...map,
[properties.title || name]: property.map(p =>
filter(p, objPath ? `${objPath}.${name}` : name)
)
};
} else if (typeof property === 'object' && property) {
return {
...map,
[properties.title || name]: filter(
property,
objPath ? `${objPath}.${name}` : name
)
};
}
return {
...map,
[properties.title || name]: convertType(property, properties)
};
}, {}
);
};
return filter(obj);
};

const arrow = <span className='ml-2 pi pi-arrow-right' />;
const Json: ComponentProps = ({
showUnchangedValues = true,
value,
previous = value,
keyValue,
className,
schema,
...props
}) => {
const classes = useStyles();
value = filterBySchema(value, schema?.properties);
previous = filterBySchema(previous, schema?.properties);
const diff = keyValue ? convert(value) : compare(value, previous);

const lineClass = line =>
Expand Down
71 changes: 71 additions & 0 deletions src/components/test/diff.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,76 @@
import merge from 'ut-function.merge';

export const schema = {
type: 'object',
properties: {
'General Info': {
type: 'object',
title: 'General Information',
properties: {
'First name': {
title: 'Customer First Name',
description: 'First name',
type: 'string'
},
'Last name': {
type: 'string'
},
'National id': {
type: 'string'
},
Gender: {
type: 'string'
},
'User Classification': {
type: 'number'
},
'Business Unit': {
type: 'string'
},
'Business UnitType': {
type: 'boolean'
},
'Lock Status': {
type: 'boolean'
}
}
},
Credentials: {
type: 'object',
title: 'User Credentials',
properties: {
'Set Username': {
type: 'string'
},
'Access Policy Status': {
type: 'number'
},
'Override User Access Policy': {
type: 'string'
}
}
},
'External Credentials': {
type: 'array',
items: {
description: 'External User',
type: 'object',
properties: {
'External System': {
type: 'string'
},
'User Type': {
type: 'string'
},
Username: {
type: 'string'
}
}
}
}
}
};

export const previous = {
'General Info': {
'First name': 'Super',
Expand Down