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..15a1d4e 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 = () => { + remove(uid); + } + return (