Skip to content

Commit

Permalink
Merge pull request #78 from NeuralTeam/feature/update-posts-for-new-api
Browse files Browse the repository at this point in the history
feat: обновлены API эндпоинты исправлены не большие ошибки
  • Loading branch information
Osbornnnnn authored Aug 4, 2024
2 parents 351f734 + 75ef602 commit 2768d1d
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 37 deletions.
20 changes: 19 additions & 1 deletion next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,25 @@ const nextConfig = {
output: "standalone",

reactStrictMode: process.env.DEV_STRICT_MODE === "true",
transpilePackages: ["next-mdx-remote"]
transpilePackages: ["next-mdx-remote"],
images: {
remotePatterns: [
{
protocol: "http",
hostname: "localhost",
port: "8000"
},
{
protocol: "http",
hostname: "127.0.0.1",
port: "8000"
},
{
protocol: "https",
hostname: "vsau.neuralteam.ru"
}
]
}
};

export default withBundleAnalyzer(nextConfig);
10 changes: 3 additions & 7 deletions src/app/news/(static)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ const NewsLayout = async ({
}: Readonly<{
children: ReactNode;
}>) => {
const topics: ITopic[] = await fetch(`${process.env.NEXT_PUBLIC_API_DOMAIN}/v1/posts/topics`, { next: { revalidate: 3600 } }).then((res) =>
res.json()
);
const response = await fetch(`${process.env.NEXT_PUBLIC_API_DOMAIN}/v1/posts?type=1&page=1&perPage=4`, {
next: { revalidate: 3600 }
});
const topics: ITopic[] = await fetch(`${process.env.NEXT_PUBLIC_API_DOMAIN}/v1/posts/topics`, { cache: "no-store" }).then((res) => res.json());
const response = await fetch(`${process.env.NEXT_PUBLIC_API_DOMAIN}/v1/posts?type=1&page=1&perPage=4`, { cache: "no-store" });
const news: { count_page: number; current_page: number; posts: IListPost[] } = await response.json();

return (
Expand All @@ -42,7 +38,7 @@ const NewsLayout = async ({
key={data.id}
id={`${slugifyReplace(data.title, { lower: true, strict: true })}-${data.id}`}
title={data.title}
createdAt={data.created_at}
createdAt={data.released_at}
/>
))}
</div>
Expand Down
37 changes: 21 additions & 16 deletions src/app/news/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export interface IPost {
title: string;
body: string;
preview_picture: string | null;
topic: string;
created_at: number;
topic: { id: number; title: string } | null;
released_at: number;
}

export interface IListPost {
Expand All @@ -31,11 +31,11 @@ export interface IListPost {
type: number;
topic: string;
updated_at: number;
created_at: number;
released_at: number;
}

async function getNewsByID(id: number): Promise<IPost> {
return await fetch(`${process.env.NEXT_PUBLIC_API_DOMAIN}/v1/posts/${id}`).then((res) => {
return await fetch(`${process.env.NEXT_PUBLIC_API_DOMAIN}/v1/posts/${id}`, { cache: "no-store" }).then((res) => {
const data = res.json();
if (res.status !== 200) notFound();

Expand All @@ -50,23 +50,22 @@ export async function generateMetadata(
const idSplit = params.id.split("-");
const id = idSplit.slice(-1)[0];
const news = await getNewsByID(+id);
const fmtDate = new Date(news.created_at * 1000).toLocaleString("ru", { year: "numeric", month: "long", day: "numeric", timeZone: "UTC" });
const fmtDate = new Date(news.released_at * 1000).toLocaleString("ru", { year: "numeric", month: "long", day: "numeric", timeZone: "UTC" });

return {
title: news.title,
openGraph: {
title: news.title,
tags: [news.topic],
tags: [news.topic != null ? news.topic.title : "Новости"],
releaseDate: fmtDate
}
};
}

const RecommendedPosts = async () => {
const posts: { count_page: number; current_page: number; posts: IListPost[] } = await fetch(
`${process.env.NEXT_PUBLIC_API_DOMAIN}/v1/posts?type=1&page=1&perPage=4`,
{
cache: "no-cache"
}
`${process.env.NEXT_PUBLIC_API_DOMAIN}/v1/posts?type=1&perPage=4`,
{ cache: "no-store" }
).then((res) => res.json());

return (
Expand All @@ -76,7 +75,7 @@ const RecommendedPosts = async () => {
key={data.id}
id={`${slugifyReplace(data.title, { lower: true, strict: true })}-${data.id}`}
title={data.title}
createdAt={data.created_at}
createdAt={data.released_at}
/>
))}
</>
Expand All @@ -95,7 +94,7 @@ const NewsIDPage = async ({ params, searchParams }: { params: { id: string }; se
redirect(`${process.env.NEXT_PUBLIC_DOMAIN}/news/${seo_title}-${id}${searchParams.ref !== undefined ? ref : ""}`);
}

const fmtDate = new Date(post.created_at * 1000).toLocaleString("ru", { year: "numeric", month: "long", day: "numeric", timeZone: "UTC" });
const fmtDate = new Date(post.released_at * 1000).toLocaleString("ru", { year: "numeric", month: "long", day: "numeric", timeZone: "UTC" });

return (
<main className="flex justify-center space-x-[5vw] pl-[calc(4vw-70px)] pr-[4vw] pt-[70px]">
Expand All @@ -112,8 +111,8 @@ const NewsIDPage = async ({ params, searchParams }: { params: { id: string }; se
{post.topic != null && (
<div className="flex text-[17px] font-normal leading-[20px] text-[#7C7C7C]">
Раздел:
<Link href="/news?topic=1" className="pl-2 text-[#0F91D6]">
{post.topic.charAt(0).toUpperCase() + post.topic.slice(1)}
<Link href={`/news${`?topic=${post.topic.id}`}`} className="pl-2 text-[#0F91D6]">
{post.topic.title.charAt(0).toUpperCase() + post.topic.title.slice(1)}
</Link>
</div>
)}
Expand Down Expand Up @@ -144,9 +143,15 @@ const NewsIDPage = async ({ params, searchParams }: { params: { id: string }; se
<NewsOpenPhotoDialog isOpen={searchParams.mediaId == 0} mediaId={0} photo={cardNewsPlug}>
<div className="overflow-hidden rounded-[10px]">
<Image
src={cardNewsPlug}
src={
post.preview_picture === null
? cardNewsPlug
: `${process.env.NEXT_PUBLIC_API_DOMAIN}/v1/storage/${post.preview_picture}`
}
width={1920}
height={250}
priority={false}
placeholder="blur"
placeholder="empty"
alt="#"
className="aspect-video object-cover transition duration-300 hover:scale-110 active:scale-100 active:opacity-50"
/>
Expand Down
12 changes: 3 additions & 9 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,15 @@ import relevantPlug from "@/shared/images/plugs/relevant.png";
import { IListPost } from "@/app/news/[id]/page";

const getNews = async (): Promise<{ count: number; posts: IListPost[] }> => {
return await fetch(`${process.env.NEXT_PUBLIC_API_DOMAIN}/v1/posts?type=1&page=1&perPage=10`, { next: { revalidate: 3600 } }).then((res) =>
res.json()
);
return await fetch(`${process.env.NEXT_PUBLIC_API_DOMAIN}/v1/posts?type=1&page=1&perPage=10`, { cache: "no-store" }).then((res) => res.json());
};

const getAds = async (): Promise<{ count: number; posts: IListPost[] }> => {
return await fetch(`${process.env.NEXT_PUBLIC_API_DOMAIN}/v1/posts?type=2&page=1&perPage=10`, { next: { revalidate: 3600 } }).then((res) =>
res.json()
);
return await fetch(`${process.env.NEXT_PUBLIC_API_DOMAIN}/v1/posts?type=2&page=1&perPage=10`, { cache: "no-store" }).then((res) => res.json());
};

const getAnnounces = async (): Promise<{ count: number; posts: IListPost[] }> => {
return await fetch(`${process.env.NEXT_PUBLIC_API_DOMAIN}/v1/posts?type=3&page=1&perPage=10`, { next: { revalidate: 3600 } }).then((res) =>
res.json()
);
return await fetch(`${process.env.NEXT_PUBLIC_API_DOMAIN}/v1/posts?type=3&page=1&perPage=10`, { cache: "no-store" }).then((res) => res.json());
};

const RootPage = async () => {
Expand Down
3 changes: 1 addition & 2 deletions src/widgets/news/news-gallery.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Image, { StaticImageData } from "next/image";
import { NewsOpenPhotoDialog } from "@/widgets/news/news-open-photo-dialog";
import { DownloadIcon, ShareIcon } from "@/shared/images/icons/other";
import Link from "next/link";
import { DownloadIcon } from "@/shared/images/icons/other";

export function NewsGallery({ mediaId, photoList }: { mediaId?: number; photoList: StaticImageData[] }) {
return (
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/news/news-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { slugifyReplace } from "@/shared/libs/slugify";
const NewsList = async ({ pagination, topic }: { pagination: { page: number; perPage: number }; topic: string }) => {
const response = await fetch(
`${process.env.NEXT_PUBLIC_API_DOMAIN}/v1/posts?${topic === undefined ? "" : `topic=${topic}&`}type=1&page=${pagination.page}&perPage=${pagination.perPage}`,
{ cache: "no-cache" }
{ cache: "no-store" }
);
const newsList: { count_page: number; current_page: number; posts: IListPost[] } = await response.json();

Expand All @@ -21,7 +21,7 @@ const NewsList = async ({ pagination, topic }: { pagination: { page: number; per
id={`${slugifyReplace(post.title, { lower: true, strict: true })}-${post.id}`}
title={post.title}
body={post.body}
createdAt={post.created_at}
createdAt={post.released_at}
currentPage={pagination.page}
/>
))}
Expand Down

0 comments on commit 2768d1d

Please sign in to comment.