Skip to content

Commit

Permalink
feat(ProjectListItemCollapsible): prisma project.getByIds endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
asabotovich committed Jun 21, 2023
1 parent e2f9386 commit a0634f8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/components/CollapsableItem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC, ReactNode, useEffect, useRef, useState } from 'react';
import { FC, ReactNode } from 'react';
import styled, { css } from 'styled-components';
import { gray7, radiusM } from '@taskany/colors';
import { nullable } from '@taskany/bricks';
Expand Down
23 changes: 5 additions & 18 deletions src/components/ProjectPage/ProjectPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,10 @@ const PageProjectListItem: FC<

const [fetchChildEnabled, setFetchChildEnabled] = useState(false);

const childrenQueries = trpc.useQueries((t) =>
(project?.children.map(({ id }) => id) || []).map((id) =>
t.project.getById(id, { enabled: fetchChildEnabled, refetchOnWindowFocus: false }),
),
);

const loading = useMemo(() => childrenQueries.some(({ status }) => status === 'loading'), [childrenQueries]);
const childrenProjects = useMemo(
() =>
childrenQueries.reduce((acum, { data }) => {
if (data) {
acum.push(data);
}
return acum;
}, [] as NonNullable<ProjectByIdReturnType>[]),
[childrenQueries],
);
const ids = useMemo(() => project?.children.map(({ id }) => id) || [], [project]);
const { data: childrenProjects = [], status } = trpc.project.getByIds.useQuery(ids, {
enabled: fetchChildEnabled,
});

const goals = useMemo(
() => projectDeepInfo?.goals.filter((g) => g.projectId === project.id),
Expand Down Expand Up @@ -114,7 +101,7 @@ const PageProjectListItem: FC<
project={project}
onCollapsedChange={onCollapsedChange}
onGoalsCollapsedChange={onGoalsCollapsedChange}
loading={loading}
loading={status === 'loading'}
{...props}
deep={deep}
>
Expand Down
76 changes: 45 additions & 31 deletions trpc/router/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,38 @@ export const addCalculatedProjectFields = <
};
};

const projectFullSchema = {
stargizers: true,
watchers: true,
parent: true,
tags: true,
children: {
include: {
parent: true,
},
},
participants: {
include: {
user: true,
ghost: true,
},
},
activity: {
include: {
user: true,
ghost: true,
},
},
_count: {
select: {
stargizers: true,
watchers: true,
participants: true,
children: true,
},
},
};

export const project = router({
suggestions: protectedProcedure.input(z.string()).query(({ input }) => {
return prisma.project.findMany({
Expand Down Expand Up @@ -131,43 +163,25 @@ export const project = router({
where: {
id,
},
include: {
stargizers: true,
watchers: true,
parent: true,
tags: true,
children: {
include: {
parent: true,
},
},
participants: {
include: {
user: true,
ghost: true,
},
},
activity: {
include: {
user: true,
ghost: true,
},
},
_count: {
select: {
stargizers: true,
watchers: true,
participants: true,
children: true,
},
},
},
include: projectFullSchema,
});

if (!project) return null;

return addCalculatedProjectFields(project, ctx.session.user.activityId);
}),
getByIds: protectedProcedure.input(z.array(z.string())).query(async ({ ctx, input }) => {
const projects = await prisma.project.findMany({
where: {
id: {
in: input,
},
},
include: projectFullSchema,
});

return projects.map((project) => addCalculatedProjectFields(project, ctx.session.user.activityId));
}),
getDeepInfo: protectedProcedure.input(projectDeepInfoSchema).query(async ({ ctx, input }) => {
const [allProjectGoals, filtredProjectGoals] = await Promise.all([
prisma.goal.findMany({
Expand Down

0 comments on commit a0634f8

Please sign in to comment.