From 5dbcafb9980d34d64cf81a55646799e626a893f5 Mon Sep 17 00:00:00 2001 From: ThibG Date: Wed, 25 Mar 2020 22:39:55 +0100 Subject: [PATCH] [Glitch] Fix frontend crash when deleting announcements Port 15c02a71609abd117d38eaf83f6e8be6df7821fb to glitch-soc Signed-off-by: Thibaut Girka --- .../getting_started/components/announcements.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/javascript/flavours/glitch/features/getting_started/components/announcements.js b/app/javascript/flavours/glitch/features/getting_started/components/announcements.js index 5f6e6d84c3..acaa78fe34 100644 --- a/app/javascript/flavours/glitch/features/getting_started/components/announcements.js +++ b/app/javascript/flavours/glitch/features/getting_started/components/announcements.js @@ -378,6 +378,14 @@ class Announcements extends ImmutablePureComponent { index: 0, }; + static getDerivedStateFromProps(props, state) { + if (props.announcements.size > 0 && state.index >= props.announcements.size) { + return { index: props.announcements.size - 1 }; + } else { + return null; + } + } + componentDidMount () { this._markAnnouncementAsRead(); } @@ -389,7 +397,7 @@ class Announcements extends ImmutablePureComponent { _markAnnouncementAsRead () { const { dismissAnnouncement, announcements } = this.props; const { index } = this.state; - const announcement = announcements.get(index) || announcements.get(index - 1); + const announcement = announcements.get(index); if (!announcement.get('read')) dismissAnnouncement(announcement.get('id')); } @@ -407,7 +415,7 @@ class Announcements extends ImmutablePureComponent { render () { const { announcements, intl } = this.props; - const index = this.state.index < announcements.size ? this.state.index : announcements.size - 1; + const { index } = this.state; if (announcements.isEmpty()) { return null;