diff --git a/src/components/Buttons.tsx b/src/components/Buttons.tsx index 3448db0c..348dd3d2 100644 --- a/src/components/Buttons.tsx +++ b/src/components/Buttons.tsx @@ -1,4 +1,5 @@ /* eslint-disable */ +/* eslint-disable */ import React from 'react'; interface Props { diff --git a/src/components/CommunityQuestions.tsx b/src/components/CommunityQuestions.tsx new file mode 100644 index 00000000..d555d88c --- /dev/null +++ b/src/components/CommunityQuestions.tsx @@ -0,0 +1,340 @@ +import React, { useState, useEffect } from 'react'; +import { useQuery, useMutation } from '@apollo/client'; +import { toast } from 'react-toastify'; +import { useTranslation } from 'react-i18next'; +import { + GET_ALL_QUESTIONS, + CREATE_QUESTION, + CREATE_ANSWER, + DELETE_QUESTION, + DELETE_ANSWER, +} from '../queries/questions.query'; + +function CommunityQuestions() { + const { loading, error, data, refetch } = useQuery(GET_ALL_QUESTIONS); + const [createQuestion, { loading: isSubmitting }] = + useMutation(CREATE_QUESTION); + const { t } = useTranslation(); + const [createAnswer] = useMutation(CREATE_ANSWER); + const [deleteQuestion] = useMutation(DELETE_QUESTION); + const [deleteAnswer] = useMutation(DELETE_ANSWER); + const [loggedUser, setLoggedUser] = useState(null); + const [questionText, setQuestionText] = useState(''); + const [answerText, setAnswerText] = useState(''); + const [selectedQuestion, setSelectedQuestion] = useState(null); + const [questionTitleText, setQuestionTitleText] = useState(null); + const [questions, setQuestions] = useState([]); + + useEffect(() => { + const authData = localStorage.getItem('auth'); + if (authData) { + const parsedAuthData = JSON.parse(authData); + setLoggedUser(parsedAuthData.userId); + } + }, []); + useEffect(() => { + if (data) setQuestions(data.getAllQuestions); + + if (data && selectedQuestion) { + const updatedQuestion = data.getAllQuestions.find( + (question: any) => question.id === selectedQuestion.id, + ); + setSelectedQuestion(updatedQuestion || null); + } + }, [data, selectedQuestion]); + + if (loading) return

Loading...

; + if (error) return

Error loading questions.

; + + const handleCreateQuestion = async () => { + if (questionText.trim()) { + await createQuestion({ + variables: { + title: questionTitleText, + content: questionText, + }, + update: (cache, { data: { createQuestion } }) => { + const existingQuestions: any = cache.readQuery({ + query: GET_ALL_QUESTIONS, + }); + cache.writeQuery({ + query: GET_ALL_QUESTIONS, + data: { + getAllQuestions: [ + ...existingQuestions.getAllQuestions, + createQuestion, + ], + }, + }); + }, + onError: (error) => { + toast.error('Unable to create question.'); + }, + onCompleted: () => { + toast.success('Question created successfully'); + refetch(); + setQuestionText(''); + setQuestionTitleText(''); + }, + }); + } else { + toast.error('Question text cannot be empty.'); + } + }; + + const handleCreateAnswer = async (questionId: string) => { + if (answerText.trim()) { + await createAnswer({ + variables: { + questionId, + content: answerText, + }, + update: (cache, { data: { createAnswer } }) => { + const existingQuestions: any = cache.readQuery({ + query: GET_ALL_QUESTIONS, + }); + const updatedQuestions = existingQuestions.getAllQuestions.map( + (question: any) => { + if (question?.id === questionId) { + return { + ...question, + answers: [...(question?.answers || []), createAnswer], + }; + } + + return question; + }, + ); + setQuestions(updatedQuestions); + cache.writeQuery({ + query: GET_ALL_QUESTIONS, + data: { getAllQuestions: updatedQuestions }, + }); + }, + onError: (error) => { + toast.error('Unable to submit your answer.'); + }, + onCompleted: () => { + toast.success('Answer submitted successfully'); + refetch(); + setAnswerText(''); + }, + }); + } else { + toast.error('Answer text cannot be empty.'); + } + }; + + const handleDeleteQuestion = async (questionId: string) => { + await deleteQuestion({ + variables: { id: questionId }, + update: (cache) => { + const existingQuestions: any = cache.readQuery({ + query: GET_ALL_QUESTIONS, + }); + const updatedQuestions = existingQuestions.getAllQuestions.filter( + (question: any) => question?.id !== questionId, + ); + cache.writeQuery({ + query: GET_ALL_QUESTIONS, + data: { getAllQuestions: updatedQuestions }, + }); + }, + onError: (error) => { + toast.error('Unable to delete the question.'); + }, + onCompleted: () => { + toast.success('Question deleted successfully'); + setSelectedQuestion(''); + + refetch(); + }, + }); + }; + + const handleDeleteAnswer = async (answerId: string) => { + await deleteAnswer({ + variables: { id: answerId }, + update: (cache) => { + const existingQuestions: any = cache.readQuery({ + query: GET_ALL_QUESTIONS, + }); + const updatedQuestions = existingQuestions.getAllQuestions.map( + (question: any) => ({ + ...question, + answers: question?.answers.filter( + (answer: any) => answer.id !== answerId, + ), + }), + ); + cache.writeQuery({ + query: GET_ALL_QUESTIONS, + data: { getAllQuestions: updatedQuestions }, + }); + }, + onError: (error) => { + toast.error('Unable to delete the answer.'); + }, + onCompleted: () => { + toast.success('Answer deleted successfully'); + // setSelectedQuestion(''); + refetch({ query: GET_ALL_QUESTIONS }); + }, + }); + }; + + return ( +
+
+

+ Questions +

+ {loggedUser && ( +
+ setQuestionTitleText(e.target.value)} + placeholder="Write question title..." + className="p-2 border dark:bg-dark-tertiary border-gray-300 dark:border-gray-600 dark:text-black rounded w-full" + /> +