Skip to content

Commit 4893610

Browse files
authored
Merge pull request #2103 from ACoullard/AC/fix-loading-state-in-newsfeed
Fix Loading State in Newsfeed
1 parent e39f298 commit 4893610

1 file changed

Lines changed: 12 additions & 12 deletions

File tree

components/Newsfeed/Newsfeed.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import ErrorPage from "next/error"
22
import { flags } from "../featureFlags"
33
import { Timestamp } from "firebase/firestore"
44
import { useTranslation } from "next-i18next"
5-
import { useEffect, useState } from "react"
5+
import { useEffect, useMemo, useState } from "react"
66
import { Frequency, useAuth } from "../auth"
77
import { Col, Row, Spinner } from "../bootstrap"
88
import { Profile, useProfile, usePublicProfile } from "../db"
@@ -33,19 +33,17 @@ export default function Newsfeed() {
3333
useState<boolean>(true)
3434

3535
const [allResults, setAllResults] = useState<Notifications>([])
36-
const [filteredResults, setFilteredResults] = useState<Notifications>([])
36+
const [notificationsLoading, setNotificationsLoading] = useState(true)
3737

38-
useEffect(() => {
39-
const results = allResults.filter(result => {
40-
if (isShowingOrgs && result.type == `testimony`) return true
41-
if (isShowingBills && result.type == `bill`) return true
42-
if (isShowingBallotQuestions && result.type == `ballotQuestion`)
38+
const filteredResults = useMemo(() => {
39+
return allResults.filter(result => {
40+
if (isShowingOrgs && result.type === "testimony") return true
41+
if (isShowingBills && result.type === "bill") return true
42+
if (isShowingBallotQuestions && result.type == "ballotQuestion")
4343
return true
4444
return false
4545
})
46-
47-
setFilteredResults(results)
48-
}, [isShowingOrgs, isShowingBills, isShowingBallotQuestions, allResults])
46+
}, [allResults, isShowingOrgs, isShowingBallotQuestions, isShowingBills])
4947

5048
const onOrgFilterChange = (isShowing: boolean) => {
5149
setIsShowingOrgs(isShowing)
@@ -65,12 +63,14 @@ export default function Newsfeed() {
6563
if (uid) {
6664
const notifications = await notificationQuery(uid)
6765
setAllResults(notifications)
68-
setFilteredResults(notifications)
6966
}
7067
} catch (error) {
7168
console.error("Error fetching notifications: " + error)
69+
} finally {
70+
setNotificationsLoading(false)
7271
}
7372
}
73+
setNotificationsLoading(true)
7474
fetchNotifications()
7575
}, [uid])
7676

@@ -223,7 +223,7 @@ export default function Newsfeed() {
223223

224224
return (
225225
<>
226-
{result.loading && uid ? (
226+
{(uid && result.loading) || notificationsLoading ? (
227227
<Row>
228228
<Spinner animation="border" className="mx-auto" />
229229
</Row>

0 commit comments

Comments
 (0)