Skip to content

Commit

Permalink
Refactor app using hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
djizco committed Feb 11, 2019
1 parent 04851a1 commit 522ce53
Show file tree
Hide file tree
Showing 27 changed files with 156 additions and 268 deletions.
24 changes: 19 additions & 5 deletions client/components/molecules/AddTodo/AddTodo.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import React from 'react';
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import Button from '_atoms/Button';

export default function AddTodo({ text, updateText, addTodo }) {
import useKeypress from '_hooks/useKeypress';

export default function AddTodo({ addTodo }) {
const [text, setText] = useState('');

const onAddTodo = () => {
if (text) {
addTodo(text);
setText('');
}
};

useKeypress('Enter', onAddTodo);

const updateText = e => setText(e.target.value);


return (
<div className="add-todo columns is-gapless">
<div className="column is-10">
Expand All @@ -11,7 +27,7 @@ export default function AddTodo({ text, updateText, addTodo }) {
<div className="column is-2">
<Button
style={{ width: '100%' }}
onClick={addTodo}
onClick={onAddTodo}
label="Add"
type="success"
/>
Expand All @@ -21,7 +37,5 @@ export default function AddTodo({ text, updateText, addTodo }) {
}

AddTodo.propTypes = {
text: PropTypes.string.isRequired,
updateText: PropTypes.func.isRequired,
addTodo: PropTypes.func.isRequired,
};
49 changes: 0 additions & 49 deletions client/components/molecules/AddTodo/AddTodoContainer.js

This file was deleted.

4 changes: 2 additions & 2 deletions client/components/molecules/AddTodo/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as R from 'ramda';
import { connect } from 'react-redux';
import { attemptAddTodo } from '_thunks/todos';
import AddTodoContainer from './AddTodoContainer';
import AddTodo from './AddTodo';

const mapStateToProps = R.pick([]);

const mapDispatchToProps = dispatch => ({
addTodo: text => dispatch(attemptAddTodo(text)),
});

export default connect(mapStateToProps, mapDispatchToProps)(AddTodoContainer);
export default connect(mapStateToProps, mapDispatchToProps)(AddTodo);
9 changes: 6 additions & 3 deletions client/components/organisms/SettingsMenu/SettingsMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import React from 'react';
import PropTypes from 'prop-types';
import { Link } from 'react-router-dom';
import classNames from 'classnames';
import R from '_utils/ramda';

export default function SettingsMenu(props) {
const { pathname, logout } = props;
export default function SettingsMenu({ pathname, attemptLogout }) {
const logout = () =>
attemptLogout()
.catch(R.identity);

const profileClasses = classNames({
'is-active': pathname.includes('profile') || pathname === '/settings' || pathname === '/settings/',
Expand Down Expand Up @@ -45,5 +48,5 @@ export default function SettingsMenu(props) {

SettingsMenu.propTypes = {
pathname: PropTypes.string.isRequired,
logout: PropTypes.func.isRequired,
attemptLogout: PropTypes.func.isRequired,
};
26 changes: 0 additions & 26 deletions client/components/organisms/SettingsMenu/SettingsMenuContainer.js

This file was deleted.

4 changes: 2 additions & 2 deletions client/components/organisms/SettingsMenu/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import R from '_utils/ramda';
import { connect } from 'react-redux';
import { attemptLogout } from '_thunks/auth';
import SettingsMenuContainer from './SettingsMenuContainer';
import SettingsMenu from './SettingsMenu';

const mapStateToProps = R.pick([]);

const mapDispatchToProps = dispatch => ({
attemptLogout: () => dispatch(attemptLogout()),
});

export default connect(mapStateToProps, mapDispatchToProps)(SettingsMenuContainer);
export default connect(mapStateToProps, mapDispatchToProps)(SettingsMenu);
17 changes: 15 additions & 2 deletions client/components/pages/HomePage/HomePage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import React from 'react';
import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
import R from '_utils/ramda';

export default function HomePage({ user, pushToLogin }) {
useEffect(() => {
if (R.isEmpty(user)) {
pushToLogin();
}
}, []);

export default function HomePage() {
return (
<div className="home-page page">
<div className="section">
Expand All @@ -13,3 +21,8 @@ export default function HomePage() {
</div>
);
}

HomePage.propTypes = {
user: PropTypes.shape({}).isRequired,
pushToLogin: PropTypes.func.isRequired,
};
24 changes: 0 additions & 24 deletions client/components/pages/HomePage/HomePageContainer.js

This file was deleted.

4 changes: 2 additions & 2 deletions client/components/pages/HomePage/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import R from '_utils/ramda';
import { connect } from 'react-redux';
import { push } from 'connected-react-router';
import HomePageContainer from './HomePageContainer';
import HomePage from './HomePage';

const mapStateToProps = R.pick(['user']);

const mapDispatchToProps = dispatch => ({
pushToLogin: () => dispatch(push('/login')),
});

export default connect(mapStateToProps, mapDispatchToProps)(HomePageContainer);
export default connect(mapStateToProps, mapDispatchToProps)(HomePage);
17 changes: 15 additions & 2 deletions client/components/pages/LoginPage/LoginPage.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import React from 'react';
import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
import R from '_utils/ramda';
import LoginSection from '_templates/LoginSection';

export default function LoginPage() {
export default function LoginPage({ user, pushToHome }) {
useEffect(() => {
if (!R.isEmpty(user)) {
pushToHome();
}
}, []);

return (
<div className="login-page page">
<LoginSection />
</div>
);
}

LoginPage.propTypes = {
user: PropTypes.shape({}).isRequired,
pushToHome: PropTypes.func.isRequired,
};
24 changes: 0 additions & 24 deletions client/components/pages/LoginPage/LoginPageContainer.js

This file was deleted.

4 changes: 2 additions & 2 deletions client/components/pages/LoginPage/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import R from '_utils/ramda';
import { connect } from 'react-redux';
import { push } from 'connected-react-router';
import LoginPageContainer from './LoginPageContainer';
import LoginPage from './LoginPage';

const mapStateToProps = R.pick(['user']);

const mapDispatchToProps = dispatch => ({
pushToHome: () => dispatch(push('/home')),
});

export default connect(mapStateToProps, mapDispatchToProps)(LoginPageContainer);
export default connect(mapStateToProps, mapDispatchToProps)(LoginPage);
17 changes: 15 additions & 2 deletions client/components/pages/RegisterPage/RegisterPage.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import React from 'react';
import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
import R from '_utils/ramda';
import Register from '_templates/RegisterSection';

export default function RegisterPage() {
export default function RegisterPage({ user, pushToHome }) {
useEffect(() => {
if (!R.isEmpty(user)) {
pushToHome();
}
}, []);

return (
<div className="register-page page">
<Register />
</div>
);
}

RegisterPage.propTypes = {
user: PropTypes.shape({}).isRequired,
pushToHome: PropTypes.func.isRequired,
};
24 changes: 0 additions & 24 deletions client/components/pages/RegisterPage/RegisterPageContainer.js

This file was deleted.

4 changes: 2 additions & 2 deletions client/components/pages/RegisterPage/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import R from '_utils/ramda';
import { connect } from 'react-redux';
import { push } from 'connected-react-router';
import RegisterPageContainer from './RegisterPageContainer';
import RegisterPage from './RegisterPage';

const mapStateToProps = R.pick(['user']);

const mapDispatchToProps = dispatch => ({
pushToHome: () => dispatch(push('/home')),
});

export default connect(mapStateToProps, mapDispatchToProps)(RegisterPageContainer);
export default connect(mapStateToProps, mapDispatchToProps)(RegisterPage);
Loading

0 comments on commit 522ce53

Please sign in to comment.