introduce remote/api
This commit is contained in:
1 parent
727c0592ce
commit
780e9ac0e8
3 files changed
+49
-28
No files matched your search
+3
-24
@@ -3,37 +3,17 @@ import { App } from "src/components/App"
|
|||||||
import { Page } from "src/components/Page"
|
import { Page } from "src/components/Page"
|
||||||
import { NextSeo } from "next-seo"
|
import { NextSeo } from "next-seo"
|
||||||
import { MetaBuilder } from "src/lib/meta"
|
import { MetaBuilder } from "src/lib/meta"
|
||||||
import { apiGet } from "@/next-lib"
|
|
||||||
import { GetStaticProps, NextPage } from "next"
|
import { GetStaticProps, NextPage } from "next"
|
||||||
import { IBreadcumbsPart, IMenu, IMeta, ITag, Sign, Token } from "@/types"
|
import { IBreadcumbsPart, IMenu, IMeta, ITag, Sign, Token } from "@/types"
|
||||||
import { Html } from "src/components/Html"
|
import { Html } from "src/components/Html"
|
||||||
import { Youtube } from "@/components/Youtube"
|
import { Youtube } from "@/components/Youtube"
|
||||||
import { createMenu, createPage, createPageUrls } from "@/remote/factory"
|
|
||||||
import { paramsToSlug } from "@/remote/lib"
|
import { paramsToSlug } from "@/remote/lib"
|
||||||
import { PageGrid } from "@/components/PageGrid"
|
import { PageGrid } from "@/components/PageGrid"
|
||||||
import { useRouter } from "next/router"
|
import { useRouter } from "next/router"
|
||||||
import { FileCard } from "@/components/FIleCard"
|
import { FileCard } from "@/components/FIleCard"
|
||||||
import { Picture } from "@/ui/Picture"
|
import { Picture } from "@/ui/Picture"
|
||||||
import { tail } from "@/lib/array"
|
import { tail } from "@/lib/array"
|
||||||
|
import { getMenu, getPageBySlug, getUrls } from "@/remote/api"
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
title: string
|
title: string
|
||||||
@@ -172,15 +152,14 @@ const Index: NextPage<Props> = props => {
|
|||||||
|
|
||||||
export const getStaticProps: GetStaticProps<Props> = async ctx => {
|
export const getStaticProps: GetStaticProps<Props> = async ctx => {
|
||||||
const slug = paramsToSlug(ctx.params?.slug ?? [])
|
const slug = paramsToSlug(ctx.params?.slug ?? [])
|
||||||
const url = `https://hudozka.tmshv.com/pages?slug=${slug}`
|
const page = await getPageBySlug(slug)
|
||||||
const page = await apiGet(createPage)(url, () => null)
|
|
||||||
if (!page) {
|
if (!page) {
|
||||||
return {
|
return {
|
||||||
notFound: true,
|
notFound: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const menu = await apiGet(createMenu)("https://hudozka.tmshv.com/menu", () => [])
|
const menu = await getMenu()
|
||||||
|
|
||||||
const description = page.description ?? undefined
|
const description = page.description ?? undefined
|
||||||
const breadcrumbSize = page?.breadcrumb?.length ?? 0
|
const breadcrumbSize = page?.breadcrumb?.length ?? 0
|
||||||
|
|||||||
+3
-4
@@ -6,9 +6,8 @@ import { HudozkaTitle } from "src/components/HudozkaTitle"
|
|||||||
import { MetaBuilder } from "src/lib/meta"
|
import { MetaBuilder } from "src/lib/meta"
|
||||||
import { GetStaticProps, NextPage } from "next"
|
import { GetStaticProps, NextPage } from "next"
|
||||||
import { IMenu, IMeta, PageCardDto } from "src/types"
|
import { IMenu, IMeta, PageCardDto } from "src/types"
|
||||||
import { apiGet } from "@/next-lib"
|
|
||||||
import { createHomeCards, createMenu } from "@/remote/factory"
|
|
||||||
import { useMobile } from "@/hooks/useMobile"
|
import { useMobile } from "@/hooks/useMobile"
|
||||||
|
import { getHomeCards, getMenu } from "@/remote/api"
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
title: string
|
title: string
|
||||||
@@ -67,8 +66,8 @@ const Index: NextPage<Props> = props => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const getStaticProps: GetStaticProps<Props> = async () => {
|
export const getStaticProps: GetStaticProps<Props> = async () => {
|
||||||
const items = await apiGet(createHomeCards)("https://hudozka.tmshv.com/home", async () => [])
|
const items = await getHomeCards()
|
||||||
const menu = await apiGet(createMenu)("https://hudozka.tmshv.com/menu", () => [])
|
const menu = await getMenu()
|
||||||
|
|
||||||
const title = "Шлиссельбургская ДХШ"
|
const title = "Шлиссельбургская ДХШ"
|
||||||
const meta = (new MetaBuilder())
|
const meta = (new MetaBuilder())
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
|
|
||||||
Reference in new issue
Block a user