Skip to content

Commit

Permalink
fix: simplify payload data as obj
Browse files Browse the repository at this point in the history
  • Loading branch information
nileshgulia1 committed Jun 3, 2022
1 parent 7de0d67 commit a061d9c
Showing 1 changed file with 14 additions and 34 deletions.
48 changes: 14 additions & 34 deletions src/components/theme/Widgets/DataProvenance.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import { Accordion, Button, Segment } from 'semantic-ui-react';
import { isArray } from 'lodash';
import {
Icon as VoltoIcon,
FormFieldWrapper,
Expand All @@ -16,9 +15,9 @@ import Schema from './schema';
import './style.css';

export const DataProvenance = (props) => {
const { id, value = {}, onChange } = props;
const { id, value = [], onChange, defaultData = {} } = props;
const predefinedSchema = Schema(props);
const flatListValue = isArray(value) ? value : Object.values(value);
const flatListValue = value?.data || [];
return (
<>
<FormFieldWrapper {...props} className="objectlist-inline-widget">
Expand All @@ -27,12 +26,14 @@ export const DataProvenance = (props) => {
compact
type="button"
onClick={(e) => {
const newId = uuid();
onChange(id, {
...value,
[newId]: {
'@id': newId,
},
data: [
...flatListValue,
{
'@id': uuid(),
...defaultData,
},
],
});
e.stopPropagation();
}}
Expand All @@ -55,15 +56,7 @@ export const DataProvenance = (props) => {
flatListValue[destination.index] = first;
flatListValue[source.index] = second;

const obj = {};
flatListValue.forEach(
(item) => (obj[item?.['@id'].toString()] = item),
);

onChange(
id,
flatListValue.reduce((a, v) => ({ ...a, [v['@id']]: v }), {}),
);
onChange(id, { data: flatListValue });
return true;
}}
>
Expand Down Expand Up @@ -91,12 +84,9 @@ export const DataProvenance = (props) => {
{`${predefinedSchema.title} #${index + 1}`}
<button
onClick={() => {
onChange(
id,
flatListValue
.filter((v, i) => i !== index)
.reduce((a, v) => ({ ...a, [v['@id']]: v }), {}),
);
onChange(id, {
data: flatListValue.filter((v, i) => i !== index),
});
}}
>
<VoltoIcon name={deleteSVG} size="16px" />
Expand All @@ -113,17 +103,7 @@ export const DataProvenance = (props) => {
const newvalue = flatListValue.map((v, i) =>
i !== index ? v : fv,
);
// const da = newvalue.map((item) => ({
// [item['@id']]: { ...item },
// }));

onChange(
id,
newvalue.reduce(
(a, v) => ({ ...a, [v['@id']]: v }),
{},
),
);
onChange(id, { data: newvalue });
}}
/>
</Segment>
Expand Down

0 comments on commit a061d9c

Please sign in to comment.