Skip to content

Commit

Permalink
feat: 简化了一些代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Plumbiu committed Feb 5, 2023
1 parent 17a7fa0 commit ee95c79
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html"
},
"x-generation-date": "2023-02-04T15:12:10.808Z"
"x-generation-date": "2023-02-05T09:35:18.646Z"
},
"x-strapi-config": {
"path": "/documentation",
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/ArticlesList/Item/Ads.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defineProps({
<template>
<li>
<ArticlesListItemLink :to="url">
<div class="flex-1">
<div class="overflow-hidden flex-1">
<ArticlesListItemBarTop
:name="author"
duration="2天前"
Expand Down
12 changes: 6 additions & 6 deletions frontend/components/ArticlesList/Item/AuthorInfo/index.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<script setup lang="ts">
defineProps({
name: String,
motto: String,
avatar: String,
rank: Number,
})
defineProps<{
name: string
motto: string
avatar: string
rank: number
}>()
</script>

<template>
Expand Down
10 changes: 5 additions & 5 deletions frontend/components/ArticlesList/Item/Bar/Bottom.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<script setup lang="ts">
defineProps({
viewed: Number,
liked: Number,
commented: Number,
})
defineProps<{
viewed: number
liked: number
commented: number
}>()
const format = (num: number) => {
return num > 10000 ? `${(num / 10000).toFixed(1)}w` : num
}
Expand Down
8 changes: 4 additions & 4 deletions frontend/components/ArticlesList/Item/Bar/Center.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<script setup lang="ts">
defineProps({
title: String,
summary: String,
})
defineProps<{
title: string
summary: string
}>()
</script>

<template>
Expand Down
6 changes: 3 additions & 3 deletions frontend/components/ArticlesList/Item/index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { ITagItem } from '~~/types/IArticleItem'
import type { ITagItem } from '~~/types/IArticleItem'
const { uid } = defineProps({
uid: String,
title: String,
Expand All @@ -16,7 +16,7 @@ const { uid } = defineProps({
type: Object,
required: true,
},
tags: Array,
tags: Array<ITagItem>,
index: Number,
})
const hideHandler = () => {
Expand All @@ -34,7 +34,7 @@ const hideHandler = () => {
<ArticlesListItemBarTop
:author-id="authorId"
:duration="formatTime(createdAt)"
:tags="tags as ITagItem[]"
:tags="tags"
/>
<ArticlesListItemBarCenter :title="title" :summary="summary" />
<ArticlesListItemBarBottom :viewed="viewed" :liked="liked" :commented="commented" />
Expand Down
76 changes: 26 additions & 50 deletions frontend/components/ArticlesList/index.vue
Original file line number Diff line number Diff line change
@@ -1,67 +1,43 @@
<script setup lang="ts">
const artlistData = useArtlist(await useFetchPostData())
const { data } = await useFetch('/api/global')
const articleList = useArtlist(await useFetchPostData())
const articleAds = (await useFetch('/api/global')).data.value.articleAds
const isLoading = useState('isLoading', () => true)
const route = useRoute()
let pagenum = 1
const isLoading = useState('isLoading', () => false)
const isEmpty = useState('isEmpty', () => false)
const articleAds = data.value.articleAds
const addArtListItem = async () => {
if (useScrollBottom()) {
const artlistVal = await useFetchPostData(route.path, route.query?.sort, pagenum++)
artlistData.value.push(...artlistVal)
}
}
const bottomHandler = useThrottle(addArtListItem)
const addArtListItem = useThrottle(async () => {
useScrollBottom() && articleList.value.push(...(await useFetchPostData(route.path, route.query?.sort, ++pagenum)))
})
watch(route, async () => {
pagenum = 1
isLoading.value = true
const artlistVal = await useFetchPostData(route.path, route.query?.sort)
if (!artlistVal?.length) {
isEmpty.value = true
return
}
artlistData.value = artlistVal
isLoading.value = false
isEmpty.value = false
articleList.value = []
articleList.value = await useFetchPostData(route.path, route.query?.sort, pagenum = 1)
}, { deep: true })
onBeforeMount(() => {
const EmployeeWindow = window as any
EmployeeWindow.addEventListener('scroll', bottomHandler)
onMounted(() => {
(window as any).addEventListener('scroll', addArtListItem)
isLoading.value = false
})
onUnmounted(() => {
const EmployeeWindow = window as any
EmployeeWindow.removeEventListener('scroll', bottomHandler)
(window as any).removeEventListener('scroll', addArtListItem)
})
</script>

<template>
<div class="articlelist">
<ArticlesListNavigation />
<ArticlesListUiSkeleton v-if="isEmpty || isLoading" />
<ArticlesListUiSkeleton v-if="isLoading || !articleList.length" />
<ul v-else>
<ArticlesListItemAds
:title="articleAds.title"
:author="articleAds.author"
:summary="articleAds.summary"
:cover="articleAds.cover"
:url="articleAds.url"
/>
<ArticlesListItem
v-for="item in artlistData"
:key="item.id"
:uid="item.id"
:title="item.title"
:viewed="item.viewed"
:liked="item.liked"
:commented="item.commented"
:summary="item.summary"
:cover="item.cover"
:created-at="item.createdAt"
:author-id="item.authorId"
:tags="item.tagIds.data"
/>
<ClientOnly>
<ArticlesListItemAds
:title="articleAds.title" :author="articleAds.author"
:summary="articleAds.summary" :cover="articleAds.cover" :url="articleAds.url"
/>
<ArticlesListItem
v-for="item in articleList"
:key="item.id" :uid="item.id" :title="item.title"
:viewed="item.viewed" :liked="item.liked" :commented="item.commented"
:summary="item.summary" :cover="item.cover" :created-at="item.createdAt"
:author-id="item.authorId" :tags="item.tagIds.data"
/>
</ClientOnly>
</ul>
</div>
</template>
Expand Down

0 comments on commit ee95c79

Please sign in to comment.