Skip to content
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
17 changes: 12 additions & 5 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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() {
Expand Down
9 changes: 3 additions & 6 deletions src/api/actions/auth.js
Original file line number Diff line number Diff line change
@@ -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 });

Expand All @@ -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 })
};
};

Expand Down
17 changes: 6 additions & 11 deletions src/api/actions/brewery.js
Original file line number Diff line number Diff line change
@@ -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 })
};
};

Expand Down
12 changes: 12 additions & 0 deletions src/api/constants/action_types.js
Original file line number Diff line number Diff line change
@@ -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;
31 changes: 23 additions & 8 deletions src/api/middlewares/middleware.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -22,7 +23,7 @@ const addFirebaseUser = (user, store) => {
email,
}).then(() => {
store.dispatch({
type: actions.logged,
type: ActionTypes.logged,
payload: user,
})
});
Expand Down Expand Up @@ -58,15 +59,29 @@ 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)
.then((payload) => {
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);
}
}
Expand Down
15 changes: 6 additions & 9 deletions src/components/brewery/beer.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
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,
[dataset['key']]: value
});
}

const onRemove = () => {
remove(uid);
}

return (
<div className="beer">
<div>
Expand All @@ -41,7 +38,7 @@ const Beer = ({ beer, edit }) => {
/>
</div>
<div>
<button onClick={remove}>Remove</button>
<button onClick={onRemove}>Remove</button>
</div>
</div>
);
Expand Down
3 changes: 2 additions & 1 deletion src/components/brewery/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => (
<Beer
edit={edit}
remove={remove}
key={beer.get('uid')}
beer={beer}
/>
Expand Down
3 changes: 2 additions & 1 deletion src/components/home/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class Container extends React.Component {
}

render() {
return <Home />
const { onLogout } = this.props;
return <Home onLogout={onLogout}/>
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/components/home/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import Brewery from 'components/brewery/list';

class Home extends React.Component {
render() {
const { onLogout } = this.props;
return (
<div>
<Toolbar />
<Toolbar onLogout={onLogout}/>
<Brewery />
</div>
);
Expand Down
23 changes: 8 additions & 15 deletions src/components/home/toolbar.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
import React from 'react';
import { connect } from 'react-redux';

const Toolbar = ({ count, user: { uid } }) => (
const Toolbar = ({ count, user: { uid }, onLogout }) => (
<div className="">
{
uid ? `Count: ${count}`: ''
}
{ uid ? `User: ${uid}`: '' }
<br/>
{ count ? `Count: ${count}`: '' }
<br/>
<button onClick={onLogout}>Logout</button>
</div>
);

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);
14 changes: 8 additions & 6 deletions src/milflux/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}),
Expand Down