* chore: rename APIs and Microservices to include "Backend" (#42515) * fix typo * fix typo * undo change * Corrected grammar mistake Corrected a grammar mistake by removing a comma. * change APIs and Microservices cert title * update title * Change APIs and Microservices certi title * Update translations.json * update title * feat(curriculum): rename apis and microservices cert * rename folder structure * rename certificate * rename learn Markdown * apis-and-microservices -> back-end-development-and-apis * update backend meta * update i18n langs and cypress test Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * fix: add development to front-end libraries (#42512) * fix: added-the-word-Development-to-front-end-libraries * fix/added-the-word-Development-to-front-end-libraries * fix/added-word-development-to-front-end-libraries-in-other-related-files * fix/added-the-word-Development-to-front-end-and-all-related-files * fix/removed-typos-from-last-commit-in-index.md * fix/reverted-changes-that-i-made-to-dependecies * fix/removed xvfg * fix/reverted changes that i made to package.json * remove unwanted changes * front-end-development-libraries changes * rename backend certSlug and README * update i18n folder names and keys * test: add legacy path redirect tests This uses serve.json from the client-config repo, since we currently use that in production * fix: create public dir before moving serve.json * fix: add missing script * refactor: collect redirect tests * test: convert to cy.location for stricter tests * rename certificate folder to 00-certificates * change crowdin config to recognise new certificates location * allow translations to be used Co-authored-by: Nicholas Carrigan (he/him) <nhcarrigan@gmail.com> * add forwards slashes to path redirects * fix cypress path tests again * plese cypress * fix: test different challenge Okay so I literally have no idea why this one particular challenge fails in Cypress Firefox ONLY. Tom and I paired and spun a full build instance and confirmed in Firefox the page loads and redirects as expected. Changing to another bootstrap challenge passes Cypress firefox locally. Absolutely boggled by this. AAAAAAAAAAAAAAA * fix: separate the test Okay apparently the test does not work unless we separate it into a different `it` statement. >:( >:( >:( >:( Co-authored-by: Sujal Gupta <55016909+heysujal@users.noreply.github.com> Co-authored-by: Noor Fakhry <65724923+NoorFakhry@users.noreply.github.com> Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> Co-authored-by: Nicholas Carrigan (he/him) <nhcarrigan@gmail.com>
153 lines
4.2 KiB
Markdown
153 lines
4.2 KiB
Markdown
---
|
|
id: 5a24c314108439a4d4036151
|
|
title: Usare un'istruzione switch per gestire più azioni
|
|
challengeType: 6
|
|
forumTopicId: 301449
|
|
dashedName: use-a-switch-statement-to-handle-multiple-actions
|
|
---
|
|
|
|
# --description--
|
|
|
|
È possibile dire allo store di Redux come gestire diversi tipi di azione. Diciamo che tu debba gestire l'autenticazione dell'utente nel tuo store di Redux. Vuoi avere una rappresentazione dello stato per quando gli utenti sono connessi e per quando sono disconnessi. Lo rappresenti con un singolo oggetto di stato con la proprietà `authenticated`. Hai anche bisogno di creatori di azione che creino azioni corrispondenti al login e al logout dell'utente, insieme agli oggetti azione stessi.
|
|
|
|
# --instructions--
|
|
|
|
L'editor di codice ha già uno store, delle azioni e dei creatori di azioni configurati per te. Completa la funzione `reducer` perché gestisca più azioni di autenticazione. Usa un'istruzione JavaScript `switch` nel `reducer` per rispondere a diversi eventi di azione. Questo è un modello standard nella scrittura di reducer Redux. L'istruzione switch dovrebbe smistare in base a `action.type` e restituire lo stato di autenticazione appropriato.
|
|
|
|
**Nota:** Non preoccuparti per l'immutabilità dello stato, dato che è piccolo e semplice in questo esempio. Per ogni azione, puoi restituire un nuovo oggetto — per esempio, `{authenticated: true}`. Inoltre, non dimenticare di scrivere un caso di `default` nella tua istruzione switch che restituisca lo `state` corrente. Questo è importante perché una volta che l'app ha più reducer, essi vengono eseguiti tutti ogni volta che viene spedita un'azione, anche quando l'azione non è correlata a quel reducer. In questo caso, vuoi assicurarti di restituire lo `state` corrente.
|
|
|
|
# --hints--
|
|
|
|
Chiamando la funzione `loginUser` dovrebbe essere restituito un oggetto con proprietà type impostata alla stringa `LOGIN`.
|
|
|
|
```js
|
|
assert(loginUser().type === 'LOGIN');
|
|
```
|
|
|
|
Chiamando la funzione `logoutUser` dovrebbe essere restituito un oggetto con proprietà type impostata alla stringa `LOGOUT`.
|
|
|
|
```js
|
|
assert(logoutUser().type === 'LOGOUT');
|
|
```
|
|
|
|
Lo store dovrebbe essere inizializzato con un oggetto con una proprietà `authenticated` impostata a `false`.
|
|
|
|
```js
|
|
assert(store.getState().authenticated === false);
|
|
```
|
|
|
|
L'invio di `loginUser` dovrebbe impostare a `true` la proprietà `authenticated` nello stato dello store.
|
|
|
|
```js
|
|
assert(
|
|
(function () {
|
|
const initialState = store.getState();
|
|
store.dispatch(loginUser());
|
|
const afterLogin = store.getState();
|
|
return (
|
|
initialState.authenticated === false && afterLogin.authenticated === true
|
|
);
|
|
})()
|
|
);
|
|
```
|
|
|
|
La spedizione di `logoutUser` dovrebbe impostare a `false` la proprietà `authenticated` nello stato dello store.
|
|
|
|
```js
|
|
assert(
|
|
(function () {
|
|
store.dispatch(loginUser());
|
|
const loggedIn = store.getState();
|
|
store.dispatch(logoutUser());
|
|
const afterLogout = store.getState();
|
|
return (
|
|
loggedIn.authenticated === true && afterLogout.authenticated === false
|
|
);
|
|
})()
|
|
);
|
|
```
|
|
|
|
La funzione `authReducer` dovrebbe gestire più tipi di azione con un'istruzione `switch`.
|
|
|
|
```js
|
|
(getUserInput) =>
|
|
assert(
|
|
getUserInput('index').toString().includes('switch') &&
|
|
getUserInput('index').toString().includes('case') &&
|
|
getUserInput('index').toString().includes('default')
|
|
);
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
const defaultState = {
|
|
authenticated: false
|
|
};
|
|
|
|
const authReducer = (state = defaultState, action) => {
|
|
// Change code below this line
|
|
|
|
// Change code above this line
|
|
};
|
|
|
|
const store = Redux.createStore(authReducer);
|
|
|
|
const loginUser = () => {
|
|
return {
|
|
type: 'LOGIN'
|
|
}
|
|
};
|
|
|
|
const logoutUser = () => {
|
|
return {
|
|
type: 'LOGOUT'
|
|
}
|
|
};
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
const defaultState = {
|
|
authenticated: false
|
|
};
|
|
|
|
const authReducer = (state = defaultState, action) => {
|
|
|
|
switch (action.type) {
|
|
|
|
case 'LOGIN':
|
|
return {
|
|
authenticated: true
|
|
}
|
|
|
|
case 'LOGOUT':
|
|
return {
|
|
authenticated: false
|
|
}
|
|
|
|
default:
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const store = Redux.createStore(authReducer);
|
|
|
|
const loginUser = () => {
|
|
return {
|
|
type: 'LOGIN'
|
|
}
|
|
};
|
|
|
|
const logoutUser = () => {
|
|
return {
|
|
type: 'LOGOUT'
|
|
}
|
|
};
|
|
```
|