Skip to content

Commit

Permalink
Merge pull request #47 from Cognifide/feature/update-world-clock-widget
Browse files Browse the repository at this point in the history
Feature/update world clock widget
  • Loading branch information
michalskrzypek authored Sep 16, 2019
2 parents c1f47cc + 482d553 commit 150cc26
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 72 deletions.
28 changes: 0 additions & 28 deletions cogboard-webapp/src/components/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,9 @@
import moment from "moment-timezone";

export const setSize = factor => ({ theme }) => `${theme.spacing(factor)}px`;

export const splitPropsGroupName = (propName) => {
return propName.includes('.') ? propName.split('.') : [undefined, propName];
};

export const getGmtTimezones = () => {
const allTimezones = moment.tz.names();
const duplicatedTimezones = ['Etc/GMT', 'Etc/GMT+0', 'Etc/GMT0'];

const gmtSort = (a, b) => {
const parts = {
a: a.split('T'),
b: b.split('T')
};
return parseInt(parts.b[1]) - parseInt(parts.a[1]);
};

return allTimezones
.filter(timezone => timezone.includes("Etc/GMT") && !duplicatedTimezones.includes(timezone))
.sort(gmtSort)
.map(timezone => {
const displayName = timezone
.replace('Etc/', '')
.replace(/[+-]/g, c => c === '+' ? '-' : '+'); //swap '+' sign with '-' sign to preserve correct time
return {
display: displayName,
value: timezone
};
});
};

export const sortByKey = (obj, key, asc = true) => Object.entries(obj)
.sort(([, { [key]: keyA }], [, { [key]: keyB }]) => (
asc ? keyA.localeCompare(keyB) : keyB.localeCompare(keyA)))
Expand Down
43 changes: 39 additions & 4 deletions cogboard-webapp/src/components/widgets/dialogFields/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { DATE_FORMATS, GMT_TIMEZONES, TIME_FORMATS } from "../types/WorldClockWidget/helpers";

