Skip to content

Commit dc832e4

Browse files
committed
vite, parts 5a and b
1 parent 2d04ad1 commit dc832e4

File tree

12 files changed

+154
-220
lines changed

12 files changed

+154
-220
lines changed

src/content/13/en/part13a.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,9 @@ In the tasks of this section, we will build a blog application backend similar t
600600

601601
#### Task 13.1.
602602

603-
Create a GitHub repository for the application and create a new Heroku or Fly.io application for it, as well as a Postgres database. Make sure you are able to establish a connection to the application database.
603+
Create a GitHub repository for the application and create a new Fly.io or Heroku application for it, as well as a Postgres database. As mentioned [here](/en/part13/using_relational_databases_with_sequelize#application-database) you might set up your database also somewhere else, and in that case the Fly.io of Heroku app is not needed.
604+
605+
Make sure you are able to establish a connection to the application database.
604606

605607
#### Task 13.2.
606608

src/content/13/fi/osa13a.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ Teemme tämän osan tehtävissä [osan 4](/osa4) tehtävien kanssa samanlaisen b
572572
573573
#### Tehtävä 13.1.
574574
575-
Tee sovellukselle GitHub-repositorio ja luo sen sisällä sovellusta varten Heroku-sovellus sekä Postgres-tietokanta. Varmista, että saat luotua yhteyden sovelluksen tietokantaan.
575+
Tee sovellukselle GitHub-repositorio ja luo sen sisällä sovellusta varten Fly.io tai Heroku-sovellus sekä Postgres-tietokanta. Varmista, että saat luotua yhteyden sovelluksen tietokantaan. Kuten [täällä](/osa13/relaatiotietokannan_kaytto_sequelize_kirjastolla#sovelluksen-tietokanta) mainittiin, voit hoitaa tietokannan myös esim. Dockerin avulla, tällöin et tarvitse Fly.io- tai Heroku-sovellusta.
576576
577577
#### Tehtävä 13.2.
578578

src/content/2/en/part2c.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ Now we can see more clearly that the function [useEffect](https://react.dev/refe
473473
474474
So by default, the effect is <i>always</i> run after the component has been rendered. In our case, however, we only want to execute the effect along with the first render.
475475

476-
The second parameter of <em>useEffect</em> is used to [specify how often the effect is run](https://reactjs.org/docs/hooks-reference.html#conditionally-firing-an-effect). If the second parameter is an empty array <em>[]</em>, then the effect is only run along with the first render of the component.
476+
The second parameter of <em>useEffect</em> is used to [specify how often the effect is run]https://react.dev/reference/react/useEffect#parameters). If the second parameter is an empty array <em>[]</em>, then the effect is only run along with the first render of the component.
477477

478478
There are many possible use cases for an effect hook other than fetching data from the server. However, this use is sufficient for us, for now.
479479

src/content/2/fi/osa2c.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ Nyt huomaamme selvemmin, että funktiolle [useEffect]((https://react.dev/referen
471471
472472
Eli oletusarvoisesti efekti suoritetaan <i>aina</i> sen jälkeen, kun komponentti renderöidään. Meidän tapauksessamme haluamme suorittaa efektin vain ensimmäisen renderöinnin yhteydessä.
473473

474-
Funktion <em>useEffect</em> toista parametria käytetään [tarkentamaan sitä, miten usein efekti suoritetaan](https://reactjs.org/docs/hooks-reference.html#conditionally-firing-an-effect). Jos toisena parametrina on tyhjä taulukko <em>[]</em>, suoritetaan efekti ainoastaan komponentin ensimmäisen renderöinnin jälkeen.
474+
Funktion <em>useEffect</em> toista parametria käytetään [tarkentamaan sitä, miten usein efekti suoritetaan](https://react.dev/reference/react/useEffect#parameters). Jos toisena parametrina on tyhjä taulukko <em>[]</em>, suoritetaan efekti ainoastaan komponentin ensimmäisen renderöinnin jälkeen.
475475

476476
Effect hookien avulla on mahdollisuus tehdä paljon muutakin kuin hakea dataa palvelimelta, mutta tämä riittää meille tässä vaiheessa.
477477

src/content/5/en/part5a.md

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ const App = () => {
8686
export default App
8787
```
8888

89-
The current application code can be found on [Github](https://github.com/fullstack-hy2020/part2-notes/tree/part5-1), branch <i>part5-1</i>. If you clone the repo, don't forget to run _npm install_ before attempting to run the frontend.
89+
The current application code can be found on [Github](https://github.com/fullstack-hy2020/part2-notes-frontend/tree/part5-1), branch <i>part5-1</i>. If you clone the repo, don't forget to run _npm install_ before attempting to run the frontend.
9090

9191
The frontend will not display any notes if it's not connected to the backend. You can start the backend with _npm run dev_ in its folder from Part 4. This will run the backend on port 3001. While that is active, in a separate terminal window you can start the frontend with _npm start_, and now you can see the notes that are saved in your MongoDB database from Part 4.
9292

@@ -117,17 +117,6 @@ const login = async credentials => {
117117
export default { login }
118118
```
119119

120-
If you have installed the eslint plugin in VS Code, you may now see the following warning:
121-
122-
![vs code warning - assign object to a variable before exporting as module default](../../images/5/50new.png)
123-
124-
We'll get back to configuring eslint in a moment. You can ignore the error for the time being or suppress it by adding the following to the line before the warning:
125-
126-
```js
127-
// eslint-disable-next-line import/no-anonymous-default-export
128-
export default { login }
129-
```
130-
131120
The method for handling the login can be implemented as follows:
132121

133122
```js
@@ -262,7 +251,7 @@ const App = () => {
262251
}
263252
```
264253

265-
A slightly odd looking, but commonly used [React trick](https://reactjs.org/docs/conditional-rendering.html#inline-if-with-logical--operator) is used to render the forms conditionally:
254+
A slightly odd looking, but commonly used [React trick](https://react.dev/learn/conditional-rendering#logical-and-operator-) is used to render the forms conditionally:
266255

267256
```js
268257
{
@@ -324,7 +313,7 @@ The solution isn't perfect, but we'll leave it for now.
324313

325314
Our main component <i>App</i> is at the moment way too large. The changes we did now are a clear sign that the forms should be refactored into their own components. However, we will leave that for an optional exercise.
326315

327-
The current application code can be found on [GitHub](https://github.com/fullstack-hy2020/part2-notes/tree/part5-2), branch <i>part5-2</i>.
316+
The current application code can be found on [GitHub](https://github.com/fullstack-hy2020/part2-notes-frontend/tree/part5-2), branch <i>part5-2</i>.
328317

329318
### Creating new notes
330319

@@ -384,7 +373,6 @@ const update = (id, newObject) => {
384373
return request.then(response => response.data)
385374
}
386375

387-
// eslint-disable-next-line import/no-anonymous-default-export
388376
export default { getAll, create, update, setToken } // highlight-line
389377
```
390378

@@ -473,7 +461,7 @@ You can also inspect the local storage using the developer tools. On Chrome, go
473461

474462
We still have to modify our application so that when we enter the page, the application checks if user details of a logged-in user can already be found on the local storage. If they can, the details are saved to the state of the application and to <i>noteService</i>.
475463

476-
The right way to do this is with an [effect hook](https://reactjs.org/docs/hooks-effect.html): a mechanism we first encountered in [part 2](/en/part2/getting_data_from_server#effect-hooks), and used to fetch notes from the server.
464+
The right way to do this is with an [effect hook](https://react.dev/reference/react/useEffect): a mechanism we first encountered in [part 2](/en/part2/getting_data_from_server#effect-hooks), and used to fetch notes from the server.
477465

478466
We can have multiple effect hooks, so let's create a second one to handle the first loading of the page:
479467

@@ -509,7 +497,7 @@ const App = () => {
509497
}
510498
```
511499

512-
The empty array as the parameter of the effect ensures that the effect is executed only when the component is rendered [for the first time](https://reactjs.org/docs/hooks-reference.html#conditionally-firing-an-effect).
500+
The empty array as the parameter of the effect ensures that the effect is executed only when the component is rendered [for the first time](https://react.dev/reference/react/useEffect#parameters).
513501

514502
Now a user stays logged in to the application forever. We should probably add a <i>logout</i> functionality, which removes the login details from the local storage. We will however leave it as an exercise.
515503

@@ -526,7 +514,7 @@ or with the command which empties <i>localstorage</i> completely:
526514
window.localStorage.clear()
527515
```
528516

529-
The current application code can be found on [GitHub](https://github.com/fullstack-hy2020/part2-notes/tree/part5-3), branch <i>part5-3</i>.
517+
The current application code can be found on [GitHub](https://github.com/fullstack-hy2020/part2-notes-frontend/tree/part5-3), branch <i>part5-3</i>.
530518

531519
</div>
532520

@@ -640,7 +628,7 @@ At the [end](/en/part4/token_authentication#problems-of-token-based-authenticati
640628

641629
There are two solutions to the problem. The first one is to limit the validity period of a token. This forces the user to re-login to the app once the token has expired. The other approach is to save the validity information of each token to the backend database. This solution is often called a <i>server-side session</i>.
642630

643-
No matter how the validity of tokens is checked and ensured, saving a token in the local storage might contain a security risk if the application has a security vulnerability that allows [Cross Site Scripting (XSS)](https://owasp.org/www-community/attacks/xss/) attacks. An XSS attack is possible if the application would allow a user to inject arbitrary JavaScript code (e.g. using a form) that the app would then execute. When using React sensibly it should not be possible since [React sanitizes](https://reactjs.org/docs/introducing-jsx.html#jsx-prevents-injection-attacks) all text that it renders, meaning that it is not executing the rendered content as JavaScript.
631+
No matter how the validity of tokens is checked and ensured, saving a token in the local storage might contain a security risk if the application has a security vulnerability that allows [Cross Site Scripting (XSS)](https://owasp.org/www-community/attacks/xss/) attacks. An XSS attack is possible if the application would allow a user to inject arbitrary JavaScript code (e.g. using a form) that the app would then execute. When using React sensibly it should not be possible since [React sanitizes](https://legacy.reactjs.org/docs/introducing-jsx.html#jsx-prevents-injection-attacks) all text that it renders, meaning that it is not executing the rendered content as JavaScript.
644632

645633
If one wants to play safe, the best option is to not store a token in local storage. This might be an option in situations where leaking a token might have tragic consequences.
646634

src/content/5/en/part5b.md

Lines changed: 59 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ Next let's define the form component inside of a <i>Togglable</i> component:
271271
</Togglable>
272272
```
273273

274-
You can find the code for our current application in its entirety in the <i>part5-4</i> branch of [this GitHub repository](https://github.com/fullstack-hy2020/part2-notes/tree/part5-4).
274+
You can find the code for our current application in its entirety in the <i>part5-4</i> branch of [this GitHub repository](https://github.com/fullstack-hy2020/part2-notes-frontend/tree/part5-4).
275275

276276
### State of the forms
277277

@@ -352,7 +352,7 @@ const App = () => {
352352

353353
We could do the same for the log in form, but we'll leave that for an optional exercise.
354354

355-
The application code can be found on [GitHub](https://github.com/fullstack-hy2020/part2-notes/tree/part5-5),
355+
The application code can be found on [GitHub](https://github.com/fullstack-hy2020/part2-notes-frontend/tree/part5-5),
356356
branch <i>part5-5</i>.
357357

358358
### References to components with ref
@@ -450,7 +450,7 @@ This trick works for changing the state of a component, but it looks a bit unple
450450

451451
There are also [other use cases](https://react.dev/learn/manipulating-the-dom-with-refs) for refs than accessing React components.
452452

453-
You can find the code for our current application in its entirety in the <i>part5-6</i> branch of [this GitHub repository](https://github.com/fullstack-hy2020/part2-notes/tree/part5-6).
453+
You can find the code for our current application in its entirety in the <i>part5-6</i> branch of [this GitHub repository](https://github.com/fullstack-hy2020/part2-notes-frontend/tree/part5-6).
454454

455455
### One point about components
456456

@@ -705,74 +705,65 @@ If the type of a passed prop is wrong, e.g. if we try to define the <i>handleSub
705705

706706
In part 3 we configured the [ESlint](/en/part3/validation_and_es_lint#lint) code style tool to the backend. Let's take ESlint to use in the frontend as well.
707707

708-
Create-react-app has installed ESlint to the project by default, so all that's left for us to do is define our desired configuration in the <i>.eslintrc.js</i> file.
709-
710-
*NB:* do not run the _eslint --init_ command. It will install the latest version of ESlint that is not compatible with the configuration file created by create-react-app!
708+
Vite has installed ESlint to the project by default, so all that's left for us to do is define our desired configuration in the <i>.eslintrc.cjs</i> file.
711709

712710
Next, we will start testing the frontend and in order to avoid undesired and irrelevant linter errors we will install the [eslint-plugin-jest](https://www.npmjs.com/package/eslint-plugin-jest) package:
713711

714712
```bash
715713
npm install --save-dev eslint-plugin-jest
716714
```
717715

718-
Let's create a <i>.eslintrc.js</i> file with the following contents:
716+
Let's create a <i>.eslintrc.cjs</i> file with the following contents:
719717

720718
```js
721-
/* eslint-env node */
722719
module.exports = {
723-
"env": {
724-
"browser": true,
725-
"es6": true,
726-
"jest/globals": true
727-
},
728-
"extends": [
729-
"eslint:recommended",
730-
"plugin:react/recommended"
731-
],
732-
"parserOptions": {
733-
"ecmaFeatures": {
734-
"jsx": true
735-
},
736-
"ecmaVersion": 2018,
737-
"sourceType": "module"
720+
root: true,
721+
env: {
722+
browser: true,
723+
es2020: true,
724+
"jest/globals": true
738725
},
739-
"plugins": [
740-
"react", "jest"
726+
extends: [
727+
'eslint:recommended',
728+
'plugin:react/recommended',
729+
'plugin:react/jsx-runtime',
730+
'plugin:react-hooks/recommended',
741731
],
742-
"rules": {
743-
"indent": [
744-
"error",
745-
2
746-
],
747-
"linebreak-style": [
748-
"error",
749-
"unix"
750-
],
751-
"quotes": [
752-
"error",
753-
"single"
754-
],
755-
"semi": [
756-
"error",
757-
"never"
758-
],
759-
"eqeqeq": "error",
760-
"no-trailing-spaces": "error",
761-
"object-curly-spacing": [
762-
"error", "always"
763-
],
764-
"arrow-spacing": [
765-
"error", { "before": true, "after": true }
766-
],
767-
"no-console": 0,
768-
"react/prop-types": 0,
769-
"react/react-in-jsx-scope": "off"
732+
ignorePatterns: ['dist', '.eslintrc.cjs'],
733+
parserOptions: { ecmaVersion: 'latest', sourceType: 'module' },
734+
settings: { react: { version: '18.2' } },
735+
plugins: ['react-refresh', 'jest'],
736+
rules: {
737+
"indent": [
738+
"error",
739+
2
740+
],
741+
"linebreak-style": [
742+
"error",
743+
"unix"
744+
],
745+
"quotes": [
746+
"error",
747+
"single"
748+
],
749+
"semi": [
750+
"error",
751+
"never"
752+
],
753+
"eqeqeq": "error",
754+
"no-trailing-spaces": "error",
755+
"object-curly-spacing": [
756+
"error", "always"
757+
],
758+
"arrow-spacing": [
759+
"error", { "before": true, "after": true }
760+
],
761+
"no-console": 0,
762+
"react/prop-types": 0,
763+
"react/react-in-jsx-scope": "off",
764+
"react/prop-types": 0,
765+
"no-unused-vars": 0
770766
},
771-
"settings": {
772-
"react": {
773-
"version": "detect"
774-
}
775-
}
776767
}
777768
```
778769

@@ -782,29 +773,20 @@ Let's create [.eslintignore](https://eslint.org/docs/user-guide/configuring#igno
782773

783774
```bash
784775
node_modules
785-
build
786-
.eslintrc.js
776+
dist
777+
.eslintrc.cjs
787778
```
788779

789-
Now the directories <em>build</em> and <em>node_modules</em> will be skipped when linting.
780+
Now the directories <em>dist</em> and <em>node_modules</em> will be skipped when linting.
790781

791-
Let us also create an npm script to run the lint:
782+
As usual, you can perform the linting either from the command line with the command
792783

793-
```js
794-
{
795-
// ...
796-
{
797-
"scripts": {
798-
"start": "react-scripts start",
799-
"build": "react-scripts build",
800-
"test": "react-scripts test",
801-
"eject": "react-scripts eject",
802-
"eslint": "eslint ." // highlight-line
803-
},
804-
// ...
805-
}
784+
```bash
785+
npm run Lint
806786
```
807787

788+
or using the editor's Eslint plugin.
789+
808790
Component _Togglable_ causes a nasty-looking warning <i>Component definition is missing display name</i>:
809791

810792
![vscode showing component definition error](../../images/5/25x.png)
@@ -828,10 +810,7 @@ Togglable.displayName = 'Togglable' // highlight-line
828810
export default Togglable
829811
```
830812

831-
You can find the code for our current application in its entirety in the <i>part5-7</i> branch of [this GitHub repository](https://github.com/fullstack-hy2020/part2-notes/tree/part5-7).
832-
833-
Note that create-react-app has also a [default ESLint-configuration](https://www.npmjs.com/package/eslint-config-react-app), that we have now overridden. [The documentation](https://create-react-app.dev/docs/setting-up-your-editor/#extending-or-replacing-the-default-eslint-config) mentions that it is ok to replace the default but does not encourage us to do so:
834-
<i>We highly recommend extending the base config, as removing it could introduce hard-to-find issues</i>.
813+
You can find the code for our current application in its entirety in the <i>part5-7</i> branch of [this GitHub repository](https://github.com/fullstack-hy2020/part2-notes-frontend/tree/part5-7).
835814

836815
</div>
837816

@@ -843,8 +822,6 @@ Note that create-react-app has also a [default ESLint-configuration](https://www
843822

844823
Define PropTypes for one of the components of your application, and add ESlint to the project. Define the configuration according to your liking. Fix all of the linter errors.
845824

846-
Create-react-app has installed ESlint to the project by default, so all that's left for you to do is define your desired configuration in the <i>.eslintrc.js</i> file.
847-
848-
*NB:* do not run the _eslint --init_ command. It will install the latest version of ESlint that is not compatible with the configuration file created by create-react-app!
825+
Vite has installed ESlint to the project by default, so all that's left for you to do is define your desired configuration in the <i>.eslintrc.cjs</i> file.
849826

850827
</div>

src/content/5/en/part5c.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const Note = ({ note, toggleImportance }) => {
3838
}
3939
```
4040

41-
Notice that the <i>li</i> element has the [CSS](https://reactjs.org/docs/dom-elements.html#classname) classname <i>note</i>, that could be used to access the component in our tests.
41+
Notice that the <i>li</i> element has the [CSS](https://react.dev/learn#adding-styles) classname <i>note</i>, that could be used to access the component in our tests.
4242

4343
### Rendering the component for tests
4444

@@ -720,7 +720,7 @@ The report will tell us the lines of untested code in each component:
720720

721721
![HTML report of the test coverage](../../images/5/19ea.png)
722722

723-
You can find the code for our current application in its entirety in the <i>part5-8</i> branch of [this GitHub repository](https://github.com/fullstack-hy2020/part2-notes/tree/part5-8).
723+
You can find the code for our current application in its entirety in the <i>part5-8</i> branch of [this GitHub repository](https://github.com/fullstack-hy2020/part2-notes-frontend/tree/part5-8).
724724
</div>
725725

726726
<div class="tasks">

0 commit comments

Comments
 (0)