-
Notifications
You must be signed in to change notification settings - Fork 45
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
fix: Update relations mapping to support dynamic zones #158
base: main
Are you sure you want to change the base?
Conversation
I went through the code and it looks good. Great job. Will play around with it tomorrow |
I have just discussed this fix with a colleague and while it works for Dynamic Zones, it does not detect relations on components. I'm looking into this now. e.g. A dynamic zone that has a component that had a relation within it does not save correctly |
Yes, I have just discovered the same issue |
const relations = getUpdatableRelations(model); | ||
relations && result.push({ [key]: relations }); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also added this here
if (attributes[key].type === "component") { const model = strapi.getModel(attributes[key].component); const relations = getUpdatableRelations(model); relations && result.push({ [key]: relations }); }
But then i also need to update the generation of populate statement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, looking at refactoring the generate statement now to account for nested objects, will hopefully be able to post soon
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I spent the past hour trying to refactor it but could not figure it out. I will try tomorrow if you are not faster to do it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, keep the branch updated if you can :)
const prevRel = previous[relation] ?? undefined; | ||
|
||
if (prevRel) { | ||
const newDataRel = parent |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm struggling with how to correct this and attach the new data for if the value is more than one level down. e.g. the relation, from the top level would be, contentType -> dynamicZone -> component -> relation
and therefore setting newData[parent][relation]
results in an error because it skips a level
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought I was on to something with these functions:
const setDeepValue = (obj, value, path) => {
let index = 0;
for (index; index < path.length; index++) {
obj = obj[path[index]];
}
obj[path[index]] = value;
};
const getDeepValue = (obj, path) => {
for (const index of path) {
obj = obj[path[index]];
return obj;
}
};
But haven't managed to get it working for all circumstances with an implementation like
const newDataRel = getDeepValue(newData, [...path, relation]);
And don't want to push a half working commit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand. So did you manage to fix the getUpdateStatement fnc? It would be great for me to have that part so I could debug this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I did not get to you quicker. I had to finish work on other project before Christmas + fixed another bug on this plugin. I should be free to deep into this one tomorrow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand. So did you manage to fix the getUpdateStatement fnc? It would be great for me to have that part so I could debug this one.
Sadly not yet, I just hard-coded a data structure that fit with my use case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, glad to be back after the holiday. I think I adjusted the populate statement generator. It works for my data structure. I think I will now encouter the problem you have described and will try to fix it.
`const generatePopulateStatement = (relations) => {
const result = {};
relations.forEach((item) => {
if (typeof item === "object" && !Array.isArray(item)) {
const key = Object.keys(item)[0];
const innerObj = {};
item[key].forEach((innerItem) => {
if (typeof innerItem === "object") {
const key = Object.keys(innerItem)[0];
innerObj[key] = {
populate: {},
};
innerItem[key].forEach((element) => {
innerObj[key]["populate"][element] = true;
});
} else {
innerObj[innerItem] = true;
}
});
result[key] = { populate: innerObj };
} else if (Array.isArray(item)) {
const key = Object.keys(item)[0];
const innerObj = {};
item[key].forEach((innerItem) => {
innerObj[innerItem] = true;
});
result[key] = { populate: innerObj };
} else {
result[item] = true;
}
});
return result;
};`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ShipleyC95 So I tried implementing it and I think I got close. However it still does not work :/ Morover, when there are multiple types of components in the dz it sometimes deletes one of them.
You can check what I have tried in https://github.com/notum-cz/strapi-plugin-content-versioning/tree/feature/dz-relations-om
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the update @omikulcik. Unfortunately, your generate method doesn't work for me, I end up with one of the keys being "[Object object]" but I will look into it and that branch you've posted there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for testing it @ShipleyC95. Working with this polymorphic data structure seems quite complex to me but hopefully we can somehow solve this. I will get into working on this again during this week.
Fixed the generate populate statement
if (!connects[parent]) { | ||
connects[parent] = [ | ||
{ | ||
...newData[parent][i], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't this be ...newData[parent]
instead of ...newData[parent][i]
, all my other dynamiczone elements are deleted and only the relation one is left if its ...newData[parent][i]
, using ...newData[parent]
fixes this issue
Fixes #132
Using a recursive function, iterates through all the dynamic zones on a model and finds the components that are relations then, builds up the connections using the new and existing data for each component.
The change to server/register.js was to enable draft pages to have their references updated without publishing. Something that I believe @omikulcik was investigating