Replies: 3 comments 6 replies
-
What version packages are you using, and can you provide the definition of ApiResource? |
Beta Was this translation helpful? Give feedback.
-
Wanna run a design idea by you: class Chatgroup extends Entity {
id = '';
static schema = {
members: new schema.Collection(ChatMember, (parent: Chatgroup, key: string) => ({groupId: parent.id}))
}
}
class ChatMember extends Entity {
id = '';
}
const getMembers = new RestEndpoint({
path: '/members',
searchParams: { groupId: '' },
// (...args) => since this isn't nested
schema: new schema.Collection(ChatMember, ({ groupId }) => ({ groupId })),
});
const getMembersNextPage = getMembers.extend({
searchParams: { groupId: '', page: '' },
// schema already removes the page, automatically appending based only on groupId
schema: getMembers.schema.push,
});
const createMember = getMembers.extend({
method: 'POST',
schema: getMembers.schema.push,
}); What this means is that, creating a member would both add to getMembers endpoint (based on groupId) but also the nested members list in ChatGroup. If you want to place at the beginning you can use schema.unshift instead, and if there's an insertion algo you want, you can run schema.insert. const members = useSuspense(getMembers, { groupId: 'bestgroup' });
const chatgroup = useSuspense(getChatGroup, { id: 'bestgroup' });
invariant(members === chatgroup.members); We could even add a specialization to the endpoint to make createMember easier: const createMember = getMembers.push;
ctrl.fetch(getMembers.push, { groupId: 'bestgroup'}, createdMemberData); Adding to multiple collections// this adds to any list *in store* that has same members as the urlParams
// so fetch(createMemberAddToMultiple, { groupId: 'bob', isAdmin: true }, data)
// would possibly add to {}, {groupId: 'bob'}, {isAdmin: true}, {groupId: 'bob', isAdmin: 'true' } - but only those already in the store
const createMemberAddToMultiple = getMembers.extend({
method: 'POST',
schema: getMembers.schema.push(urlParams => collectionKey =>
Object.values(collectionKey).every(([key, value]) => urlParams[key] === value)
),
}); Store shape{
entities: {
ChatGroup: {
'5': { id: '5', members: '{"groupId":"bestgroup"}' },
},
},
collections: {
ChatMember: {
'{"groupId":"bestgroup"}': ['firstId', 'secondId', 'thirdId'],
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Collections V1 have landed. Will release beta soon. Discussion can continue in the Collections RFC For most up to date usage see the Collection documentation |
Beta Was this translation helpful? Give feedback.
-
I'm working on a component that contains an entity named "ChatGroup" that contains a child entity named "ChatMember".
I am able to list the ChatGroup entities perfectly fine and it includes the ChatMembers as it should. But, I am having difficulty adding members to the ChatGroup.members list.
I am only able to successfully add/delete ChatMember's from a group that existed on initial load. When I add a new ChatMember, I can see that the member is being added correctly using Redux tools in inspector but it is not being added as a child of the ChatGroup.
To list the groups I am using:
const data = useSuspense(ChatGroup.list(), {})
I then iterate through the groups and list the members of each group. When you click on a group I have a separate view that lists all of the members and the options to add or remove a member.
When adding a member I am using:
fetch(ChatMember.create(), { groupId: :id }, { customerId: :id})
, which response with the newly created chat member object.After the ChatMember add, how can I ensure that it is being added to the correct ChatGroup cache item referencing the
groupId
that was used in the initial request or the groupId that was returned in the response?Beta Was this translation helpful? Give feedback.
All reactions