-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from Cognifide/feature/update-world-clock-widget
Feature/update world clock widget
- Loading branch information
Showing
8 changed files
with
158 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,5 +16,4 @@ IFrameEmbedWidget.propTypes = { | |
url: string.isRequired | ||
}; | ||
|
||
|
||
export default IFrameEmbedWidget; |
77 changes: 75 additions & 2 deletions
77
cogboard-webapp/src/components/widgets/types/WorldClockWidget/helpers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
]; |
43 changes: 30 additions & 13 deletions
43
cogboard-webapp/src/components/widgets/types/WorldClockWidget/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
13 changes: 13 additions & 0 deletions
13
cogboard-webapp/src/components/widgets/types/WorldClockWidget/styled.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters