Skip to content

Commit 4de6158

Browse files
committed
fix: separate pagination states
1 parent 5de2192 commit 4de6158

File tree

3 files changed

+43
-42
lines changed

3 files changed

+43
-42
lines changed

packages/frontend/src/pages/Executions/components/ExecutionListPage.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type { IExecution, IFlow } from '@plumber/types'
22

33
import { useQuery } from '@apollo/client'
4-
import { Center, Container, Flex } from '@chakra-ui/react'
4+
import { Center, Flex } from '@chakra-ui/react'
55
import { Pagination } from '@opengovsg/design-system-react'
66

7+
import Container from '@/components/Container'
78
import ExecutionRow from '@/components/ExecutionRow'
89
import NoResultFound from '@/components/NoResultFound'
910
import PageTitle from '@/components/PageTitle'

packages/frontend/src/pages/Executions/components/FlowListPage.tsx

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
import type { IFlow } from '@plumber/types'
22

3-
import { useEffect } from 'react'
3+
import { useEffect, useMemo } from 'react'
44
import { useNavigate } from 'react-router-dom'
5-
import { Box, Center, Container, Flex } from '@chakra-ui/react'
5+
import { useQuery } from '@apollo/client'
6+
import { Box, Center, Flex } from '@chakra-ui/react'
67
import { Pagination } from '@opengovsg/design-system-react'
78

9+
import Container from '@/components/Container'
810
import DebouncedSearchInput from '@/components/DebouncedSearchInput'
911
import FlowRow from '@/components/FlowRow'
1012
import NoResultFound from '@/components/NoResultFound'
1113
import PageTitle from '@/components/PageTitle'
1214
import PrimarySpinner from '@/components/PrimarySpinner'
1315
import * as URLS from '@/config/urls'
14-
import { PageInfo } from '@/graphql/__generated__/graphql'
16+
import { GET_FLOWS } from '@/graphql/queries/get-flows'
1517
import { usePaginationAndFilter } from '@/hooks/usePaginationAndFilter'
1618

1719
const RESULTS_PER_PAGE = 10
1820
const EXECUTIONS_TITLE = 'Select a pipe to view executions'
1921

22+
const getLimitAndOffset = (page: number) => ({
23+
limit: RESULTS_PER_PAGE,
24+
offset: (page - 1) * RESULTS_PER_PAGE,
25+
})
26+
2027
interface FlowsInternalProps {
2128
isLoading: boolean
2229
isSearching: boolean
@@ -37,7 +44,7 @@ function FlowsList({ isLoading, isSearching, flows }: FlowsInternalProps) {
3744
}
3845

3946
if (hasNoUserFlows) {
40-
return <>No Executions</>
47+
return <>Create a pipe to see executions</>
4148
}
4249

4350
if (isEmptySearchResults) {
@@ -64,26 +71,38 @@ function FlowsList({ isLoading, isSearching, flows }: FlowsInternalProps) {
6471
}
6572

6673
interface FlowsListPageProps {
67-
loading: boolean
68-
flows: IFlow[]
69-
pageInfo: PageInfo
74+
setFlows: React.Dispatch<React.SetStateAction<IFlow[]>>
7075
}
7176

72-
export default function FlowListPage({
73-
loading,
74-
flows,
75-
pageInfo,
76-
}: FlowsListPageProps) {
77+
export default function FlowListPage({ setFlows }: FlowsListPageProps) {
7778
const { input, page, status, setSearchParams, isSearching } =
7879
usePaginationAndFilter()
7980
const navigate = useNavigate()
8081

82+
const { data, loading } = useQuery(GET_FLOWS, {
83+
variables: {
84+
...getLimitAndOffset(page),
85+
name: input,
86+
},
87+
})
88+
89+
const { pageInfo, edges } = data?.getFlows || {}
90+
const flows: IFlow[] = useMemo(
91+
() => edges?.map(({ node }: { node: IFlow }) => node) ?? [],
92+
[edges],
93+
)
94+
95+
useEffect(() => {
96+
setFlows(flows)
97+
}, [flows, setFlows])
98+
8199
const totalCount: number = pageInfo?.totalCount ?? 0
82100
const hasPagination = !loading && totalCount > RESULTS_PER_PAGE
83101
const hasNoUserFlows = flows.length === 0 && !isSearching
84102

85103
// ensure invalid pages won't be accessed even after deleting flows
86104
const lastPage = Math.ceil(totalCount / RESULTS_PER_PAGE)
105+
87106
useEffect(() => {
88107
// Defer the search params update till after the initial render
89108
if (lastPage !== 0 && page > lastPage) {
@@ -92,10 +111,12 @@ export default function FlowListPage({
92111
}, [lastPage, page, setSearchParams])
93112

94113
useEffect(() => {
95-
if (!loading && input && flows.length > 0 && status === 'failure') {
96-
const pipeId = flows.filter((f) => f.name === input)?.[0]?.id
97-
if (pipeId) {
98-
navigate(`${URLS.EXECUTION_FLOW(pipeId)}&status=failure`, {
114+
if (!loading && input && flows && status === 'failure') {
115+
const matchingPipeIds = flows
116+
.filter((f) => f.name === input)
117+
.map((f) => f.id)
118+
if (matchingPipeIds.length === 1) {
119+
navigate(`${URLS.EXECUTION_FLOW(matchingPipeIds[0])}&status=failure`, {
99120
replace: true,
100121
})
101122
} else {
Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,22 @@
11
import { IFlow } from '@plumber/types'
22

3-
import { ReactElement, useMemo } from 'react'
4-
import { useQuery } from '@apollo/client'
3+
import { ReactElement, useState } from 'react'
54

6-
import { GET_FLOWS } from '@/graphql/queries/get-flows'
75
import { usePaginationAndFilter } from '@/hooks/usePaginationAndFilter'
86

97
import ExecutionListPage from './components/ExecutionListPage'
108
import FlowListPage from './components/FlowListPage'
119

12-
const RESULTS_PER_PAGE = 10
13-
14-
const getLimitAndOffset = (page: number) => ({
15-
limit: RESULTS_PER_PAGE,
16-
offset: (page - 1) * RESULTS_PER_PAGE,
17-
})
18-
1910
export default function Executions(): ReactElement {
20-
const { searchParams, input, page } = usePaginationAndFilter()
11+
const { searchParams } = usePaginationAndFilter()
2112
const flowId = searchParams.get('pipeId') || ''
22-
23-
const { data, loading } = useQuery(GET_FLOWS, {
24-
variables: {
25-
...getLimitAndOffset(page),
26-
name: input,
27-
},
28-
})
29-
30-
const { pageInfo, edges } = data?.getFlows || {}
31-
const flows: IFlow[] = useMemo(
32-
() => edges?.map(({ node }: { node: IFlow }) => node) ?? [],
33-
[edges],
34-
)
13+
const [flows, setFlows] = useState<IFlow[]>([])
3514

3615
const flow = flows.find((flow) => flow.id === flowId)
3716

3817
if (flowId && flow) {
3918
return <ExecutionListPage flow={flow} />
4019
}
4120

42-
return <FlowListPage loading={loading} flows={flows} pageInfo={pageInfo} />
21+
return <FlowListPage setFlows={setFlows} />
4322
}

0 commit comments

Comments
 (0)