CalendarScreen: Retrieving a day's tasks and sorting in time order - peophins-plasmas/pawsome-app GitHub Wiki
The main feature of the CalendarScreen component is rendering the logged-in user's tasks for each day.
To retrieve the user's tasks, a call is made to Firestore searching for all tasks for which the array listing userIds (people assigned to the task) contains the id of the logged-in user, and for which the dueDate (a date string) matches the current selected date on the CalendarStrip. tasksRef .orderBy("dueTime", "asc") .where("userId", "array-contains", userId) .where("dueDate", "==", dueDate.toString())
dueDate is run with toString to ensure the format matches the format stored in Firestore, preventing possible errors.
An empty array is initiated before the Firestore call to hold the results in object format; each task retrieved is pushed to the array. To display these tasks in time order, the array is sorted by the dueTime key, a string-formatted timestring. Each timestring in the array is copied and then split on the space into AM and PM; and then once again on colons. If PM and less than 12, the hours have 12 added to them (converting to 24-hour time; dates already in 24-hour time are not affected as they are less than 12); the objects are then sorted first by hours and then, if hours are equal, by minutes. Since the altered versions of the times are copies, the original timestrings remain in their original formatting, and tasks are visually displayed to user in this manner for ease of reading.
In Firestore, each task contains both the petName and petID of the pet it is for, so the pet's name is displayed with the task to ensure clarity for the user on which pet a particular task is for, as well as the description of the task and the time the task should be completed.
If there are no chores for a given day, a helpful "No chores for today!" message is provided.