Skip to content

Commit 37ea473

Browse files
committed
Settings of hours
1 parent e961c61 commit 37ea473

File tree

5 files changed

+53
-11
lines changed

5 files changed

+53
-11
lines changed

src/components/SettingsModal/SettingsModal.tsx

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,43 @@
11
import React, { useCallback, useState } from 'react';
2-
import { Col, Form, Modal, Row, Select, Space } from 'antd';
3-
import { createUseStyles } from 'react-jss';
2+
import { Form, Modal, Select, Space, TimePicker } from 'antd';
43
import { observer } from 'mobx-react';
4+
// eslint-disable-next-line import/named
5+
import moment, { Moment } from 'moment';
56

67
import rootStore from '../../modules/RootStore';
78
import IModalProps from '../../types/IModalProps';
89
import NewProfilePopover from './NewProfilePopover';
10+
import { timeToMs } from '../../helpers/DateTime';
11+
import { DEFAULT_SETTINGS } from '../../modules/settings/models/SettingsModel';
912

1013
const { settingsStore } = rootStore;
1114

1215
interface ISettingsModalProps extends IModalProps {}
1316

17+
const timeFormat = 'HH:mm';
18+
1419
const SettingsModal: React.VFC<ISettingsModalProps> = observer(
1520
(props: ISettingsModalProps) => {
1621
const { visible, onClose } = props;
22+
const { settings } = settingsStore;
1723

1824
const [showNewProfilePopover, setShowNewProfilePopover] = useState<boolean>(
1925
false
2026
);
21-
const [profile, setProfile] = useState<string>(
22-
settingsStore.settings.currentProfile
27+
const [profile, setProfile] = useState<string>(settings.currentProfile);
28+
const [workingHours, setWorkingHours] = useState<Moment | null>(
29+
moment(settings.numberOfWorkingHours).utcOffset(0)
2330
);
2431

2532
const handleSave = useCallback(() => {
26-
settingsStore.setActiveProfile(profile);
33+
settingsStore.setSettings({
34+
currentProfile: profile,
35+
numberOfWorkingHours: workingHours
36+
? timeToMs(workingHours?.toDate())
37+
: DEFAULT_SETTINGS.numberOfWorkingHours,
38+
});
2739
onClose();
28-
}, [profile, onClose]);
40+
}, [profile, workingHours, onClose]);
2941

3042
const handleChangeProfile = useCallback((selected: string) => {
3143
setProfile(selected);
@@ -50,14 +62,14 @@ const SettingsModal: React.VFC<ISettingsModalProps> = observer(
5062
onOk={handleSave}
5163
onCancel={onClose}
5264
>
53-
<Form.Item label="Profile" labelCol={{ span: 24 }}>
65+
<Form.Item label="Switch profile" labelCol={{ span: 24 }}>
5466
<Space>
5567
<Select
5668
value={profile}
5769
onChange={handleChangeProfile}
5870
style={{ width: 200 }}
5971
>
60-
{settingsStore.settings.profiles.map((profile) => (
72+
{settings.profiles.map((profile) => (
6173
<Select.Option key={profile} value={profile}>
6274
{profile}
6375
</Select.Option>
@@ -69,6 +81,16 @@ const SettingsModal: React.VFC<ISettingsModalProps> = observer(
6981
/>
7082
</Space>
7183
</Form.Item>
84+
<Form.Item label="Number of working hours">
85+
<TimePicker
86+
value={workingHours}
87+
onChange={(value) => setWorkingHours(value)}
88+
format={timeFormat}
89+
minuteStep={5}
90+
showNow={false}
91+
allowClear={false}
92+
/>
93+
</Form.Item>
7294
</Modal>
7395
);
7496
}

src/helpers/DateTime.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ export function msToTime(s: number, showSeconds: boolean = true) {
4545
return timeItemsToString(sign, hrs, mins, secs, showSeconds);
4646
}
4747

48+
export function timeToMs(date: Date) {
49+
const hours = date.getHours();
50+
const minutes = date.getMinutes();
51+
52+
return (hours * 60 + minutes) * 60 * 1000;
53+
}
54+
4855
export function calcDuration(taskTime: ITimeRangeModel[]): number {
4956
return taskTime.reduce((prev, timeRange) => {
5057
if (!timeRange.start) {

src/modules/settings/SettingsStore.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { makeAutoObservable } from 'mobx';
33
import SettingsModel, { DEFAULT_SETTINGS } from './models/SettingsModel';
44
import SettingsService from './SettingsService';
55
import { RootStore } from '../RootStore';
6+
import { ISettings } from './models/ISettings';
67

78
export default class SettingsStore {
89
settings: SettingsModel = new SettingsModel(DEFAULT_SETTINGS);
@@ -18,6 +19,13 @@ export default class SettingsStore {
1819
this.service.save(this.settings);
1920
}
2021

22+
setSettings(newSettings: ISettings) {
23+
const { currentProfile, numberOfWorkingHours } = newSettings;
24+
this.settings.numberOfWorkingHours = numberOfWorkingHours;
25+
this.setActiveProfile(currentProfile);
26+
this.service.save(this.settings);
27+
}
28+
2129
setActiveProfile(profile: string) {
2230
if (profile === this.settings.currentProfile) {
2331
return;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface ISettings {
2+
currentProfile: string;
3+
numberOfWorkingHours: number;
4+
}

src/modules/settings/models/SettingsModel.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@ import { makeObservable, observable } from 'mobx';
44
export const DEFAULT_SETTINGS = {
55
currentProfile: 'profile1',
66
profiles: ['profile1'],
7-
hoursInWorkingDay: 8 * 60 * 60 * 1000,
7+
numberOfWorkingHours: 8 * 60 * 60 * 1000,
88
isFirstLoad: true,
99
};
1010

1111
export default class SettingsModel extends AbstractModel {
1212
currentProfile: string = DEFAULT_SETTINGS.currentProfile;
1313
profiles: string[] = DEFAULT_SETTINGS.profiles;
14-
hoursInWorkingDay: number = DEFAULT_SETTINGS.hoursInWorkingDay;
14+
numberOfWorkingHours: number = DEFAULT_SETTINGS.numberOfWorkingHours;
1515
isFirstLoad: boolean = DEFAULT_SETTINGS.isFirstLoad;
16+
// TODO showNotifications
1617

1718
constructor(data: any) {
1819
super();
1920
this.load(data);
2021
makeObservable(this, {
2122
currentProfile: observable,
2223
profiles: observable,
23-
hoursInWorkingDay: observable,
24+
numberOfWorkingHours: observable,
2425
isFirstLoad: observable,
2526
});
2627
}

0 commit comments

Comments
 (0)