import EndpointInput from './EndpointInput';
import NumberInput from './NumberInput';
import TextInput from './TextInput';
import SonarQubeMetricsInput from './SonarQubeMetricsInput';
import DisplayValueSelect from "./DisplayValueSelect";
import { DATE_FORMATS, GMT_TIMEZONES } from "../../../constants";
import TextSizeInput from "./TextSizeInput";
import MultilineTextInput from "./MultilineTextInput";
import CheckboxInput from "./CheckboxInput";
Expand Down Expand Up @@ -72,14 +73,48 @@ const dialogFields = {
name: 'content.timeZoneId',
label: 'Timezone',
dropdownItems: GMT_TIMEZONES,
initialValue: 'Etc/GMT-2' //timezone for Poland
initialValue: GMT_TIMEZONES[0].value
},
DateTimeFormat: {
DateFormat: {
component: DisplayValueSelect,
name: 'content.dateFormat',
label: 'Date Format',
dropdownItems: DATE_FORMATS,
initialValue: DATE_FORMATS[3].value
initialValue: DATE_FORMATS[1].value
},
TimeFormat: {
component: DisplayValueSelect,
name: 'content.timeFormat',
label: 'Time Format',
dropdownItems: TIME_FORMATS,
initialValue: TIME_FORMATS[1].value
},
DisplayDate: {
component: CheckboxInput,
name: 'content.displayDate',
label: 'Display date',
initialValue: true
},
DisplayTime: {
component: CheckboxInput,
name: 'content.displayTime',
label: 'Display time',
initialValue: true
},
DateTimeSize: {
component: TextSizeInput,
name: 'content.textSize',
label: 'Text Size',
dropdownItems: {
XXL: 'h2',
XL: 'h3',
L: 'h4',
M: 'h5',
S: 'h6',
XS: 'subtitle1',
XXS: 'subtitle2'
},
initialValue: 'h5'
},
Text: {
component: MultilineTextInput,
Expand Down
2 changes: 1 addition & 1 deletion cogboard-webapp/src/components/widgets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const widgetTypes = {
WorldClockWidget: {
name: 'World Clock widget',
component: WorldClockWidget,
dialogFields: ['TimeZoneId', 'DateTimeFormat']
dialogFields: ['TimeZoneId', 'DateFormat', 'TimeFormat', 'DisplayDate', 'DisplayTime', 'DateTimeSize']
},
IFrameEmbedWidget: {
name: 'IFrame Embed widget',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ IFrameEmbedWidget.propTypes = {
url: string.isRequired
};


export default IFrameEmbedWidget;
Original file line number Diff line number Diff line change
@@ -1,5 +1,78 @@
import moment from 'moment-timezone'

export const getDate = (timezone, format) => {
return moment.tz(new Date(), timezone).format(format);
export const getDateTime = (timezone, format) => {
return moment.tz(new Date(), timezone).format(format);
};

const getGmtTimezones = () => {
const allTimezones = moment.tz.names();
const duplicatedTimezones = ['Etc/GMT', 'Etc/GMT+0', 'Etc/GMT0'];

const gmtSort = (a, b) => {
const parts = {
a: a.split('T'),
b: b.split('T')
};
return parseInt(parts.b[1]) - parseInt(parts.a[1]);
};

const gmtTimezones = allTimezones
.filter(timezone => timezone.includes("Etc/GMT") && !duplicatedTimezones.includes(timezone))
.sort(gmtSort)
.map(timezone => {
const displayName = timezone
.replace('Etc/', '')
.replace(/[+-]/g, c => c === '+' ? '-' : '+'); //swap '+' sign with '-' sign to preserve correct time
return {
display: displayName,
value: timezone
};
});

gmtTimezones.splice(0, 0, {
display: "Current time zone",
value: moment.tz.guess()
});

return gmtTimezones;
};

export const GMT_TIMEZONES = getGmtTimezones();

export const DATE_FORMATS = [
{
display: '29/08/2019',
value: 'DD/MM/YYYY'
},
{
display: 'Friday, 29/08/2019',
value: 'dddd, DD/MM/YYYY'
},
{
display: '29 Aug 2019',
value: 'DD MMM YYYY'
},
{
display: 'Friday, 29 Aug 2019',
value: 'dddd, DD MMM YYYY'
},
{
display: '29th August 2019',
value: 'Do MMMM YYYY'
},
{
display: 'Friday, 29th August 2019',
value: 'dddd, Do MMMM YYYY'
}
];

export const TIME_FORMATS = [
{
display: '12-hour (AM/PM)',
value: 'h:mm:ss a'
},
{
display: '24-hour',
value: 'HH:mm:ss'
}
];
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
import React, {useState} from 'react';
import Typography from "@material-ui/core/Typography/index";
import {string} from "prop-types";

import {useInterval} from '../../../../hooks'
import {getDate} from "./helpers";
import {getDateTime} from "./helpers";

import {Typography} from "@material-ui/core";
import { DatePre, TimePre } from "./styled";

const WorldClockWidget = props => {
const {timeZoneId, dateFormat} = props;
const { timeZoneId, dateFormat, timeFormat, displayDate, displayTime, textSize } = props;

const date = getDateTime(timeZoneId, dateFormat);

const initTime = getDateTime(timeZoneId, timeFormat);
const [time, setTime] = useState(initTime);

let date = getDate(timeZoneId, dateFormat);
let [datetime, setDateTime] = useState(date);
useInterval(() => {
setTime(getDateTime(timeZoneId, timeFormat));
}, 1000);

useInterval(() => {
setDateTime(getDate(timeZoneId, dateFormat));
}, 1000);
return (
<Typography variant={textSize}>
{displayTime && <TimePre>{time}</TimePre>}
{displayDate && <DatePre>{date}</DatePre>}
</Typography>
);
};

WorldClockWidget.propTypes = {
timeZoneId: string.isRequired,
dateFormat: string.isRequired,
timeFormat: string.isRequired,
textSize: string
};

return (
<Typography>
{datetime}
</Typography>
);
WorldClockWidget.defaultProps = {
textSize: 'h5'
};

export default WorldClockWidget;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import styled from "@emotion/styled/macro";

export const TimePre = styled.pre`
font-weight: bold;
font-family: inherit;
font-size: 180%;
margin: 0 auto;
`;

export const DatePre = styled.pre`
font-family: inherit;
margin: 0.25em 0 auto;
`;
23 changes: 0 additions & 23 deletions cogboard-webapp/src/constants/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {getGmtTimezones} from "../components/helpers";

export const URL = {
LOAD_DATA: '/api/config', // for front dev change to '/data.json'
SAVE_DATA: '/api/config/save',
Expand All @@ -10,27 +8,6 @@ export const URL = {
export const COLUMNS_MIN = 1;
export const COLUMNS_MAX = 20;

export const GMT_TIMEZONES = getGmtTimezones();

export const DATE_FORMATS = [
{
display: '29-08-2019, 15:43:12',
value: 'DD-MM-YYYY, HH:mm:ss'
},
{
display: '29 Aug 2019, 15:43:12',
value: 'DD MMM YYYY, HH:mm:ss'
},
{
display: '15:43:12, 29-08-2019',
value: 'HH:mm:ss, DD-MM-YYYY'
},
{
display: '15:43:12, 29 Aug 2019',
value: 'HH:mm:ss, DD MMM YYYY'
}
];

export const ROWS_MIN = 1;
export const ItemTypes = {
WIDGET: 'widget'
Expand Down

0 comments on commit 150cc26

Please sign in to comment.