Skip to content

Commit c7abd0e

Browse files
committed
chore: add react-query in playground
1 parent c695002 commit c7abd0e

File tree

6 files changed

+162
-71
lines changed

6 files changed

+162
-71
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as React from 'react';
2+
import { QueryClient, QueryClientProvider } from 'react-query';
3+
4+
import { Wizard } from '../../../../dist';
5+
import { Footer } from '../../../components';
6+
import Section from '../../common/section';
7+
import LazyQueryStep from './lazyQuery';
8+
import QueryStep from './queryStep';
9+
10+
const queryClient = new QueryClient();
11+
12+
const ReactQuerySection: React.FC = () => {
13+
return (
14+
<QueryClientProvider client={queryClient}>
15+
<Section
16+
title="React query"
17+
description="integration with react-query"
18+
showDivider={false}
19+
>
20+
<Wizard footer={<Footer />}>
21+
<QueryStep number={1} />
22+
<LazyQueryStep number={2} />
23+
<QueryStep number={3} />
24+
<LazyQueryStep number={4} />
25+
<QueryStep number={5} />
26+
</Wizard>
27+
</Section>
28+
</QueryClientProvider>
29+
);
30+
};
31+
32+
export default ReactQuerySection;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { styled } from 'goober';
2+
import * as React from 'react';
3+
import { useMutation } from 'react-query';
4+
5+
import { useWizard } from '../../../../dist';
6+
7+
const Container = styled('div')`
8+
display: flex;
9+
justify-content: center;
10+
align-items: center;
11+
flex-direction: column;
12+
min-height: 35vh;
13+
`;
14+
15+
const P = styled('p')`
16+
color: var(--text);
17+
`;
18+
19+
type Props = {
20+
number: number;
21+
};
22+
23+
const LazyQueryStep: React.FC<Props> = ({ number }) => {
24+
const { handleStep, isLoading } = useWizard();
25+
const { error, data, mutateAsync } = useMutation('repDataMutation', () =>
26+
fetch('https://jsonplaceholder.typicode.com/todos').then((res) =>
27+
res.json(),
28+
),
29+
);
30+
31+
handleStep(() => {
32+
return mutateAsync();
33+
});
34+
35+
const content = React.useMemo(() => {
36+
if (error) return 'An error has occurred: ' + (error as Error).message;
37+
38+
return data
39+
? data.map((todo) => (
40+
<div key={todo.id}>
41+
<h1>{data.name}</h1>
42+
</div>
43+
))
44+
: !isLoading && <p>Click next button to make request</p>;
45+
}, [error, data, isLoading]);
46+
47+
return (
48+
<Container>
49+
<P>Step {number}</P>
50+
{content}
51+
{isLoading && <P>Loading...</P>}
52+
</Container>
53+
);
54+
};
55+
56+
export default LazyQueryStep;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { styled } from 'goober';
2+
import * as React from 'react';
3+
import { useQuery } from 'react-query';
4+
5+
const Container = styled('div')`
6+
display: flex;
7+
justify-content: center;
8+
align-items: center;
9+
flex-direction: column;
10+
min-height: 35vh;
11+
`;
12+
13+
const P = styled('p')`
14+
color: var(--text);
15+
`;
16+
17+
type Props = {
18+
number: number;
19+
};
20+
21+
const QueryStep: React.FC<Props> = ({ number }) => {
22+
const { isLoading, error, data } = useQuery('repoData', () =>
23+
fetch(`https://jsonplaceholder.typicode.com/todos/${number}`).then((res) =>
24+
res.json(),
25+
),
26+
);
27+
28+
const content = React.useMemo(() => {
29+
if (isLoading) return 'Loading...';
30+
31+
if (error) return 'An error has occurred: ' + (error as Error).message;
32+
33+
return (
34+
<div>
35+
<h1>
36+
todo {data.id}: {data.title}
37+
</h1>
38+
</div>
39+
);
40+
}, [isLoading, error, data]);
41+
42+
return (
43+
<Container>
44+
<P>Step {number}</P>
45+
{content}
46+
</Container>
47+
);
48+
};
49+
50+
export default QueryStep;

playground/modules/wizard/section.tsx

Lines changed: 0 additions & 69 deletions
This file was deleted.

