forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessTheNumber.tsx
More file actions
78 lines (74 loc) · 2.04 KB
/
Copy pathGuessTheNumber.tsx
File metadata and controls
78 lines (74 loc) · 2.04 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
import { useState } from "react";
import { Button, Code, Input, Text } from "@stellar/design-system";
import { useWallet } from "../hooks/useWallet";
import game from "../contracts/guess_the_number";
import { Box } from "../components/layout/Box";
export const GuessTheNumber = () => {
const [guessedIt, setGuessedIt] = useState<boolean>();
const [theGuess, setTheGuess] = useState<number>();
const { address } = useWallet();
if (!address) {
return (
<Text as="p" size="md">
Connect wallet to play the guessing game
</Text>
);
}
const submitGuess = async () => {
if (!theGuess || !address) return;
const { result } = await game.guess({
a_number: BigInt(theGuess),
guesser: address,
});
if (result.isErr()) {
console.error(result.unwrapErr());
} else {
setGuessedIt(result.unwrap());
}
};
return (
<form
onSubmit={(e) => {
e.preventDefault();
void submitGuess();
}}
>
{guessedIt ? (
<>
<Text as="p" size="lg">
You got it!
</Text>
<Text as="p" size="lg">
Set a new number by calling <Code size="md">reset</Code> from the
CLI as the admin.
</Text>
</>
) : (
<Box gap="sm" direction="row" align="end" justify="end" wrap="wrap">
<Input
label="Guess a number from 1 to 10!p"
id="guess"
fieldSize="lg"
error={guessedIt === false && "Wrong! Guess again."}
onChange={(e) => {
setGuessedIt(undefined);
setTheGuess(Number(e.target.value));
}}
/>
<Button
type="submit"
disabled={!theGuess}
style={{ marginTop: 8 }}
variant="primary"
size="md"
>
Submit Guess
</Button>
</Box>
)}
<Text as="p" size="lg">
{/* Not sure the SDS way to add consistent spacing at the end */}
</Text>
</form>
);
};