Skip to content

Commit

Permalink
fix(ui/pv): missing pageTitle field in the PV request
Browse files Browse the repository at this point in the history
  • Loading branch information
qwqcode committed Feb 3, 2024
1 parent 22d4ec1 commit 559df60
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
1 change: 0 additions & 1 deletion ui/artalk/src/artalk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ export default class Artalk {
Stat.initCountWidget({
getApi: () => new Api(convertApiOptions(conf)),
siteName: conf.site,
pageKey: conf.pageKey,
countEl: conf.countEl,
pvEl: conf.pvEl,
pvAdd: false
Expand Down
15 changes: 9 additions & 6 deletions ui/artalk/src/plugins/stat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export interface CountOptions {
getApi(): Api

siteName: string
pageKey: string
pageKey?: string
pageTitle?: string
countEl: string
pvEl: string

Expand All @@ -15,12 +16,13 @@ export interface CountOptions {

export const PvCountWidget: ArtalkPlugin = (ctx: ContextApi) => {
ctx.watchConf([
'site', 'pageKey', 'countEl', 'pvEl',
'site', 'pageKey', 'pageTitle', 'countEl', 'pvEl',
], (conf) => {
initCountWidget({
getApi: () => ctx.getApi(),
siteName: conf.site,
pageKey: conf.pageKey,
pageTitle: conf.pageTitle,
countEl: conf.countEl,
pvEl: conf.pvEl,
pvAdd: (typeof ctx.conf.pvAdd === 'boolean' ? ctx.conf.pvAdd : true),
Expand All @@ -36,10 +38,10 @@ export async function initCountWidget(opt: CountOptions) {
}

// PV
const initialData = opt.pvAdd ? {
const initialData = (opt.pvAdd && opt.pageKey) ? {
[opt.pageKey]: (await opt.getApi().pages.logPv({
page_key: opt.pageKey,
page_title: opt.pageKey,
page_title: opt.pageTitle,
site_name: opt.siteName,
})).data.pv // pv+1 and get pv count
} : undefined
Expand Down Expand Up @@ -68,7 +70,7 @@ async function refreshStatCount(
// Get page keys which will be queried
let queryPageKeys = Array.from(document.querySelectorAll(args.numEl))
.map((e) => e.getAttribute('data-page-key') || opt.pageKey)
.filter((k) => typeof data[k] !== 'number') // filter out keys that already have data
.filter((k) => k && typeof data[k] !== 'number') // filter out keys that already have data

queryPageKeys = [...new Set(queryPageKeys)] // deduplicate

Expand All @@ -81,7 +83,8 @@ async function refreshStatCount(
data = { ...data, ...res }
}

applyCountData(args.numEl, data, data[opt.pageKey])
const defaultCount = opt.pageKey ? data[opt.pageKey] : 0
applyCountData(args.numEl, data, defaultCount)
}

function applyCountData(selector: string, data: CountData, defaultCount: number) {
Expand Down

0 comments on commit 559df60

Please sign in to comment.