introduce remote/api

This commit is contained in:
tmshv committed 2023-02-09 19:49:34 +05:00
1 parent 727c0592ce
commit 780e9ac0e8
3 files changed
+49 -28

No files matched your search

+3 -24
View File
@@ -3,37 +3,17 @@ import { App } from "src/components/App"
import { Page } from "src/components/Page"
import { NextSeo } from "next-seo"
import { MetaBuilder } from "src/lib/meta"
import { apiGet } from "@/next-lib"
import { GetStaticProps, NextPage } from "next"
import { IBreadcumbsPart, IMenu, IMeta, ITag, Sign, Token } from "@/types"
import { Html } from "src/components/Html"
import { Youtube } from "@/components/Youtube"
import { createMenu, createPage, createPageUrls } from "@/remote/factory"
import { paramsToSlug } from "@/remote/lib"
import { PageGrid } from "@/components/PageGrid"
import { useRouter } from "next/router"
import { FileCard } from "@/components/FIleCard"
import { Picture } from "@/ui/Picture"
import { tail } from "@/lib/array"
async function getUrls() {
let urls: string[] = []
const limit = 100
let start = 0
while (true) {
const url = `https://hudozka.tmshv.com/pages?_limit=${limit}&_start=${start}`
const res = await apiGet(createPageUrls)(url, () => ({ items: [] }))
if (!res || res.items.length === 0) {
break
}
start += limit
urls = [...urls, ...res.items]
}
return urls
}
import { getMenu, getPageBySlug, getUrls } from "@/remote/api"
type Props = {
title: string
@@ -172,15 +152,14 @@ const Index: NextPage<Props> = props => {
export const getStaticProps: GetStaticProps<Props> = async ctx => {
const slug = paramsToSlug(ctx.params?.slug ?? [])
const url = `https://hudozka.tmshv.com/pages?slug=${slug}`
const page = await apiGet(createPage)(url, () => null)
const page = await getPageBySlug(slug)
if (!page) {
return {
notFound: true,
}
}
const menu = await apiGet(createMenu)("https://hudozka.tmshv.com/menu", () => [])
const menu = await getMenu()
const description = page.description ?? undefined
const breadcrumbSize = page?.breadcrumb?.length ?? 0
+3 -4
View File
@@ -6,9 +6,8 @@ import { HudozkaTitle } from "src/components/HudozkaTitle"
import { MetaBuilder } from "src/lib/meta"
import { GetStaticProps, NextPage } from "next"
import { IMenu, IMeta, PageCardDto } from "src/types"
import { apiGet } from "@/next-lib"
import { createHomeCards, createMenu } from "@/remote/factory"
import { useMobile } from "@/hooks/useMobile"
import { getHomeCards, getMenu } from "@/remote/api"
type Props = {
title: string
@@ -67,8 +66,8 @@ const Index: NextPage<Props> = props => {
}
export const getStaticProps: GetStaticProps<Props> = async () => {
const items = await apiGet(createHomeCards)("https://hudozka.tmshv.com/home", async () => [])
const menu = await apiGet(createMenu)("https://hudozka.tmshv.com/menu", () => [])
const items = await getHomeCards()
const menu = await getMenu()
const title = "Шлиссельбургская ДХШ"
const meta = (new MetaBuilder())
+43
View File
@@ -0,0 +1,43 @@
import { apiGet } from "@/next-lib"
import { createHomeCards, createMenu, createPage, createPageUrls } from "@/remote/factory"
import { IMenu, IPage, PageCardDto } from "@/types"
const backendUrl = "https://hudozka.tmshv.com"
export async function getUrls(): Promise<string[]> {
let urls: string[] = []
const limit = 100
let start = 0
while (true) {
const url = `${backendUrl}/pages?_limit=${limit}&_start=${start}`
const res = await apiGet(createPageUrls)(url, () => ({ items: [] }))
if (!res || res.items.length === 0) {
break
}
start += limit
urls = [...urls, ...res.items]
}
return urls
}
export async function getPageBySlug(slug: string): Promise<IPage | null> {
const url = `${backendUrl}/pages?slug=${slug}`
const page = await apiGet(createPage)(url, () => null)
if (!page) {
return null
}
return page
}
export async function getHomeCards(): Promise<PageCardDto[]> {
const items = await apiGet(createHomeCards)(`${backendUrl}/home`, async () => [])
return items
}
export async function getMenu(): Promise<IMenu[]> {
const menu = await apiGet(createMenu)(`${backendUrl}/menu`, () => [])
return menu
}