-
Notifications
You must be signed in to change notification settings - Fork 26
Refactor procedures list handling and improve error/retry states with new components #1664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ManAnRuck
wants to merge
2
commits into
main
Choose a base branch
from
feature/list-reloading
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import React from "react"; | ||
| import { Text } from "react-native"; | ||
|
|
||
| import { Button } from "../../../../components/Button"; | ||
| import { Centered } from "../../../../components/Centered"; | ||
|
|
||
| interface ErrorStateProps { | ||
| onRetry: () => void; | ||
| } | ||
|
|
||
| export const ErrorState: React.FC<ErrorStateProps> = ({ onRetry }) => ( | ||
| <Centered> | ||
| <Text>Verbindungsfehler</Text> | ||
| <Button | ||
| onPress={onRetry} | ||
| text="Nochmal versuchen" | ||
| textColor="blue" | ||
| backgroundColor="transparent" | ||
| /> | ||
| </Centered> | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import React from "react"; | ||
| import styled from "styled-components/native"; | ||
|
|
||
| import { Centered } from "../../../../components/Centered"; | ||
| import { ListLoading } from "../../../../components/ListLoading"; | ||
|
|
||
| interface RetryStateProps { | ||
| remainingAttempts: number; | ||
| } | ||
|
|
||
| export const RetryState: React.FC<RetryStateProps> = ({ | ||
| remainingAttempts, | ||
| }) => ( | ||
| <Centered> | ||
| <ListLoading /> | ||
| <RetryMessage>Verbindung wird wiederhergestellt…</RetryMessage> | ||
| <RetryHint> | ||
| Automatischer Versuch in Kürze. Verbleibende Versuche: {remainingAttempts} | ||
| </RetryHint> | ||
| </Centered> | ||
| ); | ||
|
|
||
| const RetryMessage = styled.Text` | ||
| margin-top: 16px; | ||
| text-align: center; | ||
| color: ${({ theme }) => theme.colors.text.primary}; | ||
| `; | ||
|
|
||
| const RetryHint = styled.Text` | ||
| margin-top: 4px; | ||
| text-align: center; | ||
| color: ${({ theme }) => theme.colors.text.secondary}; | ||
| font-size: 12px; | ||
| `; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,222 @@ | ||||||||||||||||||||||||
| import { useCallback, useEffect, useMemo, useRef, useState } from "react"; | ||||||||||||||||||||||||
| import { ApolloError, ApolloQueryResult } from "@apollo/client"; | ||||||||||||||||||||||||
| import { useRecoilValue } from "recoil"; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import { useListFilter } from "../../../../api/hooks/useListFilter"; | ||||||||||||||||||||||||
| import { constituencyState } from "../../../../api/state/constituency"; | ||||||||||||||||||||||||
| import { useLegislaturePeriodStore } from "../../../../api/state/legislaturePeriod"; | ||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||
| ParlamentIdentifier, | ||||||||||||||||||||||||
| parlaments, | ||||||||||||||||||||||||
| } from "../../../../api/state/parlament"; | ||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||
| ListType, | ||||||||||||||||||||||||
| ProceduresListQuery, | ||||||||||||||||||||||||
| useProceduresListQuery, | ||||||||||||||||||||||||
| } from "../../../../__generated__/graphql"; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| export interface SegmentedData { | ||||||||||||||||||||||||
| title: string; | ||||||||||||||||||||||||
| data: ProceduresListQuery["procedures"]; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| interface UseProceduresListResult { | ||||||||||||||||||||||||
| procedures: ProceduresListQuery["procedures"]; | ||||||||||||||||||||||||
| segmentedData: SegmentedData[]; | ||||||||||||||||||||||||
| loading: boolean; | ||||||||||||||||||||||||
| isRetrying: boolean; | ||||||||||||||||||||||||
| remainingAttempts: number; | ||||||||||||||||||||||||
| error: ApolloError | undefined; | ||||||||||||||||||||||||
| hasMore: boolean; | ||||||||||||||||||||||||
| networkStatus?: number; | ||||||||||||||||||||||||
| handleManualRefetch: () => Promise<ApolloQueryResult<ProceduresListQuery>>; | ||||||||||||||||||||||||
| handleRefresh: () => Promise<ApolloQueryResult<ProceduresListQuery>>; | ||||||||||||||||||||||||
| handleEndReached: () => Promise<void>; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const MAX_RETRY_ATTEMPTS = 3; | ||||||||||||||||||||||||
| const BASE_RETRY_DELAY_MS = 1500; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| export const useProceduresList = (list: ListType): UseProceduresListResult => { | ||||||||||||||||||||||||
| const { proceduresFilter } = useListFilter(); | ||||||||||||||||||||||||
| const constituency = useRecoilValue(constituencyState); | ||||||||||||||||||||||||
| const { legislaturePeriod } = useLegislaturePeriodStore(); | ||||||||||||||||||||||||
| const parlamentIdentifier = `BT-${legislaturePeriod}` as ParlamentIdentifier; | ||||||||||||||||||||||||
| const parlament = parlaments[parlamentIdentifier]; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const constituencies = useMemo( | ||||||||||||||||||||||||
| () => (constituency ? [constituency] : []), | ||||||||||||||||||||||||
| [constituency] | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const queryVariables = useMemo( | ||||||||||||||||||||||||
| () => ({ | ||||||||||||||||||||||||
| listTypes: [list], | ||||||||||||||||||||||||
| pageSize: 10, | ||||||||||||||||||||||||
| filter: proceduresFilter, | ||||||||||||||||||||||||
| constituencies, | ||||||||||||||||||||||||
| period: parlament?.period, | ||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||
| [constituencies, list, parlament?.period, proceduresFilter] | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const retryTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null); | ||||||||||||||||||||||||
| const latestQueryVariablesRef = useRef(queryVariables); | ||||||||||||||||||||||||
| const [retryAttempt, setRetryAttempt] = useState(0); | ||||||||||||||||||||||||
| const [isRetryScheduled, setIsRetryScheduled] = useState(false); | ||||||||||||||||||||||||
| const [hasMore, setHasMore] = useState(true); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const clearRetryTimeout = useCallback(() => { | ||||||||||||||||||||||||
| if (retryTimeoutRef.current) { | ||||||||||||||||||||||||
| clearTimeout(retryTimeoutRef.current); | ||||||||||||||||||||||||
| retryTimeoutRef.current = null; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| }, []); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const { loading, data, error, fetchMore, networkStatus, refetch } = | ||||||||||||||||||||||||
| useProceduresListQuery({ | ||||||||||||||||||||||||
| fetchPolicy: "network-only", | ||||||||||||||||||||||||
| errorPolicy: "all", | ||||||||||||||||||||||||
| variables: queryVariables, | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||||||
| latestQueryVariablesRef.current = queryVariables; | ||||||||||||||||||||||||
| }, [queryVariables]); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||||||
| if (!loading && (error || !data)) { | ||||||||||||||||||||||||
| setIsRetryScheduled((prev) => { | ||||||||||||||||||||||||
| if (prev || retryAttempt >= MAX_RETRY_ATTEMPTS) { | ||||||||||||||||||||||||
| return prev; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| clearRetryTimeout(); | ||||||||||||||||||||||||
| const delay = BASE_RETRY_DELAY_MS * (retryAttempt + 1); | ||||||||||||||||||||||||
| retryTimeoutRef.current = setTimeout(() => { | ||||||||||||||||||||||||
| setIsRetryScheduled(false); | ||||||||||||||||||||||||
| setRetryAttempt((attempt) => attempt + 1); | ||||||||||||||||||||||||
| void refetch(latestQueryVariablesRef.current); | ||||||||||||||||||||||||
| }, delay); | ||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| }, [clearRetryTimeout, data, error, loading, refetch, retryAttempt]); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||||||
| if (data) { | ||||||||||||||||||||||||
| if (retryAttempt !== 0) { | ||||||||||||||||||||||||
| setRetryAttempt(0); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| if (isRetryScheduled) { | ||||||||||||||||||||||||
| setIsRetryScheduled(false); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| clearRetryTimeout(); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| }, [clearRetryTimeout, data, isRetryScheduled, retryAttempt]); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| useEffect(() => () => clearRetryTimeout(), [clearRetryTimeout]); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||||||
| setHasMore(true); | ||||||||||||||||||||||||
| setRetryAttempt(0); | ||||||||||||||||||||||||
| setIsRetryScheduled(false); | ||||||||||||||||||||||||
| clearRetryTimeout(); | ||||||||||||||||||||||||
| }, [ | ||||||||||||||||||||||||
| clearRetryTimeout, | ||||||||||||||||||||||||
| constituencies, | ||||||||||||||||||||||||
| list, | ||||||||||||||||||||||||
| parlament?.period, | ||||||||||||||||||||||||
| proceduresFilter, | ||||||||||||||||||||||||
| ]); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const handleManualRefetch = useCallback(() => { | ||||||||||||||||||||||||
| setRetryAttempt(0); | ||||||||||||||||||||||||
| setIsRetryScheduled(false); | ||||||||||||||||||||||||
| clearRetryTimeout(); | ||||||||||||||||||||||||
| return refetch(latestQueryVariablesRef.current); | ||||||||||||||||||||||||
| }, [clearRetryTimeout, refetch]); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const handleRefresh = useCallback(() => { | ||||||||||||||||||||||||
| setHasMore(true); | ||||||||||||||||||||||||
| return handleManualRefetch(); | ||||||||||||||||||||||||
| }, [handleManualRefetch]); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const handleEndReached = useCallback(() => { | ||||||||||||||||||||||||
| if (loading || !hasMore || !data) { | ||||||||||||||||||||||||
| return Promise.resolve(); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return fetchMore({ | ||||||||||||||||||||||||
| variables: { | ||||||||||||||||||||||||
| offset: data.procedures.length, | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| }).then(({ data: fetchMoreData }) => { | ||||||||||||||||||||||||
| if (!fetchMoreData || fetchMoreData.procedures.length === 0) { | ||||||||||||||||||||||||
| setHasMore(false); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
| }, [data, fetchMore, hasMore, loading]); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const segmentedData: SegmentedData[] = useMemo(() => { | ||||||||||||||||||||||||
| if (data && ListType.Top100 === list) { | ||||||||||||||||||||||||
| return [ | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| title: "", | ||||||||||||||||||||||||
| data: data.procedures, | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (!data) { | ||||||||||||||||||||||||
| return []; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return data.procedures.reduce<SegmentedData[]>((prev, procedure) => { | ||||||||||||||||||||||||
| const { voteWeek, voteYear } = procedure; | ||||||||||||||||||||||||
| const segment = voteWeek && voteYear ? `KW ${voteWeek}/${voteYear}` : ""; | ||||||||||||||||||||||||
| const index = prev.findIndex(({ title }) => title === segment); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (index !== -1) { | ||||||||||||||||||||||||
| const next = [...prev]; | ||||||||||||||||||||||||
| const existingSegment = prev[index]; | ||||||||||||||||||||||||
| next[index] = { | ||||||||||||||||||||||||
| ...existingSegment, | ||||||||||||||||||||||||
| data: [...existingSegment.data, procedure], | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
| return next; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return [ | ||||||||||||||||||||||||
| ...prev, | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| title: segment, | ||||||||||||||||||||||||
| data: [procedure], | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||
| }, []); | ||||||||||||||||||||||||
ManAnRuck marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||
| }, [data, list]); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const isNotLoading = !loading; | ||||||||||||||||||||||||
| const hasErrorOrNoData = !!error || !data; | ||||||||||||||||||||||||
| const isRetryActive = isRetryScheduled || retryAttempt > 0; | ||||||||||||||||||||||||
| const hasRemainingAttempts = retryAttempt < MAX_RETRY_ATTEMPTS; | ||||||||||||||||||||||||
| const isRetrying = | ||||||||||||||||||||||||
| isNotLoading && hasErrorOrNoData && isRetryActive && hasRemainingAttempts; | ||||||||||||||||||||||||
|
Comment on lines
+253
to
+258
|
||||||||||||||||||||||||
| const isNotLoading = !loading; | |
| const hasErrorOrNoData = !!error || !data; | |
| const isRetryActive = isRetryScheduled || retryAttempt > 0; | |
| const hasRemainingAttempts = retryAttempt < MAX_RETRY_ATTEMPTS; | |
| const isRetrying = | |
| isNotLoading && hasErrorOrNoData && isRetryActive && hasRemainingAttempts; | |
| const isRetrying = | |
| !loading && | |
| (!!error || !data) && | |
| (isRetryScheduled || retryAttempt > 0) && | |
| retryAttempt < MAX_RETRY_ATTEMPTS; |
92 changes: 92 additions & 0 deletions
92
src/screens/Bundestag/List/hooks/useProceduresListItemRenderer.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { useCallback } from "react"; | ||
| import { ListRenderItem } from "react-native"; | ||
| import { useRouter } from "expo-router"; | ||
|
|
||
| import { useLocalVotes } from "../../../../api/state/localVotesStore"; | ||
| import { communityVoteData } from "../../../../lib/PieChartCommunityData"; | ||
| import { pieChartGovernmentData } from "../../../../lib/PieChartGovernmentData"; | ||
| import { Row } from "../../../../components/Row"; | ||
| import { ListItem } from "../../../../components/ListItem"; | ||
| import { | ||
| ListType, | ||
| ProceduresListQuery, | ||
| } from "../../../../__generated__/graphql"; | ||
|
|
||
| export type ProcedureListItem = ProceduresListQuery["procedures"][0]; | ||
|
|
||
| export const useProceduresListItemRenderer = ( | ||
| list: ListType | ||
| ): ListRenderItem<ProcedureListItem> => { | ||
| const router = useRouter(); | ||
| const localVotes = useLocalVotes(); | ||
|
|
||
| const handleProcedurePress = useCallback( | ||
| (procedureId: string) => { | ||
| router.push(`/procedure/${procedureId}`); | ||
| }, | ||
| [router] | ||
| ); | ||
|
|
||
| return useCallback<ListRenderItem<ProcedureListItem>>( | ||
| ({ | ||
| item: { | ||
| procedureId, | ||
| title, | ||
| sessionTOPHeading, | ||
| subjectGroups, | ||
| voteDate, | ||
| voteEnd, | ||
| voted: votedServer, | ||
| voteResults, | ||
| votedGovernment, | ||
| communityVotes, | ||
| }, | ||
| index, | ||
| }) => { | ||
| let subline: string | null = null; | ||
| if (sessionTOPHeading) { | ||
| subline = sessionTOPHeading; | ||
| } else if (subjectGroups) { | ||
| subline = subjectGroups.join(", "); | ||
| } | ||
|
|
||
| const govSlices = pieChartGovernmentData({ | ||
| voteResults, | ||
| votedGovernment, | ||
| }); | ||
|
|
||
| const localSelection = localVotes.find( | ||
| (localVote) => localVote.procedureId === procedureId | ||
| )?.selection; | ||
| const voted = votedServer || !!localSelection; | ||
|
|
||
| const communityVoteSlices = communityVoteData({ | ||
| communityVotes, | ||
| localSelection, | ||
| voted, | ||
| }); | ||
|
|
||
| return ( | ||
| <Row | ||
| onPress={() => handleProcedurePress(procedureId)} | ||
| testID={`ListItem-${list}-${index}`} | ||
| > | ||
| <ListItem | ||
| title={title} | ||
| subline={subline} | ||
| voteDate={voteDate ? new Date(voteDate) : undefined} | ||
| endDate={voteEnd ? new Date(voteEnd) : undefined} | ||
| voted={voted} | ||
| votes={communityVotes ? communityVotes.total || 0 : 0} | ||
| govermentChart={{ | ||
| votes: govSlices, | ||
| large: true, | ||
| }} | ||
| communityVotes={communityVoteSlices} | ||
| /> | ||
| </Row> | ||
| ); | ||
| }, | ||
| [handleProcedurePress, list, localVotes] | ||
| ); | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.