From ab297fdf168a8e91678ac516a0cf18e24cf94f7f Mon Sep 17 00:00:00 2001 From: wnhlee <2wheeh@gmail.com> Date: Tue, 27 Feb 2024 02:38:49 +0900 Subject: [PATCH] feat: add listReviews --- ui/src/lib/apis/review/server.ts | 37 ++++++++++++++++++++++++++++++++ ui/src/lib/definitions/review.ts | 4 ++++ 2 files changed, 41 insertions(+) create mode 100644 ui/src/lib/apis/review/server.ts diff --git a/ui/src/lib/apis/review/server.ts b/ui/src/lib/apis/review/server.ts new file mode 100644 index 00000000..4f44ae2b --- /dev/null +++ b/ui/src/lib/apis/review/server.ts @@ -0,0 +1,37 @@ +'use server'; + +import { unstable_noStore as noStore } from 'next/cache'; + +import type { ListReviewsQuery, ListReviewsResponse } from '@/lib/definitions/review'; +import dummyReviewList from '@/lib/dummy/review'; + +export async function listReviews(query: ListReviewsQuery): Promise { + noStore(); + + const params = Object.entries(query).reduce((acc, [key, value]) => { + if (value) { + acc.set(key, String(value)); + } + + return acc; + }, new URLSearchParams()); + + try { + const response = await fetch(`${process.env.BACKEND_URL}/api/v1/reviews?${params.toString()}`); + + if (!response.ok) { + throw new Error( + `Network Error: Failed to fetch reviews list from BACKEND API: ${response.statusText}` + ); + } + + return (await response.json()) as ListReviewsResponse; + } catch (error) { + // TODO: handle error + // console.error('Fetch Error:', error); + // throw new Error('Internal Server Error: Failed to fetch reviews list'); + + // TODO: DELETE ME when reviews api works + return await new Promise((resolve) => setTimeout(() => resolve(dummyReviewList), 1000)); + } +} diff --git a/ui/src/lib/definitions/review.ts b/ui/src/lib/definitions/review.ts index 3b21bef9..71037c25 100644 --- a/ui/src/lib/definitions/review.ts +++ b/ui/src/lib/definitions/review.ts @@ -23,3 +23,7 @@ export interface ListReviewsResponse { pagination: Pagination; filter?: ReviewFilter; } + +export interface ListReviewsQuery + extends ReviewFilter, + Partial> {}