diff --git a/app/User/Polls/[id]/page.tsx b/app/User/Polls/[id]/page.tsx deleted file mode 100644 index 3652ef0..0000000 --- a/app/User/Polls/[id]/page.tsx +++ /dev/null @@ -1,9 +0,0 @@ -import React from 'react' - -function page() { - return ( -
page
- ) -} - -export default page \ No newline at end of file diff --git a/app/User/Polls/[pollsId]/page.tsx b/app/User/Polls/[pollsId]/page.tsx new file mode 100644 index 0000000..1ffc4f4 --- /dev/null +++ b/app/User/Polls/[pollsId]/page.tsx @@ -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 ( +
+ +
+ ) +} + +export default page \ No newline at end of file diff --git a/components/News/NewsDetails.tsx b/components/News/NewsDetails.tsx index 9f61255..accd769 100644 --- a/components/News/NewsDetails.tsx +++ b/components/News/NewsDetails.tsx @@ -33,7 +33,7 @@ const NewsDetails: React.FC = ({ id }) => { // const id = Number(useParams()); // const { id } = useParams(); - const [details, setDetails] = useState(null); + const [details, setDetails] = useState(null); const { data, isLoading, isError, isSuccess, refetch } = useQuery({ queryKey: ["NewsDetails", id], queryFn: () => getSingleNews(id), diff --git a/components/Polls/Poll.tsx b/components/Polls/Poll.tsx index 447cc41..9f11434 100644 --- a/components/Polls/Poll.tsx +++ b/components/Polls/Poll.tsx @@ -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(/ /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(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 ( - - -

عنوان

-

سوال

-

- تاریخ و زمان -

- +
+ {isLoading ? ( +
+ +
+ ) : ( + <> + {isSuccess && + polls?.map((item: any) => ( + + +

+ {item.title} +

+

+ {STR.removeTags(item.question)} +

+

+ {/* {convertFullTime(item?.created)} */} +

+ + ))} + + )} +
); }; diff --git a/components/Polls/PollDetails.tsx b/components/Polls/PollDetails.tsx new file mode 100644 index 0000000..5cfa15a --- /dev/null +++ b/components/Polls/PollDetails.tsx @@ -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 = ({ id }) => { + const [details, setDetails] = useState(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 ( +
+

+ + {/* {details.pollType === 0 && ( + handleChange(values, "selectedChoices")} + selecteds={values.selectedChoices} + /> + )} + {details.pollType === 1 && ( + handleChange(values, "selectedChoices")} + selecteds={values.selectedChoices} + /> + )} */} + + {!details?.answer && ( + + )} + {details?.answer && ( +

+ شما قبلا در این نظرسنجی شرکت کرده اید. +

+ )} +
+ ); +}; + +export default PollDetails; diff --git a/components/Polls/Polls.tsx b/components/Polls/Polls.tsx index aaf4686..e59e52d 100644 --- a/components/Polls/Polls.tsx +++ b/components/Polls/Polls.tsx @@ -8,10 +8,6 @@ function Polls() { {/* SuccessFull */} - - - - ); } diff --git a/utils/CitizenPolls.ts b/utils/CitizenPolls.ts new file mode 100644 index 0000000..cf5c7aa --- /dev/null +++ b/utils/CitizenPolls.ts @@ -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; +}