playmak3r commited on
Commit
e4d8414
1 Parent(s): 10a5d6b

fix interface suspense reativity

Browse files
Files changed (1) hide show
  1. src/Home/index.tsx +11 -12
src/Home/index.tsx CHANGED
@@ -1,4 +1,4 @@
1
- import { createSignal, useTransition } from 'solid-js'
2
  import './style.css'
3
 
4
 
@@ -9,34 +9,33 @@ interface ITranslation {
9
  }
10
 
11
  export default function App() {
12
- const [ isPending, start ] = useTransition()
13
- const [ translation, setTranslation ] = createSignal<ITranslation | null>(null)
14
- const placeholder_text = () => isPending()? 'Translating...' : 'Translation'
15
  let textarea: HTMLTextAreaElement | undefined
16
 
17
- async function fetchData(text: string) {
18
- if (text === translation()?.translatedText || !text) { return }
19
- if (translation()) { setTranslation(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
- setTranslation({ originalText: text, translatedText: response })
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={placeholder_text()} readonly value={translation()?.translatedText ?? ''} />
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
  )