Skip to content

Commit

Permalink
FIx unix() is not a function
Browse files Browse the repository at this point in the history
  • Loading branch information
RomaricMourgues committed Dec 20, 2022
1 parent 856fd49 commit 30f8bc8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
10 changes: 4 additions & 6 deletions twake/frontend/src/app/components/drive/ui/file.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import FileType from './file-type';
import TagPicker from 'components/tag-picker/tag-picker';
import { addApiUrlIfNeeded } from 'app/features/global/utils/URLUtils';
import '../drive.scss';
import { formatTime } from '@features/global/utils/Numbers';

export default class File extends React.Component {
constructor() {
Expand All @@ -20,16 +21,13 @@ export default class File extends React.Component {
render() {
var date = false;
if (this.props.data.modified) {
date = moment(this.props.data.modified * 1000);
date = new Date(this.props.data.modified * 1000);
}
if (this.props.data.added && this.props.isVersion) {
date = moment(this.props.data.added * 1000);
date = new Date(this.props.data.added * 1000);
}

var date_string = date ? date.format('L') : '-';
if (new Date().getTime() - date.unix() * 1000 < 1000 * 23 * 60 * 60) {
date_string = date ? date.format('LT') : '-';
}
var date_string = date ? formatTime(date) : '-';

return [
<div
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ class NotificationPreferencesService extends Observable {
);

const isNightBreak = this.isInPeriod(nightBreakIntrv[0], nightBreakIntrv[1]);
const isDeactivate = moment
.unix(this.notificationPreferences.preferences.deactivate_notifications_until)
.diff(moment());
const isDeactivate =
new Date().getTime() <
this.notificationPreferences.preferences.deactivate_notifications_until;

return isNightBreak ? false : isDeactivate > 0 ? false : true;
return isNightBreak ? false : isDeactivate ? false : true;
}

return false;
Expand All @@ -56,9 +56,9 @@ class NotificationPreferencesService extends Observable {
* @param timeToAdd Time to add
* @param format Unit of time
*/
deactivateNotificationsUntil(timeToAdd: number, format: 's' | 'm' | 'h' | 'd' | 'y'): void {
deactivateNotificationsUntil(timeToAddMs: number): void {
this.save([
{ key: 'deactivate_notifications_until', value: moment().add(timeToAdd, format).unix() },
{ key: 'deactivate_notifications_until', value: new Date().getTime() + timeToAddMs },
]);
}

Expand Down
24 changes: 24 additions & 0 deletions twake/frontend/src/app/features/global/utils/Numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,27 @@ export default class Numbers {
return ret;
}
}

export const formatTime = (
time: number | string,
locale?: string,
options: { keepTime?: boolean; keepSeconds?: boolean; keepDate?: boolean } = {
keepTime: true,
}
) => {
time = new Date(time).getTime();
locale = locale || navigator.language;
const now = Date.now();
const year = new Date(time).getFullYear();
const nowYear = new Date(now).getFullYear();
const day = 24 * 60 * 60 * 1000;
return new Intl.DateTimeFormat(locale, {
year: nowYear !== year || options?.keepDate ? "numeric" : undefined,
month: now - time >= day || options?.keepDate ? "short" : undefined,
day: now - time >= day || options?.keepDate ? "numeric" : undefined,
hour: now - time < day || options?.keepTime ? "numeric" : undefined,
minute: now - time < day || options?.keepTime ? "numeric" : undefined,
second: options?.keepSeconds ? "numeric" : undefined,
}).format(new Date(time));
};

Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@ export default () => {
{
type: 'menu',
text: Languages.t('scenes.app.channelsbar.currentuser.disabling_notifications'),
onClick: () => NotificationPreferences.deactivateNotificationsUntil(1, 'h'),
onClick: () => NotificationPreferences.deactivateNotificationsUntil(1 * 60 * 60 * 1000),
},
{
type: 'menu',
text: '2 hours', // Add translation
onClick: () => NotificationPreferences.deactivateNotificationsUntil(2, 'h'),
onClick: () => NotificationPreferences.deactivateNotificationsUntil(2 * 60 * 60 * 1000),
},
{
type: 'menu',
text: Languages.t('scenes.app.channelsbar.currentuser.disabling_notifications_until'),
onClick: () =>
NotificationPreferences.deactivateNotificationsUntil(hoursUntilTomorrowMorning, 'h'),
NotificationPreferences.deactivateNotificationsUntil(
hoursUntilTomorrowMorning * 60 * 60 * 1000,
),
},
{
type: 'menu',
Expand All @@ -48,8 +50,8 @@ export default () => {
),
onClick: () => {
status
? NotificationPreferences.deactivateNotificationsUntil(24, 'y')
: NotificationPreferences.deactivateNotificationsUntil(0, 's');
? NotificationPreferences.deactivateNotificationsUntil(10 * 365 * 24 * 60 * 60 * 1000)
: NotificationPreferences.deactivateNotificationsUntil(0);
},
},
{
Expand Down

0 comments on commit 30f8bc8

Please sign in to comment.