Skip to content

Commit

Permalink
refactor: add fields to search condition
Browse files Browse the repository at this point in the history
  • Loading branch information
N-Laboratory committed May 17, 2023
1 parent b4092a1 commit 23add20
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/app/api/list/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export const POST = async (request: NextRequest) => {

const keyword = htmlspecialchars(result.data.keyword.trim())
const articleList =
keyword == '' ? await getArticleList() : await getArticleListByKeyword(keyword)
keyword == ''
? await getArticleList('id,title,overview,svgPath,createdDate')
: await getArticleListByKeyword(keyword, 'id,title,overview,svgPath,createdDate')
if (!articleList) {
return NextResponse.json({ articleListPerPage }, { status: 500 })
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/articles/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { notFound } from 'next/navigation'

// Dynamic Route使用時にSSGでビルドする
export async function generateStaticParams() {
const response = await getArticleList()
const response = await getArticleList('id')
const articleList = response?.contents

return !articleList
Expand Down
2 changes: 1 addition & 1 deletion src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const metadata = {

const Home = async () => {
let hasError = false
const articleList = await getArticleList().catch(() => {
const articleList = await getArticleList('id,title,overview,svgPath,createdDate').catch(() => {
hasError = true
})

Expand Down
17 changes: 13 additions & 4 deletions src/libs/microcms/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ export const getArticle = async (id: string) => {
return article
}

export const getArticleList = async () => {
export const getArticleList = async (filedNames?: string) => {
const articleList = await client
.get<ArticleList>({
endpoint: 'article',
queries: { limit: 100, clearCache: 'true' } as CustomMicroCMSQueries,
queries: {
limit: 100,
clearCache: 'true',
fields: filedNames ?? '',
} as CustomMicroCMSQueries,
})
.then((res) => res)
.catch((err) => {
Expand All @@ -46,11 +50,16 @@ export const getArticleList = async () => {
return articleList
}

export const getArticleListByKeyword = async (keyword: string) => {
export const getArticleListByKeyword = async (keyword: string, filedNames?: string) => {
const articleList = await client
.getList<Article>({
endpoint: 'article',
queries: { q: keyword, limit: 100, clearCache: 'true' } as CustomMicroCMSQueries,
queries: {
q: keyword,
limit: 100,
clearCache: 'true',
fields: filedNames ?? '',
} as CustomMicroCMSQueries,
})
.then((res) => res)
.catch((err) => {
Expand Down

1 comment on commit 23add20

@vercel
Copy link

@vercel vercel bot commented on 23add20 May 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

next-strapi-cms – ./

next-strapi-cms-n-laboratory.vercel.app
n-laboratory.jp
next-strapi-cms-git-main-n-laboratory.vercel.app

Please sign in to comment.