fix interface suspense reativity
Browse files- src/Home/index.tsx +11 -12
src/Home/index.tsx
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
import { createSignal,
|
2 |
import './style.css'
|
3 |
|
4 |
|
@@ -9,34 +9,33 @@ interface ITranslation {
|
|
9 |
}
|
10 |
|
11 |
export default function App() {
|
12 |
-
const [
|
13 |
-
const [ translation,
|
14 |
-
const placeholder_text = () => isPending()? 'Translating...' : 'Translation'
|
15 |
let textarea: HTMLTextAreaElement | undefined
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
if (
|
|
|
20 |
const response = await fetch(`/api/translate?text=${text}`, { method: "POST" }).then(response => {
|
21 |
if (response.status === 200) { return response.json() }
|
22 |
|
23 |
}).then(response => response?.[0])
|
24 |
.catch(() => null)
|
25 |
|
26 |
-
|
27 |
}
|
28 |
|
|
|
29 |
return (
|
30 |
<main class="w-screen my-24 mx-5">
|
31 |
<div class="w-1/2 py-4 px-2 flex justify-end">
|
32 |
-
<button onclick={() =>
|
33 |
-
start(() => fetchData(textarea?.value as string))
|
34 |
-
} }>Translate</button>
|
35 |
</div>
|
36 |
<section class="flex">
|
37 |
{/* amber-500 */}
|
38 |
<textarea class="h-64 p-2 w-1/2 bg-zinc-700" placeholder="Insert input text..." ref={textarea} />
|
39 |
-
<textarea class="h-64 p-7 w-1/2 text-2xl" placeholder={
|
40 |
</section>
|
41 |
</main>
|
42 |
)
|
|
|
1 |
+
import { createSignal, createResource } from 'solid-js'
|
2 |
import './style.css'
|
3 |
|
4 |
|
|
|
9 |
}
|
10 |
|
11 |
export default function App() {
|
12 |
+
const [ text, setText ] = createSignal<string | null | undefined>(null)
|
13 |
+
const [ translation, { mutate } ] = createResource(text, fetchData)
|
|
|
14 |
let textarea: HTMLTextAreaElement | undefined
|
15 |
|
16 |
+
|
17 |
+
async function fetchData(text: string): Promise<ITranslation | null> {
|
18 |
+
if (!text) { return null }
|
19 |
+
if (translation()) { mutate(null) }
|
20 |
const response = await fetch(`/api/translate?text=${text}`, { method: "POST" }).then(response => {
|
21 |
if (response.status === 200) { return response.json() }
|
22 |
|
23 |
}).then(response => response?.[0])
|
24 |
.catch(() => null)
|
25 |
|
26 |
+
return { originalText: text, translatedText: response }
|
27 |
}
|
28 |
|
29 |
+
|
30 |
return (
|
31 |
<main class="w-screen my-24 mx-5">
|
32 |
<div class="w-1/2 py-4 px-2 flex justify-end">
|
33 |
+
<button onclick={() => setText(textarea?.value)}>Translate</button>
|
|
|
|
|
34 |
</div>
|
35 |
<section class="flex">
|
36 |
{/* amber-500 */}
|
37 |
<textarea class="h-64 p-2 w-1/2 bg-zinc-700" placeholder="Insert input text..." ref={textarea} />
|
38 |
+
<textarea class="h-64 p-7 w-1/2 text-2xl" placeholder={translation.loading? "Translating..." : "Translation"} readonly value={translation()?.translatedText ?? ''} />
|
39 |
</section>
|
40 |
</main>
|
41 |
)
|