-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from DenisCodeB/testing
FM: update
- Loading branch information
Showing
14 changed files
with
853 additions
and
809 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,9 @@ | ||
export const fetchRandomExploreStuff = async <T>(url: string) => { | ||
const res = await fetch(url, { next: { revalidate: false } }) | ||
|
||
if (!res.ok) { | ||
throw new Error('An error occurred while fetching the data.') | ||
} | ||
|
||
return res.json() | ||
} |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,50 @@ | ||
import { useCallback } from 'react' | ||
import useSWRImmutable from 'swr/immutable' | ||
import { fetchRandomExploreStuff } from '@/api/fetchRandomExploreStuff' | ||
|
||
const INITIAL_QUOTE = { | ||
id: 1, | ||
quote: 'Mathematics is the music of reason.', | ||
author: 'James Joseph Sylvester', | ||
} | ||
|
||
const INITIAL_FACT = { | ||
id: 1, | ||
fact: 'The hour and minute hand of a clock coincide 22 times in a day.', | ||
} | ||
|
||
export const useApiDataFetch = <T>(url: string): T => { | ||
let { | ||
data = {} as T, | ||
isLoading, | ||
error, | ||
} = useSWRImmutable(url, fetchRandomExploreStuff<T>) | ||
|
||
const toEachRoute = useCallback( | ||
(obj1: any, obj2: any) => { | ||
if (/\/quote/.test(url)) data = obj1 | ||
else if (/\/fact/.test(url)) data = obj2 | ||
}, | ||
[isLoading, error] | ||
) | ||
|
||
if (isLoading) { | ||
toEachRoute( | ||
{ | ||
id: 0, | ||
quote: 'Loading', | ||
author: 'Loading', | ||
}, | ||
{ | ||
id: 0, | ||
fact: 'Loading', | ||
} | ||
) | ||
} | ||
|
||
if (error) { | ||
toEachRoute(INITIAL_QUOTE, INITIAL_FACT) | ||
} | ||
|
||
return data | ||
} |
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
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,5 @@ | ||
export const getRandomIntInclusive = (min: number, max: number) => { | ||
min = Math.ceil(min) | ||
max = Math.floor(max) | ||
return Math.floor(Math.random() * (max - min + 1) + min) | ||
} |
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,5 +1,10 @@ | ||
const ROOT_URL = process.env.NEXT_PUBLIC_ROOT_URL | ||
|
||
export const HOME_ROUTE = '/' | ||
export const ABOUT_ROUTE = '/about' | ||
export const CONTACTS_ROUTE = '/contacts' | ||
export const PLAYGROUND_ROUTE = '/playground' | ||
export const NOT_FOUND_ROUTE = '*' | ||
|
||
export const QUOTE_API = '/api/quote' | ||
export const FACT_API = '/api/fact' |