- The app starts on the "activities" page.
- The four possible pages of App are activities, register, login, and 404.
<Layout>allows all pages in the app to share the same<Navbar>.<Navbar>navigates users to different pages viasetPage, which changes the page provided fromPageContext.<Navbar>is getting the token via theuseAuthhook, which is exported fromAuthContext.- If the user is logged in,
<Navbar>renders a link to log out. - If the user is not logged in,
<Navbar>renders links to register and log in.
-
AuthContextkeeps a token in state and provides the token as well as functions to register, log in, or log out. -
registermakes a request toPOST /users/register. -
If the request is successful, the token in state is updated using the response from the API.
-
They are essentially the same except
loginmakes a request toPOST /users/logininstead. -
When a user logs out, the token is removed from state.
-
The user is redirected to the "activities" page after they successfully register.
-
The user is not redirected if there is an error; instead, the error message is rendered onto the page.
-
They are essentially the same except for slight differences in text, and they call the corresponding
registerorloginfunction.
-
The
datastate variable stores the response of querying the API. -
Loadingrepresents whether we are still waiting to get the response back from the API.Errorstarts as null, but is set if something goes wrong with the API request. -
In the
finallyblock,loadingis set tofalse. -
The
useEffectruns only once because its dependency is an empty array. It will call two functions:provideTagandquery. -
provideTagandrequestare both coming from theuseApihook, which is imported fromApiContext. -
The
useQueryhook returns three state variables: the data, whether the request is loading, and the error if there was one. -
useMutationadditionally takes amethod. It also takestagsToInvalidateinstead oftag.- The
requestinvolves setting themethodand sending abody, but is otherwise the same. useMutationreturns almost the same thing asuseQuery, but rather than making the API call automatically viauseEffect, it returns amutatefunction which will request the API when it is called.
-
ApiContextuses the token fromAuthContextto set the "Authorization" header if the token exists. -
requestwill check if the content type of the response is JSON, and will only parse the response as JSON if so. -
If the API call succeeds, then
requestwill return either the parsed JSON data orundefined. If the API call fails, then nothing will be returned; instead,requestwill throw an error. -
Defining
requestinApiContextallowsuseQueryanduseMutationto share the logic around making afetchcall and parsing the response. It also allows them both to "automatically" use the same token fromAuthContextin the request headers without needing to manually repeat that logic. -
The initial value of
tagsis an empty object. -
useQueryprovides two things: a tag and the definedqueryfunction. -
provideTagadds an entry into thetagsobject where thetagis the key, and thequeryfunction is the value. -
useMutationpasses an array oftagsToInvalidateas the argument intoinvalidateTags. -
invalidateTagswill loop through all of the tags intagsToInvalidateand try to find the corresponding query function. If found, it will call that query function, which will make a new request to the API and update the state inuseQueryaccordingly.