From 704ff5a6127ab3d3bb6f721084989e58cc672ac6 Mon Sep 17 00:00:00 2001 From: Alexandre Bodin Date: Wed, 22 Sep 2021 20:10:48 +0200 Subject: [PATCH] Fix components ordering --- .../core/database/lib/query/query-builder.js | 2 +- .../lib/services/entity-service/components.js | 33 ++++++++++++++++--- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/packages/core/database/lib/query/query-builder.js b/packages/core/database/lib/query/query-builder.js index aeb4c8c26e..715ec8f493 100644 --- a/packages/core/database/lib/query/query-builder.js +++ b/packages/core/database/lib/query/query-builder.js @@ -226,7 +226,7 @@ const createQueryBuilder = (uid, db) => { }); const orderByColumns = state.orderBy.map(({ column }) => column); - state.select = _.uniq([...state.select, ...orderByColumns, ...joinsOrderByColumns]); + state.select = _.uniq([...joinsOrderByColumns, ...orderByColumns, ...state.select]); } }, diff --git a/packages/core/strapi/lib/services/entity-service/components.js b/packages/core/strapi/lib/services/entity-service/components.js index 61b8d0abba..bdf0c1e73a 100644 --- a/packages/core/strapi/lib/services/entity-service/components.js +++ b/packages/core/strapi/lib/services/entity-service/components.js @@ -131,11 +131,26 @@ const updateComponents = async (uid, entityToUpdate, data) => { componentValue.map(value => updateOrCreateComponent(componentUID, value)) ); - // TODO: add order - componentBody[attributeName] = components.filter(_.negate(_.isNil)).map(({ id }) => id); + componentBody[attributeName] = components.filter(_.negate(_.isNil)).map(({ id }, idx) => { + return { + id, + __pivot: { + order: idx + 1, + field: attributeName, + component_type: componentUID, + }, + }; + }); } else { const component = await updateOrCreateComponent(componentUID, componentValue); - componentBody[attributeName] = component && component.id; + componentBody[attributeName] = component && { + id: component.id, + __pivot: { + order: 1, + field: attributeName, + component_type: componentUID, + }, + }; } continue; @@ -151,9 +166,17 @@ const updateComponents = async (uid, entityToUpdate, data) => { } componentBody[attributeName] = await Promise.all( - dynamiczoneValues.map(async value => { + dynamiczoneValues.map(async (value, idx) => { const { id } = await updateOrCreateComponent(value.__component, value); - return { id, __component: value.__component }; + + return { + id, + __component: value.__component, + __pivot: { + order: idx + 1, + field: attributeName, + }, + }; }) );