Skip to content

[DEV] Raphael Martins #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
276 changes: 276 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
"node": "16"
},
"dependencies": {
"@reduxjs/toolkit": "^1.8.1",
"@testing-library/jest-dom": "^5.16.1",
"@testing-library/react": "^12.1.2",
"@testing-library/user-event": "^13.5.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-redux": "^8.0.1",
"react-scripts": "4.0.3",
"redux": "^4.2.0",
"web-vitals": "^2.1.4"
},
"scripts": {
Expand Down
25 changes: 11 additions & 14 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import logo from './logo.svg';
import './App.css';
import Password from './components/Password';
import Length from './components/Length';
import Settings from './components/Settings';
import GeneratePasswordBtn from './components/GeneratePasswordBtn';

function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
<main>
<h1>password generator</h1>
<Password />
<Length />
<Settings />
<GeneratePasswordBtn />
</main>
</div>
);
}
Expand Down
27 changes: 27 additions & 0 deletions src/components/GeneratePasswordBtn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from "react";
import { useDispatch, useSelector } from "react-redux";
import { actionGeneratePassword } from "../redux/actions";

function GeneratePasswordBtn() {
const dispatch = useDispatch();
const { includeUppercase, includeLowercase, includeNumbers, includeSymbols } =
useSelector((state) => state.settings);
const settings = [
includeUppercase,
includeLowercase,
includeNumbers,
includeSymbols,
];

return (
<button
type="button"
onClick={() => dispatch(actionGeneratePassword())}
disabled={settings.every((option) => option === false)}
>
generate password
</button>
);
}

export default GeneratePasswordBtn;
28 changes: 28 additions & 0 deletions src/components/Length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from "react";
import { useDispatch, useSelector } from "react-redux";
import { actionChangeSettings } from "../redux/actions";

function Length() {
const { length } = useSelector((state) => state.settings);
const dispatch = useDispatch();

return (
<fieldset>
<legend>
length: <span>{length}</span>
</legend>
<input
type="range"
min="4"
max="32"
name="length"
value={length}
onChange={({ target }) =>
dispatch(actionChangeSettings(target.name, target.value))
}
/>
</fieldset>
);
}

export default Length;
30 changes: 30 additions & 0 deletions src/components/Option.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import PropTypes from "prop-types";
import React from "react";
import { useDispatch, useSelector } from "react-redux";
import { actionChangeSettings } from "../redux/actions";

function Option({ label, name }) {
const settings = useSelector((state) => state.settings);
const dispatch = useDispatch();

return (
<label>
{label}
<input
type="checkbox"
name={name}
checked={settings[name]}
onChange={({ target }) =>
dispatch(actionChangeSettings(target.name, target.checked))
}
/>
</label>
);
}

Option.propTypes = {
label: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
};

export default Option;
10 changes: 10 additions & 0 deletions src/components/Password.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from "react";
import { useSelector } from "react-redux";

function Password() {
const { password } = useSelector((state) => state.settings);

return <p>{password}</p>;
}

export default Password;
16 changes: 16 additions & 0 deletions src/components/Settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import Option from './Option';

function Settings() {
return (
<fieldset>
<legend>settings</legend>
<Option label="include uppercase" name="includeUppercase" />
<Option label="include lowercase" name="includeLowercase" />
<Option label="include numbers" name="includeNumbers" />
<Option label="include symbols" name="includeSymbols" />
</fieldset>
);
}

export default Settings;
12 changes: 8 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import { Provider } from 'react-redux';
import App from './App';
import './index.css';
import store from './redux/store';
import reportWebVitals from './reportWebVitals';

ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
<Provider store={ store }>
<React.StrictMode>
<App />
</React.StrictMode>
</Provider>,
document.getElementById('root')
);

Expand Down
19 changes: 19 additions & 0 deletions src/redux/actions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const CHANGE_SETTINGS = 'CHANGE_SETTINGS';
const GENERATE_PASSWORD = 'GENERATE_PASSWORD';

const actionChangeSettings = (name, value) => ({
type: CHANGE_SETTINGS,
name,
value,
});

const actionGeneratePassword = () => ({
type: GENERATE_PASSWORD,
});

export {
CHANGE_SETTINGS,
actionChangeSettings,
GENERATE_PASSWORD,
actionGeneratePassword,
};
65 changes: 65 additions & 0 deletions src/redux/reducers/characters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
const lowerCaseLetters = [
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
];
const upperCaseLetters = lowerCaseLetters.map((letter) => letter.toUpperCase());
const symbols = [
'!',
'#',
'$',
'%',
'&',
'\'',
'(',
')',
'*',
'+',
',',
'-',
'.',
'/',
':',
';',
'<',
'=',
'>',
'?',
'@',
'[',
'\\',
']',
'^',
'_',
'`',
'{',
'|',
'}',
'~',
];

export { numbers, lowerCaseLetters, upperCaseLetters, symbols };
6 changes: 6 additions & 0 deletions src/redux/reducers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { combineReducers } from 'redux';
import settings from './settings';

const rootReducer = combineReducers({ settings });

export default rootReducer;
45 changes: 45 additions & 0 deletions src/redux/reducers/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { CHANGE_SETTINGS, GENERATE_PASSWORD } from '../actions';
import { lowerCaseLetters, numbers, symbols, upperCaseLetters } from './characters';

const INITIAL_STATE = {
password: 'CLICK GENERATE',
length: 4,
includeUppercase: false,
includeLowercase: false,
includeNumbers: false,
includeSymbols: false,
};

const generatePassword = (settings) => {
let password = [];

if (settings.includeLowercase) password = [...password, ...lowerCaseLetters];
if (settings.includeUppercase) password = [...password, ...upperCaseLetters];
if (settings.includeNumbers) password = [...password, ...numbers];
if (settings.includeSymbols) password = [...password, ...symbols];

const shuffle = 0.5;
password.sort(() => Math.random() - shuffle);
password = password.join('').slice(0, settings.length);

return password;
};

const settings = (state = INITIAL_STATE, action) => {
switch (action.type) {
case CHANGE_SETTINGS:
return {
...state,
[action.name]: action.value,
};
case GENERATE_PASSWORD:
return {
...state,
password: generatePassword(state),
};
default:
return state;
}
};

export default settings;
8 changes: 8 additions & 0 deletions src/redux/store/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { configureStore } from '@reduxjs/toolkit';
import rootReducer from '../reducers';

const store = configureStore({
reducer: rootReducer,
});

export default store;