-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check in/75754/submit travel claims (#28485)
* added hook and api call for multi-travel * refined plurality of claims vs appointments and minor refactors to naming. * copy array so count doesnt get maniplulated. * Removed commented out test. * Updated mileage page content. * added tests and fixed error setting to use hook. * updates to success alert tests. * Fixed typo ✏️ * added a relational value to sub keys of key. * claims set to required 🔒 * added difference in day and test for submit hook * text change for select label.
- Loading branch information
Showing
12 changed files
with
339 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { useEffect, useState, useMemo } from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
import { differenceInCalendarDays, parseISO } from 'date-fns'; | ||
import { api } from '../api'; | ||
import { useStorage } from './useStorage'; | ||
import { makeSelectForm, makeSelectCurrentContext } from '../selectors'; | ||
import { APP_NAMES } from '../utils/appConstants'; | ||
|
||
const usePostTravelClaims = () => { | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [isComplete, setIsComplete] = useState(false); | ||
const [travelPayClaimError, setTravelPayClaimError] = useState(false); | ||
const selectForm = useMemo(makeSelectForm, []); | ||
const { data } = useSelector(selectForm); | ||
const { facilitiesToFile } = data; | ||
const selectCurrentContext = useMemo(makeSelectCurrentContext, []); | ||
const { token: uuid } = useSelector(selectCurrentContext); | ||
const { setTravelPaySent, getTravelPaySent } = useStorage( | ||
APP_NAMES.TRAVEL_CLAIM, | ||
true, | ||
); | ||
const travelPaySent = getTravelPaySent(window); | ||
|
||
const faciltiesToPost = facilitiesToFile.filter( | ||
facility => | ||
!(facility.stationNo in travelPaySent) || | ||
differenceInCalendarDays( | ||
Date.now(), | ||
parseISO(travelPaySent[facility.stationNo]), | ||
), | ||
); | ||
useEffect( | ||
() => { | ||
const markTravelPayClaimSent = facilities => { | ||
facilities.forEach(facility => { | ||
travelPaySent[facility.stationNo] = new Date(); | ||
setTravelPaySent(window, travelPaySent); | ||
}); | ||
}; | ||
if (isLoading && !isComplete) { | ||
return; | ||
} | ||
setIsLoading(true); | ||
if (!faciltiesToPost.length) { | ||
setIsLoading(false); | ||
return; | ||
} | ||
api.v2 | ||
.postTravelPayClaims(faciltiesToPost, uuid) | ||
.catch(() => { | ||
setTravelPayClaimError(true); | ||
}) | ||
.finally(() => { | ||
markTravelPayClaimSent(faciltiesToPost); | ||
setIsLoading(false); | ||
setIsComplete(true); | ||
}); | ||
}, | ||
[ | ||
isLoading, | ||
setTravelPaySent, | ||
uuid, | ||
isComplete, | ||
faciltiesToPost, | ||
travelPaySent, | ||
], | ||
); | ||
|
||
return { | ||
travelPayClaimError, | ||
isLoading, | ||
}; | ||
}; | ||
|
||
export { usePostTravelClaims }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.