forked from isaacplmann/sturdy-uis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
93 lines (87 loc) · 2.6 KB
/
Copy pathApp.tsx
File metadata and controls
93 lines (87 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { useMachine } from '@xstate/react';
import React from 'react';
import { fetchPeople, mockFetch, fetchPlanets } from './api';
import './App.css';
import { fetchMachine } from './machines/fetch';
export interface Person {
name: string;
homeworld: string;
}
function App() {
const [peopleMachine, sendToPeopleMachine] = useMachine(fetchMachine, {
actions: {
fetchData: () => {
fetchPeople()
.then(r => r.results)
.then(
results => {
sendToPeopleMachine({ type: 'RESOLVE', results });
},
message => {
sendToPeopleMachine({ type: 'REJECT', message });
}
);
}
}
});
const [planetMachine, sendToPlanetMachine] = useMachine(fetchMachine, {
actions: {
fetchData: () => {
fetchPlanets()
.then(r => r.results)
.then(
results => {
sendToPlanetMachine({ type: 'RESOLVE', results });
},
message => {
sendToPlanetMachine({ type: 'REJECT', message });
}
);
}
}
});
return (
<div className="App">
<button onClick={() => sendToPeopleMachine({ type: 'FETCH' })}>
Fetch People
</button>
{peopleMachine.matches('idle') ? <p>Idle</p> : null}
{peopleMachine.matches('pending') ? <p>Loading</p> : null}
{peopleMachine.matches('fulfilled.withData') ? (
<ul>
{peopleMachine.context.results &&
peopleMachine.context.results.map((person, index) => (
<li key={index}>{person.name}</li>
))}
</ul>
) : null}
{peopleMachine.matches('fulfilled.withoutData') ? (
<p>No results</p>
) : null}
{peopleMachine.matches('rejected') ? (
<p>{peopleMachine.context.message}</p>
) : null}
<hr></hr>
<button onClick={() => sendToPlanetMachine({ type: 'FETCH' })}>
Fetch Planets
</button>
{planetMachine.matches('idle') ? <p>Idle</p> : null}
{planetMachine.matches('pending') ? <p>Loading</p> : null}
{planetMachine.matches('fulfilled.withData') ? (
<ul>
{planetMachine.context.results &&
planetMachine.context.results.map((planet, index) => (
<li key={index}>{planet.name}</li>
))}
</ul>
) : null}
{planetMachine.matches('fulfilled.withoutData') ? (
<p>No results</p>
) : null}
{planetMachine.matches('rejected') ? (
<p>{planetMachine.context.message}</p>
) : null}
</div>
);
}
export default App;