Skip to content

Commit

Permalink
fix(client): article list count show error fix #157
Browse files Browse the repository at this point in the history
  • Loading branch information
lizheming committed Mar 8, 2021
1 parent 209e86c commit 7a429ac
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 46 deletions.
24 changes: 23 additions & 1 deletion packages/client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,29 @@ export default function Waline({

//visitor count
if(visitor) {
path ? Visitor.add({serverURL, path}) : Visitor.show({serverURL});
const addPromise = Visitor.post({serverURL, path});

const els = document.querySelectorAll('.leancloud_visitors,.leancloud-visitors');
const countEls = [].filter.call(els, el => el.getAttribute('id'));
const ids = [].map.call(countEls, el => {
let id = el.getAttribute('id');
try {
id = decodeURI(id);
} catch(e) {
//ignore error
}
return id;
});
const restIds = ids.filter(id => id !== path);

if(!restIds.length) {
addPromise.then(count => Visitor.render(count, countEls));
} else {
const hasPath = restIds.length !== ids.length;
(hasPath ? addPromise : Promise.resolve())
.then(_ => Visitor.get({serverURL, path: ids}))
.then(counts => Visitor.render(counts, countEls));
}
}

//comment count
Expand Down
61 changes: 16 additions & 45 deletions packages/client/src/utils/visitor.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,30 @@
import { fetchVisitCount, postVisitCount } from "./fetch";

export default {
add({serverURL, path, countEl = '.leancloud_visitors,.leancloud-visitors'}) {
post({serverURL, path}) {
if(!serverURL || !path) {
return;
return null;
}

return postVisitCount({serverURL, path}).then(count => {
const countEls = [].filter.call(
document.querySelectorAll(countEl),
el => el.getAttribute('id')
);
if(!countEls.length) {
return;
}

countEls.forEach(el => {
let counterEl = el.querySelector('.leancloud-visitors-count');
if(!counterEl) {
counterEl = el;
}
counterEl.innerText = count;
});
});
return postVisitCount({serverURL, path});
},
show({serverURL, countEl = '.leancloud_visitors,.leancloud-visitors'}) {
const countEls = [].filter.call(
document.querySelectorAll(countEl),
el => el.getAttribute('id')
);
if(!countEls.length) {
get({serverURL, path}) {
if(!path.length) {
return;
}

const ids = [].map.call(countEls, el => {
let id = el.getAttribute('id');
try {
id = decodeURI(id);
} catch(e) {
//ignore error
}
return id;
});

fetchVisitCount({serverURL, path: ids.join()}).then(counts => {
if(!Array.isArray(counts)) {
counts = [counts];
return fetchVisitCount({serverURL, path: path.join()});
},
render(counts, countEls) {
if(!Array.isArray(counts)) {
counts = new Array(countEls.length).fill(counts);
}
countEls.forEach((el, idx) => {
let counterEl = el.querySelector('.leancloud-visitors-count');
if(!counterEl) {
counterEl = el;
}
countEls.forEach((el, idx) => {
let counterEl = el.querySelector('.leancloud-visitors-count');
if(!counterEl) {
counterEl = el;
}
counterEl.innerText = counts[idx];
});
counterEl.innerText = counts[idx];
});
}
}

0 comments on commit 7a429ac

Please sign in to comment.