4IT580: Docs
4IT580 WebGithub

2nd Homework

Goal of this homework is create new page in React, connect it to GraphQL, and make it interactive while using clean code practices.

You can see example of working implementation here.

There are 6 steps:

Deadline: Monday 11/10/2020 - 23:59:59

If you have any problems contact your teacher using MS Teams: Tomas Horacek (@hort19).

Step 1: Create Initial Version of Practical03 Page

Create Practical03.js

Update Routes.js

Test That it is Working

Step 2: Display List of Quacks

Handle Special States

Error and loading states are sometimes called "special states".

Display Quack Texts

Use Snippet To Display Quacks

Replace this:

with this:

Connect name and userName in Quack

Step 3: Add "Select" Functionality

Edit Practical03 component so it support selecting one quack.

You can see example of working implementation here by clicking on Select buttons.

Add useState

Display Selection

You have to replace false and true in code above with:

Now you should be able to click on different quacks and select them

Step 4: Clean up Code (Atoms, Molecules, Templates)

Now let's clean up Practical03.js in four steps:

Step 4.1: Create QuackText Atom

children is a special prop. Anything that you wrap inside <QuackText>...</QuackText> will be passed to QuackText in this children prop.

Use QuackText in Practical03.js

In Practical03 component replace this:

with this:

Step 4.2: Create SelectableQuackWrapper Atom

Repeat the same steps as in QuackText for SelectableQuackWrapper.

Replace the selectable <div> (that one with changing background color) with new atom SelectableQuackWrapper:

It should look like this:

Step 4.3: Create SelectableQuack Molecule

Repeat similar steps for SelectableQuack. Difference is that this time you are creating molecule inside molecules folder.

Open frontend/src/pages/Practical03.js:

Step 4.4: Create Practical03Template Template

Open Practical03.js and import Practical03Template:

Use Practical03Template in Practical03 component

Good job!

Code should now look clean!

Step 5: Add Auto-Refetch Functionality

Refetch GraphQL Query

You can call quacksState.refetch() to re-fetch data from backend.

It can be used for example like this:

We will use it for refetching data every 5 seconds after data finished fetching.

useEffect

For this we have to know when data finished loading.

And we can use useEffect hook for this:

useEffect takes two arguments:

So useEffect(callback, [quacksState.loading]) means that callback will be called when there is and update to quacksState.loading


If we put it together:

This means that alert() called whenever loading is changed to false.

setTimeout

We can use JavaScript global function setTimeout to call some code after delay time.

It takes two arguments:

This code:

will call quacksState.refetch() after 5 seconds.

clearTimeout

clearTimeout is global JavaScript function that will cancel previously created timeout.

This is good, bucause user can leave the page before those 5 seconds will pass.
If we would not cancel the timeout it would call quacksState.refetch() for page that does not exist.
And this is bad!

We can use clearTimeout like this:

useEffect cleanup callback

useEffect hook have support for creating cleanup callbacks.

In simple case the "cleanup callback" is called when component unmounts (e.g. user navigates to different page).

There is a bit more to "cleanup callback", but for now you can think about it this way:

Correct usage of setTimeout in React would be this:

Put it All Together

Use useEffect to:

Step 6: Send a Link to Working App Page

Once you are finished:

Deadline: Monday 11/10/2020 - 23:59:59

Happy Coding!