Skip to content

Commit

Permalink
fix: accessibility issue and add delete answer
Browse files Browse the repository at this point in the history
  • Loading branch information
mmtftr committed Dec 16, 2024
1 parent 35b3b21 commit 42dd7a1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 26 deletions.
5 changes: 2 additions & 3 deletions frontend/src/components/AnswerCard.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import placeholderProfile from "@/assets/placeholder_profile.png";
import { Card } from "@/components/ui/card";
import { ArrowRight, CornerDownRight, Star } from "lucide-react";
import React from "react";
import { Link } from "react-router-dom";
import placeholderProfile from "@/assets/placeholder_profile.png";

interface AnswerCardProps {
id: number;
Expand Down Expand Up @@ -49,8 +49,7 @@ export const AnswerCard: React.FC<AnswerCardProps> = ({
<div className="flex items-center justify-between">
<Link to={`/users/${author.id}`} className="h-10 w-10">
<img
src={author?.profilePicture ||
placeholderProfile}
src={author?.profilePicture || placeholderProfile}
alt={"Profile picture"}
className="rounded-full object-cover"
/>
Expand Down
65 changes: 45 additions & 20 deletions frontend/src/components/AnswerItem.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
import placeholderProfile from "@/assets/placeholder_profile.png";
import { Button } from "@/components/ui/button";
import { Card } from "@/components/ui/card";
import { useDeleteAnswer } from "@/services/api/programmingForumComponents";
import { AnswerDetails } from "@/services/api/programmingForumSchemas";
import useAuthStore from "@/services/auth";
import { ThumbsDown, ThumbsUp } from "lucide-react";
import { ThumbsDown, ThumbsUp, Trash2 } from "lucide-react";
import React from "react";
import { Link } from "react-router-dom";
import { ContentWithSnippets } from "./ContentWithSnippets";
import placeholderProfile from "@/assets/placeholder_profile.png";

interface AnswerItemProps {
answer: AnswerDetails;
onUpvote: () => void;
onDownvote: () => void;
onDelete: () => void;
}

export const AnswerItem: React.FC<AnswerItemProps> = ({
answer,
onUpvote,
onDownvote,
onDelete,
}) => {
const { token } = useAuthStore();
const { token, selfProfile } = useAuthStore();

const isSelfAnswer = answer.author?.id === selfProfile?.id;
const { mutateAsync: deleteAnswer } = useDeleteAnswer();
return (
<Card className="border-none bg-neutral-100 px-6 py-8 shadow-sm">
<div className="flex flex-col gap-4">
Expand Down Expand Up @@ -55,23 +60,43 @@ export const AnswerItem: React.FC<AnswerItemProps> = ({
</div>
)}
</div>
<div className="flex flex-col items-end gap-1">
<Link
to={`/users/${answer.author?.id}`}
className="flex items-center gap-2"
>
<img
src={
answer.author?.profilePicture || placeholderProfile
}
alt={"Profile picture"}
className="h-8 w-8 rounded-full object-cover"
/>
<span className="text-sm font-medium">{answer.author?.name}</span>
</Link>
<span className="text-xs text-gray-700">
Answered: {new Date(answer.createdAt || "").toLocaleDateString()}
</span>
<div className="flex items-center gap-4">
<div className="flex flex-col items-end gap-1">
<Link
to={`/users/${answer.author?.id}`}
className="flex items-center gap-2"
>
<img
src={answer.author?.profilePicture || placeholderProfile}
alt={"Profile picture"}
className="h-8 w-8 rounded-full object-cover"
/>
<span className="text-sm font-medium">
{answer.author?.name}
</span>
</Link>
<span className="text-xs text-gray-700">
Answered:{" "}
{new Date(answer.createdAt || "").toLocaleDateString()}
</span>
</div>
{isSelfAnswer && (
<Button
variant="destructive"
size="icon"
onClick={() => {
deleteAnswer({
pathParams: {
answerId: answer.id,
},
}).then(() => {
onDelete();
});
}}
>
<Trash2 className="h-4 w-4" />
</Button>
)}
</div>
</div>
</div>
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/components/Answers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ vi.mock("@/services/api/programmingForumComponents", () => ({
useGetQuestionAnswers: vi.fn(),
useUpvoteAnswer: vi.fn(),
useDownvoteAnswer: vi.fn(),
useDeleteAnswer: vi.fn().mockReturnValue({
mutateAsync: vi.fn(),
}),
}));

// Mock the auth store
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/Answers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ export function Answers({ questionId }: AnswersProps) {
answer={answer}
onUpvote={() => handleVote(answer.id, 1)}
onDownvote={() => handleVote(answer.id, -1)}
onDelete={() => refetch()}
/>
))}
{answers.length === 0 && (
<span>
{" "}
This question doesn't have an answer yet. Contribute to the discussion
by answering this question.
</span>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ blockquote {
}

code {
@apply relative rounded bg-muted px-[0.3rem] py-[0.2rem] font-mono text-sm font-semibold;
@apply relative rounded px-[0.3rem] py-[0.2rem] font-mono text-sm font-semibold;
}

@layer base {
Expand Down
1 change: 0 additions & 1 deletion frontend/src/routes/question.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ export default function QuestionPage() {

useEffect(() => {
if (!isEditing && question.tags) {
console.log("set tags", question.tags);
setTags(question.tags.map((t) => Number(t.id)));
}
}, [question?.tags, isEditing]);
Expand Down

0 comments on commit 42dd7a1

Please sign in to comment.