|
1 | | -This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). |
| 1 | +<img src="https://i.imgur.com/wMoRpTr.jpg"> |
2 | 2 |
|
3 | | -## Available Scripts |
| 3 | +# Mastermind Settings Lab |
4 | 4 |
|
5 | | -In the project directory, you can run: |
| 5 | +--- |
6 | 6 |
|
7 | | -### `npm start` |
| 7 | +## Intro |
8 | 8 |
|
9 | | -Runs the app in the development mode.<br> |
10 | | -Open [http://localhost:3000](http://localhost:3000) to view it in the browser. |
| 9 | +In the lesson earlier you: |
11 | 10 |
|
12 | | -The page will reload if you make edits.<br> |
13 | | -You will also see any lint errors in the console. |
| 11 | +1. Learned how to use React Router to perform client-side routing. |
14 | 12 |
|
15 | | -### `npm test` |
| 13 | +2. Refactored the react-mastermind app to render a `<GamePage>` component at the root route. |
16 | 14 |
|
17 | | -Launches the test runner in the interactive watch mode.<br> |
18 | | -See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. |
| 15 | +3. Added a "Difficulty" `<Link>` to the `<GamePage>` used to navigate to a `<SettingsPage>` component. |
19 | 16 |
|
20 | | -### `npm run build` |
| 17 | +3. As a practice exercise, added an additional `<Route>` with a path of `/settings`. |
21 | 18 |
|
22 | | -Builds the app for production to the `build` folder.<br> |
23 | | -It correctly bundles React in production mode and optimizes the build for the best performance. |
| 19 | +4. Created a minimal `<SettingsPage>` component that included a "HOME" `<Link>`. |
24 | 20 |
|
25 | | -The build is minified and the filenames include the hashes.<br> |
26 | | -Your app is ready to be deployed! |
| 21 | +In this lab, you'll continue to have fun building out react-mastermind using what you know about components, state, props, styling, methods, event handlers, routing and of course, JavaScript. |
27 | 22 |
|
28 | | -See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. |
| 23 | +## Set Up |
29 | 24 |
|
30 | | -### `npm run eject` |
| 25 | +The starter code for this lab is the same as the completed code from the _React Router_ lesson. |
31 | 26 |
|
32 | | -**Note: this is a one-way operation. Once you `eject`, you can’t go back!** |
| 27 | +To get set up for this lesson: |
33 | 28 |
|
34 | | -If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. |
| 29 | +- `cd` into the folder for this lab. |
| 30 | +- Install the Node modules: `$ npm i` |
| 31 | +- Open the project in VS Code: `$ code .` |
| 32 | +- Open a terminal in VS Code (`ctrl + backtick`) |
| 33 | +- Start the React Dev Server: `$ npm start` |
35 | 34 |
|
36 | | -Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. |
| 35 | +After the server starts up, you should see the following in the browser at `localhost:3000`: |
37 | 36 |
|
38 | | -You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. |
| 37 | +<img src="https://i.imgur.com/ibMTm9k.png"> |
39 | 38 |
|
40 | | -## Learn More |
| 39 | +## Exercises |
41 | 40 |
|
42 | | -You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). |
| 41 | +In this lab, you'll be adding the "Difficulty" setting functionality by completing the exercises below. |
43 | 42 |
|
44 | | -To learn React, check out the [React documentation](https://reactjs.org/). |
| 43 | +When completed, clicking the "Difficulty" link (styled as a button) will display the following: |
45 | 44 |
|
46 | | -### Code Splitting |
| 45 | +<img src="https://i.imgur.com/gFjNSt0.png"> |
47 | 46 |
|
48 | | -This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting |
| 47 | +As you can see, the settings page allows the player to change the difficulty level by selecting the number of colors available to choose from! |
49 | 48 |
|
50 | | -### Analyzing the Bundle Size |
| 49 | +1. Currently, the `<GamePage>` component is relying on CSS classes defined in **App.css**. Refactor to cure this inappropriateness by copying the classes in **App.css** over to **GamePage.css** created during the lesson. Update the class names and update **GamePage.jsx** as required to use those class names. |
51 | 50 |
|
52 | | -This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size |
| 51 | +2. Since both `<App>` & `<GamePage>` rely on a `*-header-footer` class with the same styling, refactor by renaming it to `header-footer` and putting it in **index.css** instead. Update **App.js** & **GamePage.jsx** to use `header-footer`, then you can delete `GamePage-header-footer` from **GamePage.css** and all of the CSS in **App.css**. |
53 | 52 |
|
54 | | -### Making a Progressive Web App |
| 53 | +3. There will be three levels of difficulty: 'Easy'; 'Moderate'; and 'Difficult'. The game's difficulty will be held in a state property named `difficulty`. Add the `difficulty` property to `state` and initialize it with a value of `'Easy'`. However, `difficulty` should not be "reset" if the player clicks the **[New Game]** button. |
55 | 54 |
|
56 | | -This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app |
| 55 | + Hint: Not resetting `difficulty` requires that it not be part of the object returned by the `getInitialState` method. Instead, `difficulty` should be initialized one time on the `state` state object within the `constructor`. |
57 | 56 |
|
58 | | -### Advanced Configuration |
| 57 | +4. Using strings such as 'Easy', etc., to represent the `difficulty` is a fantastic way to access the array of colors for a particular difficulty level by using an object as a lookup. Refactor the `colors` array in **App.js** to be an object with keys of `Easy`, `'Moderate` and `Difficult` which hold arrays of 4, 5, or 6 color strings respectively. |
59 | 58 |
|
60 | | -This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration |
| 59 | + Hint: The first couple of lines will look like this |
61 | 60 |
|
62 | | -### Deployment |
| 61 | + ```js |
| 62 | + const colors = { |
| 63 | + Easy: ['#7CCCE5', '#FDE47F', '#E04644', '#B576AD'], |
| 64 | + ... |
| 65 | + ``` |
63 | 66 |
|
64 | | -This section has moved here: https://facebook.github.io/create-react-app/docs/deployment |
| 67 | +5. Changing the structure of `colors` expectedly broke the code because we were used to passing `colors` as an array to `<GamePage>`. Refactor the value that is assigned to the `colors` prop in `<GamePage>` such that the appropriate array in the refactored `colors` object is passed according to the value of the `difficulty` state property. With this step complete, the react-mastermind will be working again. |
65 | 68 |
|
66 | | -### `npm run build` fails to minify |
| 69 | +6. Now comes the "fun" part - building out the `<SettingsPage>` component so that: |
67 | 70 |
|
68 | | -This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify |
| 71 | + - It displays the UI shown above, including the three difficulty levels, with a button to select the level and the colors rendered as pegs. Also, theres a "Cancel" link used to return to the root without changing the difficulty. |
| 72 | + |
| 73 | + - The button to select the difficulty level is disabled for the currently selected difficulty. |
| 74 | + |
| 75 | + - Clicking one of difficulty buttons should update the `difficulty` state, initialize a new game, and programmatically route back to the `<GamePage>` page (root route). |
| 76 | + |
| 77 | + Hints: |
| 78 | + |
| 79 | + - You can try downloading and using React Developer Tools to browse components and check/modify state & props. |
| 80 | + |
| 81 | + - `<SettingsPage>` is going to need both the `colors` object and the `difficulty` state property. |
| 82 | + |
| 83 | + - Since `difficulty` lives in the state of `<App>`, guess where the method to update it will live. |
| 84 | + |
| 85 | + - You can reuse the `handleNewGameClick` method by passing it down to `<SettingsPage>` calling it to reset the game after you call the method to update the `difficulty`. |
| 86 | + |
| 87 | + - After updating `difficulty` & invoking `handleNewGameClick`, you can use the technique shown in the _React Router_ lesson to programmatically route to `/`. |
| 88 | + |
| 89 | +Choosing the **Difficult** level will result in the root route displaying this: |
| 90 | + |
| 91 | +<img src="https://i.imgur.com/IaKWyLR.png"> |
| 92 | + |
| 93 | +Good luck cracking the code! |
0 commit comments