Skip to content

Commit

Permalink
feat: allow to use custom date output formats
Browse files Browse the repository at this point in the history
  • Loading branch information
d-koppenhagen authored and bastianccm committed Jan 11, 2022
1 parent e0113c4 commit dc84d19
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 10 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ To change the logo, create a public folder in your application and put your `log

For reference have a look at [public/logo.svg](./public/logo.svg).

### Change the logo
By default the Date format used in the app is `"MMMM YYYY"`.
You can change this by editing the config file as shown below.
Please be sure you are entering a valid [moment.js format string](https://momentjs.com/docs/#/displaying/format).

```json
{
// ...
"dateFormat": "MMMM YYYY"
}
```

For reference have a look at [public/logo.svg](./public/logo.svg).

### Change the rings and quadrants config
To change the default rings and quadrants of the radar, you can place a custom `config.json` file within the `public` folder.
The `showEmptyRings` option can be enabled to display the header for a ring even when it contains no items (helpful to
Expand Down
5 changes: 3 additions & 2 deletions public/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@
"arcWidth": 2
}
]
}
}
},
"dateFormat": "MMMM YYYY"
}
10 changes: 8 additions & 2 deletions src/components/ItemRevision/ItemRevision.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ import Badge from "../Badge/Badge";
import { formatRelease } from "../../date";
import { Revision } from "../../model";

export default function ItemRevision({ revision }: { revision: Revision }) {
export default function ItemRevision({
revision,
dateFormat,
}: {
revision: Revision;
dateFormat?: string
}) {
return (
<div className="item-revision">
<div>
<Badge type={revision.ring}>
{revision.ring} | {formatRelease(revision.release)}
{revision.ring} | {formatRelease(revision.release, dateFormat)}
</Badge>
</div>
<div
Expand Down
6 changes: 4 additions & 2 deletions src/components/ItemRevisions/ItemRevisions.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import React from "react";
import HeadlineGroup from "../HeadlineGroup/HeadlineGroup";
import ItemRevision from "../ItemRevision/ItemRevision";
import { Revision } from "../../model";
import "./item-revisions.scss";
import { useMessages } from "../../context/MessagesContext";

export default function ItemRevisions({
revisions,
dateFormat,
}: {
revisions: Revision[];
dateFormat?: string
}) {
const { revisionsText } = useMessages();
return (
Expand All @@ -17,7 +19,7 @@ export default function ItemRevisions({
</HeadlineGroup>

{revisions.map((revision) => (
<ItemRevision key={revision.release} revision={revision} />
<ItemRevision key={revision.release} revision={revision} dateFormat={dateFormat} />
))}
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/PageIndex/PageIndex.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default function PageIndex({
<QuadrantGrid items={featuredOnly(items)} config={config} />
)}
<div className="publish-date">
{publishedLabel} {formatRelease(newestRelease)}
{publishedLabel} {formatRelease(newestRelease, config.dateFormat)}
</div>
</Fadeable>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/PageItem/PageItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const PageItem: React.FC<Props> = ({ pageName, items, config, leaving, onLeave }
dangerouslySetInnerHTML={{ __html: item.body }}
/>
{item.revisions.length > 1 && (
<ItemRevisions revisions={item.revisions.slice(1)} />
<ItemRevisions revisions={item.revisions.slice(1)} dateFormat={config.dateFormat} />
)}
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface ConfigData {
ringsAttributes: {radius: number, arcWidth: number}[]
};
homepageContent: HomepageOption;
dateFormat?: string;
}

export const radarName =
Expand Down
11 changes: 11 additions & 0 deletions src/date.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import moment from "moment";
import { formatRelease } from "./date";

describe("formatRelease", () => {
it("should format a date object using default output format", () => {
expect(formatRelease(moment('2022-01-05'))).toEqual('January 2022')
});
it("should format a date object using a custom output format", () => {
expect(formatRelease(moment('2022-01-05'), 'DD.MM.YYYY')).toEqual('05.01.2022')
});
});
4 changes: 2 additions & 2 deletions src/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import moment from "moment";
const isoDateToMoment = (isoDate: moment.MomentInput) =>
moment(isoDate, "YYYY-MM-DD");

export const formatRelease = (isoDate: moment.MomentInput) =>
isoDateToMoment(isoDate).format("MMMM YYYY");
export const formatRelease = (isoDate: moment.MomentInput, format: string = "MMMM YYYY") =>
isoDateToMoment(isoDate).format(format);

0 comments on commit dc84d19

Please sign in to comment.