Skip to content

Commit

Permalink
Admin Menu
Browse files Browse the repository at this point in the history
  • Loading branch information
kevindavee committed Aug 3, 2017
1 parent 99ab8fa commit 5589b3a
Show file tree
Hide file tree
Showing 15 changed files with 266 additions and 37 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"react-native-animatable": "^1.2.3",
"react-native-easy-grid": "^0.1.14",
"react-native-router-flux": "^3.35.0",
"react-native-swipeable": "^0.6.0",
"react-native-vector-icons": "^4.2.0",
"react-redux": "^5.0.5",
"redux": "^3.7.2",
Expand Down
19 changes: 7 additions & 12 deletions src/actions/ProfileActions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _ from 'lodash';
import firebase from 'firebase';
import { PROFILE_FULLNAME_CHANGE, PROFILE_COMPANY_CHANGE,
INITIAL_PROFILE, FETCH_PROFILE, PROFILE_SAVE_CHANGES,
Expand All @@ -9,16 +8,10 @@ export const initialProfile = () => {

return (dispatch) => {
dispatch({ type: FETCH_PROFILE });
firebase.database().ref('/profiles/').orderByChild('uid').equalTo(currentUser.uid)
firebase.database().ref(`/profiles/${currentUser.uid}`)
.on('value', snapshot => {
const email = currentUser.email;
const profile = _.map(snapshot.val(), (val, uid) => {
return { ...val, uid };
});
const fullName = profile[0].fullName;
const company = profile[0].company;
const uid = profile[0].uid;
const payload = { email, fullName, company, uid };
const { fullName, company } = snapshot.val();
const payload = { email: currentUser.email, fullName, company };
dispatch({ type: INITIAL_PROFILE, payload });
});
};
Expand All @@ -38,11 +31,13 @@ export const profileCompanyChange = (text) => {
};
};

export const profileSaveChanges = ({ fullName, company, uid }) => {
export const profileSaveChanges = ({ fullName, company }) => {
const { currentUser } = firebase.auth();

return (dispatch) => {
dispatch({ type: PROFILE_SAVE_CHANGES });

firebase.database().ref(`/profiles/${uid}/`)
firebase.database().ref(`/profiles/${currentUser.uid}`)
.update({ fullName, company })
.then(() => {
dispatch({ type: PROFILE_CHANGES_SUCCESS });
Expand Down
40 changes: 40 additions & 0 deletions src/actions/ProjectListActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import _ from 'lodash';
import firebase from 'firebase';
import { PROJECT_LIST_FETCH_SUCCESS, PROJECT_LIST_VALIDATING,
PROJECT_LIST_VALIDATED } from './types';

export const projectListFetch = () => {
return (dispatch) => {
firebase.database().ref('/projects/')
.on('value', snapshot => {
const projects = _.map(snapshot.val(), (val, projectId) => {
return { ...val, projectId };
});

_.remove(projects, (item) => {
if ('isValid' in item) {
return item;
}
});

dispatch({ type: PROJECT_LIST_FETCH_SUCCESS, payload: projects });
});
};
};

export const projectListValidating = (isValid, id) => {
return (dispatch) => {
dispatch({ type: PROJECT_LIST_VALIDATING });

firebase.database().ref(`/projects/${id}/`)
.update({ isValid })
.then(() => {
projectValidated(dispatch);
});
};
};

const projectValidated = (dispatch) => {
dispatch({ type: PROJECT_LIST_VALIDATED });
};

4 changes: 2 additions & 2 deletions src/actions/RegisterActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export const registerUser = ({ email, password, company, fullName }) => {
firebase.auth().createUserWithEmailAndPassword(email, password)
.then((user) => {
const dbRoot = firebase.database().ref();
dbRoot.child('profiles')
.push({ fullName, company, uid: user.uid })
dbRoot.child(`/profiles/${user.uid}`)
.set({ fullName, company })
.then(() => {
Alert.alert('Success', 'Berhasil mendaftar ! Silahkan cek email anda untuk email verifikasi');
const { currentUser } = firebase.auth();
Expand Down
16 changes: 10 additions & 6 deletions src/actions/RewardActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@ export const projectInputChange = (text) => {
export const projectSubmit = ({ project }) => {
console.log('project submitted');
const { currentUser } = firebase.auth();
const currentDate = new Date().toLocaleDateString();
const currentDate = new Date().toLocaleDateString();

return (dispatch) => {
dispatch({ type: PROJECT_SUBMIT });

firebase.database().ref().child('projects')
.push({ project, date: currentDate, uid: currentUser.uid })
.then(() => {
dispatch({ type: PROJECT_SUBMIT_SUCCESS });
Alert.alert('Berhasil', 'Project anda berhasil di daftar kan !');
firebase.database().ref(`/profiles/${currentUser.uid}/`)
.on('value', snapshot => {
const { fullName, company } = snapshot.val();
firebase.database().ref().child('projects')
.push({ project, date: currentDate, uid: currentUser.uid, fullName, company })
.then(() => {
dispatch({ type: PROJECT_SUBMIT_SUCCESS });
Alert.alert('Berhasil', 'Project anda berhasil di daftar kan !');
});
});
};
};
Expand Down
2 changes: 1 addition & 1 deletion src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export * from './ProductActions';
export * from './RewardActions';
export * from './ProfileActions';
export * from './ChangePasswordActions';

export * from './ProjectListActions';
5 changes: 5 additions & 0 deletions src/actions/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ export const UPDATE_PASSWORD = 'update_password';
export const CHANGE_PASSWORD_SUCCESS = 'change_password_success';
export const CHANGE_PASSWORD_FAIL = 'change_password_fail';
export const PASSWORD_INITIAL_SCREEN = 'password_initial_screen';

export const PROJECT_LIST_FETCH_SUCCESS = 'project_list_fetch_success';
export const PROJECT_LIST_VALIDATED = 'project_list_validated';
export const PROJECT_LIST_VALIDATING = 'project_list_validating';
export const PROJECT_LIST_VALIDATING_ERROR = 'project_list_validating_error';
124 changes: 124 additions & 0 deletions src/component/admin/ProjectList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import React, { Component } from 'react';
import { DrawerLayoutAndroid, ScrollView, RefreshControl, Alert } from 'react-native';
import { Container, List, Content, Button, Text, ListItem } from 'native-base';
import Swipeable from 'react-native-swipeable';
import { connect } from 'react-redux';
import { CustomHeader, MyDrawer } from '../commons/';
import { projectListFetch, projectListValidating } from '../../actions';
import { DRAWER_MENU } from '../commons/ButtonConst';


class ProjectList extends Component {
componentWillMount() {
this.props.projectListFetch();
}

onRefresh() {
this.props.projectListFetch();
}

openDrawer() {
this.refs['.DRAWER'].openDrawer();
}

renderContent() {
if (!this.props.loading) {
return (
<List
dataArray={this.props.project}
renderRow={this.renderRow.bind(this)}
/>
);
}
}

renderRow(project) {
const rightButtons = [
<Button
full
light
style={{ alignItems: 'flex-start', flexDirection: 'column', flex: 1 }}
onPress={() =>
Alert.alert('Info', `Project: ${project.project} \nTanggal: ${project.date} \nNama: ${project.fullName} \nPerusahaan: ${project.company}`)
}
>
<Text>Info</Text>
</Button>,
<Button
full
success
style={{ alignItems: 'flex-start', flexDirection: 'column', flex: 1 }}
onPress={() =>
Alert.alert(
'Validate',
'Apakah project ini valid ?',
[
{ text: 'Ya', onPress: () => this.props.projectListValidating(true, project.projectId) },
{ text: 'Tidak', onPress: () => this.props.projectListValidating(false, project.projectId) },
{ text: 'Kembali' }
]
)
}

>
<Text>Validate</Text>
</Button>
];

return (
<Swipeable
rightButtons={rightButtons}
rightButtonWidth={125}

>
<ListItem>
<Text>{project.project}</Text>
</ListItem>
</Swipeable>
);
}

render() {
const navigationView = (<MyDrawer />);

return (

<DrawerLayoutAndroid
drawerWidth={300}
drawerPosition={DrawerLayoutAndroid.positions.Left}
renderNavigationView={() => navigationView}
ref={'.DRAWER'}
>
<Container>
<CustomHeader
headerText="Project List"
leftButton={DRAWER_MENU}
drawerOpen={() => this.openDrawer.bind(this)}
/>
<ScrollView
refreshControl={
<RefreshControl
refreshing={this.props.loading}
onRefresh={this.onRefresh.bind(this)}
colors={['#d9534f']}
/>
}
>
<Content>
{this.renderContent()}
</Content>
</ScrollView>
</Container>
</DrawerLayoutAndroid>
);
}
}

const mapStateToProps = ({ projectList }) => {
const project = projectList.projects;
const loading = projectList.loading;

return { project, loading };
};

export default connect(mapStateToProps, { projectListFetch, projectListValidating })(ProjectList);
47 changes: 38 additions & 9 deletions src/component/commons/MyDrawer.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,70 @@
import React, { Component } from 'react';
import firebase from 'firebase';
import { Container, Content, List, CardItem,
Left, Icon, Text } from 'native-base';
Left, Icon, Text, View } from 'native-base';
import { Actions } from 'react-native-router-flux';
import styles from '../styles';

class MyDrawer extends Component {
state = { isAdmin: false };

componentWillMount() {
const { currentUser } = firebase.auth();

firebase.database().ref(`/admins/${currentUser.uid}`)
.once('value', snapshot => {
this.setState({ isAdmin: snapshot.val() });
});
}

renderAdminMenu() {
if (this.state.isAdmin) {
return (
<View>
<CardItem icon button onPress={() => Actions.projectlist()}>
<Left>
<Icon name="list" style={styles.drawerIconStyle} />
<Text style={styles.drawerTextStyle}>Project List</Text>
</Left>
</CardItem>
</View>
);
}
}

render() {
const { containerStyle } = styles;
const { containerStyle } = thisStyles;
return (
<Container style={containerStyle}>
<Content>
<List>
<CardItem icon button onPress={() => Actions.product()}>
<Left>
<Icon name="bulb" />
<Text> Products</Text>
<Icon name="bulb" style={styles.drawerIconStyle} />
<Text style={styles.drawerTextStyle}>Products</Text>
</Left>
</CardItem>
<CardItem icon button onPress={() => Actions.reward()}>
<Left>
<Icon name="star" />
<Text> Rewards</Text>
<Icon name="star" style={styles.drawerIconStyle} />
<Text style={styles.drawerTextStyle}>Rewards</Text>
</Left>
</CardItem>
<CardItem icon button onPress={() => Actions.profile()}>
<Left>
<Icon name="contact" />
<Text> Profile</Text>
<Icon name="contact" style={styles.drawerIconStyle} />
<Text style={styles.drawerTextStyle}>Profile</Text>
</Left>
</CardItem>
{this.renderAdminMenu()}
</List>
</Content>
</Container>
);
}
}

const styles = {
const thisStyles = {
containerStyle: {
flex: 1,
backgroundColor: '#fff'
Expand Down
1 change: 0 additions & 1 deletion src/component/main_app/CategoryList.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class CategoryList extends Component {
<List
dataArray={this.props.categories}
renderRow={this.renderRow}

/>
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/component/main_app/Profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class Profile extends Component {
}

onChangeProfile() {
const { fullName, company, uid } = this.props;
this.props.profileSaveChanges({ fullName, company, uid });
const { fullName, company } = this.props;
this.props.profileSaveChanges({ fullName, company });
}

onLogOut() {
Expand Down Expand Up @@ -147,9 +147,9 @@ class Profile extends Component {
}

const mapStateToProps = ({ profile }) => {
const { fullName, company, loading, email, loadingSubmit, uid } = profile;
const { fullName, company, loading, email, loadingSubmit } = profile;

return { fullName, company, loading, email, loadingSubmit, uid };
return { fullName, company, loading, email, loadingSubmit };
};

export default connect(mapStateToProps,
Expand Down
8 changes: 8 additions & 0 deletions src/component/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ const styles = {
inputLabelStyle: {
fontSize: 13,
flex: 1
},
drawerIconStyle: {
flexDirection: 'column',
flex: 2
},
drawerTextStyle: {
flexDirection: 'column',
flex: 8
}
};

Expand Down
Loading

0 comments on commit 5589b3a

Please sign in to comment.