Skip to content

Commit

Permalink
fikser for paginering
Browse files Browse the repository at this point in the history
  • Loading branch information
di0nys1us committed Jun 3, 2024
1 parent 4174bb4 commit 08c9800
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
21 changes: 12 additions & 9 deletions client/src/mocks/handlers/oppgaver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { addWeeks } from 'date-fns'
import { delay, http, HttpResponse } from 'msw'

import type { OppgavelisteResponse } from '../../oppgaveliste/useOppgaveliste.ts'
import {
DokumentOppgaveStatusType,
OmrådeFilter,
Expand All @@ -14,7 +16,6 @@ import {
import { formaterNavn } from '../../utils/formater'
import type { StoreHandlersFactory } from '../data'
import { enheter } from '../data/enheter'
import { addWeeks } from 'date-fns'

export const oppgaveHandlers: StoreHandlersFactory = ({
journalpostStore,
Expand Down Expand Up @@ -60,15 +61,15 @@ export const oppgaveHandlers: StoreHandlersFactory = ({

const pagedOppgaver: OppgaverResponse = {
oppgaver: oppgaver,
totalCount: oppgaver.length,
totalElements: oppgaver.length,
}

return HttpResponse.json(pagedOppgaver)
} else {
const oppgaver = await oppgaveStore.alle()
const pagedOppgaver: OppgaverResponse = {
oppgaver: oppgaver,
totalCount: oppgaver.length,
totalElements: oppgaver.length,
}
return HttpResponse.json(pagedOppgaver)
}
Expand All @@ -80,10 +81,10 @@ export const oppgaveHandlers: StoreHandlersFactory = ({
const sakerFilter = url.searchParams.get('saksbehandler')
const områdeFilter = url.searchParams.get('område')
const sakstypeFilter = url.searchParams.get('type')
const currentPage = Number(url.searchParams.get('page'))
const pageNumber = Number(url.searchParams.get('page'))
const pageSize = Number(url.searchParams.get('limit'))

const startIndex = currentPage - 1
const startIndex = pageNumber - 1
const endIndex = startIndex + pageSize
const oppgaver = [...(await sakStore.oppgaver()), ...(await barnebrillesakStore.oppgaver())]
const filtrerteOppgaver = oppgaver
Expand All @@ -105,11 +106,13 @@ export const oppgaveHandlers: StoreHandlersFactory = ({

const haster = (oppgave: Oppgave) => oppgave.hast?.årsaker?.length || 0

const response = {
const response: OppgavelisteResponse = {
oppgaver: !filterApplied ? oppgaver.slice(startIndex, endIndex) : filtrerteOppgaver.slice(startIndex, endIndex),
totalCount: !filterApplied ? oppgaver.length : filtrerteOppgaver.length,
pageSize: pageSize,
currentPage: currentPage,
totalElements: !filterApplied ? oppgaver.length : filtrerteOppgaver.length,
pageRequest: {
pageNumber,
pageSize,
},
antallHaster: !filterApplied ? oppgaver.filter(haster).length : filtrerteOppgaver.filter(haster).length,
}

Expand Down
4 changes: 2 additions & 2 deletions client/src/oppgaveliste/Oppgaveliste.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function Oppgaveliste() {
const [currentPage, setCurrentPage] = useLocalStorageState('currentPage', 1)
const [sort, setSort] = useLocalStorageState<SortState>('sortState', { orderBy: 'MOTTATT', direction: 'ascending' })

const { oppgaver, totalCount, antallHaster, isLoading, error, mutate } = useOppgaveliste(currentPage, sort, {
const { oppgaver, totalElements, antallHaster, isLoading, error, mutate } = useOppgaveliste(currentPage, sort, {
sakerFilter,
statusFilter,
sakstypeFilter,
Expand Down Expand Up @@ -305,7 +305,7 @@ export function Oppgaveliste() {
</Table.Body>
</Table>
<Paging
totalCount={totalCount}
totalCount={totalElements}
currentPage={currentPage}
onPageChange={(page: number) => setCurrentPage(page)}
/>
Expand Down
24 changes: 13 additions & 11 deletions client/src/oppgaveliste/useOppgaveliste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { PAGE_SIZE } from './paging/Paging'

interface DataResponse {
oppgaver: Oppgave[]
totalCount: number
totalElements: number
currentPage: number
pageSize: number
antallHaster: number
Expand All @@ -27,22 +27,24 @@ interface PathConfigType {
queryParams: Record<string, string>
}

interface Filters {
interface OppgavelisteFilters {
sakerFilter: string
statusFilter: string
sakstypeFilter: string
områdeFilter: string
}

interface OppgavelisteResponse {
export interface OppgavelisteResponse {
oppgaver: Oppgave[]
totalCount: number
currentPage: number
pageSize: number
totalElements: number
pageRequest: {
pageNumber: number
pageSize: number
}
antallHaster: number
}

function pathConfig(currentPage: number, sort: SortState, filters: Filters): PathConfigType {
function pathConfig(currentPage: number, sort: SortState, filters: OppgavelisteFilters): PathConfigType {
const sortDirection = sort.direction === 'ascending' ? 'ASC' : 'DESC'
const pagingParams = { limit: PAGE_SIZE, page: currentPage }
const sortParams = { sort_by: `${sort.orderBy}.${sortDirection}` }
Expand Down Expand Up @@ -75,7 +77,7 @@ function buildQueryParamString(queryParams: Record<string, string>) {
.join('&')
}

export function useOppgaveliste(currentPage: number, sort: SortState, filters: Filters): DataResponse {
export function useOppgaveliste(currentPage: number, sort: SortState, filters: OppgavelisteFilters): DataResponse {
const { path, queryParams } = pathConfig(currentPage, sort, filters)
const fullPath = `${path}?${buildQueryParamString(queryParams)}`
const { data, error, mutate } = useSwr<{ data: OppgavelisteResponse }>(fullPath, httpGet, { refreshInterval: 10000 })
Expand All @@ -90,9 +92,9 @@ export function useOppgaveliste(currentPage: number, sort: SortState, filters: F

return {
oppgaver: data?.data.oppgaver || [],
totalCount: data?.data.totalCount || 0,
currentPage: data?.data.currentPage || currentPage,
pageSize: data?.data.pageSize || PAGE_SIZE,
totalElements: data?.data.totalElements || 0,
currentPage: data?.data.pageRequest.pageNumber || currentPage,
pageSize: data?.data.pageRequest.pageSize || PAGE_SIZE,
antallHaster: data?.data.antallHaster || 0,
isLoading: !error && !data,
error,
Expand Down
2 changes: 1 addition & 1 deletion client/src/types/types.internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ export interface OppgaveV2 {

export interface OppgaverResponse {
oppgaver: OppgaveV2[]
totalCount: number
totalElements: number
}

export interface Bydel {
Expand Down

0 comments on commit 08c9800

Please sign in to comment.