Skip to content

Commit

Permalink
refactor(INTERNAL-1185): drop extra cte statements
Browse files Browse the repository at this point in the history
  • Loading branch information
LamaEats committed Oct 18, 2024
1 parent fdb3133 commit 5f9a7f7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/components/ProjectPage/ProjectPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ export const ProjectPage = ({ user, ssrTime, params: { id }, defaultPresetFallba
</Breadcrumbs>
))}

<ListView onKeyboardClick={handleItemEnter}>
{nullable(ctx.project, (p) => (
{nullable(ctx.project, (p) => (
<ListView onKeyboardClick={handleItemEnter}>
<ProjectListItemConnected
key={p.id}
mainProject
Expand All @@ -129,8 +129,8 @@ export const ProjectPage = ({ user, ssrTime, params: { id }, defaultPresetFallba
filterPreset={preset}
subTree={projectTreeQuery.data?.[p.id]}
/>
))}
</ListView>
</ListView>
))}

<PresetModals preset={preset} />
</Page>
Expand Down
10 changes: 9 additions & 1 deletion src/components/ProjectSettingsPage/ProjectSettingsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const ModalOnEvent = dynamic(() => import('../ModalOnEvent'));

export const ProjectSettingsPage = ({ user, ssrTime, params: { id } }: ExternalPageProps) => {
const router = useRouter();
const project = trpc.v2.project.getById.useQuery({ id });
const project = trpc.v2.project.getById.useQuery({ id, includeChildren: true });

const { updateProject, deleteProject, transferOwnership } = useProjectResource(id);
const { data: childrenIds = [] } = trpc.v2.project.deepChildrenIds.useQuery({ in: [{ id }] });
Expand All @@ -86,6 +86,14 @@ export const ProjectSettingsPage = ({ user, ssrTime, params: { id } }: ExternalP
mode: 'onChange',
reValidateMode: 'onChange',
shouldFocusError: true,
values: {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
id: project.data!.id,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
title: project.data!.title,
description: project.data?.description,
parent: project.data?.parent,
},
defaultValues: {
id: project.data?.id,
title: project.data?.title,
Expand Down
2 changes: 1 addition & 1 deletion src/pages/projects/[id]/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { declareSsrProps } from '../../../utils/declareSsrProps';
export const getServerSideProps = declareSsrProps(
async ({ ssrHelpers, params: { id } }) => {
try {
const project = await ssrHelpers.v2.project.getById.fetch({ id });
const project = await ssrHelpers.v2.project.getById.fetch({ id, includeChildren: true });
await ssrHelpers.v2.project.deepChildrenIds.fetch({ in: [{ id }] });

if (!project) {
Expand Down
22 changes: 14 additions & 8 deletions trpc/queries/projectV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -758,18 +758,15 @@ export const getProjectChildrenTreeQuery = ({ id, goalsQuery }: { id: string; go

export const getProjectById = ({ id, ...user }: { id: string; activityId: string; role: Role }) => {
return db
.with('parentProjectIds', () => getParentProjectsId({ in: [{ id }] }))
.with('parentProjects', (qb) =>
qb
.selectFrom('Project')
.select(['Project.id', 'Project.title'])
.where('Project.id', 'in', ({ selectFrom }) => selectFrom('parentProjectIds').select('id')),
)
.with('calculatedFields', (qb) =>
qb
.selectFrom('Project')
.leftJoinLateral(
({ selectFrom }) => selectFrom('parentProjects').distinctOn('Project.id').selectAll().as('parent'),
({ selectFrom }) =>
selectFrom('Project')
.selectAll('Project')
.where('Project.id', 'in', () => getParentProjectsId({ in: [{ id }] }))
.as('parent'),
(join) => join.onTrue(),
)
.leftJoinLateral(
Expand Down Expand Up @@ -861,3 +858,12 @@ export const getProjectById = ({ id, ...user }: { id: string; activityId: string
)} or "project"."_isParticipant") and not "project"."personal")`.as('_isEditable'),
]);
};

export const getChildrenProjectByParentProjectId = ({ id }: { id: string }) => {
return db
.selectFrom('Project')
.select(['Project.id', 'Project.title'])
.where('Project.id', 'in', ({ selectFrom }) =>
selectFrom('_parentChildren').select('_parentChildren.B').where('_parentChildren.A', '=', id),
);
};
7 changes: 6 additions & 1 deletion trpc/router/projectV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
getAllProjectsQuery,
getProjectChildrenTreeQuery,
getProjectById,
getChildrenProjectByParentProjectId,
} from '../queries/projectV2';
import { queryWithFiltersSchema, sortableProjectsPropertiesArraySchema } from '../../src/schema/common';
import {
Expand Down Expand Up @@ -117,6 +118,7 @@ type ProjectById = Omit<ProjectResponse, 'goals'> &
parent: Array<{ id: string; title: string }>;
accessUsers: Array<ProjectActivity>;
teams: Array<Team>;
children?: Array<{ id: string; title: string }>;
};

export const project = router({
Expand Down Expand Up @@ -360,21 +362,23 @@ export const project = router({
.input(
z.object({
id: z.string(),
includeChildren: z.boolean().optional(),
goalsQuery: queryWithFiltersSchema.optional(),
}),
)
.use(projectAccessMiddleware)
.query(async ({ input, ctx }) => {
const { id } = input;

const [project, accessUsers] = await Promise.all([
const [project, accessUsers, children = null] = await Promise.all([
getProjectById({
...ctx.session.user,
id,
})
.$castTo<ProjectById>()
.executeTakeFirst(),
getAccessUsersByProjectId({ projectId: id }).execute(),
input.includeChildren ? getChildrenProjectByParentProjectId({ id }).execute() : Promise.resolve(null),
]);

if (project == null) {
Expand All @@ -383,6 +387,7 @@ export const project = router({

return {
...project,
...(children != null ? { children } : undefined),
parent: pickUniqueValues(project.parent, 'id'),
accessUsers,
};
Expand Down

0 comments on commit 5f9a7f7

Please sign in to comment.