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
90 lines (85 loc) · 2.78 KB
/
App.tsx
File metadata and controls
90 lines (85 loc) · 2.78 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
import React, { useState } from 'react';
import { fetchPeople, fetchPlanets } from './api';
import './App.css';
import List, { SideOfTheForce } from './list/List';
import { matchingMachine } from './machines/matching';
import { useMachine } from '@xstate/react';
export interface Person {
name: string;
homeworld: string;
}
function App() {
const [machine, send] = useMachine(matchingMachine, {
guards: {
isCorrect: (ctx, event) =>
ctx.leftSelectedItem.homeworld === ctx.rightSelectedItem.url
}
});
const [mode, setMode] = useState<SideOfTheForce>(SideOfTheForce.Light);
return (
<div className="App">
{machine.matches('answering.notReadyToSubmit') ? (
<>
<List
fetchData={() => fetchPeople({ maxDelay: 200, errorRate: 0 })}
selectedItem={machine.context.leftSelectedItem}
sideOfTheForce={mode}
onSelection={selectedItem => {
send({ type: 'selectLeft', selectedItem });
}}
></List>
<hr></hr>
<List
fetchData={() => fetchPlanets({ maxDelay: 200, errorRate: 0 })}
selectedItem={machine.context.rightSelectedItem}
sideOfTheForce={mode}
onSelection={selectedItem => {
send({ type: 'selectRight', selectedItem });
}}
></List>
<button
onClick={() =>
setMode(
mode === SideOfTheForce.Light
? SideOfTheForce.Dark
: SideOfTheForce.Light
)
}
>
{mode === SideOfTheForce.Light
? 'Come to the Dark'
: 'Return to the Light'}{' '}
Side
</button>
<button onClick={() => send({ type: 'continue' })}>Continue</button>
</>
) : null}
{machine.matches('answering.readyToSubmit') ? (
<>
<label>Chosen items:</label>
<p>
{machine.context.leftSelectedItem &&
machine.context.leftSelectedItem.name}{' '}
and
{machine.context.rightSelectedItem &&
machine.context.rightSelectedItem.name}
</p>
<button onClick={() => send({ type: 'changeAnswers' })}>
Change Answers
</button>
<button onClick={() => send({ type: 'submit' })}>Submit</button>
</>
) : null}
{machine.matches('submitted') ? (
<button onClick={() => send({ type: 'reset' })}>Reset</button>
) : null}
{machine.matches('submitted.correct') ? (
<p>The force is strong with this one.</p>
) : null}
{machine.matches('submitted.incorrect') ? (
<p>Do, or do not. There is no try.</p>
) : null}
</div>
);
}
export default App;