shahrbin-citizen/components/Polls/PollDetails.tsx

146 lines
4.4 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"use client";
import React, { useEffect, useState } from "react";
import { useMutation, useQuery } from "@tanstack/react-query";
import { useParams, useRouter } from "next/navigation";
import { getSinglePoll, postCitizenPollsAnswer } from "@/utils/CitizenPolls";
import { Button } from "../ui/button";
import { RadioGroup, RadioGroupItem } from "../ui/radio-group";
import { Label } from "../ui/label";
import { toast } from "sonner";
interface PollsProps {
id: number;
}
export class STR {
static removeTags(str = "") {
return str.replace(/(<([^>])+>)/gi, "").replace(/&nbsp;/gi, "");
}
static parseHtml(str: any) {
const div = document.createElement("div");
div.innerHTML = str;
const images = div.getElementsByTagName("img");
// if (images.length > 0) {
// Array.from(images).forEach((image) => {
// let imageSrc = image.getAttribute("src");
// image.src = URI.createMediaUri(imageSrc);
// });
// }
return div.innerHTML;
}
}
type PostAnswerArgs = {
id: number;
payload: {
text: string;
choicesIds: number[];
};
};
const PollDetails: React.FC<PollsProps> = ({ id }) => {
const router = useRouter();
const [details, setDetails] = useState<any | null>(null);
const [satisfaction, setSatisfaction] = useState<number[]>([]);
const { data, isLoading, isError, isSuccess, refetch } = useQuery({
queryKey: ["PollsDetails", id],
queryFn: () => getSinglePoll(id),
});
useEffect(() => {
if (data) {
console.log(data);
console.log(data.data);
console.log(data.data[0]);
setDetails(data.data);
}
}, [data]);
const postPollAnswerMutation = useMutation({
mutationKey: ["postAnswer"],
mutationFn: ({ id, payload }: PostAnswerArgs) =>
postCitizenPollsAnswer(id, payload),
onSuccess: () => {
toast.success("پاسخ شما با موفقیت ثبت شد");
router.push("/User/Polls");
},
onError: (error: any) => {
toast.error(`خطا در ارسال پاسخ: ${error.message}`);
},
});
const onPollAnswerSubmit = () => {
postPollAnswerMutation.mutate({
id,
payload: {
text: "",
choicesIds: satisfaction,
},
});
};
return (
<div className="bg-white w-1/2 mx-auto p-5 h-full flex flex-col items-center">
<h1
className="font-bold mb-4"
dangerouslySetInnerHTML={{ __html: details?.question }}
></h1>
<div className=" flex flex-row flex-wrap w-full justify-between">
{details?.pollType === 0 &&
details?.choices.map((item: any) => (
<RadioGroup
onValueChange={(value) => setSatisfaction([Number(value)])}
className="flex flex-row mt-4 w-[48%] "
dir="rtl"
>
<div className="flex items-center space-x-2 border border-gray-500 rounded-md w-full p-3 gap-4">
<RadioGroupItem value={item.id} id={item.id} />
<Label htmlFor={item.id}>
<p
dangerouslySetInnerHTML={{
__html: STR.parseHtml(item.text),
}}
></p>
{/* {STR.removeTags(item.text)} */}
</Label>
</div>
</RadioGroup>
// <SelectivePoll
// {...poll}
// multiple={false}
// disabled={poll.answer ? true : false}
// onChange={(values) => handleChange(values, "selectedChoices")}
// selecteds={values.selectedChoices}
// />
))}
</div>
{/* {details.pollType === 1 && (
<SelectivePoll
multiple
{...poll}
disabled={poll.answer ? true : false}
onChange={(values) => handleChange(values, "selectedChoices")}
selecteds={values.selectedChoices}
/>
)} */}
{!details?.answer && (
<Button
className="w-full text-black bg-yellow rounded-md hover:bg-yellow/80 my-5"
// onClick={makeRequest}
// className={styles.pollButton}
// loading={submitLoading}
onClick={onPollAnswerSubmit}
>
ثبت
</Button>
)}
{details?.answer && (
<p className="mt-8 text-red">شما قبلا در این نظرسنجی شرکت کرده اید.</p>
)}
</div>
);
};
export default PollDetails;