From 54f30c2d6deed661b3dcc10687bcf35b7a9e54bd Mon Sep 17 00:00:00 2001 From: Jeff Sieu Date: Wed, 19 Oct 2022 17:23:18 +0800 Subject: [PATCH] [question][fix] fix frontend --- apps/portal/src/pages/questions/index.tsx | 5 + .../router/questions-question-router.ts | 97 +++++++++---------- 2 files changed, 52 insertions(+), 50 deletions(-) diff --git a/apps/portal/src/pages/questions/index.tsx b/apps/portal/src/pages/questions/index.tsx index 93ac9725..8c370610 100644 --- a/apps/portal/src/pages/questions/index.tsx +++ b/apps/portal/src/pages/questions/index.tsx @@ -26,6 +26,8 @@ import { } from '~/utils/questions/useSearchFilter'; import { trpc } from '~/utils/trpc'; +import { SortOrder, SortType } from '~/types/questions'; + export default function QuestionsHomePage() { const router = useRouter(); @@ -70,6 +72,9 @@ export default function QuestionsHomePage() { locations: selectedLocations, questionTypes: selectedQuestionTypes, roles: [], + // TODO: Implement sort order and sort type choices + sortOrder: SortOrder.DESC, + sortType: SortType.BEST, startDate, }, ], diff --git a/apps/portal/src/server/router/questions-question-router.ts b/apps/portal/src/server/router/questions-question-router.ts index ed2ada70..1431c1be 100644 --- a/apps/portal/src/server/router/questions-question-router.ts +++ b/apps/portal/src/server/router/questions-question-router.ts @@ -7,7 +7,6 @@ import { createProtectedRouter } from './context'; import type { Question } from '~/types/questions'; import { SortOrder, SortType } from '~/types/questions'; - export const questionsQuestionRouter = createProtectedRouter() .query('getQuestionsByFilter', { input: z.object({ @@ -18,8 +17,8 @@ export const questionsQuestionRouter = createProtectedRouter() questionTypes: z.nativeEnum(QuestionsQuestionType).array(), roles: z.string().array(), sortOrder: z.nativeEnum(SortOrder), - startDate: z.date().optional() - sortType : z.nativeEnum(SortType), + sortType: z.nativeEnum(SortType), + startDate: z.date().optional(), }), async resolve({ ctx, input }) { const questionsData = await ctx.prisma.questionsQuestion.findMany({ @@ -56,68 +55,67 @@ export const questionsQuestionRouter = createProtectedRouter() }, } : {}), - encounters : { + encounters: { some: { ...(input.companies.length > 0 ? { - company : { - in : input.companies - } - } + company: { + in: input.companies, + }, + } : {}), ...(input.locations.length > 0 ? { location: { - in: input.locations + in: input.locations, }, - } + } : {}), ...(input.roles.length > 0 ? { - role : { - in: input.roles - } - } + role: { + in: input.roles, + }, + } : {}), - } - } + }, + }, }, }); - return questionsData - .map((data) => { - const votes: number = data.votes.reduce( - (previousValue: number, currentValue) => { - let result: number = previousValue; + return questionsData.map((data) => { + const votes: number = data.votes.reduce( + (previousValue: number, currentValue) => { + let result: number = previousValue; - switch (currentValue.vote) { - case Vote.UPVOTE: - result += 1; - break; - case Vote.DOWNVOTE: - result -= 1; - break; - } - return result; - }, - 0, - ); + switch (currentValue.vote) { + case Vote.UPVOTE: + result += 1; + break; + case Vote.DOWNVOTE: + result -= 1; + break; + } + return result; + }, + 0, + ); - const question: Question = { - company: data.encounters[0].company, - content: data.content, - id: data.id, - location: data.encounters[0].location ?? 'Unknown location', - numAnswers: data._count.answers, - numComments: data._count.comments, - numVotes: votes, - role: data.encounters[0].role ?? 'Unknown role', - seenAt: data.encounters[0].seenAt, - type: data.questionType, - updatedAt: data.updatedAt, - user: data.user?.name ?? '', - }; - return question; - }); + const question: Question = { + company: data.encounters[0].company, + content: data.content, + id: data.id, + location: data.encounters[0].location ?? 'Unknown location', + numAnswers: data._count.answers, + numComments: data._count.comments, + numVotes: votes, + role: data.encounters[0].role ?? 'Unknown role', + seenAt: data.encounters[0].seenAt, + type: data.questionType, + updatedAt: data.updatedAt, + user: data.user?.name ?? '', + }; + return question; + }); }, }) .query('getQuestionById', { @@ -264,7 +262,6 @@ export const questionsQuestionRouter = createProtectedRouter() const { content, questionType } = input; return await ctx.prisma.questionsQuestion.update({ - data: { content, questionType,