From 8cfe22f6a99990a9fbe1cb918e74532e1406ee88 Mon Sep 17 00:00:00 2001 From: yuriadams Date: Tue, 13 Jun 2017 18:21:10 -0300 Subject: [PATCH 1/2] remove beer and logout features --- src/App.js | 17 ++++++++++++----- src/api/actions/auth.js | 9 +++------ src/api/actions/brewery.js | 17 ++++++----------- src/api/constants/action_types.js | 12 ++++++++++++ src/api/middlewares/middleware.js | 31 +++++++++++++++++++++++-------- src/components/brewery/beer.js | 15 ++++++--------- src/components/brewery/list.js | 3 ++- src/components/home/container.js | 3 ++- src/components/home/home.js | 3 ++- src/components/home/toolbar.js | 23 ++++++++--------------- src/milflux/store.js | 14 ++++++++------ 11 files changed, 84 insertions(+), 63 deletions(-) create mode 100644 src/api/constants/action_types.js diff --git a/src/App.js b/src/App.js index fe13990..9d62d85 100644 --- a/src/App.js +++ b/src/App.js @@ -15,6 +15,8 @@ import Home from 'components/home/container'; import middleware from 'api/middlewares/middleware.js'; import reducers from 'api/reducers'; + +import ActionTypes from 'api/constants/action_types'; window.uuid = uuid; class App extends Component { @@ -41,11 +43,16 @@ class App extends Component { } onAuthStateChanged = (payload) => { - const action = { - type: 'logged', - payload, - }; - this.store.dispatch(action); + if(!payload) { + this.store.dispatch({ uid: '', type: '' }); + } else { + const action = { + type: 'logged', + payload + }; + + this.store.dispatch(action); + } } render() { diff --git a/src/api/actions/auth.js b/src/api/actions/auth.js index b5a2755..452f80d 100644 --- a/src/api/actions/auth.js +++ b/src/api/actions/auth.js @@ -1,9 +1,5 @@ import { push } from 'react-router-redux'; - -export const actions = { - login: 'BREWERY_AUTH_LOGIN', - logged: 'BREWERY_AUTH_LOGGED', -} +import ActionTypes from '../constants/action_types'; export const mapStateToProps = ({ user }) => ({ user }); @@ -19,7 +15,8 @@ export const mapDispatchToProps = (dispatch) => { dispatch(push('/login')); } }, - onLogin: () => dispatch({ type: 'login' }) + onLogin: () => dispatch({ type: ActionTypes.login }), + onLogout: () => dispatch({ type: ActionTypes.logout }) }; }; diff --git a/src/api/actions/brewery.js b/src/api/actions/brewery.js index 59676e6..7a97793 100644 --- a/src/api/actions/brewery.js +++ b/src/api/actions/brewery.js @@ -1,26 +1,21 @@ import { push } from 'react-router-redux'; - -export const actions = { - add: 'BREWERY_LIST_ADD', - edit: 'BREWERY_LIST_EDIT', - request:'BREWERY_LIST_REQUEST', - requestSuccess: 'BREWERY_LIST_REQUEST_SUCCESS', -} +import ActionTypes from '../constants/action_types'; export const mapStateToProps = ({ list }) => ({ list }); export const mapDispatchToProps = (dispatch) => { return { - request: () => dispatch({ type: actions.request }), + request: () => dispatch({ type: ActionTypes.request }), requestSuccess: payload => dispatch({ - type: actions.requestSuccess, + type: ActionTypes.requestSuccess, payload }), - add: () => dispatch({ type: actions.add }), + add: () => dispatch({ type: ActionTypes.add }), edit: payload => dispatch({ - type: actions.edit, + type: ActionTypes.edit, payload }), + remove: payload => dispatch({ type: ActionTypes.remove, payload }) }; }; diff --git a/src/api/constants/action_types.js b/src/api/constants/action_types.js new file mode 100644 index 0000000..018c9c1 --- /dev/null +++ b/src/api/constants/action_types.js @@ -0,0 +1,12 @@ +const ActionTypes = { + add: 'BREWERY_LIST_ADD', + edit: 'BREWERY_LIST_EDIT', + remove: 'BREWERY_LIST_REMOVE', + request:'BREWERY_LIST_REQUEST', + requestSuccess: 'BREWERY_LIST_REQUEST_SUCCESS', + login: 'BREWERY_AUTH_LOGIN', + logout: 'BREWERY_AUTH_LOGOUT', + logged: 'BREWERY_AUTH_LOGGED' +}; + +export default ActionTypes; diff --git a/src/api/middlewares/middleware.js b/src/api/middlewares/middleware.js index 92d6356..fbf9a9c 100644 --- a/src/api/middlewares/middleware.js +++ b/src/api/middlewares/middleware.js @@ -1,13 +1,14 @@ import uuid from 'uuid'; import firebase from 'firebase'; -import { actions } from 'api/actions/auth'; +import ActionTypes from '../constants/action_types'; +import { push } from 'react-router-redux'; const config = { - apiKey: "AIzaSyDGYMxpnYaAJYyquEUM6Y__yQjhPP_skx0", - authDomain: "feedback-140018.firebaseapp.com", - databaseURL: "https://feedback-140018.firebaseio.com", - projectId: "feedback-140018", - storageBucket: "feedback-140018.appspot.com", + apiKey: "AIzaSyDF8QusstyjG6K4xRFbabmsGs3se3WYA_o", + authDomain: "addressbook-4960c.firebaseapp.com", + databaseURL: "https://addressbook-4960c.firebaseio.com", + projectId: "addressbook-4960c", + storageBucket: "addressbook-4960c.appspot.com", messagingSenderId: "71457068040" }; firebase.initializeApp(config); @@ -22,7 +23,7 @@ const addFirebaseUser = (user, store) => { email, }).then(() => { store.dispatch({ - type: actions.logged, + type: ActionTypes.logged, payload: user, }) }); @@ -58,8 +59,13 @@ function middleware(store) { }); } + if (action.type === ActionTypes.remove) { + const uid = action.payload; + const ref = firebase.database().ref(`/breweries/${uid}`); + ref.remove(); + } - if (action.type === actions.login) { + if (action.type === ActionTypes.login) { const authProvider = new firebase.auth.GoogleAuthProvider(); firebase.auth() .signInWithPopup(authProvider) @@ -67,6 +73,15 @@ function middleware(store) { addFirebaseUser(payload.user, store); }); } + + if (action.type === ActionTypes.logout) { + const authProvider = new firebase.auth.GoogleAuthProvider(); + firebase.auth() + .signOut() + .then((payload) => { + dispatch(push('/login')); + }); + } return dispatch(action); } } diff --git a/src/components/brewery/beer.js b/src/components/brewery/beer.js index 891e916..bcb87f2 100644 --- a/src/components/brewery/beer.js +++ b/src/components/brewery/beer.js @@ -1,16 +1,9 @@ import React from 'react'; -const Beer = ({ beer, edit }) => { +const Beer = ({ beer, edit, remove }) => { const { uid, name, brewery } = beer.toJS(); - const remove = () => { - // dispatch({ - // type: 'remove', - // payload: uid, - // }) - } - const onChange = ({ target: { value, dataset } }) => { edit({ uid, @@ -18,6 +11,10 @@ const Beer = ({ beer, edit }) => { }); } + const onRemove = ({ target: { value } }) => { + remove(value); + } + return (
@@ -41,7 +38,7 @@ const Beer = ({ beer, edit }) => { />
- +
); diff --git a/src/components/brewery/list.js b/src/components/brewery/list.js index c240da1..3ba9434 100644 --- a/src/components/brewery/list.js +++ b/src/components/brewery/list.js @@ -15,10 +15,11 @@ class List extends Component { } render() { - const { list, edit } = this.props; + const { list, edit, remove } = this.props; const beers = list.map(beer => ( diff --git a/src/components/home/container.js b/src/components/home/container.js index 30d4d36..ca7925a 100644 --- a/src/components/home/container.js +++ b/src/components/home/container.js @@ -12,7 +12,8 @@ class Container extends React.Component { } render() { - return + const { onLogout } = this.props; + return } } diff --git a/src/components/home/home.js b/src/components/home/home.js index 76e9c2f..1c5f5dc 100644 --- a/src/components/home/home.js +++ b/src/components/home/home.js @@ -4,9 +4,10 @@ import Brewery from 'components/brewery/list'; class Home extends React.Component { render() { + const { onLogout } = this.props; return (
- +
); diff --git a/src/components/home/toolbar.js b/src/components/home/toolbar.js index 36fd310..09ea565 100644 --- a/src/components/home/toolbar.js +++ b/src/components/home/toolbar.js @@ -1,26 +1,19 @@ import React from 'react'; import { connect } from 'react-redux'; -const Toolbar = ({ count, user: { uid } }) => ( +const Toolbar = ({ count, user: { uid }, onLogout }) => (
- { - uid ? `Count: ${count}`: '' - } + { uid ? `User: ${uid}`: '' } +
+ { count ? `Count: ${count}`: '' } +
+
); export default connect(state => { return { - count: 0, - // state.list.reduce( - // (count, item) => { - // if (item.premium) { - // return count + 1; - // } - // return count; - // }, - // 0 - // ), - user: state.user, + count: state.list.size, + user: state.user } })(Toolbar); diff --git a/src/milflux/store.js b/src/milflux/store.js index b0133c5..a6cad9a 100644 --- a/src/milflux/store.js +++ b/src/milflux/store.js @@ -48,11 +48,11 @@ class Store extends React.Component { componentWillMount() { const config = { - apiKey: "AIzaSyDGYMxpnYaAJYyquEUM6Y__yQjhPP_skx0", - authDomain: "feedback-140018.firebaseapp.com", - databaseURL: "https://feedback-140018.firebaseio.com", - projectId: "feedback-140018", - storageBucket: "feedback-140018.appspot.com", + apiKey: "AIzaSyDF8QusstyjG6K4xRFbabmsGs3se3WYA_o", + authDomain: "addressbook-4960c.firebaseapp.com", + databaseURL: "https://faddressbook-4960c.firebaseio.com", + projectId: "addressbook-4960c", + storageBucket: "addressbook-4960c.appspot.com", messagingSenderId: "71457068040" }; firebase.initializeApp(config); @@ -121,13 +121,15 @@ class Store extends React.Component { ...this.state, list: list.map(item => { if (item.uid === name) { - item.premium = (value === 'Baden'); if (dataset['name'] === 'brewery') { item.brewery = value; } if (dataset['name'] === 'name') { item.name = value; } + if (dataset['name'] === 'premium') { + item.premium = value; + } } return item; }), From ce82288da907bf2d33cb44a7053dedeeb48a75b2 Mon Sep 17 00:00:00 2001 From: yuriadams Date: Thu, 15 Jun 2017 07:15:40 -0300 Subject: [PATCH 2/2] using uid --- src/components/brewery/beer.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/brewery/beer.js b/src/components/brewery/beer.js index bcb87f2..15a1d4e 100644 --- a/src/components/brewery/beer.js +++ b/src/components/brewery/beer.js @@ -11,8 +11,8 @@ const Beer = ({ beer, edit, remove }) => { }); } - const onRemove = ({ target: { value } }) => { - remove(value); + const onRemove = () => { + remove(uid); } return ( @@ -38,7 +38,7 @@ const Beer = ({ beer, edit, remove }) => { />
- +
);