Added News Component

master
YasamanKahdooei 2024-07-01 09:40:54 +03:30
parent e9a1549c21
commit d72a0bdcec
6 changed files with 201 additions and 3 deletions

View File

@ -0,0 +1,15 @@
import NewsDetails from '@/components/News/NewsDetails';
import React from 'react'
function page({params}: {params : any}) {
const id = Number(params.newsId);
console.log(id);
return (
<div>
<NewsDetails id={id}/>
</div>
)
}
export default page

View File

@ -1,7 +1,11 @@
import React from 'react'
import News from "@/components/News/News";
import React from "react";
export default function page() {
return (
<div>page</div>
)
// <section className='w-full grid grid-cols-[auto-fit_minmax(250px,_1fr)_100px] gap-5 auto-rows-[150px)]'>
<section className="w-full flex flex-row justify-between">
<News />
</section>
);
}

View File

@ -119,3 +119,7 @@
src: url(../assets/fonts/ttf/iranyekanwebregularfanum.ttf);
}
.news-description img{
max-width: 700px;
width: 100%;
}

88
components/News/News.tsx Normal file
View File

@ -0,0 +1,88 @@
"use client";
import React, { useEffect, useState } from "react";
import { useQuery } from "@tanstack/react-query";
import Image from "next/image";
import Link from "next/link";
import { convertFullTime } from "@/lib/utils";
import { useRouter } from "next/navigation";
import { getNews } from "@/utils/CitizenNews";
import { ThreeDots } from "react-loader-spinner";
interface News {
id: string;
title: string;
url: string;
imageFile: {
id: string;
url: string;
url2: string;
url3: string;
url4: string;
alternateText: string;
title: string;
mediaType: 0;
};
created: string;
isDeleted: boolean;
}
function News() {
const router = useRouter();
const [news, setNews] = useState<any | null>(null);
const { data, isLoading, isError, isSuccess, refetch } = useQuery({
queryKey: ["News"],
queryFn: () => getNews(),
});
useEffect(() => {
if (data) {
console.log(data);
console.log(data.data);
console.log(data.data[0]);
setNews(data.data);
}
}, [data]);
if (isError) {
return <div>Error loading data</div>;
}
return (
<div className="w-full h-full flex flex-row 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 &&
news?.map((item: any) => (
<Link
href={`/User/News/${item.id}`}
className="flex justify-end items-center w-1/3 h-36 mx-3 rounded-md bg-no-repeat bg-cover cursor-pointer"
style={{
backgroundImage: `url(
${process.env.NEXT_PUBLIC_BASE_URL_APP}${item?.imageFile?.url4})`,
}}
>
<span className=" bg-white/50 rounded-md ml-2">
<p className="text-black p-1">{item.title}</p>
</span>
</Link>
))}
</>
)}
</div>
);
}
export default News;

View File

@ -0,0 +1,65 @@
"use client";
import { convertFullTime } from "@/lib/utils";
import { getSingleNews } from "@/utils/CitizenNews";
import { useQuery } from "@tanstack/react-query";
import { useParams } from "next/navigation";
import React, { useEffect, useState } from "react";
interface NewsDetails {
id: string;
title: string;
description: string;
url: string;
imageFile: {
id: string;
url: string;
url2: string;
url3: string;
url4: string;
alternateText: string;
title: string;
mediaType: 0;
};
created: string;
isDeleted: boolean;
}
interface NewsProps {
id: number;
}
const NewsDetails: React.FC<NewsProps> = ({ id }) => {
// const NewsDetails: React.FC<NewsProps> = () => {
// const id = Number(useParams());
// const { id } = useParams();
const [details, setDetails] = useState<any | null>(null);
const { data, isLoading, isError, isSuccess, refetch } = useQuery({
queryKey: ["NewsDetails", id],
queryFn: () => getSingleNews(id),
});
useEffect(() => {
if (data) {
console.log(data);
console.log(data.data);
console.log(data.data[0]);
setDetails(data.data);
}
}, [data]);
return (
<div className=" w-full">
<div className=" flex justify-between items-center w-full">
<h1 className=" text-xl text-blue">{details?.title}</h1>
<p className=" text-gray-600">{convertFullTime(details?.created)}</p>
</div>
<div
className="w-full mt-2 news-description"
dangerouslySetInnerHTML={{ __html: details?.description }}
></div>
</div>
);
};
export default NewsDetails;

22
utils/CitizenNews.ts Normal file
View File

@ -0,0 +1,22 @@
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 News
export async function getNews() {
const data = await axios.get(`/api/CitizenNews/News`);
return data.data;
}
// Get News {ID}
export async function getSingleNews(id: number) {
// export async function getSingleNews(id : any) {
const data = await axios.get(`/api/CitizenNews/${id}`);
return data.data;
}