Skip to content

Commit

Permalink
fix(ProjectListItemConnected): add query state to goals counter
Browse files Browse the repository at this point in the history
  • Loading branch information
asabotovich committed Jun 27, 2023
1 parent 4109d7a commit dc71266
Show file tree
Hide file tree
Showing 15 changed files with 294 additions and 194 deletions.
50 changes: 31 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"@aws-sdk/client-s3": "3.347.1",
"@hookform/resolvers": "3.1.1",
"@next-auth/prisma-adapter": "1.0.6",
"@prisma/client": "4.14.1",
"@sentry/nextjs": "7.50.0",
"@tanstack/react-query": "4.29.5",
"@tanstack/react-query-devtools": "4.29.5",
Expand Down Expand Up @@ -72,6 +71,7 @@
"@commitlint/config-conventional": "17.6.1",
"@faker-js/faker": "7.6.0",
"@next/bundle-analyzer": "13.2.1",
"@prisma/client": "4.16.1",
"@svgr/webpack": "7.0.0",
"@types/cors": "2.8.13",
"@types/jest": "29.5.1",
Expand Down Expand Up @@ -105,7 +105,7 @@
"jest": "29.5.0",
"lint-staged": "13.1.2",
"prettier": "2.8.8",
"prisma": "4.15.0",
"prisma": "4.16.1",
"ts-jest": "29.1.0",
"ts-node": "10.9.1",
"typescript": "5.1.3",
Expand Down
11 changes: 8 additions & 3 deletions src/components/ProjectCreateForm/ProjectCreateForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,14 @@ const ProjectCreateForm: React.FC = () => {

const isKeyEnoughLength = Boolean(keyWatcher?.length >= 3);
const flowRecomendations = trpc.flow.recommedations.useQuery();
const existingProject = trpc.project.getById.useQuery(keyWatcher, {
enabled: isKeyEnoughLength,
});
const existingProject = trpc.project.getById.useQuery(
{
id: keyWatcher,
},
{
enabled: isKeyEnoughLength,
},
);

const isKeyUnique = Boolean(!existingProject?.data);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ interface ProjectListItemCollapsableProps {
collapsedGoals: boolean;
onClick?: () => void;
onGoalsClick?: () => void;
goalsCounter?: number;
loading?: boolean;
deep?: number;
}
Expand All @@ -37,7 +36,6 @@ export const ProjectListItemCollapsable: React.FC<ProjectListItemCollapsableProp
children,
goals,
loading = false,
goalsCounter,
deep = 0,
}) => {
const contentHidden = collapsed || loading;
Expand Down Expand Up @@ -70,12 +68,12 @@ export const ProjectListItemCollapsable: React.FC<ProjectListItemCollapsableProp
watching={project._isWatching}
>
<StyledGoalsButtonContainer>
{goalsCounter ? (
{project._count.goals ? (
<Button
ghost={collapsedGoals}
onClick={onGoalsButtonClick}
text={tr('Goals')}
iconRight={<Badge size="s">{goalsCounter}</Badge>}
iconRight={<Badge size="s">{project._count.goals}</Badge>}
/>
) : (
<Text color={gray7}>{tr('No goals')}</Text>
Expand Down
16 changes: 8 additions & 8 deletions src/components/ProjectListItemConnected.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC, MouseEventHandler, useState, useMemo, useCallback } from 'react';
import { FC, MouseEventHandler, useState, useMemo, useCallback, useEffect } from 'react';

import { GoalByIdReturnType, ProjectByIdReturnType } from '../../trpc/inferredTypes';
import { trpc } from '../utils/trpcClient';
Expand Down Expand Up @@ -34,7 +34,7 @@ export const ProjectListItemConnected: FC<{
const { data: projectDeepInfo } = trpc.project.getDeepInfo.useQuery(
{
id: project.id,
...queryState,
goalsQuery: queryState,
},
{
enabled: !collapsedGoals,
Expand All @@ -44,9 +44,12 @@ export const ProjectListItemConnected: FC<{
);

const ids = useMemo(() => project?.children.map(({ id }) => id) || [], [project]);
const { data: childrenProjects = [], status } = trpc.project.getByIds.useQuery(ids, {
enabled: !collapsed,
});
const { data: childrenProjects = [], status } = trpc.project.getByIds.useQuery(
{ ids, goalsQuery: queryState },
{
enabled: !collapsed,
},
);

const goals = useMemo(
() => projectDeepInfo?.goals.filter((g) => g.projectId === project.id),
Expand All @@ -61,8 +64,6 @@ export const ProjectListItemConnected: FC<{
setIsCollapsedGoals((value) => !value);
}, []);

const goalsCounter = goals ? goals.length : project._count.goals;

return (
<ProjectListItemCollapsable
href={routes.project(project.id)}
Expand Down Expand Up @@ -96,7 +97,6 @@ export const ProjectListItemConnected: FC<{
onClick={onClick}
onGoalsClick={onGoalsClick}
loading={status === 'loading'}
goalsCounter={goalsCounter}
deep={deep}
>
{childrenProjects.map((p) => (
Expand Down
8 changes: 6 additions & 2 deletions src/components/ProjectPage/ProjectPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,15 @@ export const ProjectPage = ({ user, ssrTime, params: { id } }: ExternalPageProps
preset: preset?.data,
});

const project = trpc.project.getById.useQuery(id);
const project = trpc.project.getById.useQuery({
id,
goalsQuery: queryState,
});

const { data: projectDeepInfo, isLoading } = trpc.project.getDeepInfo.useQuery(
{
id,
...queryState,
goalsQuery: queryState,
},
{
keepPreviousData: true,
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 @@ -43,7 +43,7 @@ export const ProjectSettingsPage = ({ user, ssrTime, params: { id } }: ExternalP
const [currentProjectCache, setCurrentProjectCache] = useLocalStorage('currentProjectCache');
const [recentProjectsCache, setRecentProjectsCache] = useLocalStorage('recentProjectsCache', {});

const project = trpc.project.getById.useQuery(id);
const project = trpc.project.getById.useQuery({ id });

const { updateProject, deleteProject, transferOwnership } = useProjectResource(id);

Expand Down
9 changes: 5 additions & 4 deletions src/components/ProjectsPage/ProjectsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ export const ProjectsPage = ({ user, ssrTime }: ExternalPageProps) => {
const [preview, setPreview] = useState<GoalByIdReturnType | null>(null);
const { toggleFilterStar } = useFilterResource();

const { data: projects = [] } = trpc.project.getAll.useQuery({
firstLevel: true,
});

const utils = trpc.useContext();
const preset = trpc.filter.getById.useQuery(String(nextRouter.query.filter), {
enabled: Boolean(nextRouter.query.filter),
Expand Down Expand Up @@ -63,6 +59,11 @@ export const ProjectsPage = ({ user, ssrTime }: ExternalPageProps) => {
preset: preset?.data,
});

const { data: projects = [] } = trpc.project.getAll.useQuery({
firstLevel: true,
goalsQuery: queryState,
});

const shadowPreset = userFilters.data?.filter((f) => f.params === queryString)[0];

const onGoalPrewiewShow = useCallback(
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useProjectResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const useProjectResource = (id: string) => {
const transferOwnershipMutation = trpc.project.transferOwnership.useMutation();

const invalidate = useCallback(() => {
utils.project.getById.invalidate(id);
utils.project.getById.invalidate({ id });
}, [id, utils.project.getById]);

const createProject = useCallback(
Expand Down
11 changes: 9 additions & 2 deletions src/pages/explore/projects.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { ExploreProjectsPage } from '../../components/ExporeProjectsPage/ExporeProjectsPage';
import { declareSsrProps } from '../../utils/declareSsrProps';
import { parseFilterValues } from '../../hooks/useUrlFilterParams';

export const getServerSideProps = declareSsrProps(
async ({ ssrHelpers }) => {
await ssrHelpers.project.getAll.fetch();
async ({ ssrHelpers, query }) => {
const preset =
typeof query.filter === 'string' ? await ssrHelpers.filter.getById.fetch(query.filter) : undefined;

await ssrHelpers.project.getAll.fetch({
firstLevel: true,
goalsQuery: parseFilterValues(preset ? Object.fromEntries(new URLSearchParams(preset.params)) : query),
});
},
{
private: true,
Expand Down
5 changes: 3 additions & 2 deletions src/pages/projects/[id]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ export const getServerSideProps = declareSsrProps(
const preset =
typeof query.filter === 'string' ? await ssrHelpers.filter.getById.fetch(query.filter) : undefined;
await ssrHelpers.filter.getUserFilters.fetch();
const project = await ssrHelpers.project.getById.fetch(id);
const goalsQuery = parseFilterValues(preset ? Object.fromEntries(new URLSearchParams(preset.params)) : query);
const project = await ssrHelpers.project.getById.fetch({ id, goalsQuery });
await ssrHelpers.project.getDeepInfo.fetch({
id,
...parseFilterValues(preset ? Object.fromEntries(new URLSearchParams(preset.params)) : query),
goalsQuery,
});

if (!project) {
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 @@ -3,7 +3,7 @@ import { declareSsrProps } from '../../../utils/declareSsrProps';

export const getServerSideProps = declareSsrProps(
async ({ ssrHelpers, params: { id } }) => {
const data = await ssrHelpers.project.getById.fetch(id);
const data = await ssrHelpers.project.getById.fetch({ id });

if (!data) {
return {
Expand Down
12 changes: 10 additions & 2 deletions src/pages/projects/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@ import { parseFilterValues } from '../../hooks/useUrlFilterParams';
import { declareSsrProps } from '../../utils/declareSsrProps';

export const getServerSideProps = declareSsrProps(
async ({ ssrHelpers }) => {
await ssrHelpers.project.getAll.fetch();
async ({ ssrHelpers, query }) => {
const preset =
typeof query.filter === 'string' ? await ssrHelpers.filter.getById.fetch(query.filter) : undefined;
await ssrHelpers.filter.getUserFilters.fetch();
const goalsQuery = parseFilterValues(preset ? Object.fromEntries(new URLSearchParams(preset.params)) : query);

await ssrHelpers.project.getAll.fetch({
firstLevel: true,
goalsQuery,
});
},
{
private: true,
Expand Down
Loading

0 comments on commit dc71266

Please sign in to comment.