Skip to content

Adding a child list item and updating the parent list #2554

Description

@osbash

Discussed in #2553

Originally posted by osbash April 1, 2023
I'm working on a component that contains an entity named "ChatGroup" that contains a child entity named "ChatMember".

class ChatGroup extends ApiResource {
    static readonly urlRoot = `/api/account/chats`
    readonly id: Data.ID = ''
    readonly name: string = ''
    readonly description: string = ''
    readonly uuid: string = ''
    readonly private: boolean = false
    readonly type: ChatGroupType = 'group'
    readonly members: ChatMember[] = []
    readonly graphic?: string
}

static schema = {
  members: [ChatMember]
}
class ChatMember extends ApiResource {
    static readonly urlRoot = `/api/account/chats/:groupId/members/:id`
    readonly id: Data.ID = ''
    readonly admin?: boolean
    readonly customerId: Data.ID = ''
    readonly inviteEnabled: boolean = false
    readonly messagesEnabled: boolean = false
    readonly groupId: Data.ID = ''
    readonly firstName: string = ''
    readonly lastName: string = ''
    readonly middleName: string = ''
    readonly fullName: string = ''
    readonly profilePhoto: string = ''
    readonly createdOn: Date = new Date()
}

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions