Skip to content

Commit

Permalink
decouple datetime component from angular service (superdesk#4729)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaskikutis authored Jan 21, 2025
1 parent 2e1f329 commit f3a916e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
50 changes: 27 additions & 23 deletions scripts/core/datetime/datetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,32 +137,36 @@ export function scheduledFormat(__item: IArticle): {short: string, long: string}
};
}

/**
* Get short representation of given datetime
*
* It returns time for current day, day + time for current week, date otherwise.
*
* @param {String} d iso format datetime
* @return {String}
*/
export function shortFormat(d) {
var m = moment(d);
var now = moment();

if (isSameDay(m, now)) {
return m.format(TIME_FORMAT);
} else if (isSameWeek(m, now)) {
return m.format(WEEK_FORMAT);
} else if (isArchiveYear(m, now)) {
return m.format(ARCHIVE_FORMAT);
}

return m.format(DATE_FORMAT);
}

export const longFormat = (date) => formatDate(date, {longFormat: true});

DateTimeService.$inject = [];
function DateTimeService() {
/**
* Get short representation of given datetime
*
* It returns time for current day, day + time for current week, date otherwise.
*
* @param {String} d iso format datetime
* @return {String}
*/
this.shortFormat = function(d) {
var m = moment(d);
var now = moment();

if (isSameDay(m, now)) {
return m.format(TIME_FORMAT);
} else if (isSameWeek(m, now)) {
return m.format(WEEK_FORMAT);
} else if (isArchiveYear(m, now)) {
return m.format(ARCHIVE_FORMAT);
}

return m.format(DATE_FORMAT);
};
this.shortFormat = shortFormat;

this.longFormat = (date) => formatDate(date, {longFormat: true});
this.longFormat = longFormat;
}

DateTimeHelperService.$inject = [];
Expand Down
7 changes: 3 additions & 4 deletions scripts/core/ui/components/DateTime.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import React from 'react';
import ng from 'core/services/ng';
import {IPropsDateTime} from 'superdesk-api';
import {longFormat, shortFormat} from 'core/datetime/datetime';

export class DateTime extends React.PureComponent<IPropsDateTime> {
render() {
const datetimeService = ng.get('datetime');
const {dateTime} = this.props;

const dateShort = datetimeService.shortFormat(dateTime);
const dateLong = datetimeService.longFormat(dateTime);
const dateShort = shortFormat(dateTime);
const dateLong = longFormat(dateTime);
const tooltip = this.props.tooltip == null ? dateLong : this.props.tooltip(dateLong, dateShort);

return (
Expand Down

0 comments on commit f3a916e

Please sign in to comment.