playground/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"dependencies": {
1313
"framer-motion": "^3.1.1",
1414
"goober": "^2.0.19-next.1",
15-
"react-app-polyfill": "^2.0.0"
15+
"react-app-polyfill": "^2.0.0",
16+
"react-query": "^3.5.11"
1617
},
1718
"alias": {
1819
"react": "../node_modules/react",

playground/yarn.lock

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@
807807
"@babel/types" "^7.4.4"
808808
esutils "^2.0.2"
809809

810-
"@babel/runtime@^7.4.4", "@babel/runtime@^7.8.4":
810+
"@babel/runtime@^7.12.5", "@babel/runtime@^7.4.4", "@babel/runtime@^7.5.5", "@babel/runtime@^7.8.4":
811811
version "7.12.5"
812812
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.12.5.tgz#410e7e487441e1b360c29be715d870d9b985882e"
813813
integrity sha512-plcc+hbExy3McchJCEQG3knOsuh3HH+Prx1P6cLIkET/0dLuQDEnrT+s27Axgc9bqfsmNUNHfscgMUdBpC9xfg==
@@ -3199,6 +3199,14 @@ map-visit@^1.0.0:
31993199
dependencies:
32003200
object-visit "^1.0.0"
32013201

3202+
match-sorter@^6.0.2:
3203+
version "6.1.0"
3204+
resolved "https://registry.yarnpkg.com/match-sorter/-/match-sorter-6.1.0.tgz#7fec6808d94311a35fef7fd842a11634f2361bd7"
3205+
integrity sha512-sKPMf4kbF7Dm5Crx0bbfLpokK68PUJ/0STUIOPa1ZmTZEA3lCaPK3gapQR573oLmvdkTfGojzySkIwuq6Z6xRQ==
3206+
dependencies:
3207+
"@babel/runtime" "^7.12.5"
3208+
remove-accents "0.4.2"
3209+
32023210
md5.js@^1.3.4:
32033211
version "1.3.5"
32043212
resolved "https://registry.yarnpkg.com/md5.js/-/md5.js-1.3.5.tgz#b5d07b8e3216e3e27cd728d72f70d1e6a342005f"
@@ -4259,6 +4267,14 @@ react-app-polyfill@^2.0.0:
42594267
regenerator-runtime "^0.13.7"
42604268
whatwg-fetch "^3.4.1"
42614269

4270+
react-query@^3.5.11:
4271+
version "3.5.11"
4272+
resolved "https://registry.yarnpkg.com/react-query/-/react-query-3.5.11.tgz#7dbb21e3783437d612465ab938a854bbdec8d785"
4273+
integrity sha512-tn4IX68WnZ5bKVcmkltPf8B1ZO2Uh212bldobwSyyHUYTt8Z3cOqldBkigF2OmY8IZI77A9rKe6wV6s1YOk4vg==
4274+
dependencies:
4275+
"@babel/runtime" "^7.5.5"
4276+
match-sorter "^6.0.2"
4277+
42624278
readable-stream@^2.0.2, readable-stream@^2.2.2, readable-stream@^2.3.3, readable-stream@^2.3.6, readable-stream@~2.3.3, readable-stream@~2.3.6:
42634279
version "2.3.7"
42644280
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.7.tgz#1eca1cf711aef814c04f62252a36a62f6cb23b57"
@@ -4356,6 +4372,11 @@ relateurl@^0.2.7:
43564372
resolved "https://registry.yarnpkg.com/relateurl/-/relateurl-0.2.7.tgz#54dbf377e51440aca90a4cd274600d3ff2d888a9"
43574373
integrity sha1-VNvzd+UUQKypCkzSdGANP/LYiKk=
43584374

4375+
4376+
version "0.4.2"
4377+
resolved "https://registry.yarnpkg.com/remove-accents/-/remove-accents-0.4.2.tgz#0a43d3aaae1e80db919e07ae254b285d9e1c7bb5"
4378+
integrity sha1-CkPTqq4egNuRngeuJUsoXZ4ce7U=
4379+
43594380
remove-trailing-separator@^1.0.1:
43604381
version "1.1.0"
43614382
resolved "https://registry.yarnpkg.com/remove-trailing-separator/-/remove-trailing-separator-1.1.0.tgz#c24bce2a283adad5bc3f58e0d48249b92379d8ef"

0 commit comments

Comments
 (0)