UNPKG

4.4 kBJavaScriptView Raw
1// @flow
2
3import * as React from 'react';
4import type { ComponentType } from 'react';
5
6import translate from './adsumClock.lang.json';
7
8type LangType = 'en' | 'zh' | 'fr';
9type TimeFormatType = '24hrs' | '12hrs';
10type AdsumClockPropsType = {
11 lang: LangType,
12 timeFormat: TimeFormatType
13};
14type StateType = {
15 +year: string,
16 +month: string,
17 +day: string,
18 +hours: string,
19 +minutes: string,
20 +dateStr: string,
21 +timeStr: string
22};
23type ClockUIPropsType = AdsumClockPropsType & StateType;
24
25function ClockCreator<T: ComponentType<ClockUIPropsType>>(ClockUI: T): Element<typeof AdsumClock> {
26 return class AdsumClock extends React.Component<AdsumClockPropsType, StateType> {
27 static defaultProps = {
28 lang: 'en',
29 timeFormat: '24hrs',
30 };
31
32 timerID: IntervalId = null;
33
34 state = {
35 year: '',
36 month: '',
37 day: '',
38 hours: '',
39 minutes: '',
40 dateStr: '',
41 timeStr: '',
42 };
43
44 /**
45 * Clock calls for update every sec
46 */
47 componentDidMount() {
48 this.timerID = setInterval(
49 (): void => this.getTime(),
50 1000,
51 );
52 }
53
54 /**
55 * Removing interval
56 */
57 componentWillUnmount() {
58 clearInterval(this.timerID);
59 }
60
61 getTime = () => {
62 const { timeFormat } = this.props;
63 let { lang } = this.props;
64
65 if (Object.keys(translate)
66 .indexOf(lang) === -1) {
67 console.warn(`AdsumClock does not support language: ${lang}, goes to default ${AdsumClock.defaultProps.lang}`);
68 // eslint-disable-next-line prefer-destructuring
69 lang = AdsumClock.defaultProps.lang;
70 }
71
72 const time = new Date();
73 const day = translate[lang].days[time.getDay()];
74
75 const dateStr = (time.getDate()
76 .toString().length === 1) ? `0${time.getDate()}${translate[lang].date}` : `${time.getDate()}${translate[lang].date}`;
77 const month = (lang === 'zh') ? `${time.getMonth() + 1}${translate[lang].month}` : translate[lang].month[time.getMonth()];
78 const year = `${time.getFullYear()}${translate[lang].year}`;
79
80 let hours = '';
81 let minutes = '';
82 let timeStr = '';
83
84 if (timeFormat === '12hrs') {
85 if (time.getHours() === 12) {
86 hours = `${time.getHours()}`;
87 minutes = (time.getMinutes()
88 .toString().length === 1) ? `0${time.getMinutes()}` : `${time.getMinutes()}`;
89 timeStr = `${hours}:${minutes} ${translate[lang].pm}`;
90 } else if (time.getHours() > 12) {
91 hours = `${time.getHours() - 12}`;
92 minutes = (time.getMinutes()
93 .toString().length === 1) ? `0${time.getMinutes()}` : `${time.getMinutes()}`;
94 timeStr = `${hours}:${minutes} ${translate[lang].pm}`;
95 } else {
96 hours = `${time.getHours()}`;
97 minutes = (time.getMinutes()
98 .toString().length === 1) ? `0${time.getMinutes()}` : `${time.getMinutes()}`;
99 timeStr = `${hours}:${minutes} ${translate[lang].am}`;
100 }
101 } else {
102 hours = (time.getHours()
103 .toString().length === 1) ? `0${time.getHours()}` : `${time.getHours()}`;
104 minutes = (time.getMinutes()
105 .toString().length === 1) ? `0${time.getMinutes()}` : `${time.getMinutes()}`;
106 timeStr = `${hours}:${minutes}`;
107 }
108
109 const newState = {
110 year,
111 month,
112 day,
113 hours,
114 minutes,
115 timeStr,
116 };
117
118 if (lang === 'zh') {
119 newState.dateStr = `${day}, ${year} ${month} ${dateStr}`;
120 } else {
121 newState.dateStr = `${day}, ${dateStr} ${month} ${year}`;
122 }
123
124 this.setState(newState);
125 };
126
127 render(): T {
128 return <ClockUI {...this.props} {...this.state} />;
129 }
130 };
131}
132
133export default ClockCreator;
134
\No newline at end of file