|
1 | 1 | import { useState, useEffect } from 'react';
|
2 | 2 | import { useOracle } from './hooks/use-oracle';
|
3 |
| -import { useBlockchain } from './hooks/use-blockchain'; |
| 3 | +import { Input } from './components/ui/input'; |
| 4 | + |
| 5 | +import { Tabs, TabsContent, TabsList, TabsTrigger } from './components/ui/tabs'; |
| 6 | +import { Button } from './components/ui/button'; |
| 7 | +import { Loader2, Send, Terminal } from 'lucide-react'; |
| 8 | +import { toast } from 'sonner'; |
| 9 | +import { Separator } from './components/ui/separator'; |
| 10 | + |
| 11 | +import { Alert, AlertDescription, AlertTitle } from './components/ui/alert'; |
4 | 12 |
|
5 | 13 | const AppPage = () => {
|
6 |
| - const { askOracle, subscribeOracle } = useOracle(); |
7 |
| - const { getData } = useBlockchain(); |
8 |
| - const [question, setQuestion] = useState<string>(''); |
9 |
| - const [callbackAddress, setCallbackAddress] = useState<string>(''); |
| 14 | + const { askOracle, getStringResponse, getNumericResponse } = useOracle(); |
| 15 | + const [stringQuestion, setStringQuestion] = useState<string>(''); |
| 16 | + const [numericQuestion, setNumericQuestion] = useState<string>(''); |
| 17 | + const [stringResponse, setStringResponse] = useState<string | null>(null); |
| 18 | + const [numericResponse, setNumericResponse] = useState<number | null>(null); |
| 19 | + const [loading, setLoading] = useState<boolean>(false); |
10 | 20 |
|
11 |
| - const handleSubmit = async () => { |
| 21 | + const submitStringQuestion = async () => { |
| 22 | + setLoading(true); |
| 23 | + if (!stringQuestion) { |
| 24 | + toast.error('請輸入問題'); |
| 25 | + setLoading(false); |
| 26 | + return; |
| 27 | + } |
12 | 28 | try {
|
13 | 29 | await askOracle({
|
14 | 30 | dataType: 'String',
|
15 |
| - question: question, |
16 |
| - callBackAddress: callbackAddress, |
| 31 | + question: stringQuestion, |
| 32 | + }); |
| 33 | + toast.success('已將問題提交給 Oracle'); |
| 34 | + setStringQuestion(''); |
| 35 | + } catch (error) { |
| 36 | + console.error('oracle error:', error); |
| 37 | + toast.error('Oracle 發生錯誤'); |
| 38 | + } |
| 39 | + setLoading(false); |
| 40 | + }; |
| 41 | + |
| 42 | + const submitNumericQuestion = async () => { |
| 43 | + setLoading(true); |
| 44 | + if (!numericQuestion) { |
| 45 | + toast.error('請輸入問題'); |
| 46 | + setLoading(false); |
| 47 | + return; |
| 48 | + } |
| 49 | + try { |
| 50 | + await askOracle({ |
| 51 | + dataType: 'Numeric', |
| 52 | + question: numericQuestion, |
17 | 53 | });
|
18 |
| - console.log('Ask oracle success!'); |
| 54 | + toast.success('已將問題提交給 Oracle'); |
| 55 | + setNumericQuestion(''); |
19 | 56 | } catch (error) {
|
20 | 57 | console.error('oracle error:', error);
|
| 58 | + toast.error('Oracle 發生錯誤'); |
21 | 59 | }
|
| 60 | + setLoading(false); |
22 | 61 | };
|
23 | 62 |
|
24 | 63 | useEffect(() => {
|
25 |
| - subscribeOracle(); |
26 |
| - const fetchData = async () => { |
27 |
| - const res = await getData(); |
28 |
| - console.log(res); |
| 64 | + const fetchOracleResponse = async () => { |
| 65 | + const stringResponse = await getStringResponse(); |
| 66 | + const numericResponse = await getNumericResponse(); |
| 67 | + setStringResponse(stringResponse); |
| 68 | + setNumericResponse(numericResponse); |
29 | 69 | };
|
30 |
| - fetchData(); |
| 70 | + fetchOracleResponse(); |
31 | 71 | }, []);
|
32 | 72 |
|
33 | 73 | return (
|
34 |
| - <div className="bg-slate-50 h-screen w-screen items-center justify-center flex"> |
35 |
| - <div className="gap-6 flex flex-col w-1/2"> |
36 |
| - <h1 className="text-3xl font-semibold text-slate-900">Sample DApp</h1> |
37 |
| - <div> |
38 |
| - <input |
39 |
| - className="h-12 w-full rounded-lg border-slate-300 border px-4" |
40 |
| - placeholder="請輸入問題" |
41 |
| - value={question} |
42 |
| - onChange={(e) => setQuestion(e.target.value)} |
43 |
| - /> |
44 |
| - <input |
45 |
| - className="h-12 w-full rounded-lg border-slate-300 border px-4" |
46 |
| - placeholder="請輸入回調地址" |
47 |
| - value={callbackAddress} |
48 |
| - onChange={(e) => setCallbackAddress(e.target.value)} |
49 |
| - /> |
50 |
| - </div> |
51 |
| - <div> |
52 |
| - <button |
53 |
| - className="border-slate-300 border rounded-lg px-3 py-2" |
54 |
| - onClick={handleSubmit} |
55 |
| - > |
56 |
| - 送出 |
57 |
| - </button> |
58 |
| - </div> |
59 |
| - <div> |
60 |
| - <p>Oracle 回應:</p> |
61 |
| - </div> |
| 74 | + <div className="container"> |
| 75 | + <h1 className="text-2xl font-semibold mt-12">Sample DApp</h1> |
| 76 | + <Tabs defaultValue="string" className="mt-6"> |
| 77 | + <TabsList> |
| 78 | + <TabsTrigger value="string">字串問題</TabsTrigger> |
| 79 | + <TabsTrigger value="numeric">數字問題</TabsTrigger> |
| 80 | + </TabsList> |
| 81 | + <TabsContent value="string"> |
| 82 | + <h2 className="mt-4 text-lg">請輸入字串問題</h2> |
| 83 | + <div className="flex mt-2 space-x-2"> |
| 84 | + <Input |
| 85 | + placeholder="請輸入問題" |
| 86 | + value={stringQuestion} |
| 87 | + onChange={(e) => setStringQuestion(e.target.value)} |
| 88 | + /> |
| 89 | + <Button |
| 90 | + className="flex-shrink-0" |
| 91 | + onClick={submitStringQuestion} |
| 92 | + disabled={loading} |
| 93 | + > |
| 94 | + {loading ? ( |
| 95 | + <Loader2 className="animate-spin h-4 w-4 mr-2" /> |
| 96 | + ) : ( |
| 97 | + <Send className="h-4 w-4 mr-2" /> |
| 98 | + )} |
| 99 | + {loading ? '送出中' : '送出'} |
| 100 | + </Button> |
| 101 | + </div> |
| 102 | + </TabsContent> |
| 103 | + <TabsContent value="numeric"> |
| 104 | + <h2 className="mt-4 text-lg">請輸入數字問題</h2> |
| 105 | + <div className="flex mt-2 space-x-2"> |
| 106 | + <Input |
| 107 | + placeholder="請輸入問題" |
| 108 | + value={numericQuestion} |
| 109 | + onChange={(e) => setNumericQuestion(e.target.value)} |
| 110 | + /> |
| 111 | + <Button |
| 112 | + className="flex-shrink-0" |
| 113 | + onClick={submitNumericQuestion} |
| 114 | + disabled={loading} |
| 115 | + > |
| 116 | + {loading ? ( |
| 117 | + <Loader2 className="animate-spin h-4 w-4 mr-2" /> |
| 118 | + ) : ( |
| 119 | + <Send className="h-4 w-4 mr-2" /> |
| 120 | + )} |
| 121 | + {loading ? '送出中' : '送出'} |
| 122 | + </Button> |
| 123 | + </div> |
| 124 | + </TabsContent> |
| 125 | + </Tabs> |
| 126 | + <Separator className="my-8" /> |
| 127 | + <div className="flex space-x-3"> |
| 128 | + <Alert> |
| 129 | + <Terminal className="h-4 w-4" /> |
| 130 | + <AlertTitle>Oracle 字串回應</AlertTitle> |
| 131 | + <AlertDescription> |
| 132 | + {stringResponse ?? '等待 Oracle 回應中'} |
| 133 | + </AlertDescription> |
| 134 | + </Alert> |
| 135 | + <Alert> |
| 136 | + <Terminal className="h-4 w-4" /> |
| 137 | + <AlertTitle>Oracle 數字回應</AlertTitle> |
| 138 | + <AlertDescription> |
| 139 | + {numericResponse ?? '等待 Oracle 回應中'} |
| 140 | + </AlertDescription> |
| 141 | + </Alert> |
62 | 142 | </div>
|
63 | 143 | </div>
|
64 | 144 | );
|
|
0 commit comments