chore: Remove momentjs, use date-fns (#1233)

This commit is contained in:
Pranav Raj S 2020-09-14 15:14:26 +05:30 committed by GitHub
parent f3a357d832
commit 1312d07aa2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 41 additions and 45 deletions

View file

@ -1,21 +1,17 @@
/* eslint no-console: 0 */
/* eslint no-undef: "error" */
/* eslint no-unused-expressions: ["error", { "allowShortCircuit": true }] */
import moment from 'moment';
import fromUnixTime from 'date-fns/fromUnixTime';
import format from 'date-fns/format';
import formatDistanceToNow from 'date-fns/formatDistanceToNow';
export default {
methods: {
messageStamp(time) {
const createdAt = time * 1000;
return moment(createdAt).format('h:mm A');
},
wootTime(time) {
const createdAt = time * 1000;
return moment(createdAt);
const unixTime = fromUnixTime(time);
return format(unixTime, 'h:mm a');
},
dynamicTime(time) {
const createdAt = moment(time * 1000);
return createdAt.fromNow();
const unixTime = fromUnixTime(time);
return formatDistanceToNow(unixTime, { addSuffix: true });
},
},
};

View file

@ -41,7 +41,11 @@
<script>
import { mapGetters } from 'vuex';
import moment from 'moment';
import startOfDay from 'date-fns/startOfDay';
import subDays from 'date-fns/subDays';
import getUnixTime from 'date-fns/getUnixTime';
import fromUnixTime from 'date-fns/fromUnixTime';
import format from 'date-fns/format';
export default {
data() {
@ -57,15 +61,12 @@ export default {
accountReport: 'getAccountReports',
}),
to() {
const m = moment.utc();
m.set({ hour: 23, minute: 59, second: 59, millisecond: 999 });
return m.unix();
return getUnixTime(startOfDay(new Date()));
},
from() {
const diff = this.currentDateRangeSelection.id ? 29 : 6;
const m = moment.utc().subtract(diff, 'days');
m.set({ hour: 0, minute: 0, second: 0, millisecond: 0 });
return m.unix();
const fromDate = subDays(new Date(), diff);
return getUnixTime(startOfDay(fromDate));
},
collection() {
if (this.accountReport.isFetching) {
@ -73,7 +74,7 @@ export default {
}
if (!this.accountReport.data.length) return {};
const labels = this.accountReport.data.map(element =>
moment.unix(element.timestamp).format('DD/MMM')
format(fromUnixTime(element.timestamp), 'dd/MMM')
);
const data = this.accountReport.data.map(element => element.value);
return {

View file

@ -1,7 +1,8 @@
/* eslint no-console: 0 */
/* eslint no-param-reassign: 0 */
/* eslint no-shadow: 0 */
import moment from 'moment';
import compareAsc from 'date-fns/compareAsc';
import fromUnixTime from 'date-fns/fromUnixTime';
import * as types from '../mutation-types';
import Report from '../../api/reports';
@ -41,7 +42,9 @@ const actions = {
reportObj.to
).then(accountReport => {
let { data } = accountReport;
data = data.filter(el => moment() > moment.unix(el.timestamp));
data = data.filter(
el => compareAsc(new Date(), fromUnixTime(el.timestamp)) > -1
);
if (
reportObj.metric === 'avg_first_response_time' ||
reportObj.metric === 'avg_resolution_time'

View file

@ -1,5 +1,6 @@
/* eslint no-param-reassign: 0 */
import moment from 'moment';
import fromUnixTime from 'date-fns/fromUnixTime';
import differenceInDays from 'date-fns/differenceInDays';
import Cookies from 'js-cookie';
import { frontendURL } from '../../helper/URLHelper';
@ -12,15 +13,16 @@ export const setLoadingStatus = (state, status) => {
export const setUser = (userData, expiryDate) =>
Cookies.set('user', userData, {
expires: expiryDate.diff(moment(), 'days'),
expires: differenceInDays(expiryDate, new Date()),
});
export const getHeaderExpiry = response => moment.unix(response.headers.expiry);
export const getHeaderExpiry = response =>
fromUnixTime(response.headers.expiry);
export const setAuthCredentials = response => {
const expiryDate = getHeaderExpiry(response);
Cookies.set('auth_data', response.headers, {
expires: expiryDate.diff(moment(), 'days'),
expires: differenceInDays(expiryDate, new Date()),
});
setUser(response.data.data, expiryDate);
};

View file

@ -1,13 +1,7 @@
import moment from 'moment';
import fromUnixTime from 'date-fns/fromUnixTime';
import format from 'date-fns/format';
class DateHelper {
constructor(date) {
this.date = moment(date * 1000);
}
format(dateFormat = 'MMM DD, YYYY') {
return this.date.format(dateFormat);
}
}
export default DateHelper;
export const formatUnixDate = (date, dateFormat = 'MMM dd, yyyy') => {
const unixDate = fromUnixTime(date);
return format(unixDate, dateFormat);
};

View file

@ -9,7 +9,7 @@ import {
} from 'widget/api/conversation';
import { MESSAGE_TYPE } from 'widget/helpers/constants';
import { playNotificationAudio } from 'shared/helpers/AudioNotificationHelper';
import DateHelper from 'shared/helpers/DateHelper';
import { formatUnixDate } from 'shared/helpers/DateHelper';
import { isASubmittedFormMessage } from 'shared/helpers/MessageTypeHelper';
import getUuid from '../../helpers/uuid';
@ -98,7 +98,7 @@ export const getters = {
getGroupedConversation: _state => {
const conversationGroupedByDate = groupBy(
Object.values(_state.conversations),
message => new DateHelper(message.created_at).format()
message => formatUnixDate(message.created_at)
);
return Object.keys(conversationGroupedByDate).map(date => ({
date,

View file

@ -21,13 +21,13 @@
"chart.js": "~2.5.0",
"copy-text-to-clipboard": "^2.1.1",
"core-js": "3",
"date-fns": "^2.16.1",
"dotenv": "^8.0.0",
"foundation-sites": "~6.5.3",
"highlight.js": "^9.15.10",
"ionicons": "~2.0.1",
"js-cookie": "^2.2.1",
"lodash.groupby": "^4.6.0",
"moment": "~2.19.3",
"query-string": "5",
"spinkit": "~1.2.5",
"tween.js": "~16.6.0",

View file

@ -3497,6 +3497,11 @@ date-fns@^1.27.2:
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-1.30.1.tgz#2e71bf0b119153dbb4cc4e88d9ea5acfb50dc05c"
integrity sha512-hBSVCvSmWC+QypYObzwGOd9wqdDpOt+0wl0KbU+R+uuZBS1jN8VsD1ss3irQDknRj5NvxiTF6oj/nDRnN/UQNw==
date-fns@^2.16.1:
version "2.16.1"
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.16.1.tgz#05775792c3f3331da812af253e1a935851d3834b"
integrity sha512-sAJVKx/FqrLYHAQeN7VpJrPhagZc9R4ImZIWYRFZaaohR3KzmuK88touwsSwSVT8Qcbd4zoDsnGfX4GFB4imyQ==
de-indent@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/de-indent/-/de-indent-1.0.2.tgz#b2038e846dc33baa5796128d0804b455b8c1e21d"
@ -7171,11 +7176,6 @@ moment@2.26.0, moment@^2.10.6:
resolved "https://registry.yarnpkg.com/moment/-/moment-2.26.0.tgz#5e1f82c6bafca6e83e808b30c8705eed0dcbd39a"
integrity sha512-oIixUO+OamkUkwjhAVE18rAMfRJNsNe/Stid/gwHSOfHrOtw9EhAY2AHvdKZ/k/MggcYELFCJz/Sn2pL8b8JMw==
moment@~2.19.3:
version "2.19.4"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.19.4.tgz#17e5e2c6ead8819c8ecfad83a0acccb312e94682"
integrity sha512-1xFTAknSLfc47DIxHDUbnJWC+UwgWxATmymaxIPQpmMh7LBm7ZbwVEsuushqwL2GYZU0jie4xO+TK44hJPjNSQ==
move-concurrently@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/move-concurrently/-/move-concurrently-1.0.1.tgz#be2c005fda32e0b29af1f05d7c4b33214c701f92"