Skip to content

Commit

Permalink
chore(nuxt3): use vueuse functions
Browse files Browse the repository at this point in the history
  • Loading branch information
LarchLiu committed Jan 26, 2024
1 parent 9688fe9 commit 0756cc2
Showing 1 changed file with 37 additions and 28 deletions.
65 changes: 37 additions & 28 deletions server/nuxt3/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import type { StorageData } from '@stargram/core/storage'

const cardWidth = 350
const cardHeight = 246
type LoadMoreStatus = 'idle' | 'loading' | 'no-more'
type LoadMoreStatus = 'idle' | 'loading' | 'no-more' | 'error'

const runtimeConfig = useRuntimeConfig()
const _userId = useLocalStorage('userId', '')
const pageSize = ref(10)
const { width: windowWidth, height: windowHeight } = useWindowSize()
const pageSize = computed(() => {
return Math.floor(windowWidth.value / cardWidth) * 10
})
const page = ref<string | number | undefined>()
const dataList = ref<StorageData[]>([])
const list = ref<HTMLDivElement>()
Expand All @@ -26,23 +29,28 @@ async function getDataList() {
userId: _userId.value,
pageSize: pageSize.value,
}
const data = await $fetch<{
data: StorageData[]
nextPage: string | number | undefined
}>('/api/data-list', {
method: 'POST',
body,
})
if (data?.nextPage) {
page.value = data.nextPage
loadMoreStatus.value = 'idle'
try {
const data = await $fetch<{
data: StorageData[]
nextPage: string | number | undefined
}>('/api/data-list', {
method: 'POST',
body,
})
if (data?.nextPage) {
page.value = data.nextPage
loadMoreStatus.value = 'idle'
}
else {
page.value = undefined
loadMoreStatus.value = 'no-more'
}
if (data?.data)
dataList.value = dataList.value.concat(data.data)
}
else {
page.value = undefined
loadMoreStatus.value = 'no-more'
catch (error) {
loadMoreStatus.value = 'error'
}
if (data?.data)
dataList.value = dataList.value.concat(data.data)
}
function urlBase64ToUint8Array(base64String: string) {
const padding = '='.repeat((4 - base64String.length % 4) % 4)
Expand Down Expand Up @@ -171,18 +179,14 @@ const displayButton = computed(() => {
return !Notification || (Notification.permission !== 'granted' && _userId.value)
})

useEventListener('scroll', async (evt) => {

Check warning on line 182 in server/nuxt3/pages/index.vue

View workflow job for this annotation

GitHub Actions / Lint

'evt' is defined but never used. Allowed unused args must match /^_/u
if (loadMoreStatus.value === 'idle' && list.value && list.value.getBoundingClientRect().bottom < (windowHeight.value + cardHeight))
await getDataList()
})

onMounted(async () => {
pageSize.value = Math.floor(window.innerWidth / cardWidth) * 10
if (_userId.value)
await getDataList()

window.addEventListener('scroll', async (evt) => {
if (loadMoreStatus.value === 'idle' && list.value && list.value.getBoundingClientRect().bottom < (window.innerHeight + cardHeight))
await getDataList()
})
window.addEventListener('resize', (event) => {
pageSize.value = Math.floor(window.innerWidth / cardWidth) * 10
})
})
</script>

Expand All @@ -201,7 +205,7 @@ onMounted(async () => {
<div ref="list" flex flex-col items-center justify-center>
<div my-4 flex flex-wrap justify-center gap-4 rounded lt-sm:flex-col class="lt-sm:w-4/5">
<div v-for="item in dataList" :key="item.url">
<div border="1px solid #636161" class="lt-sm:w-full!" h-246px w-350px rounded>
<div border="1px solid #636161" class="lt-sm:w-full!" :class="pageSize > 10 ? 'h-246px' : ''" w-350px rounded>
<div h-180px w-full flex justify-center>
<img :src="item.meta.ogImage" h-full w-full rounded-t-3px object-cover>
</div>
Expand All @@ -216,9 +220,14 @@ onMounted(async () => {
<div v-if="loadMoreStatus === 'loading'" mb-4>
<div uno-eos-icons-bubble-loading />
</div>
<div v-if="loadMoreStatus === 'no-more'" mb-4>
<div v-else-if="loadMoreStatus === 'no-more'" mb-4>
No more data
</div>
<div v-else-if="loadMoreStatus === 'error'">
<button btn @click="getDataList">
Reload
</button>
</div>
</div>
</div>
</ClientOnly>
Expand Down

1 comment on commit 0756cc2

@vercel
Copy link

@vercel vercel bot commented on 0756cc2 Jan 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

star-nexus – ./

star-nexus-git-main-larchliu.vercel.app
star-nexus-larchliu.vercel.app
star-nexus.vercel.app

Please sign in to comment.