Skip to content

Commit

Permalink
fix helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
IldarKamalov committed Aug 28, 2023
1 parent 13ec6a8 commit f5bb67d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
18 changes: 5 additions & 13 deletions client/src/components/Filters/Services/ScheduleForm/TimeSelect.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import React, { useState } from 'react';
import PropTypes from 'prop-types';

import { getTimeFromMs } from './helpers';

const convertHoursToMs = (value) => value * 60 * 60 * 1000;
const convertMinutesToMs = (value) => value * 60 * 1000;

const convertOnlyHoursToMs = (value) => Math.floor(value / (60 * 60 * 1000)) * 60 * 60 * 1000;
const convertOnlyMinutesToMs = (value) => value % (60 * 60 * 1000);
import { getTimeFromMs, convertTimeToMs } from './helpers';

export const TimeSelect = ({
value,
Expand All @@ -16,21 +10,19 @@ export const TimeSelect = ({
const { hours: initialHours, minutes: initialMinutes } = getTimeFromMs(value);

const [hours, setHours] = useState(initialHours);
const [minute, setMinutes] = useState(initialMinutes);
const [minutes, setMinutes] = useState(initialMinutes);

const hourOptions = Array.from({ length: 24 }, (_, i) => i.toString().padStart(2, '0'));
const minuteOptions = Array.from({ length: 60 }, (_, i) => i.toString().padStart(2, '0'));

const onHourChange = (event) => {
setHours(event.target.value);
const newHour = parseInt(event.target.value, 10);
onChange(convertHoursToMs(newHour) + convertOnlyMinutesToMs(value));
onChange(convertTimeToMs(event.target.value, minutes));
};

const onMinuteChange = (event) => {
setMinutes(event.target.value);
const newMinute = parseInt(event.target.value, 10);
onChange(convertOnlyHoursToMs(value) + convertMinutesToMs(newMinute));
onChange(convertTimeToMs(hours, event.target.value));
};

return (
Expand All @@ -48,7 +40,7 @@ export const TimeSelect = ({
</select>
&nbsp;:&nbsp;
<select
value={minute}
value={minutes}
onChange={onMinuteChange}
className="form-control custom-select"
>
Expand Down
14 changes: 11 additions & 3 deletions client/src/components/Filters/Services/ScheduleForm/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@ export const getShortDayName = (t, abbreviation) => {
};

export const getTimeFromMs = (value) => {
const totalMinutes = Math.floor(value / (60 * 1000));
const hours = Math.floor(totalMinutes / 60);
const minutes = totalMinutes % 60;
const selectedTime = new Date(value);
const hours = selectedTime.getUTCHours();
const minutes = selectedTime.getUTCMinutes();

return {
hours: hours.toString().padStart(2, '0'),
minutes: minutes.toString().padStart(2, '0'),
};
};

export const convertTimeToMs = (hours, minutes) => {
const selectedTime = new Date(0);
selectedTime.setUTCHours(parseInt(hours, 10));
selectedTime.setUTCMinutes(parseInt(minutes, 10));

return selectedTime.getTime();
};

0 comments on commit f5bb67d

Please sign in to comment.