Skip to content

Commit 2227d8b

Browse files
author
Cassio Zen
committed
Added full state as param to guard + send as param to effect
1 parent 721f25c commit 2227d8b

17 files changed

+16073
-22
lines changed

example/index.html

Lines changed: 0 additions & 14 deletions
This file was deleted.
File renamed without changes.

examples/fetch/index.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
* {
2+
box-sizing: border-box;
3+
}
4+
5+
body {
6+
background-color: #1d1f23;
7+
color: #ededed;
8+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans',
9+
'Droid Sans', 'Helvetica Neue', sans-serif;
10+
font-size: 24px;
11+
-webkit-font-smoothing: antialiased;
12+
-moz-osx-font-smoothing: grayscale;
13+
}

examples/fetch/index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
8+
<title>Fetch with Retry</title>
9+
</head>
10+
11+
<body>
12+
<div id="root"></div>
13+
<script src="./index.tsx"></script>
14+
</body>
15+
16+
</html>

examples/fetch/index.tsx

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import * as React from 'react';
2+
import * as ReactDOM from 'react-dom';
3+
import './index.css';
4+
import useStateMachine from '../../dist';
5+
6+
type Coffee = {
7+
id: number;
8+
title: string;
9+
description: string;
10+
ingredients: string[];
11+
};
12+
13+
function App() {
14+
const [machine, send] = useStateMachine<{ retryCount: number; data?: Coffee[]; error?: string }>({ retryCount: 0 })({
15+
initial: 'loading',
16+
states: {
17+
loading: {
18+
on: {
19+
SUCCESS: 'loaded',
20+
FAILURE: 'error',
21+
},
22+
effect(send, update) {
23+
const fetchCoffees = async () => {
24+
let response: Response;
25+
try {
26+
response = await fetch('https://api.sampleapis.com/coffee/hot');
27+
if (!response.ok) {
28+
throw new Error(`An error has occured: ${response.status}`);
29+
}
30+
const coffees = await response.json();
31+
update(context => ({ data: coffees, ...context }));
32+
send('SUCCESS');
33+
} catch (error) {
34+
update(context => ({ error: error.message, ...context }));
35+
send('FAILURE');
36+
}
37+
};
38+
fetchCoffees();
39+
},
40+
},
41+
loaded: {},
42+
error: {
43+
on: {
44+
RETRY: {
45+
target: 'loading',
46+
guard: state => state.context.retryCount < 3,
47+
},
48+
},
49+
effect(send, update) {
50+
update(context => ({ ...context, retryCount: context.retryCount + 1 }));
51+
send('RETRY');
52+
},
53+
},
54+
},
55+
});
56+
57+
return (
58+
<div className="coffees">
59+
{machine.value === 'loading' && <p>Loading</p>}
60+
{machine.value === 'error' && <p>{machine.context.error}</p>}
61+
{machine.value === 'loaded' && (
62+
<ul>
63+
{machine.context.data?.map(coffee => (
64+
<li key={coffee.id}>{coffee.title}</li>
65+
))}
66+
</ul>
67+
)}
68+
</div>
69+
);
70+
}
71+
72+
ReactDOM.render(<App />, document.getElementById('root'));

0 commit comments

Comments
 (0)