File tree 14 files changed +246
-31
lines changed
14 files changed +246
-31
lines changed Original file line number Diff line number Diff line change 12
12
"storybook:build" : " build-storybook"
13
13
},
14
14
"dependencies" : {
15
+ "@tailwindcss/forms" : " ^0.5.3" ,
16
+ "axios" : " ^0.27.2" ,
15
17
"clsx" : " ^1.2.1" ,
16
18
"next" : " 12.2.5" ,
19
+ "ramda" : " ^0.28.0" ,
17
20
"react" : " ^18.2.0" ,
18
- "react-dom" : " ^18.2.0"
21
+ "react-dom" : " ^18.2.0" ,
22
+ "react-query" : " ^3.39.2"
19
23
},
20
24
"devDependencies" : {
21
25
"@babel/core" : " ^7.18.13" ,
30
34
"@svgr/webpack" : " ^6.3.1" ,
31
35
"@types/jest" : " ^29.0.0" ,
32
36
"@types/node" : " ^18.7.14" ,
37
+ "@types/ramda" : " ^0.28.15" ,
33
38
"@types/react" : " ^18.0.18" ,
34
39
"@types/react-dom" : " ^18.0.6" ,
35
40
"autoprefixer" : " ^10.4.8" ,
Original file line number Diff line number Diff line change @@ -16,6 +16,4 @@ const Blank = ({ name }: Props): JSX.Element => {
16
16
) ;
17
17
} ;
18
18
19
- Blank . defaultProps = { } ;
20
-
21
19
export default Blank ;
Original file line number Diff line number Diff line change 1
- import React , { useState } from 'react' ;
1
+ import React , { useEffect , useState } from 'react' ;
2
2
3
3
import Icon from 'components/Icon' ;
4
4
import { add , subtract } from 'utils' ;
5
5
6
6
export type CounterProps = {
7
- count : number ;
7
+ initial : number ;
8
+ onChange : ( c : number ) => void ;
8
9
} ;
9
10
10
- const Counter = ( { count : defaultCount } : CounterProps ) : JSX . Element => {
11
- const [ count , setCount ] = useState ( defaultCount ) ;
11
+ const Counter = ( { initial, onChange } : CounterProps ) : JSX . Element => {
12
+ const [ count , setCount ] = useState ( initial ) ;
13
+
14
+ useEffect ( ( ) => {
15
+ onChange ( count ) ;
16
+ } , [ count , onChange ] ) ;
12
17
13
18
return (
14
- < div
15
- id = "counter"
16
- className = "flex items-center justify-center w-full h-screen"
17
- >
19
+ < div id = "counter" >
18
20
< button
19
21
type = "button"
20
22
onClick = { ( ) => setCount ( subtract ( count , 1 ) ) }
@@ -37,8 +39,4 @@ const Counter = ({ count: defaultCount }: CounterProps): JSX.Element => {
37
39
) ;
38
40
} ;
39
41
40
- Counter . defaultProps = {
41
- count : 0 ,
42
- } ;
43
-
44
42
export default Counter ;
Original file line number Diff line number Diff line change
1
+ .blank {
2
+ @apply text-orange-500;
3
+ }
Original file line number Diff line number Diff line change
1
+ import React from 'react' ;
2
+ import { ComponentMeta , ComponentStory } from '@storybook/react' ;
3
+
4
+ import Fetcher from './Fetcher' ;
5
+
6
+ export default {
7
+ title : 'Demo/Fetcher' ,
8
+ component : Fetcher ,
9
+ } as ComponentMeta < typeof Fetcher > ;
10
+
11
+ const Template : ComponentStory < typeof Fetcher > = args => < Fetcher { ...args } /> ;
12
+
13
+ export const Default = Template . bind ( { } ) ;
14
+ Default . args = {
15
+ name : 'John' ,
16
+ } ;
Original file line number Diff line number Diff line change
1
+ import React , { useState } from 'react' ;
2
+ import { isNil } from 'ramda' ;
3
+
4
+ import useAgify from 'hooks/useAgify' ;
5
+
6
+ const Fetcher = ( ) : JSX . Element => {
7
+ const [ name , setName ] = useState < string | null > ( null ) ;
8
+ const { data, isLoading } = useAgify ( name ) ;
9
+
10
+ console . log ( data ) ;
11
+
12
+ return (
13
+ < div >
14
+ { /* eslint-disable-next-line jsx-a11y/control-has-associated-label */ }
15
+ < input
16
+ type = "text"
17
+ name = "name"
18
+ className = "block w-full border-gray-300 rounded-md shadow-sm focus:border-blue-500 focus:ring-blue-500 sm:text-sm"
19
+ placeholder = "Your name..."
20
+ onChange = { e => setName ( e . target . value ) }
21
+ value = { name ?? '' }
22
+ />
23
+ { ! isLoading && ! isNil ( data ) && (
24
+ < p className = "mt-4" >
25
+ { name } , your age is < b > { data . age } </ b > 😇
26
+ </ p >
27
+ ) }
28
+ </ div >
29
+ ) ;
30
+ } ;
31
+
32
+ export default Fetcher ;
Original file line number Diff line number Diff line change
1
+ export { default } from './Fetcher' ;
Original file line number Diff line number Diff line change @@ -7,13 +7,11 @@ type Props = {
7
7
} ;
8
8
9
9
const Layout = ( { children } : Props ) : JSX . Element => (
10
- // eslint-disable-next-line react-hooks/rules-of-hooks
11
10
< div >
12
11
< Icons />
13
12
{ children }
14
13
</ div >
15
14
) ;
16
- Layout . defaultProps = { } ;
17
15
18
16
// For Storybook
19
17
export const LayoutDecorator = ( Story : FunctionComponent ) : JSX . Element => (
Original file line number Diff line number Diff line change
1
+ import { useQuery , UseQueryResult } from 'react-query' ;
2
+ import axios from 'axios' ;
3
+ import { isNil } from 'ramda' ;
4
+
5
+ import AgifyResponse from '../types/agify-response' ;
6
+
7
+ const getAgify = async ( name : string | null ) : Promise < AgifyResponse > => {
8
+ // This axios method could be extract in a service to handle cancel tokens
9
+ const { data } = await axios ( {
10
+ url : `https://api.agify.io/?name=${ name } ` ,
11
+ method : 'GET' ,
12
+ } ) ;
13
+
14
+ return data ;
15
+ } ;
16
+
17
+ const useAgify = ( name : string | null ) : UseQueryResult < AgifyResponse , Error > =>
18
+ useQuery ( `agify_${ name } ` , ( ) => getAgify ( name ) , {
19
+ enabled : ! isNil ( name ) ,
20
+ } ) ;
21
+
22
+ export default useAgify ;
Original file line number Diff line number Diff line change 1
1
import React from 'react' ;
2
+ import { QueryClient , QueryClientProvider } from 'react-query' ;
2
3
import type { AppProps } from 'next/app' ;
3
4
4
5
import 'styles/globals.css' ;
5
6
7
+ const queryClient = new QueryClient ( ) ;
8
+
6
9
const MyApp = ( { Component, pageProps } : AppProps ) : JSX . Element => (
7
- < Component { ...pageProps } />
10
+ < QueryClientProvider client = { queryClient } >
11
+ < Component { ...pageProps } />
12
+ </ QueryClientProvider >
8
13
) ;
9
14
10
15
export default MyApp ;
Original file line number Diff line number Diff line change 1
1
import React from 'react' ;
2
2
3
- import Blank from 'components/Blank' ;
4
3
import Counter from 'components/Counter' ;
4
+ import Fetcher from 'components/Fetcher' ;
5
5
import Layout from 'components/Layout' ;
6
6
7
7
const Home = ( ) : JSX . Element => (
8
8
< Layout >
9
- < Counter />
10
- < Blank name = "john" />
9
+ < div className = "flex flex-col items-center justify-center w-full h-screen" >
10
+ < div >
11
+ < h2 className = "my-4 text-xl font-bold" > Counter example:</ h2 >
12
+ < Counter initial = { 0 } onChange = { c => console . log ( c ) } />
13
+ < h2 className = "my-4 text-xl font-bold" > Fetch example:</ h2 >
14
+ < Fetcher />
15
+ </ div >
16
+ </ div >
11
17
</ Layout >
12
18
) ;
13
19
Original file line number Diff line number Diff line change
1
+ type AgifyResponse = {
2
+ name : string ;
3
+ age : number ;
4
+ count : number ;
5
+ } ;
6
+
7
+ export default AgifyResponse ;
Original file line number Diff line number Diff line change @@ -6,5 +6,7 @@ module.exports = {
6
6
theme : {
7
7
extend : { } ,
8
8
} ,
9
- plugins : [ ] ,
9
+ plugins : [
10
+ require ( '@tailwindcss/forms' ) ,
11
+ ] ,
10
12
}
You can’t perform that action at this time.
0 commit comments