Skip to content

Commit

Permalink
Merge pull request #109 from Qfeed-Dev/develop
Browse files Browse the repository at this point in the history
1.0.0 (16)
  • Loading branch information
hamo-o authored Oct 28, 2023
2 parents 96890c8 + df1404f commit 40c1f80
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 97 deletions.
5 changes: 5 additions & 0 deletions src/apis/questions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export const postQuestions = async (body: any) =>
.then(({ data }) => data)
.catch((err) => err.response);

export const deleteQuestions = async (questionId: number) => {
const response = await qFeedAxios.patch(`/questions/${questionId}`);
return response.data;
};

export const postQuestionsIdChoices = async (questionId: any, choice: string) =>
await qFeedAxios
.post(`/questions/${questionId}/choices`, { value: choice })
Expand Down
2 changes: 1 addition & 1 deletion src/app/(padding)/friend/[id]/@modal/block/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function BlockModal({ params }: { params: { id: number } }) {
<Modal
handleClose={() => router.back()}
title="차단하시겠어요?"
detail="쪽지 수신 및 발신이 모두 차단되며,\n다시 해제하실 수 있습니다."
detail="해당 유저의 질문을 볼 수 없으며,\n다시 해제하실 수 있습니다."
rightText="차단"
rightClick={() => {
blockFriend.mutate(params.id);
Expand Down
164 changes: 95 additions & 69 deletions src/app/(padding)/question/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,37 @@ import Text from "src/components/common/Text";
import NavigationTopBack from "src/components/navigations/NavigationTopBack";

import { useEffect, useState } from "react";
import { useRouter } from "next/navigation";
import { useAppDispatch, useAppSelector } from "src/hooks/useReduxHooks";
import {
changeAction,
changeVisibleType
} from "src/reducer/slices/bottomSheet/bottomSheetSlice";
import Image from "src/components/Image";

import { useGetQuestionsId } from "src/hooks/questions/useGetQuestionId";
import VoteButton from "src/components/Button/VoteButton";
import { useUserQuery } from "src/hooks/account/useUserQuery";
import useQChoiceMutation from "src/hooks/questions/useQChoiceMutation";
import useDeleteQuestionMutation from "src/hooks/questions/useDeleteQuestionMutation";
import Loading from "src/components/common/Loading";
import Icon from "src/components/Icon";

export default function Page({ params }: { params: { id: number } }) {
const { data: questionData, isLoading } = useGetQuestionsId({
questionId: params.id
});
console.log(questionData);

const { user } = useUserQuery();
const user = useUserQuery();
const { mutate } = useQChoiceMutation(params.id);
const { deleteQMutation } = useDeleteQuestionMutation();
const dispatch = useAppDispatch();

const router = useRouter();

// choices에 본인이 있는지 확인
const checkName = (el: any) => {
if (el?.user?.id === user?.id) {
if (el?.user?.id === user?.user?.id) {
return true;
}
};
Expand All @@ -40,13 +45,20 @@ export default function Page({ params }: { params: { id: number } }) {
const checkBest = () => {
const choices = questionData?.choiceList;
let best = [];
for (let i = 0; i < questionData?.choiceList?.length; i++) {
for (
let i = 0;
i <
(questionData?.choiceList?.length
? questionData?.choiceList?.length
: 0);
i++
) {
const value = questionData?.choices?.filter(
(data: any, idx: number) => {
return data?.value == choices[i];
return choices && data?.value == choices[i];
}
);
best.push(value.length);
value && best.push(value.length);
}
setBest(best);
};
Expand All @@ -67,18 +79,18 @@ export default function Page({ params }: { params: { id: number } }) {
useEffect(() => {
setTypeNum(
questionData?.choices.some(checkName) ||
questionData?.owner?.id === user?.id
questionData?.owner?.id === user?.user?.id
? 2
: 0
);
const s = questionData?.choices?.filter(
(data: any) => data?.user?.id === user?.id
(data: any) => data?.user?.id === user?.user?.id
);
setSelected(s?.length !== 0 ? Number(s?.[0]?.value) : -1);
checkBest();
}, [questionData]);

return isLoading ? (
return isLoading || user.isLoading ? (
<Loading />
) : (
<Flex height="100%" direction="column">
Expand All @@ -104,21 +116,32 @@ export default function Page({ params }: { params: { id: number } }) {
<Text typo="Subtitle1r">
{questionData?.choices?.length}명 응답
</Text>
<Icon
icon="DotsHoriz"
onClick={() =>
dispatch(
changeVisibleType({
type: "bottomSheet",
value: [
1,
"reportBlock",
questionData?.owner?.id
]
})
)
}
/>
{questionData?.owner?.id === user?.user?.id ? (
<Icon
icon="Trash"
onClick={() => {
questionData &&
deleteQMutation.mutate(questionData.id);
router.back();
}}
/>
) : (
<Icon
icon="DotsHoriz"
onClick={() =>
dispatch(
changeVisibleType({
type: "bottomSheet",
value: [
1,
"reportBlock",
questionData?.owner?.id
]
})
)
}
/>
)}
</Flex>
}
transparent
Expand All @@ -135,51 +158,54 @@ export default function Page({ params }: { params: { id: number } }) {
<QuestionWrapper height="100%">
<Question title={questionData?.title} />
</QuestionWrapper>

<BottomButton width="100%" direction="column" gap={12}>
{questionData.choiceList?.map(
(choice: string, idx: number) => {
return (
<VoteButton
key={idx}
idx={idx}
type={
questionData?.backgroundImage !== ""
? "primary"
: "default"
}
$typeNum={typeNum} // 0 1 2
action={
typeNum === 2 &&
questionData?.choices?.filter(
(data2: any) =>
data2.value ==
questionData?.choiceList[idx]
).length === Math.max(...best) // best
? 2
: idx === selected
? 1
: 0
} // 0 1 2
selected={selected}
onClick={
typeNum === 2
? () => {}
: () => clickChoice(idx, choice)
}
count={
questionData?.choices?.filter(
(choiceItem: any) =>
choiceItem.value == choice
).length
}
>
{choice}
</VoteButton>
);
}
)}
</BottomButton>
{questionData && (
<BottomButton width="100%" direction="column" gap={12}>
{questionData.choiceList?.map(
(choice: string, idx: number) => {
return (
<VoteButton
key={idx}
idx={idx}
type={
questionData?.backgroundImage !== ""
? "primary"
: "default"
}
$typeNum={typeNum} // 0 1 2
action={
typeNum === 2 &&
questionData?.choices?.filter(
(data2: any) =>
data2.value ==
questionData?.choiceList[
idx
]
).length === Math.max(...best) // best
? 2
: idx === selected
? 1
: 0
} // 0 1 2
selected={selected}
onClick={
typeNum === 2
? () => {}
: () => clickChoice(idx, choice)
}
count={
questionData?.choices?.filter(
(choiceItem: any) =>
choiceItem.value == choice
).length
}
>
{choice}
</VoteButton>
);
}
)}
</BottomButton>
)}
</Flex>
</Flex>
);
Expand Down
19 changes: 19 additions & 0 deletions src/hooks/questions/useDeleteQuestionMutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { deleteQuestions } from "src/apis/questions";
import { questionKeys } from "src/constants/queryKeys/questionKeys";

const useDeleteQuestionMutation = () => {
const queryClient = useQueryClient();
const deleteQMutation = useMutation(
(questionId: number) => deleteQuestions(questionId),
{
onSuccess: () => {
queryClient.invalidateQueries(questionKeys.all);
},
onError: () => {}
}
);
return { deleteQMutation };
};

export default useDeleteQuestionMutation;
3 changes: 2 additions & 1 deletion src/hooks/questions/useGetQuestionId.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useQuery } from "@tanstack/react-query";
import { getQuestionsId } from "src/apis/questions";
import { questionKeys } from "src/constants/queryKeys/questionKeys";
import { QuestionById } from "src/models/questions";

export const useGetQuestionsId = ({ questionId }: { questionId: number }) => {
const { data, isLoading, error, refetch } = useQuery(
const { data, isLoading, error, refetch } = useQuery<QuestionById>(
questionKeys.detail(questionId),
async () => {
const result = getQuestionsId(questionId);
Expand Down
31 changes: 31 additions & 0 deletions src/models/questions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { User } from "./account";

export type Qtype = "official" | "personal";

export interface Questions {
Expand Down Expand Up @@ -57,3 +59,32 @@ export interface Question {
backgroundImage: string;
isBlind: boolean;
}

export interface QuestionById {
id: number;
owner: Partial<User>;
title: string;
choiceList: string[];
backgroundImage: string;
Qtype: Qtype;
isBlind: boolean;
viewHistories: [
{
id: number;
user: Partial<User>;
createdAt: string;
updatedAt: string;
}
];
choices: [
{
id: number;
user: Partial<User>;
value: string;
createdAt: string;
updatedAt: string;
}
];
createdAt: string;
updatedAt: string;
}
12 changes: 7 additions & 5 deletions src/pages-edit/home/components/MakeOfficial.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,13 @@ const MakeOfficial = (props: QuestionProps) => {
{QSet.questionCursor[0].currentQ}
</Text>
<Text typo="Caption1r" color="light_qblack">
아직
{QSet.questionCursor[0].QsetLength -
QSet.questionCursor[0].cursor +
1}
문제 남았어요!
<span>아직 </span>
<span>
{QSet.questionCursor[0].QsetLength -
QSet.questionCursor[0].cursor +
1}
문제 남았어요!
</span>
</Text>
<BottomButton>
<Text
Expand Down
Loading

0 comments on commit 40c1f80

Please sign in to comment.