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
120 lines (116 loc) · 3.94 KB
/
App.tsx
File metadata and controls
120 lines (116 loc) · 3.94 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { useMachine } from '@xstate/react';
import React, { useEffect } from 'react';
import { fetchPeople, fetchPlanets } from './api';
import './App.css';
import { fetchMachine } from './machines/fetch';
import { matchingMachine } from './machines/matching';
export interface Person {
name: string;
homeworld: string;
}
function App() {
const [matchingState, sendToMatchingMachine] = useMachine(matchingMachine, {
guards: {
isCorrect: ctx => {
return ctx.topSelectedItem.homeworld === ctx.bottomSelectedItem.url;
}
}
});
const [fetchPeopleState, sendToPeopleMachine] = useMachine(fetchMachine, {
services: {
fetchData: () => fetchPeople().then(r => r.results)
}
});
const [fetchPlanetState, sendToPlanetMachine] = useMachine(fetchMachine, {
services: {
fetchData: () => fetchPlanets().then(r => r.results)
}
});
useEffect(() => {
sendToPeopleMachine({ type: 'FETCH' });
sendToPlanetMachine({ type: 'FETCH' });
}, [sendToPeopleMachine, sendToPlanetMachine]);
return (
<div className="App">
{matchingState.matches('answering') ? (
<>
<button onClick={() => sendToPeopleMachine({ type: 'FETCH' })}>
Fetch
</button>
{fetchPeopleState.matches('pending') ? <p>Loading</p> : null}
{fetchPeopleState.matches('successful.withData') ? (
<ul>
{fetchPeopleState.context.results &&
fetchPeopleState.context.results.map((person, index) => (
<li key={index}>
<button
style={{
backgroundColor:
matchingState.context.topSelectedItem === person
? 'lightblue'
: ''
}}
onClick={() =>
sendToMatchingMachine({
type: 'SELECT_TOP',
selectedItem: person
})
}
>
{person.name}
</button>
</li>
))}
</ul>
) : null}
{fetchPeopleState.matches('successful.withoutData') ? (
<p>No data available</p>
) : null}
{fetchPeopleState.matches('failed') ? (
<p>{fetchPeopleState.context.message}</p>
) : null}
<hr />
<button onClick={() => sendToPlanetMachine({ type: 'FETCH' })}>
Fetch
</button>
{fetchPlanetState.matches('pending') ? <p>Loading</p> : null}
{fetchPlanetState.matches('successful') ? (
<ul>
{fetchPlanetState.context.results &&
fetchPlanetState.context.results.map((planet, index) => (
<li key={index}>
<button
style={{
backgroundColor:
matchingState.context.bottomSelectedItem === planet
? 'lightblue'
: ''
}}
onClick={() =>
sendToMatchingMachine({
type: 'SELECT_BOTTOM',
selectedItem: planet
})
}
>
{planet.name}
</button>
</li>
))}
</ul>
) : null}
{fetchPlanetState.matches('failed') ? (
<p>{fetchPlanetState.context.message}</p>
) : null}{' '}
</>
) : null}
{matchingState.matches('submitted.correct') ? (
<p>The Force is strong with this one.</p>
) : null}
{matchingState.matches('submitted.incorrect') ? (
<p>Do or do not. There is no try.</p>
) : null}
</div>
);
}
export default App;