1

In my Angular app, I have the following Conversation & Message models:

export class Conversation {
    constructor(
        public id: string,
        public userId: string,
        public mechanicId: string,
        public messages: Message[]
    ) { }
}

export class Message {
    constructor(
        public id: string,
        public text: string,
        public userId: string,
        public timestamp: Date
    ) { }
}

Here is where I am populating the data:

private _conversations = new BehaviorSubject<Conversation[]>([
    new Conversation(
      'conversation1',
      'user3',
      'user1',
      [
        new Message('message1', 'Test message', 'user3', new Date(2018, 0O5, 0O5, 17, 23, 42, 11)),
        new Message('message2', 'Another message', 'user1', new Date(2018, 0O6, 0O5, 17, 23, 42, 11))
      ]),
    new Conversation(
      'conversation2',
      'user4',
      'user2',
      [
        new Message('message3', 'my  message', 'user4', new Date(2018, 0O7, 0O7, 17, 23, 42, 11)),
        new Message('message4', 'more messages', 'user2', new Date(2018, 0O7, 0O7, 17, 23, 42, 11)),
      ])
  ]);

I am trying to delete a specified Message within a Conversation.

For example, I want to delete the message with id = message3, which is inside conversation2.

I am currently able to delete an entire Conversation:

deleteConversation(convoId: string) {
    return this.conversations.pipe(
      take(1),
      delay(1000),
      tap(conversations => {
        this._conversations.next(conversations.filter(convo => convo.id !== convoId));
      }));
  }

But I am struggling applying this code to deleteMessage():

deleteMessage(conversationId: string, messageId: string) {
    return this.conversations.pipe(
      take(1),
      delay(1000),
      tap(conversations => {

      })
    )
  }

2 Answers 2

2

When you have the convoToUpdate, why dont you do the same filter?

convoToUpdate.messages = [...convoToUpdate.messages.filter(message => message.id != messageId)]
Sign up to request clarification or add additional context in comments.

Comments

1

Try:

deleteMessage(conversationId: string, messageId: string) {
    return this.conversations.pipe(
        take(1),
        delay(1000),
        tap(conversations => {
            this._conversations.next(conversations.map(convo => {
                if (convo.id === conversationId) {
                    convo.messages = convo.messages.filter(mes => mes.id !== messageId)
                }
                return convo;
            }));
        })
    )
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.