Webchat icon indicating copy to clipboard operation
Webchat copied to clipboard

mutated redux state

Open DiesIrae opened this issue 7 years ago • 2 comments

Hi!

The code

fmtMessages = () => {
    return reduceRight(
      this.props.messages,
      (acc, cur) => {
        const nextMessage = acc[0]

        cur.displayIcon = !nextMessage || nextMessage.participant.isBot !== cur.participant.isBot

        acc.unshift(cur)
        return acc
      },
      [],
    )
  }

is muting redux state, which is dangerous practise. I believe it to be the source of a bug that duplicates sent messages, although I'm not sure.

this is my correction to the code:

fmtMessages = () => {
    return reduceRight(
      this.props.messages,
      (acc, cur) => {
        const nextMessage = acc[0]

        cur.displayIcon = !nextMessage || nextMessage.participant.isBot !== cur.participant.isBot

        acc.unshift(cur)
        return acc
      },
      [],
    )
  }

DiesIrae avatar Apr 03 '18 08:04 DiesIrae

Hi @DiesIrae,

Thanks for taking your time to debug our application. It seems that you c/c the same code, so we can't test it.

Don't hesitate to do a merge request. It will help us a lot !

OlivierNguyen avatar Apr 03 '18 15:04 OlivierNguyen

Hi @OlivierNguyen !

You're welcome. Indeed, here is the updated code:

fmtMessages = () => {
    return reduceRight(
      this.props.messages,
      (acc, cur) => {
        const nextMessage = acc[0]

        const displayIcon = !nextMessage || nextMessage.participant.isBot !== cur.participant.isBot
        const newCur = {...cur, displayIcon}

        acc.unshift(newCur)
        return acc
      },
      [],
    )
  }

DiesIrae avatar Apr 03 '18 15:04 DiesIrae