Skip to content

Commit

Permalink
fix(utils): rid redundant data at formatDataForMutations
Browse files Browse the repository at this point in the history
  • Loading branch information
Selnapenek committed Aug 17, 2021
1 parent 6188332 commit 1e6b60b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 14 deletions.
8 changes: 8 additions & 0 deletions packages/core/utils/src/formatters/formatDataForMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ const formatDataForMutation = (
formatedFieldData = mutate(formatedFieldData, data[fieldName], fieldSchema);
}

if (typeof formatedFieldData === 'string' && formatedFieldData === initialFieldData) {
return result;
}

if (Array.isArray(formatedFieldData) && R.equals(formatedFieldData, initialFieldData)) {
return result;
}

return {
...result,
[fieldName]: formatedFieldData,
Expand Down
1 change: 1 addition & 0 deletions packages/core/utils/src/formatters/formatFieldDataList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const formatFieldDataList = (
fieldSchema,
schema,
initialData: item.initialData,
initialListData: initialData,
},
options,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ interface IFormatFieldDataListItemMeta {
fieldSchema: FieldSchema;
schema: Schema;
initialData?: any;
initialListData?: any;
}

export const formatFieldDataListItem = (
type: MutationType,
data: any,
{ fieldSchema, schema, initialData }: IFormatFieldDataListItemMeta,
{ fieldSchema, schema, initialData, initialListData }: IFormatFieldDataListItemMeta,
options?: FormatDataForMutationOptions,
) => {
let nextData = data;
Expand All @@ -40,6 +41,10 @@ export const formatFieldDataListItem = (
}

if (typeof nextData === 'string') {
if (Array.isArray(initialListData) && initialListData.includes(nextData)) {
return null;
}

return {
data: { id: nextData },
type: 'connect',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ Object {
"update": Array [
Object {
"data": Object {
"fileId": "file-id",
"filename": "Screenshot at авг. 13 15-22-49.png",
"id": "1234",
},
"filter": Object {
"id": "1234",
Expand Down Expand Up @@ -128,7 +126,6 @@ Object {
},
Object {
"data": Object {
"id": "relation-list-1",
"nestedRelation": Object {
"create": Object {
"scalar": "New Nested Relation Scalar Value",
Expand All @@ -155,7 +152,6 @@ Object {
},
Object {
"data": Object {
"id": "relation-list-2",
"nestedRelation": Object {
"update": Object {
"scalar": null,
Expand All @@ -165,7 +161,6 @@ Object {
"update": Array [
Object {
"data": Object {
"id": "nested-relation-2-2",
"scalar": "Relation List Nested Relation List Scalar Value",
"scalarList": Array [
"Relation List Nested Relation List Scalar List Value",
Expand Down
52 changes: 44 additions & 8 deletions packages/core/utils/test/__tests__/formatDataForMutation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ describe('As developer, I can format for update mutation,', () => {
const data = {
relation: {
id: 'id',
scalar: 'Relation Scalar Value',
scalar: 'New Relation Scalar Value',
scalarList: ['Relation Scalar List Value'],
},
};
Expand All @@ -601,7 +601,9 @@ describe('As developer, I can format for update mutation,', () => {
formatDataForMutation(MUTATION_TYPE.UPDATE, data, { tableName: 'tableSchema', schema: SCHEMA, initialData }),
).toEqual({
relation: {
update: R.dissoc('id', data.relation),
update: {
scalar: data.relation.scalar,
},
},
});
});
Expand Down Expand Up @@ -715,7 +717,6 @@ describe('As developer, I can format for update mutation,', () => {
formatDataForMutation(MUTATION_TYPE.UPDATE, data, { tableName: 'tableSchema', schema: SCHEMA, initialData }),
).toEqual({
relationList: {
connect: [{ id: '5b32159b66a450c047285628' }],
disconnect: [{ id: '5b32159b66a450fae928562a' }],
},
});
Expand Down Expand Up @@ -749,8 +750,8 @@ describe('As developer, I can format for update mutation,', () => {
'inline-relation-01',
{
id: 'update-relation-01',
scalar: 'Update relation scalar value',
scalarList: ['Update relation scalar list value'],
scalar: 'New Update relation scalar value',
scalarList: ['Update relation scalar list value', 'New Update relation scalar list value'],
},
{
scalar: 'New relation scalar value',
Expand All @@ -777,6 +778,41 @@ describe('As developer, I can format for update mutation,', () => {
],
};

const expectedRelationListWithoutDisconnectWithInitial = {
connect: [
{
id: 'inline-relation-01',
},
],
update: [
{
data: {
scalar: 'New Update relation scalar value',
scalarList: ['Update relation scalar list value', 'New Update relation scalar list value'],
},
filter: {
id: 'update-relation-01',
},
},
// TODO: For now, it will update despite there no such id in `initialData`.
// We probably should connect instead of update in that case.
{
data: {
id: 'connect-relation-01',
},
filter: {
id: 'connect-relation-01',
},
},
],
create: [
{
scalar: 'New relation scalar value',
scalarList: ['New relation scalar list value'],
},
],
};

const expectedRelationListWithoutDisconnect = {
connect: [
{
Expand All @@ -787,8 +823,8 @@ describe('As developer, I can format for update mutation,', () => {
{
data: {
id: 'update-relation-01',
scalar: 'Update relation scalar value',
scalarList: ['Update relation scalar list value'],
scalar: 'New Update relation scalar value',
scalarList: ['Update relation scalar list value', 'New Update relation scalar list value'],
},
filter: {
id: 'update-relation-01',
Expand Down Expand Up @@ -821,7 +857,7 @@ describe('As developer, I can format for update mutation,', () => {
}),
).toEqual({
relationList: {
...expectedRelationListWithoutDisconnect,
...expectedRelationListWithoutDisconnectWithInitial,
disconnect: [{ id: 'removed-relation-01' }],
},
});
Expand Down

0 comments on commit 1e6b60b

Please sign in to comment.