-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
168 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Post": { | ||
"title": "string", | ||
"views": "int", | ||
"published": "boolean", | ||
"createdAt": "date" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,21 @@ | ||
import React, { FC } from 'react' | ||
import AppRouter from '@AppRouter' | ||
import { SWRConfig as SWRConfigProvider } from 'swr' | ||
import { get } from '@data' | ||
|
||
const App: FC = () => ( | ||
<div className="app-body"> | ||
<AppRouter /> | ||
</div> | ||
<SWRConfigProvider | ||
// https://swr.vercel.app/docs/global-configuration | ||
value={{ | ||
fetcher: get, | ||
shouldRetryOnError: false, | ||
revalidateOnFocus: false, | ||
}} | ||
> | ||
<div className="app-body"> | ||
<AppRouter /> | ||
</div> | ||
</SWRConfigProvider> | ||
) | ||
|
||
export default App |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import React, { FC } from 'react' | ||
|
||
export const Post: FC = () => <div>Hello, World!</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Post } from './Post' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './Home' | ||
export * from './Landing' | ||
export { FooterButtonGroup } from './FooterButtonGroup' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React, { FC } from 'react' | ||
import { usePost } from '@data' | ||
|
||
export const Post: FC = () => { | ||
const { data: post, loading, error } = usePost(1) | ||
|
||
return ( | ||
<div className="h-screen flex flex-col items-center justify-center"> | ||
{loading && <div>Loading...</div>} | ||
{/* usually error could be handle in axios like show a message, but feel free if you want to add error display when request error */} | ||
{error && <div>Error</div>} | ||
<div>{post?.title}</div> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Post as PostOrganisms } from './Post' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { Landing } from './Landing' | ||
export { Landing as LandingOrganisms } from './Landing' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './Home' | ||
export * from './Landing' |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import axios from 'axios' | ||
|
||
export const fetcher = axios.create({ | ||
// baseURL: 'https://mockend.com/wwwenjie/react-starter/', | ||
baseURL: 'https://jsonplaceholder.typicode.com/', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-type': 'application/json', | ||
}, | ||
}) | ||
|
||
const onRequestError = async (error: any) => { | ||
// logic when request error | ||
} | ||
|
||
const onResponseError = async (error: any) => { | ||
if (error?.response?.status) { | ||
await onResponseStatus(error.response.status) | ||
} | ||
|
||
// logic when response error | ||
} | ||
|
||
const onResponseStatus = async (status: number) => { | ||
const statusMapper = new Map<number, () => void>() | ||
.set(401, () => { | ||
// redirect to login or anything you want | ||
}) | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
.set(404, () => {}) | ||
|
||
const func = statusMapper.get(status) | ||
if (func) { | ||
await func() | ||
} | ||
} | ||
|
||
fetcher.interceptors.request.use( | ||
(config) => config, | ||
async (error) => { | ||
await onRequestError(error) | ||
return Promise.reject(error) | ||
}, | ||
) | ||
|
||
fetcher.interceptors.response.use( | ||
async (response) => response.data, | ||
async (error) => { | ||
await onResponseError(error) | ||
return Promise.reject(error) | ||
}, | ||
) | ||
|
||
export const { get, post, patch, delete: deleteFetch } = fetcher |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import useSWR from 'swr' | ||
|
||
export type Post = { | ||
readonly title: string | ||
readonly views: number | ||
readonly published: boolean | ||
readonly createdAt: Date | ||
} | ||
|
||
export const usePosts = () => { | ||
const { data, error } = useSWR<readonly Post[]>('posts') | ||
|
||
return { | ||
data, | ||
loading: !error && !data, | ||
error, | ||
} | ||
} | ||
|
||
export const usePost = (postId: number) => { | ||
const { data, error } = useSWR<Post>(`posts/${postId}`, { | ||
dedupingInterval: 600, | ||
}) | ||
|
||
return { | ||
data, | ||
loading: !error && !data, | ||
error, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './Featcher' | ||
export * from './Post' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import React, { FC } from 'react' | ||
import { PostOrganisms } from '@components/organisms' | ||
|
||
export const Post: FC = () => <PostOrganisms /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Post } from './Post' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import React, { FC } from 'react' | ||
import { Landing as LandingOrganisms } from '@components/organisms' | ||
import { LandingOrganisms } from '@components/organisms' | ||
|
||
export const Landing: FC = () => <LandingOrganisms /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './Home' | ||
export * from './Landing' | ||
export * from './NotFound' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters