I am working in React Native and trying to set a very large object, with its keys as dates that will be obtained from two different arrays- meetings and assignments. The object I am populated, markedDates needs to look something like this...
{
"2022-08-31": {"activeOpacity": 0.5, "color": "white", "dots": [[Object]], "marked": true},
"2022-09-12": {"activeOpacity": 0.5, "color": "white", "dots": [[Object]], "marked": true},
"2022-09-14": {"activeOpacity": 0.5, "color": "white", "dots": [[Object]], "marked": true},
"2022-09-16": {"activeOpacity": 0.5, "color": "white", "dots": [[Object]], "marked": true},
"2022-11-07": {"activeOpacity": 0.5, "color": "white", "dots": [[Object]], "marked": true}
}
where the dots property will look like one of FOUR things...
dots = [assignDot]
dots = [meetingDot]
dots = [assignDot, meetingDot]
dots = []
How this works, is it first goes through the meetings array, and for each meeting, it finds the date and plugs it into the object. It then does the same thing for assignments, with the added caveat that if there is an existing markedDates object key for the current date (for example, if the selected day was 2022-09-12 and markedDates[2022-09-21] existed) then it checks to see if markedDates[2022-09-21].dots is populated. If it is, it will add an assignmentDot, if not, the dots array will just be assignments.
The issue I am facing, is it runs these two functions simultaneously, meaning when going through the assignments array, it does not know whether or not any meeting dates were added to markedDates, so it returns [assignDot] every time, when it should be returning [assignDot, meetingDot]
The functions that set all of this are as follows...
// EXTRACTS MEETINGS AND ASSIGNMNETS, ASSIGNS THEM TO STATE AND MARKEDATES
async function handleAllDates(){
return await handleAllMeetings()
.then( async () => {
await handleAllAssignmnets()
})
.then(() => {
return true
})
}
// Sets markedDate for Meetings. RUNS FIRST
async function handleAllMeetings(){
//////////////////
// All Meetings //
await meetings.forEach( async meeting => {
let jsTime = (convertDateTimeToJavaScript(meeting.meetingDateTime))
let timekey = `${jsTime.year}-${(jsTime.month)}-${(jsTime.date)}`
await setMeetingDays(...meetingDays, timekey)
await setMarkedDateObjects( markedDateObjects => ({
...markedDateObjects, [timekey]: { marked: true, dots: [meetingDot], color: 'white', activeOpacity: 0.5 }
}))
})
return true
}
// Sets markedDate for Assignments. RUNS SECOND
async function handleAllAssignmnets(){
/////////////////////
// All Assignments //
await assignments[0].forEach( async assign => {
let year = assign.dateDue.slice(7, 11)
let day = (assign.dateDue.slice(4, 6))
let month = assign.dateDue.slice(0, 4)
month = (convertMonthToNumber(month))
let timekey = (`${year}-${month.toString().padStart(2, '0')}-${(day)}`)
await setAssDays(...assDays, )
let dots = [assignDot]
if (markedDateObjects[timekey]){
if (markedDateObjects[timekey].dots){
dots = [...markedDateObjects[timekey].dots, assignDot]
}
}
await setMarkedDateObjects( markedDateObjects => ({
...markedDateObjects, [timekey]: { marked: true, dots: dots, color: 'white', activeOpacity: 0.5 }
}))
})
return true
}
//////////////////////////////
// Sets All Calendar States //
useState(() => {
handleAllDates()
.then(() => {
setLoading(false)
})
}, [])