94 lines
2.0 KiB
TypeScript
94 lines
2.0 KiB
TypeScript
export async function getPage(
|
|
base_url_data,
|
|
pathPage,
|
|
current_page = 1,
|
|
count_per_page = 6
|
|
) {
|
|
const url = `${base_url_data}?_embed&per_page=${count_per_page}&page=${current_page}`
|
|
let totalPage = 0
|
|
const posts = await fetch(url).then((res) => {
|
|
console.log('header:', res.headers.get('x-wp-totalpages'))
|
|
totalPage = parseInt(res.headers.get('x-wp-totalpages'))
|
|
return res.json()
|
|
})
|
|
|
|
const paginations = []
|
|
for (let i = 0; i < totalPage; i++) {
|
|
let page = i + 1
|
|
paginations.push({
|
|
url: `${pathPage}${page}`,
|
|
active: current_page == page ? true : false,
|
|
page,
|
|
})
|
|
}
|
|
return {
|
|
posts,
|
|
paginations,
|
|
}
|
|
}
|
|
export async function getPaginationStatic(base_url, count_per_page = 6) {
|
|
let totalPage = 0
|
|
const url = `${base_url}?per_page=${count_per_page}`
|
|
|
|
const posts = await fetch(url).then((res) => {
|
|
totalPage = parseInt(res.headers.get('x-wp-totalpages'))
|
|
return res.json()
|
|
})
|
|
const paginations = []
|
|
for (let i = 0; i < totalPage; i++) {
|
|
paginations.push({
|
|
params: {
|
|
page: (i + 1).toString(),
|
|
},
|
|
})
|
|
}
|
|
return paginations
|
|
}
|
|
|
|
// whatsnew
|
|
const WP_API_URL = process.env.WORDPRESS_GRAPHQL_ENDPOINT
|
|
|
|
async function fetchWhatsNewAPI(query, variables = {}) {
|
|
const headers = { 'Content-Type': 'application/json' }
|
|
|
|
const res = await fetch(WP_API_URL, {
|
|
method: 'POST',
|
|
headers,
|
|
body: JSON.stringify({ query, variables }),
|
|
})
|
|
|
|
const json = await res.json()
|
|
if (json.errors) {
|
|
console.log(json.errors)
|
|
throw new Error('Failed to fetch API')
|
|
}
|
|
return json.data
|
|
}
|
|
|
|
export async function getLatestMonthlyRelease() {
|
|
const data = await fetchWhatsNewAPI(
|
|
`
|
|
query LatestMR {
|
|
monthlyReleases {
|
|
edges {
|
|
node {
|
|
excerpt(format: RENDERED)
|
|
id
|
|
slug
|
|
title
|
|
featuredImage {
|
|
node {
|
|
altText
|
|
sourceUrl
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
`
|
|
)
|
|
|
|
return data?.monthlyReleases
|
|
}
|