-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7e3b89d
commit 29ac53d
Showing
4 changed files
with
107 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<template lang="pug"> | ||
.artworks-by-user(ref='containerRef') | ||
NFlex(align='center' justify='center') | ||
NPagination( | ||
:item-count='artworkIds.length', | ||
:page-size='pageSize' | ||
v-model:page='curPage' | ||
) | ||
ArtworkList( | ||
:list='curArtworks', | ||
:loading='!curArtworks.length ? pageSize : false' | ||
) | ||
NFlex(align='center' justify='center') | ||
NPagination( | ||
:item-count='artworkIds.length', | ||
:page-size='pageSize' | ||
v-model:page='curPage' | ||
) | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { type ArtworkInfo } from '@/types' | ||
import { NPagination } from 'naive-ui' | ||
import {} from 'vue' | ||
const props = withDefaults( | ||
defineProps<{ | ||
userId: string | ||
workCategory?: 'illust' | 'manga' | ||
}>(), | ||
{ | ||
workCategory: 'illust', | ||
} | ||
) | ||
const containerRef = ref<HTMLElement>() | ||
const artworkIds = ref<string[]>([]) | ||
const pageSize = 24 | ||
const curPage = ref(1) | ||
const cachedArtworks = ref<Record<number, ArtworkInfo[]>>({}) | ||
const curArtworks = computed(() => { | ||
return cachedArtworks.value[curPage.value] || [] | ||
}) | ||
onMounted(async () => { | ||
firstInit() | ||
}) | ||
watch(curPage, (page) => { | ||
backToTop() | ||
fetchArtworksByPage(page) | ||
}) | ||
function backToTop() { | ||
const container = containerRef.value! | ||
const top = container.getBoundingClientRect().top + window.scrollY - 120 | ||
window.scrollTo({ | ||
top, | ||
behavior: 'smooth', | ||
}) | ||
} | ||
async function firstInit() { | ||
artworkIds.value = [] | ||
curPage.value = 1 | ||
cachedArtworks.value = {} | ||
artworkIds.value = await fetchAllArtworkIds() | ||
await fetchArtworksByPage(1) | ||
} | ||
async function fetchAllArtworkIds() { | ||
const { data } = await ajax.get<{ | ||
illusts: Record<string, null> | ||
manga: Record<string, null> | ||
}>(`/ajax/user/${props.userId}/profile/all`) | ||
return props.workCategory === 'illust' | ||
? Object.keys(data.illusts) | ||
: Object.keys(data.manga) | ||
} | ||
function getArtworkIdsByPage(page: number) { | ||
return artworkIds.value.slice((page - 1) * pageSize, page * pageSize) | ||
} | ||
async function fetchArtworksByPage(page: number) { | ||
if (cachedArtworks.value[page]) return cachedArtworks.value[page] | ||
const ids = getArtworkIdsByPage(page) | ||
const { data } = await ajax.get<{ | ||
works: Record<string, ArtworkInfo> | ||
}>(`/ajax/user/${props.userId}/profile/illusts`, { | ||
params: { | ||
ids, | ||
work_category: props.workCategory, | ||
is_first_page: 0, | ||
}, | ||
}) | ||
cachedArtworks.value[page] = Object.values(data.works) | ||
return data | ||
} | ||
</script> | ||
|
||
<style scoped lang="sass"></style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters