Skip to content

Commit

Permalink
update page route to app route
Browse files Browse the repository at this point in the history
  • Loading branch information
ntnhan90 committed Jan 9, 2024
1 parent e34671b commit 0c5e8a9
Show file tree
Hide file tree
Showing 28 changed files with 182 additions and 933 deletions.
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions src/app/api/health/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export async function GET() {
return Response.json({ health: true })
}
10 changes: 10 additions & 0 deletions src/app/api/mock/logout/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { serializeCookie } from '@/lib/cookie'

export async function POST() {
const cookie = serializeCookie('auth', {}, { path: '/', expires: new Date(Date.now()) })
return Response.json({ logout: true }, {
headers: {
'Set-Cookie': cookie,
},
})
}
51 changes: 51 additions & 0 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Head from "next/head";
import Header from "@components/header"
import { useTranslation } from 'next-i18next';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
import { usePathname } from 'next/navigation'
import { useState } from 'react';

type LayoutType = {
title?: string;
children?: React.ReactNode;
};


export default ({ children, title = "2ALL" }: LayoutType) => {
const pathname = usePathname()
const [onTop, setOnTop] = useState(false);

if(pathname === "/landingpage") {
}

return (
<div className="app-main">
<Head>
<title>{title}</title>
<link rel="icon" href="/images/logo.ico" />
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Quicksand:wght@400;500;600;700&display=swap"
/>
<link
href="https://fonts.cdnfonts.com/css/svn-gilroy"
rel="stylesheet"
/>

<link
rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Inter:wght@600&display=swap"
/>
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Poppins:wght@500&display=swap"
/>
</Head>
{/* landing-page-option-3 */}
<main className=" d-flex flex-column">
<Header />
{children}
</main>
</div>
);
};
64 changes: 64 additions & 0 deletions src/hooks/useSWRAxios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import useSWR from 'swr'
import axios, {
AxiosError, AxiosHeaders,
AxiosRequestConfig,
AxiosResponse,
AxiosResponseTransformer,
} from 'axios'

type AtLeast<T, K extends keyof T> = & Pick<T, K> & Partial<T>

type AxiosResponseWithFallbackData<T> = AtLeast<AxiosResponse<T>, 'data'>

/**
* `useSWR` wrapper with `axios` as fetcher.
* Requires `fallbackData` for initial fetch.
*
* @param axiosRequest
* @param axiosFallbackData
*/
export default function useSWRAxios<T>(
axiosRequest: AxiosRequestConfig,
axiosFallbackData: AxiosResponseWithFallbackData<T>,
) {
const initFallbackData: AxiosResponse<T> = {
data: axiosFallbackData.data,
headers: {},
status: 200,
statusText: 'Initial',
config: {
headers: new AxiosHeaders(),
},
}

const fallbackData: AxiosResponse<T> = { ...initFallbackData, ...axiosFallbackData }

return useSWR(
JSON.stringify(axiosRequest),
() => axios.request<T>(axiosRequest),
{
fallbackData,
onErrorRetry: (error: AxiosError<T>, key, config, revalidate, { retryCount }) => {
// Never retry on 404.
if (error.response?.status === 404) return

// Only retry up to `maxRetry` times.
const maxRetry = parseInt(process.env.NEXT_PUBLIC_API_MAX_RETRY ?? '5', 10)
if (retryCount >= maxRetry) return

// Retry after `retryInterval` seconds.
const retryInterval = parseInt(process.env.NEXT_PUBLIC_API_RETRY_INTERVAL_IN_SECONDS ?? '5', 10)
setTimeout(() => revalidate({ retryCount }), retryInterval * 1000)
},
},
)
}

// Disable style for better reading.
// eslint-disable-next-line arrow-body-style
export const transformResponseWrapper = (transformer: AxiosResponseTransformer) => {
return ([] as AxiosResponseTransformer[]).concat(
axios.defaults.transformResponse as AxiosResponseTransformer,
transformer,
) as AxiosResponseTransformer[]
}
44 changes: 44 additions & 0 deletions src/models/resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export interface Resource<T> {
data: T[];
meta: {
current_page: number;
last_page: number;
from: number;
to: number;
per_page: number;
total: number;
};
}

const getTo = (total: number, page: number, perPage: number) => {
if (page === 1) {
return total < perPage ? total : perPage
}

return (page - 1) * perPage + perPage
}

const getLastPage = (total: number, perPage: number) => {
if (total <= 1) {
return 1
}

return Math.ceil(total / perPage)
}

export const newResource = <T>(
data: T[],
total: number,
page: number,
perPage: number,
): Resource<T> => ({
data,
meta: {
current_page: page,
last_page: getLastPage(total, perPage),
from: page === 1 ? 1 : (page - 1) * perPage + 1,
to: getTo(total, page, perPage),
per_page: perPage,
total,
},
})
54 changes: 0 additions & 54 deletions src/pages/_app.tsx

This file was deleted.

39 changes: 0 additions & 39 deletions src/pages/_document.tsx

This file was deleted.

42 changes: 0 additions & 42 deletions src/pages/about.tsx

This file was deleted.

15 changes: 0 additions & 15 deletions src/pages/account/address.tsx

This file was deleted.

15 changes: 0 additions & 15 deletions src/pages/account/order.tsx

This file was deleted.

15 changes: 0 additions & 15 deletions src/pages/account/profile.tsx

This file was deleted.

21 changes: 0 additions & 21 deletions src/pages/api/auth/[...nextauth].ts

This file was deleted.

Loading

0 comments on commit 0c5e8a9

Please sign in to comment.