Skip to content

Commit

Permalink
refacto : pagination dto
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
Pascal-Delange committed Jan 22, 2024
1 parent ada3786 commit 6ee354b
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 27 deletions.
14 changes: 5 additions & 9 deletions packages/app-builder/src/components/PaginationButtons.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { type PaginationParams } from '@app-builder/models/pagination';
import {
type PaginatedResponse,
type PaginationParams,
} from '@app-builder/models/pagination';
import { Trans, useTranslation } from 'react-i18next';
import { Button } from 'ui-design-system';
import { Icon } from 'ui-icons';
Expand All @@ -13,13 +16,6 @@ export const paginationSchema = z.object({
sorting: z.enum(['created_at']).optional(),
});

export type PaginatedResponse<T> = {
items: T[];
total_count: { value: number; is_max_count: boolean };
startIndex: number;
endIndex: number;
};

type ItemWithId = {
id: string;
};
Expand All @@ -30,7 +26,7 @@ type PaginationsButtonsProps = PaginatedResponse<ItemWithId> & {

export const PaginationButtons = ({
items,
total_count: { value: total, is_max_count: isMaxCount },
totalCount: { value: total, isMaxCount },
startIndex,
endIndex,
onPaginationChange,
Expand Down
26 changes: 26 additions & 0 deletions packages/app-builder/src/models/pagination.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
import { type Pagination as PaginationDto } from 'marble-api';

export type Pagination = {
totalCount: { value: number; isMaxCount: boolean };
startIndex: number;
endIndex: number;
};

export type PaginatedResponse<T> = {
items: T[];
totalCount: { value: number; isMaxCount: boolean };
startIndex: number;
endIndex: number;
};

export function fromPaginationDto(pagination: PaginationDto): Pagination {
return {
totalCount: {
value: pagination.total_count.value,
isMaxCount: pagination.total_count.is_max_count,
},
startIndex: pagination.start_index,
endIndex: pagination.end_index,
};
}

export type PaginationParams = {
next?: true | false;
previous?: true | false;
Expand Down
14 changes: 11 additions & 3 deletions packages/app-builder/src/repositories/CaseRepository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { type PaginatedResponse } from '@app-builder/components/PaginationButtons';
import { type MarbleApi } from '@app-builder/infra/marble-api';
import { adaptCaseDetailDto, type CaseDetail } from '@app-builder/models/cases';
import { type FiltersWithPagination } from '@app-builder/models/pagination';
import {
type FiltersWithPagination,
fromPaginationDto,
type PaginatedResponse,
} from '@app-builder/models/pagination';
import { add } from 'date-fns/add';
import { type Case, type CaseStatus, type UpdateCaseBody } from 'marble-api';
import { Temporal } from 'temporal-polyfill';
Expand Down Expand Up @@ -53,11 +56,16 @@ export function getCaseRepository() {
startDate = add(new Date(), fromNowDuration).toISOString();
}

return marbleApiClient.listCases({
const { items, ...pagination } = await marbleApiClient.listCases({
...rest,
startDate,
endDate,
});

return {
items,
...fromPaginationDto(pagination),
};
},
getCase: async ({ caseId }) => {
const result = await marbleApiClient.getCase(caseId);
Expand Down
14 changes: 11 additions & 3 deletions packages/app-builder/src/repositories/DecisionRepository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { type PaginatedResponse } from '@app-builder/components';
import { type MarbleApi } from '@app-builder/infra/marble-api';
import { type FiltersWithPagination } from '@app-builder/models/pagination';
import {
type FiltersWithPagination,
fromPaginationDto,
type PaginatedResponse,
} from '@app-builder/models/pagination';
import { add } from 'date-fns/add';
import { type Decision, type Outcome } from 'marble-api';
import { Temporal } from 'temporal-polyfill';
Expand Down Expand Up @@ -44,11 +47,16 @@ export function getDecisionRepository() {
startDate = add(new Date(), fromNowDuration).toISOString();
}

return marbleApiClient.listDecisions({
const { items, ...pagination } = await marbleApiClient.listDecisions({
...rest,
startDate,
endDate,
});

return {
items,
...fromPaginationDto(pagination),
};
},
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ export default function Inbox() {
<span className="font-bold">
{t('settings:inboxes.inbox_details.case_count')}
</span>
{!caseList.total_count.is_max_count
? caseList.total_count.value + '+'
: caseList.total_count.value}
{!caseList.totalCount.isMaxCount
? caseList.totalCount.value + '+'
: caseList.totalCount.value}
</div>
</CollapsiblePaper.Content>
</CollapsiblePaper.Container>
Expand Down Expand Up @@ -140,7 +140,7 @@ export default function Inbox() {
</CollapsiblePaper.Content>
</CollapsiblePaper.Container>

{caseList.total_count.value === 0 ? (
{caseList.totalCount.value === 0 ? (
<DeleteInbox inbox={inbox} />
) : (
<Tooltip.Default
Expand Down
8 changes: 4 additions & 4 deletions packages/marble-api/scripts/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3958,13 +3958,13 @@ components:
Pagination:
type: object
required:
- startIndex
- endIndex
- start_index
- end_index
- total_count
properties:
startIndex:
start_index:
type: integer
endIndex:
end_index:
type: integer
total_count:
$ref: '#/components/schemas/PaginationCount'
Expand Down
4 changes: 2 additions & 2 deletions packages/marble-api/src/fixtures/decisions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ const fakeDecisions: Decision[] = Array.from({
const fakeDecisionsWithPagination = {
items: fakeDecisions,
total_count: { value: fakeDecisions.length, is_max_count: false },
startIndex: 0,
endIndex: fakeDecisions.length,
start_index: 0,
end_index: fakeDecisions.length,
};

export const listDecisionsFake: typeof listDecisions = () =>
Expand Down
4 changes: 2 additions & 2 deletions packages/marble-api/src/generated/marble-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export type PaginationCount = {
is_max_count: boolean;
};
export type Pagination = {
startIndex: number;
endIndex: number;
start_index: number;
end_index: number;
total_count: PaginationCount;
};
export type Error = {
Expand Down

0 comments on commit 6ee354b

Please sign in to comment.