diff --git a/src/actions/timeEntries.js b/src/actions/timeEntries.js index 2b91775258..9625363ced 100644 --- a/src/actions/timeEntries.js +++ b/src/actions/timeEntries.js @@ -10,6 +10,7 @@ import { GET_TIME_ENTRIES_PERIOD_BULK, } from '../constants/timeEntries'; import { ENDPOINTS } from '~/utils/URL'; +import { fetchServerTime, getServerMoment } from '../utils/serverTime'; export const setTimeEntriesForWeek = (data, offset) => ({ type: GET_TIME_ENTRIES_WEEK, @@ -47,6 +48,7 @@ export const getTimeEntriesForWeek = (userId, offset) => { .format('YYYY-MM-DDTHH:mm:ss'); const url = ENDPOINTS.TIME_ENTRIES_PERIOD(userId, fromDate, toDate); + return async dispatch => { let loggedOut = false; const res = await axios.get(url).catch(error => { @@ -197,21 +199,67 @@ export const getTimeStartDateEntriesByPeriod = (userId, fromDate, toDate) => { } }; }; + +/* +export const postTimeEntry = (timeEntry, { displayedUserId } = {}) => { + const url = ENDPOINTS.TIME_ENTRY(); + return async dispatch => { + try { + const res = await axios.post(url, timeEntry); + if (timeEntry.entryType === 'default' || timeEntry.entryType === 'person') { + dispatch(updateTimeEntries(timeEntry, undefined, displayedUserId)); + } + return res.status; + } catch (e) { + return e.response.status; + } + }; +}; +*/ + + export const postTimeEntry = (timeEntry, { displayedUserId } = {}) => { const url = ENDPOINTS.TIME_ENTRY(); + return async dispatch => { try { + // Get backend server time + await fetchServerTime(); + + const serverNow = moment(getServerMoment()); + + const selectedDate = moment(timeEntry.dateOfWork); + + console.log('Server time:', serverNow.format()); + console.log('Selected time entry date:', selectedDate.format()); + + // // Prevent future date/time logging + // if (selectedDate.isAfter(serverNow)) { + // toast.error('You cannot log time for a future date or time.'); + // return 400; + // } + + // Prevent logging on previous or future dates + if (!selectedDate.isSame(serverNow, 'day')) { + toast.error('You can only log time for today.'); + return 400; + } + const res = await axios.post(url, timeEntry); + if (timeEntry.entryType === 'default' || timeEntry.entryType === 'person') { dispatch(updateTimeEntries(timeEntry, undefined, displayedUserId)); } + return res.status; + } catch (e) { return e.response.status; } }; }; + export const editTimeEntry = (timeEntryId, timeEntry, oldDateOfWork, { displayedUserId } = {}) => { const url = ENDPOINTS.TIME_ENTRY_CHANGE(timeEntryId); return async dispatch => { @@ -270,4 +318,4 @@ const updateTimeEntries = (timeEntry, oldDateOfWork, displayedUserId) => { // export const setTimeEntriesForPeriod = data => ({ // type: GET_TIME_ENTRIES_PERIOD, // payload: data, -// }); +// }); \ No newline at end of file diff --git a/src/utils/serverTime.js b/src/utils/serverTime.js new file mode 100644 index 0000000000..1e4d1a3870 --- /dev/null +++ b/src/utils/serverTime.js @@ -0,0 +1,13 @@ +import axios from 'axios'; + +let serverTime = null; + +export const fetchServerTime = async () => { + const response = await axios.get('/api/servertime'); + serverTime = response.data.serverTime; + return serverTime; +}; + +export const getServerMoment = () => { + return serverTime ? serverTime : new Date(); +}; \ No newline at end of file