Added part of Polls Component

master
YasamanKahdooei 2024-07-03 16:43:23 +03:30
parent d72a0bdcec
commit 2bb6da03cb
7 changed files with 211 additions and 26 deletions

View File

@ -1,9 +0,0 @@
import React from 'react'
function page() {
return (
<div>page</div>
)
}
export default page

View File

@ -0,0 +1,14 @@
import PollDetails from '@/components/Polls/PollDetails';
import React from 'react'
function page({params}: {params : any}) {
const id = Number(params.pollsId);
return (
<div>
<PollDetails id={id}/>
</div>
)
}
export default page

View File

@ -1,20 +1,99 @@
"use client";
import React, { useEffect, useState } from "react";
import { useQuery } from "@tanstack/react-query";
import Link from "next/link";
import React from "react";
import { FaRegChartBar } from "react-icons/fa6";
import { useRouter } from "next/navigation";
import { ThreeDots } from "react-loader-spinner";
import { getPolls } from "@/utils/CitizenPolls";
import { convertFullTime } from "@/lib/utils";
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;
}
}
interface Poll {
id: string;
title: string;
pollType: number;
question: string;
choices: any;
pollState: number;
created: string;
expiration: string;
answer: any;
isDeleted: boolean;
}
const Poll = () => {
const router = useRouter();
const [polls, setPolls] = useState<any | null>(null);
const { data, isLoading, isError, isSuccess, refetch } = useQuery({
queryKey: ["Polls"],
queryFn: () => getPolls(),
});
useEffect(() => {
if (data) {
console.log(data);
console.log(data.data);
console.log(data.data[0]);
setPolls(data.data);
}
}, [data]);
return (
<div className="w-full h-full flex flex-row flex-wrap justify-between">
{isLoading ? (
<div className="flex justify-center items-center text-center w-full h-full">
<ThreeDots
visible={true}
height="50"
width="50"
color="#003778"
radius="9"
ariaLabel="three-dots-loading"
wrapperStyle={{}}
wrapperClass=""
/>
</div>
) : (
<>
{isSuccess &&
polls?.map((item: any) => (
<Link
href={"#"}
className="flex flex-col items-start justify-around w-72 h-48 bg-white shadow-[0_0_2px_1px_rgba(0,0,0,0.1)] rounded-lg p-3"
href={`/User/Polls/${item.id}`}
className="flex flex-col items-start justify-around min-w-72 h-48 bg-white shadow-[0_0_2px_1px_rgba(0,0,0,0.1)] rounded-lg p-3 mb-4"
>
<FaRegChartBar className="text-2xl text-blue mb-3" />
<p className="text-lg font-bold text-black mb-3">عنوان</p>
<p className="text-[15px] text-gray-400 mb-3">سوال</p>
<p className="text-lg font-bold text-black mb-3">
{item.title}
</p>
<p className="text-[15px] text-gray-400 mb-3">
{STR.removeTags(item.question)}
</p>
<p className="text-xs w-full mt-3 pt-3 border-t-[1px] border-t-gray-200 font-bold text-gray-400">
تاریخ و زمان
{/* {convertFullTime(item?.created)} */}
</p>
</Link>
))}
</>
)}
</div>
);
};

View File

@ -0,0 +1,72 @@
"use client";
import React, { useEffect, useState } from "react";
import { useQuery } from "@tanstack/react-query";
import { useParams } from "next/navigation";
import { getSinglePoll } from "@/utils/CitizenPolls";
import { Button } from "../ui/button";
interface PollsProps {
id: number;
}
const PollDetails: React.FC<PollsProps> = ({ id }) => {
const [details, setDetails] = useState<any | null>(null);
const { data, isLoading, isError, isSuccess, refetch } = useQuery({
queryKey: ["PollssDetails", id],
queryFn: () => getSinglePoll(id),
});
useEffect(() => {
if (data) {
console.log(data);
console.log(data.data);
console.log(data.data[0]);
setDetails(data.data);
}
}, [data]);
return (
<div>
<h1
className=""
dangerouslySetInnerHTML={{ __html: details?.question }}
></h1>
{/* {details.pollType === 0 && (
<SelectivePoll
{...poll}
multiple={false}
disabled={poll.answer ? true : false}
onChange={(values) => handleChange(values, "selectedChoices")}
selecteds={values.selectedChoices}
/>
)}
{details.pollType === 1 && (
<SelectivePoll
multiple
{...poll}
disabled={poll.answer ? true : false}
onChange={(values) => handleChange(values, "selectedChoices")}
selecteds={values.selectedChoices}
/>
)} */}
{!details?.answer && (
<Button
// onClick={makeRequest}
// className={styles.pollButton}
// loading={submitLoading}
>
ارسال
</Button>
)}
{details?.answer && (
<p className="mt-8 text-red-600">
شما قبلا در این نظرسنجی شرکت کرده اید.
</p>
)}
</div>
);
};
export default PollDetails;

View File

@ -8,10 +8,6 @@ function Polls() {
{/* SuccessFull */}
<Poll />
<Poll />
<Poll />
<Poll />
<Poll />
</>
);
}

33
utils/CitizenPolls.ts Normal file
View File

@ -0,0 +1,33 @@
import axios from "axios";
import { getCookie } from "cookies-next";
axios.defaults.baseURL = process.env.NEXT_PUBLIC_BASE_URL_APP;
axios.interceptors.request.use(function (config) {
const token = getCookie("Shahrbin_Token");
config.headers.Authorization = token ? `Bearer ${token}` : "";
return config;
});
// Get Polls
export async function getPolls() {
const data = await axios.get(`/api/CitizenPolls/Polls`);
return data.data;
}
// Get Polls {ID}
export async function getSinglePoll(id: number) {
const data = await axios.get(`/api/CitizenPolls/${id}`);
return data.data;
}
// Post CitizenPollsAnswer {id}
export async function postCitizenPollsAnswer(
id: number,
payload: { text: string; choicesIds: [number] }
) {
const data = await axios.post(
`/api/CitizenPolls/Polls/Answer/${id}`,
payload
);
return data.data;
}