Skip to content

Commit

Permalink
fix(INTERNAL-1185): use new project query for settings page
Browse files Browse the repository at this point in the history
  • Loading branch information
LamaEats committed Oct 18, 2024
1 parent 28204c5 commit fbaf6f4
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/components/ProjectAccessUser/ProjectAccessUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FC, useCallback, useMemo, useState } from 'react';
import { nullable } from '@taskany/bricks';
import { Fieldset, Switch, SwitchControl } from '@taskany/bricks/harmony';

import { ProjectByIdReturnType } from '../../../trpc/inferredTypes';
import { ProjectByIdReturnTypeV2 } from '../../../trpc/inferredTypes';
import { useProjectResource } from '../../hooks/useProjectResource';
import { ModalEvent, dispatchModalEvent } from '../../utils/dispatchModal';
import { SettingsCard, SettingsCardItem } from '../SettingsContent/SettingsContent';
Expand All @@ -12,7 +12,7 @@ import s from './ProjectAccessUser.module.css';
import { tr } from './ProjectAccessUser.i18n';

interface ProjectAccessUserProps {
project: NonNullable<ProjectByIdReturnType>;
project: NonNullable<ProjectByIdReturnTypeV2>;
}

export const ProjectAccessUser: FC<ProjectAccessUserProps> = ({ project }) => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/ProjectParticipants/ProjectParticipants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import { tr } from './ProjectParticipants.i18n';

interface ProjectParticipantsProps {
id: string;
participants: ComponentProps<typeof UserEditableList>['users'];
participants: ComponentProps<typeof UserEditableList>['users'] | null;
}

export const ProjectParticipants: FC<ProjectParticipantsProps> = ({ id, participants }) => {
const filterIds = useMemo(() => participants.map(({ id }) => id), [participants]);
const filterIds = useMemo(() => (participants ?? []).map(({ id }) => id), [participants]);
const { onProjectParticipantAdd, onProjectParticipantRemove } = useProjectResource(id);

const onAdd = useCallback(
Expand Down
2 changes: 1 addition & 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.project.getById.useQuery({ id });
const project = trpc.v2.project.getById.useQuery({ id });

const { updateProject, deleteProject, transferOwnership } = useProjectResource(id);
const { data: childrenIds = [] } = trpc.v2.project.deepChildrenIds.useQuery({ in: [{ id }] });
Expand Down
2 changes: 1 addition & 1 deletion src/components/UserEditableList/UserEditableList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { tr } from './UserEditableList.i18n';

export const UserEditableList: FC<{
editable?: boolean;
users: { id: string; user: User | null }[];
users: { id: string; user: User | null }[] | null;
filterIds: string[];
onRemove: (id: string) => void;
onAdd: (id: string) => void;
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.project.getById.fetch({ id });
const project = await ssrHelpers.v2.project.getById.fetch({ id });
await ssrHelpers.v2.project.deepChildrenIds.fetch({ in: [{ id }] });

if (!project) {
Expand Down
2 changes: 1 addition & 1 deletion src/schema/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const projectUpdateSchema = z.object({
title: z.string(),
}),
)
.optional(),
.nullish(),
accessUsers: z
.array(
z.object({
Expand Down
11 changes: 11 additions & 0 deletions trpc/queries/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,14 @@ export const getUserActivity = () => {
.select([sql`"User"`.as('user'), sql`"Ghost"`.as('ghost')])
.$castTo<Activity>();
};

export const getAccessUsersByProjectId = ({ projectId }: { projectId: string }) => {
return db
.selectFrom('_projectAccess')
.innerJoinLateral(
() => getUserActivity().as('activity'),
(join) => join.onRef('activity.id', '=', '_projectAccess.A'),
)
.selectAll('activity')
.where('_projectAccess.B', '=', projectId);
};
20 changes: 13 additions & 7 deletions trpc/router/projectV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { ExtractTypeFromGenerated, pickUniqueValues } from '../utils';
import { baseCalcCriteriaWeight } from '../../src/utils/recalculateCriteriaScore';
import { getGoalsQuery } from '../queries/goalV2';
import { projectAccessMiddleware } from '../access/accessMiddlewares';
import { getAccessUsersByProjectId } from '../queries/activity';

type ProjectActivity = ExtractTypeFromGenerated<Activity> & {
user: ExtractTypeFromGenerated<User> | null;
Expand All @@ -44,7 +45,7 @@ type ProjectResponse = ExtractTypeFromGenerated<Project> & {
_isOwner: boolean;
_isEditable: boolean;
activity: ProjectActivity;
participants: ProjectActivity[];
participants: ProjectActivity[] | null;
goals?: any[]; // this prop is overrides below
children: ExtractTypeFromGenerated<Project>[] | null;
};
Expand Down Expand Up @@ -113,6 +114,7 @@ export interface ProjectTreeRow {
type ProjectById = Omit<ProjectResponse, 'goals'> &
Pick<DashboardProject, '_count'> & {
parent: Array<{ id: string; title: string }>;
accessUsers: Array<ProjectActivity>;
};

export const project = router({
Expand Down Expand Up @@ -363,12 +365,15 @@ export const project = router({
.query(async ({ input, ctx }) => {
const { id } = input;

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

if (project == null) {
throw new TRPCError({ code: 'NOT_FOUND' });
Expand All @@ -377,6 +382,7 @@ export const project = router({
return {
...project,
parent: pickUniqueValues(project.parent, 'id'),
accessUsers,
};
}),

Expand Down

0 comments on commit fbaf6f4

Please sign in to comment.