Skip to content

Commit

Permalink
rydd litt i pagineringen
Browse files Browse the repository at this point in the history
  • Loading branch information
sondrele committed Dec 21, 2023
1 parent 6bb045c commit 32cf4ad
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -1,44 +1,41 @@
import { Heading, Select } from "@navikt/ds-react";
import { BodyShort, Select } from "@navikt/ds-react";
import styles from "./PagineringOversikt.module.scss";

interface Props {
page: number;
pageSize: number;
onChangePageSize?: (value: number) => void;
antall: number;
maksAntall?: number;
maksAntall: number;
type: string;
antallVises?: number;
setAntallVises?: (value: number) => void;
}

const antallSize = [15, 50, 100, 250, 500, 1000];

export function PagineringsOversikt({
page,
pageSize,
onChangePageSize,
antall,
maksAntall = 0,
maksAntall,
type,
antallVises = antall,
setAntallVises,
}: Props) {
const start = (page - 1) * pageSize + 1;
const end = antall + (page - 1) * pageSize;
const summary =
antall === 0 ? `Viser 0 av 0 ${type}` : `Viser ${start}-${end} av ${maksAntall} ${type}`;
return (
<Heading level="1" size="xsmall" className={styles.container}>
{antall < 1 ? (
<span>Viser 0 av 0 {type} </span>
) : (
<span>
Viser {(page - 1) * antallVises + 1}-{antall + (page - 1) * antallVises} av {maksAntall}{" "}
{type}{" "}
</span>
)}
<div className={styles.container}>
<BodyShort weight="semibold">{summary}</BodyShort>

{setAntallVises ? (
{onChangePageSize ? (
<Select
size="small"
label="Velg antall"
hideLabel
name="size"
value={antallVises}
onChange={(e) => setAntallVises(Number.parseInt(e.currentTarget.value))}
value={pageSize}
onChange={(e) => onChangePageSize(Number.parseInt(e.currentTarget.value))}
>
{antallSize.map((ant) => (
<option key={ant} value={ant}>
Expand All @@ -47,6 +44,6 @@ export function PagineringsOversikt({
))}
</Select>
) : null}
</Heading>
</div>
);
}
12 changes: 6 additions & 6 deletions frontend/mr-admin-flate/src/components/tabell/AvtaleTabell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ export const AvtaleTabell = ({ filterAtom }: Props) => {
<div className={styles.flex}>
<PagineringsOversikt
page={filter.page}
pageSize={filter.pageSize}
antall={avtaler.length}
maksAntall={pagination?.totalCount}
maksAntall={pagination.totalCount}
type="avtaler"
antallVises={filter.pageSize}
setAntallVises={(value) => {
onChangePageSize={(value) => {
updateFilter({
page: 1,
pageSize: value,
Expand Down Expand Up @@ -238,19 +238,19 @@ export const AvtaleTabell = ({ filterAtom }: Props) => {
<PagineringContainer>
<PagineringsOversikt
page={filter.page}
pageSize={filter.pageSize}
antall={avtaler.length}
maksAntall={pagination?.totalCount}
maksAntall={pagination.totalCount}
type="avtaler"
antallVises={filter.pageSize}
/>
<Pagination
className={styles.pagination}
size="small"
page={filter.page}
count={pagination.totalPages}
onPageChange={(page) => {
updateFilter({ page });
}}
count={Math.ceil((pagination?.totalCount ?? filter.pageSize) / filter.pageSize)}
data-version="v1"
/>
</PagineringContainer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ export const TiltaksgjennomforingsTabell = ({ skjulKolonner, filterAtom }: Props
<div className={styles.flex}>
<PagineringsOversikt
page={filter.page}
pageSize={filter.pageSize}
antall={tiltaksgjennomforinger.length}
maksAntall={pagination?.totalCount}
maksAntall={pagination.totalCount}
type="tiltaksgjennomføringer"
antallVises={filter.pageSize}
setAntallVises={(size) => {
onChangePageSize={(size) => {
updateFilter({
page: 1,
pageSize: size,
Expand Down Expand Up @@ -310,19 +310,19 @@ export const TiltaksgjennomforingsTabell = ({ skjulKolonner, filterAtom }: Props
<PagineringContainer>
<PagineringsOversikt
page={filter.page}
pageSize={filter.pageSize}
antall={tiltaksgjennomforinger.length}
maksAntall={pagination?.totalCount}
maksAntall={pagination.totalCount}
type="tiltaksgjennomføringer"
antallVises={filter.pageSize}
/>
<Pagination
className={pageStyles.pagination}
size="small"
page={filter.page}
count={pagination.totalPages}
onPageChange={(page) => {
updateFilter({ page });
}}
count={Math.ceil((pagination?.totalCount ?? filter.pageSize) / filter.pageSize)}
data-version="v1"
/>
</PagineringContainer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const mockNotifikasjoner: PaginertUserNotifications = {
totalCount: 2,
currentPage: 1,
pageSize: 50,
totalPages: 1,
},
data: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export const paginertMockTiltaksgjennomforinger: PaginertTiltaksgjennomforing =
totalCount: 18,
currentPage: 1,
pageSize: 50,
totalPages: 1,
},
data: mockTiltaksgjennomforinger,
};
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const paginertMockTiltakstyper: PaginertTiltakstype = {
totalCount: Object.values(mockTiltakstyper).length,
currentPage: 1,
pageSize: 50,
totalPages: 1,
},
data: Object.values(mockTiltakstyper),
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package no.nav.mulighetsrommet.api.routes.v1.responses

import kotlinx.serialization.Serializable
import no.nav.mulighetsrommet.api.utils.PaginationParams
import kotlin.math.ceil

@Serializable
data class PaginatedResponse<T>(
Expand All @@ -13,9 +15,25 @@ data class PaginatedResponse<T>(
* Utility to wrap the [data] in a [PaginatedResponse] with a default [Pagination] derived from [data].
*/
fun <T> of(data: List<T>): PaginatedResponse<T> = PaginatedResponse(
pagination = Pagination(totalCount = data.size, currentPage = 1, pageSize = data.size),
pagination = Pagination(
totalCount = data.size,
totalPages = 1,
currentPage = 1,
pageSize = data.size,
),
data = data,
)

fun <T> of(pagination: PaginationParams, totalCount: Int, data: List<T>): PaginatedResponse<T> =
PaginatedResponse(
pagination = Pagination(
totalCount = totalCount,
currentPage = pagination.page,
pageSize = pagination.limit,
totalPages = ceil((totalCount.toDouble() / pagination.limit)).toInt(),
),
data = data,
)
}
}

Expand All @@ -24,4 +42,5 @@ data class Pagination(
val totalCount: Int,
val currentPage: Int,
val pageSize: Int,
val totalPages: Int,
)
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,7 @@ class AvtaleService(
administratorNavIdent = filter.administratorNavIdent,
)

return PaginatedResponse(
data = items,
pagination = Pagination(
totalCount = totalCount,
currentPage = pagination.page,
pageSize = pagination.limit,
),
)
return PaginatedResponse.of(pagination, totalCount, items)
}

fun getAllAvtalerSomNarmerSegSluttdato(): List<AvtaleNotificationDto> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,7 @@ class TiltaksgjennomforingService(
skalMigreres = true,
)
.let { (totalCount, data) ->
PaginatedResponse(
pagination = Pagination(
totalCount = totalCount,
currentPage = pagination.page,
pageSize = pagination.limit,
),
data = data,
)
PaginatedResponse.of(pagination, totalCount, data)
}

fun getAllVeilederflateTiltaksgjennomforing(
Expand Down Expand Up @@ -130,14 +123,7 @@ class TiltaksgjennomforingService(
administratorNavIdent = filter.administratorNavIdent,
)
.let { (totalCount, data) ->
PaginatedResponse(
pagination = Pagination(
totalCount = totalCount,
currentPage = pagination.page,
pageSize = pagination.limit,
),
data = data,
)
PaginatedResponse.of(pagination, totalCount, data)
}

fun getAllGjennomforingerSomNarmerSegSluttdato(): List<TiltaksgjennomforingNotificationDto> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.github.benmanes.caffeine.cache.Cache
import com.github.benmanes.caffeine.cache.Caffeine
import no.nav.mulighetsrommet.api.repositories.TiltakstypeRepository
import no.nav.mulighetsrommet.api.routes.v1.responses.PaginatedResponse
import no.nav.mulighetsrommet.api.routes.v1.responses.Pagination
import no.nav.mulighetsrommet.api.utils.PaginationParams
import no.nav.mulighetsrommet.api.utils.TiltakstypeFilter
import no.nav.mulighetsrommet.domain.dto.TiltakstypeDto
Expand All @@ -21,22 +20,15 @@ class TiltakstypeService(private val tiltakstypeRepository: TiltakstypeRepositor
.build()

fun getWithFilter(
tiltakstypeFilter: TiltakstypeFilter,
paginationParams: PaginationParams,
filter: TiltakstypeFilter,
pagination: PaginationParams,
): PaginatedResponse<TiltakstypeDto> {
val (totalCount, items) = tiltakstypeRepository.getAllSkalMigreres(
tiltakstypeFilter,
paginationParams,
filter,
pagination,
)

return PaginatedResponse(
data = items,
pagination = Pagination(
totalCount = totalCount,
currentPage = paginationParams.page,
pageSize = paginationParams.limit,
),
)
return PaginatedResponse.of(pagination, totalCount, items)
}

fun getById(id: UUID): TiltakstypeDto? {
Expand Down
7 changes: 7 additions & 0 deletions mulighetsrommet-api/src/main/resources/web/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1718,10 +1718,17 @@ components:
properties:
totalCount:
type: integer
totalPages:
type: integer
currentPage:
type: integer
pageSize:
type: integer
required:
- totalCount
- totalPages
- currentPage
- pageSize

PaginertTiltakstype:
type: object
Expand Down

0 comments on commit 32cf4ad

Please sign in to comment.