@zohodesk/hooks
Version:
Unified Component Library - Hooks
41 lines (37 loc) • 1 kB
JavaScript
import useCommonReducer from "../utils/useCommonReducer";
import defaultReducer from "./reducers/timeReducer";
import { CHANGETIME } from "./reducers/constants";
import { timeDecode } from "./utils/timeConverter";
function useTime(props) {
let customReducer = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : (state, action) => state;
let {
time
} = props;
let {
hours,
minutes,
noon
} = timeDecode(time);
let ch = noon === "AM" && hours == 12 ? `00` : noon === "PM" && hours > 12 ? hours + 12 : hours;
let initialState = {
time: `${ch}:${minutes}:${noon}`
};
const {
state,
dispatch
} = useCommonReducer(defaultReducer, customReducer, initialState); //4:00:pm
function changeTime(time) {
let currentTime = timeDecode(state.time);
dispatch({
type: CHANGETIME,
data: { ...currentTime,
...time
}
});
}
return {
time: state.time,
setTime: changeTime
};
}
export default useTime;