Skip to content

Commit ace0afd

Browse files
committed
dates.format() wraps date-fns/format() to centralise data formatting
1 parent b7d17a0 commit ace0afd

File tree

5 files changed

+35
-11
lines changed

5 files changed

+35
-11
lines changed

client/modules/IDE/components/CollectionList/CollectionListRow.jsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import format from 'date-fns/format';
21
import PropTypes from 'prop-types';
32
import React from 'react';
43
import { connect } from 'react-redux';
@@ -9,11 +8,10 @@ import * as ProjectActions from '../../actions/project';
98
import * as CollectionsActions from '../../actions/collections';
109
import * as IdeActions from '../../actions/ide';
1110
import * as ToastActions from '../../actions/toast';
11+
import dates from '../../../../utils/formatDate';
1212

1313
import DownFilledTriangleIcon from '../../../../images/down-filled-triangle.svg';
1414

15-
const formatDateCell = (date, mobile = false) => format(new Date(date), 'MMM D, YYYY');
16-
1715
class CollectionListRowBase extends React.Component {
1816
static projectInCollection(project, collection) {
1917
return collection.items.find(item => item.project.id === project.id) != null;
@@ -214,8 +212,8 @@ class CollectionListRowBase extends React.Component {
214212
{this.renderCollectionName()}
215213
</span>
216214
</th>
217-
<td>{mobile && 'Created: '}{format(new Date(collection.createdAt), 'MMM D, YYYY')}</td>
218-
<td>{mobile && 'Updated: '}{formatDateCell(collection.updatedAt)}</td>
215+
<td>{mobile && 'Created: '}{dates.format(collection.createdAt)}</td>
216+
<td>{mobile && 'Updated: '}{dates.format(collection.updatedAt)}</td>
219217
<td>{mobile && '# sketches: '}{(collection.items || []).length}</td>
220218
<td className="sketch-list__dropdown-column">
221219
{this.renderActions()}

client/modules/IDE/components/SketchList.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import format from 'date-fns/format';
21
import PropTypes from 'prop-types';
32
import React from 'react';
43
import { Helmet } from 'react-helmet';
@@ -8,6 +7,7 @@ import { Link } from 'react-router';
87
import { bindActionCreators } from 'redux';
98
import classNames from 'classnames';
109
import slugify from 'slugify';
10+
import dates from '../../../utils/formatDate';
1111
import * as ProjectActions from '../actions/project';
1212
import * as ProjectsActions from '../actions/projects';
1313
import * as CollectionsActions from '../actions/collections';
@@ -24,7 +24,7 @@ import ArrowDownIcon from '../../../images/sort-arrow-down.svg';
2424
import DownFilledTriangleIcon from '../../../images/down-filled-triangle.svg';
2525

2626

27-
const formatDateCell = (date, mobile = false) => format(new Date(date), mobile ? 'MMM D, YYYY' : 'MMM D, YYYY h:mm A');
27+
const formatDateCell = (date, mobile = false) => dates.format(date, { showTime: !mobile });
2828

2929
class SketchListRowBase extends React.Component {
3030
constructor(props) {

client/modules/User/components/APIKeyList.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import PropTypes from 'prop-types';
22
import React from 'react';
3-
import format from 'date-fns/format';
43
import distanceInWordsToNow from 'date-fns/distance_in_words_to_now';
54
import orderBy from 'lodash/orderBy';
65

76
import { APIKeyPropType } from './APIKeyForm';
87

8+
import dates from '../../../utils/formatDate';
99
import TrashCanIcon from '../../../images/trash-can.svg';
1010

1111
function APIKeyList({ apiKeys, onRemove, t }) {
@@ -28,7 +28,7 @@ function APIKeyList({ apiKeys, onRemove, t }) {
2828
return (
2929
<tr key={key.id}>
3030
<td>{key.label}</td>
31-
<td>{format(new Date(key.createdAt), 'MMM D, YYYY h:mm A')}</td>
31+
<td>{dates.format(key.createdAt)}</td>
3232
<td>{lastUsed}</td>
3333
<td className="api-key-list__action">
3434
<button

client/modules/User/components/Collection.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import format from 'date-fns/format';
21
import PropTypes from 'prop-types';
32
import React, { useState, useRef, useEffect } from 'react';
43
import { Helmet } from 'react-helmet';
@@ -23,6 +22,7 @@ import Overlay from '../../App/components/Overlay';
2322
import AddToCollectionSketchList from '../../IDE/components/AddToCollectionSketchList';
2423
import CopyableInput from '../../IDE/components/CopyableInput';
2524
import { SketchSearchbar } from '../../IDE/components/Searchbar';
25+
import dates from '../../../utils/formatDate';
2626

2727
import ArrowUpIcon from '../../../images/sort-arrow-up.svg';
2828
import ArrowDownIcon from '../../../images/sort-arrow-down.svg';
@@ -101,7 +101,7 @@ const CollectionItemRowBase = ({
101101
<th scope="row">
102102
{name}
103103
</th>
104-
<td>{format(new Date(item.createdAt), 'MMM D, YYYY h:mm A')}</td>
104+
<td>{dates.format(item.createdAt)}</td>
105105
<td>{sketchOwnerUsername}</td>
106106
<td className="collection-row__action-column ">
107107
{isOwner &&

client/utils/formatDate.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import format from 'date-fns/format';
2+
import isValid from 'date-fns/is_valid';
3+
import parseISO from 'date-fns/parse';
4+
5+
function parse(maybeDate) {
6+
const date = maybeDate instanceof Date ? maybeDate : parseISO(maybeDate);
7+
8+
if (isValid(date)) {
9+
return date;
10+
}
11+
12+
return null;
13+
}
14+
15+
export default {
16+
format(date, { showTime = true } = {}) {
17+
const parsed = parse(date);
18+
const formatType = showTime ? 'MMM D, YYYY h:mm A' : 'MMM D, YYYY';
19+
20+
if (parsed) {
21+
return format(parsed, formatType);
22+
}
23+
24+
return '';
25+
}
26+
};

0 commit comments

Comments
 (0)