Skip to content

Commit 770e473

Browse files
committed
Start - still needs api call changes
1 parent 9c0cc65 commit 770e473

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed

site/src/pages/WorkspacesPage/WorkspacesPage.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,25 @@ import { WorkspacesPageView } from "./WorkspacesPageView"
1010
const WorkspacesPage: FC = () => {
1111
const [searchParams, setSearchParams] = useSearchParams()
1212
const filter = searchParams.get("filter") ?? workspaceFilterQuery.me
13+
const currentPage = searchParams.get("page")
14+
? Number(searchParams.get("page"))
15+
: 1
1316
const [workspacesState, send] = useMachine(workspacesMachine, {
1417
context: {
18+
page: currentPage,
19+
limit: 25,
1520
filter,
1621
},
22+
actions: {
23+
onPageChange: ({ page }) => {
24+
navigate({
25+
search: `?page=${page}`,
26+
})
27+
},
28+
},
1729
})
1830

19-
const { workspaceRefs } = workspacesState.context
31+
const { workspaceRefs, count, page, limit } = workspacesState.context
2032

2133
return (
2234
<>
@@ -28,6 +40,18 @@ const WorkspacesPage: FC = () => {
2840
filter={workspacesState.context.filter}
2941
isLoading={!workspaceRefs}
3042
workspaceRefs={workspaceRefs}
43+
count={count}
44+
page={page}
45+
limit={limit}
46+
onNext={() => {
47+
send("NEXT")
48+
}}
49+
onPrevious={() => {
50+
send("PREVIOUS")
51+
}}
52+
onGoToPage={(page) => {
53+
send("GO_TO_PAGE", { page })
54+
}}
3155
onFilter={(query) => {
3256
setSearchParams({ filter: query })
3357
send({

site/src/pages/WorkspacesPage/WorkspacesPageView.tsx

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import Link from "@material-ui/core/Link"
2+
import { Maybe } from "components/Conditionals/Maybe"
3+
import { PaginationWidget } from "components/PaginationWidget/PaginationWidget"
24
import { FC } from "react"
35
import { Link as RouterLink } from "react-router-dom"
46
import { Margins } from "../../components/Margins/Margins"
@@ -26,13 +28,30 @@ export const Language = {
2628
export interface WorkspacesPageViewProps {
2729
isLoading?: boolean
2830
workspaceRefs?: WorkspaceItemMachineRef[]
31+
count?: number
32+
page: number
33+
limit: number
2934
filter?: string
3035
onFilter: (query: string) => void
36+
onNext: () => void
37+
onPrevious: () => void
38+
onGoToPage: (page: number) => void
3139
}
3240

3341
export const WorkspacesPageView: FC<
3442
React.PropsWithChildren<WorkspacesPageViewProps>
35-
> = ({ isLoading, workspaceRefs, filter, onFilter }) => {
43+
> = ({
44+
isLoading,
45+
workspaceRefs,
46+
count,
47+
page,
48+
limit,
49+
filter,
50+
onFilter,
51+
onNext,
52+
onPrevious,
53+
onGoToPage,
54+
}) => {
3655
const presetFilters = [
3756
{ query: workspaceFilterQuery.me, name: Language.yourWorkspacesButton },
3857
{ query: workspaceFilterQuery.all, name: Language.allWorkspacesButton },
@@ -72,6 +91,19 @@ export const WorkspacesPageView: FC<
7291
workspaceRefs={workspaceRefs}
7392
filter={filter}
7493
/>
94+
95+
<Maybe condition={count !== undefined && count > limit}>
96+
<PaginationWidget
97+
prevLabel=""
98+
nextLabel=""
99+
onPrevClick={onPrevious}
100+
onNextClick={onNext}
101+
onPageClick={onGoToPage}
102+
numRecords={count}
103+
activePage={page}
104+
numRecordsPerPage={limit}
105+
/>
106+
</Maybe>
75107
</Margins>
76108
)
77109
}

site/src/xServices/workspaces/workspacesXService.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,17 @@ interface WorkspacesContext {
206206
workspaceRefs?: WorkspaceItemMachineRef[]
207207
filter: string
208208
getWorkspacesError?: Error | unknown
209+
page: number
210+
count?: number
211+
limit: number
209212
}
210213

211214
type WorkspacesEvent =
212215
| { type: "GET_WORKSPACES"; query?: string }
213216
| { type: "UPDATE_VERSION"; workspaceId: string }
217+
| { type: "NEXT" }
218+
| { type: "PREVIOUS" }
219+
| { type: "GO_TO_PAGE"; page: number }
214220

215221
export const workspacesMachine = createMachine(
216222
{
@@ -235,12 +241,24 @@ export const workspacesMachine = createMachine(
235241
on: {
236242
GET_WORKSPACES: {
237243
actions: "assignFilter",
238-
target: ".gettingWorkspaces",
244+
target: "gettingWorkspaces",
239245
internal: false,
240246
},
241247
UPDATE_VERSION: {
242248
actions: "triggerUpdateVersion",
243249
},
250+
NEXT: {
251+
actions: ["assignNextPage", "onPageChange"],
252+
target: "gettingWorkspaces",
253+
},
254+
PREVIOUS: {
255+
actions: ["assignPreviousPage", "onPageChange"],
256+
target: "gettingWorkspaces",
257+
},
258+
GO_TO_PAGE: {
259+
actions: ["assignPage", "onPageChange"],
260+
target: "gettingWorkspaces",
261+
},
244262
},
245263
initial: "gettingWorkspaces",
246264
states: {
@@ -320,6 +338,7 @@ export const workspacesMachine = createMachine(
320338
},
321339
assignUpdatedWorkspaceRefs: assign({
322340
workspaceRefs: (_, event) => {
341+
323342
const newWorkspaceRefs = event.data.newWorkspaces.map((workspace) =>
324343
spawn(
325344
workspaceItemMachine.withContext({ data: workspace }),
@@ -329,6 +348,18 @@ export const workspacesMachine = createMachine(
329348
return event.data.refsToKeep.concat(newWorkspaceRefs)
330349
},
331350
}),
351+
assignNextPage: assign({
352+
page: ({ page }) => page + 1,
353+
}),
354+
assignPreviousPage: assign({
355+
page: ({ page }) => page - 1,
356+
}),
357+
assignPage: assign({
358+
page: (_, { page }) => page,
359+
}),
360+
assignCount: assign({
361+
count: (_, { count }) => count
362+
})
332363
},
333364
services: {
334365
getWorkspaces: (context) =>

0 commit comments

Comments
 (0)