From 5c9d0d611917097f6fe122d174ae7fad5583c17e Mon Sep 17 00:00:00 2001 From: Jack Date: Wed, 25 Jul 2018 11:54:17 +0300 Subject: [PATCH] Fix incorrect edit comment countdown timer calculation #74 --- web/app/common/fetcher.js | 10 +++++++++- web/app/components/comment/comment.jsx | 3 ++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/web/app/common/fetcher.js b/web/app/common/fetcher.js index 39285155..a19f8430 100644 --- a/web/app/common/fetcher.js +++ b/web/app/common/fetcher.js @@ -2,6 +2,7 @@ import axios from 'axios'; import { BASE_URL, API_BASE } from './constants'; import { siteId } from './settings'; +import store from './store'; const fetcher = {}; const methods = ['get', 'post', 'put', 'patch', 'delete', 'head']; @@ -35,7 +36,14 @@ methods.forEach(method => { } axios(parameters) - .then(res => resolve(res.data)) + .then(res => { + const date = ('date' in res.headers && res.headers.date) || ''; + const timestamp = isNaN(Date.parse(date)) ? 0 : Date.parse(date); + const timeDiff = (new Date() - timestamp) / 1000; + store.set('serverClientTimeDiff', timeDiff); + + resolve(res.data); + }) .catch(error => reject(error)); }); }; diff --git a/web/app/components/comment/comment.jsx b/web/app/components/comment/comment.jsx index 74b139d5..db7b1576 100644 --- a/web/app/components/comment/comment.jsx +++ b/web/app/components/comment/comment.jsx @@ -83,7 +83,8 @@ export default class Comment extends Component { if (userId === commentUserId) { const editDuration = store.get('config') && store.get('config').edit_duration; - const getEditTimeLeft = () => Math.floor(editDuration - (new Date() - new Date(data.time)) / 1000); + const timeDiff = store.get('serverClientTimeDiff') || 0; + const getEditTimeLeft = () => Math.floor(editDuration - ((new Date() - new Date(data.time)) / 1000 - timeDiff)); if (getEditTimeLeft() > 0) { this.editTimerInterval = setInterval(() => {