generated from halo-dev/plugin-starter
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fetch comments with related replies
Signed-off-by: Ryan Wang <[email protected]>
- Loading branch information
Showing
9 changed files
with
199 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,5 +41,5 @@ build { | |
} | ||
|
||
halo { | ||
version = "2.13.0" | ||
version = "comment" | ||
} |
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
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { CommentVo, ReplyVo } from '@halo-dev/api-client'; | ||
import { CommentVo, ReplyVo, ReplyVoList } from '@halo-dev/api-client'; | ||
import { LitElement, css, html } from 'lit'; | ||
import { property, state } from 'lit/decorators.js'; | ||
import { repeat } from 'lit/directives/repeat.js'; | ||
import { consume } from '@lit/context'; | ||
import { baseUrlContext, toastContext } from './context'; | ||
import { baseUrlContext, toastContext, withRepliesContext } from './context'; | ||
import './reply-item'; | ||
import './loading-block'; | ||
import './reply-form'; | ||
|
@@ -16,12 +16,25 @@ export class CommentReplies extends LitElement { | |
@property({ attribute: false }) | ||
baseUrl = ''; | ||
|
||
@consume({ context: withRepliesContext, subscribe: true }) | ||
@state() | ||
withReplies = false; | ||
|
||
@property({ type: Object }) | ||
comment: CommentVo | undefined; | ||
|
||
@property({ type: Boolean }) | ||
showReplyForm = false; | ||
|
||
@state() | ||
replies: ReplyVo[] = []; | ||
|
||
@state() | ||
page = 1; | ||
|
||
@state() | ||
hasNext = false; | ||
|
||
@state() | ||
loading = false; | ||
|
||
|
@@ -34,10 +47,8 @@ export class CommentReplies extends LitElement { | |
|
||
override render() { | ||
return html`<div class="replies__wrapper"> | ||
<reply-form @reload=${this.fetchReplies} .comment=${this.comment}></reply-form> | ||
${this.loading | ||
? html`<loading-block></loading-block>` | ||
: html` | ||
${this.replies.length | ||
? html` | ||
<div class="replies__list"> | ||
${repeat( | ||
this.replies, | ||
|
@@ -53,55 +64,115 @@ export class CommentReplies extends LitElement { | |
></reply-item>` | ||
)} | ||
</div> | ||
`} | ||
` | ||
: ''} | ||
${this.loading ? html`<loading-block></loading-block>` : ''} | ||
${this.hasNext && !this.loading | ||
? html`<div class="replies__next-wrapper"> | ||
<button @click=${this.fetchNext}>加载更多</button> | ||
</div>` | ||
: ''} | ||
</div>`; | ||
} | ||
|
||
onSetActiveQuoteReply(event: CustomEvent) { | ||
this.activeQuoteReply = event.detail.quoteReply; | ||
} | ||
|
||
async fetchReplies() { | ||
if (this.replies.length === 0) { | ||
async fetchReplies(options?: { append: boolean }) { | ||
try { | ||
this.loading = true; | ||
} | ||
|
||
try { | ||
// Reload replies list | ||
if (!options?.append) { | ||
this.page = 1; | ||
} | ||
|
||
const queryParams = [`page=${this.page || 0}`, `size=10`]; | ||
|
||
const response = await fetch( | ||
`${this.baseUrl}/apis/api.halo.run/v1alpha1/comments/${this.comment?.metadata.name}/reply` | ||
`${this.baseUrl}/apis/api.halo.run/v1alpha1/comments/${this.comment?.metadata.name}/reply?${queryParams.join('&')}` | ||
); | ||
|
||
if (!response.ok) { | ||
throw new Error('加载回复列表失败,请稍后重试'); | ||
} | ||
|
||
const data = await response.json(); | ||
this.replies = data.items; | ||
const data = (await response.json()) as ReplyVoList; | ||
|
||
if (options?.append) { | ||
this.replies = this.replies.concat(data.items); | ||
} else { | ||
this.replies = data.items; | ||
} | ||
|
||
this.hasNext = data.hasNext; | ||
this.page = data.page; | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
this.toastManager?.error(error.message); | ||
} | ||
} finally { | ||
this.loading = false; | ||
} | ||
} | ||
|
||
this.loading = false; | ||
async fetchNext() { | ||
this.page++; | ||
this.fetchReplies({ append: true }); | ||
} | ||
|
||
override connectedCallback(): void { | ||
super.connectedCallback(); | ||
this.fetchReplies(); | ||
|
||
if (this.withReplies) { | ||
// TODO: Fix ts error | ||
// Needs @halo-dev/[email protected] | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
this.replies = this.comment?.replies.items; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
this.page = this.comment?.replies.page; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
this.hasNext = this.comment?.replies.hasNext; | ||
} else { | ||
this.fetchReplies(); | ||
} | ||
} | ||
|
||
static override styles = [ | ||
varStyles, | ||
baseStyles, | ||
css` | ||
.replies__wrapper { | ||
margin-top: 0.5em; | ||
} | ||
.replies__list { | ||
margin-top: 0.875em; | ||
} | ||
.replies__next-wrapper { | ||
display: flex; | ||
justify-content: center; | ||
margin: 0.5em 0; | ||
} | ||
.replies__next-wrapper button { | ||
border-radius: var(--base-border-radius); | ||
color: var(--base-color); | ||
font-size: 0.875em; | ||
display: inline-flex; | ||
align-items: center; | ||
font-weight: 600; | ||
padding: 0.4em 0.875em; | ||
transition-property: all; | ||
transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1); | ||
transition-duration: 0.15s; | ||
border: 1px solid transparent; | ||
} | ||
.replies__next-wrapper button:hover { | ||
background-color: var(--component-pagination-button-bg-color-hover); | ||
} | ||
`, | ||
]; | ||
} | ||
|
Oops, something went wrong.