Add Task states - peophins-plasmas/pawsome-app GitHub Wiki

The user is passed down in props from the CalendarScreen component, as is the date selected on the calendar (such that the date selected on the calendar will be the default date selected in the DateTimePicker).

Add Task requires a large amount of information to function properly:

  • an array of ids of all pets the logged-in user owns (ownedPetIds)
  • an array of the ids of all caretakers for the user's pets (caretakersIds)
  • an array of all the ids of all co-Owners of the user's pets (future feature: coOwnersIds)

For proper rending of components, these ids need extra information, so new arrays are made after obtaining further information from Firestore:

  • ownedPets, an array of arrays; each nested array holds two elements: petName and id
  • allAssociatedUsers, a combined array of the logged-in user, co-owners, and caretakers; each nested object has keys firstName, id, lastName

Firebase calls are made accordingly to obtain this data. Each call is currently made in its own useEffect. This allows logic to be lumped together for easier understanding. Calls that require data from other calls (finding pet names for each pet Id and finding first and last names for all associated user ids) must be made after those previous calls have been completed and the setState has finished updating; thus the call to get pet names must be made in a separate useEffect from the call that collected the petIds from the logged-in user's data (etc).

It is of note that all await calls in the same async function are called synchronously with each other. It is also noteworthy that useEffect cannot itself be an async function, so async functions within useEffect are made by first defining the async function, and then immediately invoking it. setSate functions (setCoOwnersIds, for example) will occur after the promise from the await has completed, and after the end of the useEffect.

In addition, to create a new task, the following states need to be filled:

  • entityText (description of task)
  • dueDate (date task is due)
  • entityDueTime (time task is due)
  • entityStatus (open, as it has not yet been completed--future feature)
  • entityFrequency (future feature)
  • userId (array of userIds to whom task will be assigned)
  • petName (name of pet task is for)
  • petId (id of pet task is for)

When submitting to Firebase, all fields are reset so that the form is empty, with time being reset to 12:00pm by default.

In order for recently added pets to be displayed in the radio button, a Firestore check must be made.

Future features include utilizing local storage to reduce the number Firestore calls, to improve scalability of the project.

To prevent memory leaks, each useEffect contains a return statement to unmount the component, which prevents undesired extra Firestore calls.