]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
chore: more work
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 20 Jul 2021 17:06:28 +0000 (19:06 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Tue, 20 Jul 2021 17:06:28 +0000 (19:06 +0200)
playground/src/stores/jokes-swrv.ts [new file with mode: 0644]
playground/src/views/Jokes.vue
playground/src/views/swrv.vue

diff --git a/playground/src/stores/jokes-swrv.ts b/playground/src/stores/jokes-swrv.ts
new file mode 100644 (file)
index 0000000..6f95b53
--- /dev/null
@@ -0,0 +1,27 @@
+import { ref, unref, watch } from 'vue'
+import { acceptHMRUpdate, defineStore } from '../../../src'
+import { getRandomJoke, Joke } from '../views/api/jokes'
+import useSWRV from 'swrv'
+
+export const useJokesSetup = defineStore('jokes-swrv-setup', () => {
+  // const current = ref<null | Joke>(null)
+  const history = ref<Joke[]>([])
+
+  const { data, error, isValidating, mutate, revalidate } = useSWRV(
+    'jokes',
+    getRandomJoke
+  )
+
+  watch(data, (joke) => {
+    if (joke) {
+      history.value.push(joke)
+    }
+  })
+
+  return { current: data, history, fetchJoke: revalidate }
+})
+
+if (import.meta.hot) {
+  // import.meta.hot.accept(acceptHMRUpdate(useJokes, import.meta.hot))
+  import.meta.hot.accept(acceptHMRUpdate(useJokesSetup, import.meta.hot))
+}
index 2554d5d735df641e81596c3b1b0dcbf4a3fa079b..5741f5b5a94722b33992463f19587d31eb74548b 100644 (file)
       </button>
 
       <div style="min-height: 9rem" v-if="jokes.current">
-
-          <blockquote :key="jokes.current.id">
-            <i>{{ jokes.current.setup }}</i>
-            <br />
-            <br />
-            <p class="appear" @animationend="state = 'ready'">
-              {{ jokes.current.punchline }}
-            </p>
-          </blockquote>
+        <blockquote :key="jokes.current.id">
+          <i>{{ jokes.current.setup }}</i>
+          <br />
+          <br />
+          <p class="appear" @animationend="state = 'ready'">
+            {{ jokes.current.punchline }}
+          </p>
+        </blockquote>
       </div>
     </section>
   </main>
@@ -60,8 +59,8 @@ onMounted(() => {
   console.log('mounted')
   // @ts-expect-error
   window.jo = jokes
-    console.log('new pending')
-    fetchRandomJoke()
+  console.log('new pending')
+  fetchRandomJoke()
 })
 </script>
 
index a63adb89e775e4743c375748fbc69cdddfe6fa89..ca976375a58d7003c23c7433db715d06071b210f 100644 (file)
@@ -1,3 +1,80 @@
 <template>
-  <h1></h1>
+  <h3>Pinia + <a href="https://github.com/posva/vue-promised">SWRV</a></h3>
+
+  <main>
+    <section>
+      <button
+        :disabled="state !== 'ready'"
+        @click="fetchRandomJoke"
+        style="margin-bottom: 4px"
+      >
+        {{ buttonText }}
+      </button>
+
+      <div style="min-height: 9rem" v-if="jokes.current">
+        <blockquote :key="jokes.current.id">
+          <i>{{ jokes.current.setup }}</i>
+          <br />
+          <br />
+          <p class="appear" @animationend="state = 'ready'">
+            {{ jokes.current.punchline }}
+          </p>
+        </blockquote>
+      </div>
+    </section>
+  </main>
+
+  <pre>{{ jokes.$state }}</pre>
 </template>
+
+<script lang="ts" setup>
+import { computed, onMounted, ref } from 'vue'
+import { useJokesSetup } from '../stores/jokes-swrv'
+
+// const jokes = useJokes()
+const jokes = useJokesSetup()
+
+const texts = {
+  loading: 'Fetching the joke...',
+  waiting: 'Wait for it...',
+  ready: 'Another one?',
+}
+
+const state = ref<'waiting' | 'loading' | 'ready'>('waiting')
+
+const buttonText = computed(() => texts[state.value])
+
+function fetchRandomJoke() {
+  state.value = 'loading'
+
+  jokes.fetchJoke().finally(() => {
+    state.value = 'waiting'
+    console.log('done fetching', jokes.current)
+  })
+}
+
+onMounted(() => {
+  console.log('mounted')
+  // @ts-expect-error
+  window.jo = jokes
+  console.log('new pending')
+  fetchRandomJoke()
+})
+</script>
+
+<style>
+@keyframes appear {
+  from {
+    opacity: 0;
+  }
+  to {
+    opacity: 1;
+  }
+}
+
+.appear {
+  opacity: 0;
+  animation: appear 1s ease-in-out 3s;
+  animation-fill-mode: forwards;
+}
+</style>