Skip to content

Commit

Permalink
feat(Dashboard): apply sorting rules
Browse files Browse the repository at this point in the history
  • Loading branch information
LamaEats committed Jun 20, 2024
1 parent 0d673a5 commit aa69c45
Show file tree
Hide file tree
Showing 5 changed files with 614 additions and 286 deletions.
26 changes: 26 additions & 0 deletions cypress/e2e/projects.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const testProjectTitle = 'Test project title in e2e';
const testProjectDescription = 'Test project description in e2e';
const testProjectKey = keyPredictor(testProjectTitle);
const customKey = 'CUSTOMKEY';
const fifthProjectForTest = 'Fifth project';
const fifthProjectKey = keyPredictor(fifthProjectForTest);
const sixthProjectForTest = 'Sixth project';
const sixthProjectKey = keyPredictor(sixthProjectForTest);

const testProjectTitleRu = 'Тестовый проект';
const testProjectKeyRu = keyPredictor(testProjectTitleRu);
Expand Down Expand Up @@ -206,6 +210,28 @@ describe('Projects', () => {
});

describe('project page', () => {
before(() => {
cy.task('db:create:project', {
title: fifthProjectForTest,
key: fifthProjectKey,
ownerEmail: Cypress.env('ADMIN_EMAIL'),
}).then((p) => {
Cypress.env('fifthProject', p.id);
});
cy.task('db:create:project', {
title: sixthProjectForTest,
key: sixthProjectKey,
ownerEmail: Cypress.env('ADMIN_EMAIL'),
}).then((p) => {
Cypress.env('sixthProject', p.id);
});
});

after(() => {
cy.task('db:remove:project', { id: Cypress.env('sixthProject') });
cy.task('db:remove:project', { id: Cypress.env('fifthProject') });
});

beforeEach(() => {
cy.get(filtersPanelResetButton.query).should('exist').click();
cy.get(projectListItem.query).should('exist').and('have.length.greaterThan', 1);
Expand Down
8 changes: 6 additions & 2 deletions src/components/DashboardPage/DashboardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const DashboardPage = ({ user, ssrTime, defaultPresetFallback }: External
trpc.v2.project.userProjectsWithGoals.useInfiniteQuery(
{ goalsQuery: queryState },
{
getNextPageParam: (p) => p.pagiantion.offset,
getNextPageParam: ({ pagination }) => pagination.offset,
keepPreviousData: true,
staleTime: refreshInterval,
},
Expand All @@ -52,7 +52,11 @@ export const DashboardPage = ({ user, ssrTime, defaultPresetFallback }: External
return acc;
}, []);

return [gr, gr.flatMap((group) => group.goals).length, pages?.[pages.length - 1].totalGoalsCount ?? 0];
return [
gr,
gr.reduce((acc, group) => acc + group._count.goals, 0),
pages?.[pages.length - 1].totalGoalsCount ?? 0,
];
}, [pages]);

useFMPMetric(!!data);
Expand Down
54 changes: 51 additions & 3 deletions trpc/queries/goalV2.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,64 @@
import { sql } from 'kysely';
import { AnyColumnWithTable, Expression, OrderByExpression, sql } from 'kysely';
import { jsonBuildObject } from 'kysely/helpers/postgres';
import { OrderByDirection, DirectedOrderByStringReference } from 'kysely/dist/cjs/parser/order-by-parser';

import { db } from '../connection/kysely';
import { QueryWithFilters } from '../../src/schema/common';
import { Role } from '../../generated/kysely/types';
import { DB, Role } from '../../generated/kysely/types';

interface GoalQueryParams {
query?: QueryWithFilters;
activityId: string;
role: Role;
}

const mapSortParamsToTableColumns = (sort: QueryWithFilters['sort']): Array<OrderByExpression<DB, 'Goal', unknown>> => {
if (!sort) {
return ['Goal.updatedAt desc'];
}

const mapToTableColumn: Record<
keyof NonNullable<QueryWithFilters['sort']>,
AnyColumnWithTable<DB, 'Goal'> | Record<OrderByDirection, Expression<string>>
> = {
title: 'Goal.title',
state: {
asc: sql`state.title asc`,
desc: sql`state.title desc`,
},
priority: {
asc: sql`priority.value asc`,
desc: sql`priority.value desc`,
},
project: {
asc: sql`project.title asc`,
desc: sql`project.title desc`,
},
activity: {
asc: sql`activity.name asc`,
desc: sql`activity.name desc`,
},
owner: {
asc: sql`owner.name asc`,
desc: sql`owner.name desc`,
},
updatedAt: 'Goal.updatedAt',
createdAt: 'Goal.createdAt',
};

return (
Object.entries(sort) as Array<[keyof NonNullable<QueryWithFilters['sort']>, NonNullable<OrderByDirection>]>
).map<OrderByExpression<DB, 'Goal', unknown>>(([key, dir]) => {
const rule = mapToTableColumn[key];

if (typeof rule === 'string') {
return `${rule} ${dir}` as DirectedOrderByStringReference<DB, 'Goal', unknown>;
}

return rule[dir];
});
};

export const getGoalList = (params: GoalQueryParams) => {
return db
.selectFrom('Goal')
Expand Down Expand Up @@ -157,6 +205,6 @@ export const getGoalList = (params: GoalQueryParams) => {
),
)
.limit(30)
.orderBy('Goal.updatedAt desc')
.orderBy(mapSortParamsToTableColumns(params.query?.sort))
.groupBy(['Goal.id', 'owner.id', 'state.id', 'priority.id']);
};
Loading

0 comments on commit aa69c45

Please sign in to comment.