Commit 81ccd73d by HaOuiha

update apollo

parent 2b78fbeb
...@@ -12,6 +12,8 @@ import { Provider } from 'react-redux'; ...@@ -12,6 +12,8 @@ import { Provider } from 'react-redux';
import rootStore from './reduxStore'; import rootStore from './reduxStore';
import { Root } from 'native-base'; import { Root } from 'native-base';
import { fixTimerWarning } from './utils/fixTimerWarning'; import { fixTimerWarning } from './utils/fixTimerWarning';
import { ApolloProvider } from '@apollo/react-hooks';
import { client } from './utils/apollo/apollo';
import { useScreens } from 'react-native-screens'; import { useScreens } from 'react-native-screens';
console.disableYellowBox = true; console.disableYellowBox = true;
...@@ -25,7 +27,9 @@ const Application = () => { ...@@ -25,7 +27,9 @@ const Application = () => {
<Root> <Root>
<ApplicationProvider mapping={mapping} theme={myTheme}> <ApplicationProvider mapping={mapping} theme={myTheme}>
<Provider store={rootStore}> <Provider store={rootStore}>
<AppContainer /> <ApolloProvider client={client}>
<AppContainer />
</ApolloProvider>
</Provider> </Provider>
</ApplicationProvider> </ApplicationProvider>
</Root> </Root>
......
import React from 'react'; import React, { Component } from 'react';
import { Field, reduxForm } from 'redux-form'; import { Field, reduxForm } from 'redux-form';
import { Text, StyleSheet, View } from 'react-native'; import { Text, StyleSheet, View } from 'react-native';
import Input from './Input'; import Input from './Input';
...@@ -18,7 +18,7 @@ const special = value => ...@@ -18,7 +18,7 @@ const special = value =>
value && !/^(?=.*[!@#\$%\^&\*])/i.test(value) ? 'Password must contain at least 1 special character' : undefined; value && !/^(?=.*[!@#\$%\^&\*])/i.test(value) ? 'Password must contain at least 1 special character' : undefined;
const cfpassword = (value, allValues) => (value !== allValues.newPass ? 'Password not match' : undefined); const cfpassword = (value, allValues) => (value !== allValues.newPass ? 'Password not match' : undefined);
class ChangePassword extends React.Component { class ChangePassword extends Component {
state = { state = {
passVisible: false, passVisible: false,
}; };
...@@ -62,7 +62,7 @@ class ChangePassword extends React.Component { ...@@ -62,7 +62,7 @@ class ChangePassword extends React.Component {
forwardRef forwardRef
ref={c => (this.cfNewPass = c)} ref={c => (this.cfNewPass = c)}
refField="cfNewPass" refField="cfNewPass"
returnKeyType="next" // returnKeyType="next"
name="cfNewPass" name="cfNewPass"
keyboardType="default" keyboardType="default"
component={Input} component={Input}
......
import React, { Component } from 'react'; import React, { Component } from 'react';
import { Field, reduxForm } from 'redux-form'; import { Field, reduxForm } from 'redux-form';
import { KeyboardAvoidingView } from 'react-native'; import { KeyboardAvoidingView, Keyboard } from 'react-native';
import Input from './Input'; import Input from './Input';
import { theme } from '../../constants/Styles'; import { theme } from '../../constants/Styles';
import GradientBtn from '../GradientBtn'; import GradientBtn from '../GradientBtn';
...@@ -46,7 +46,13 @@ class Login extends Component { ...@@ -46,7 +46,13 @@ class Login extends Component {
/> />
</KeyboardAvoidingView> </KeyboardAvoidingView>
{this.props.children} {this.props.children}
<GradientBtn onPress={this.props.handleSubmit} title={'login'} /> <GradientBtn
onPress={() => {
this.props.handleSubmit();
Keyboard.dismiss();
}}
title={'login'}
/>
</> </>
); );
} }
......
...@@ -10,11 +10,15 @@ ...@@ -10,11 +10,15 @@
"lint": "eslint ." "lint": "eslint ."
}, },
"dependencies": { "dependencies": {
"@apollo/react-hooks": "^3.1.1",
"@eva-design/eva": "^1.0.1", "@eva-design/eva": "^1.0.1",
"@react-native-community/async-storage": "^1.6.1", "@react-native-community/async-storage": "^1.6.1",
"apollo-boost": "^0.4.4",
"axios": "^0.19.0", "axios": "^0.19.0",
"date-fns": "^1.30.1", "date-fns": "^1.30.1",
"firebase": "^6.5.0", "firebase": "^6.5.0",
"graphql": "^14.5.7",
"graphql-tag": "^2.10.1",
"lodash": "^4.17.15", "lodash": "^4.17.15",
"moment": "^2.24.0", "moment": "^2.24.0",
"native-base": "^2.12.2", "native-base": "^2.12.2",
......
...@@ -22,7 +22,7 @@ export const errorAction = error => ({ ...@@ -22,7 +22,7 @@ export const errorAction = error => ({
}); });
export const getAllMainDeviceInfo = () => async (dispatch, getState) => { export const getAllMainDeviceInfo = () => async (dispatch, getState) => {
const { currentUserReducer } = getState(); const { currentUserReducer } = await getState();
const currentUserId = currentUserReducer.uid; const currentUserId = currentUserReducer.uid;
try { try {
dispatch(loadingAction(true)); dispatch(loadingAction(true));
...@@ -104,7 +104,7 @@ const getBreakerStatus = async deviceId => { ...@@ -104,7 +104,7 @@ const getBreakerStatus = async deviceId => {
const breakerStatus = tempBreakerStatus.SM1.BK_S; const breakerStatus = tempBreakerStatus.SM1.BK_S;
return breakerStatus; return breakerStatus;
} catch (e) { } catch (e) {
// console.log(e || 'error'); console.log(e || 'error');
return 0; return 0;
} }
}; };
import { fireStore } from '../../firebase';
export const GET_CURRENT_USER = 'GET_CURRENT_USER'; export const GET_CURRENT_USER = 'GET_CURRENT_USER';
export const getCurrentUserAction = ( export const getCurrentUserAction = (displayName, email, emailVerified, phoneNumber, uid, photoURL) => ({
displayName,
email,
emailVerified,
phoneNumber,
uid,
firstName,
lastName,
profileImg
) => ({
type: GET_CURRENT_USER, type: GET_CURRENT_USER,
displayName, displayName,
email, email,
emailVerified, emailVerified,
phoneNumber, phoneNumber,
uid, uid,
firstName, photoURL,
lastName,
profileImg,
}); });
export const getCurrentUser = user => async dispatch => { export const getCurrentUser = user => async dispatch => {
const { displayName, email, emailVerified, uid } = user; const { displayName, email, emailVerified, uid, phoneNumber, photoURL } = user;
const currentUser = await fireStore
.collection('user')
.doc(uid)
.get();
const currentUserData = await currentUser.data();
dispatch( dispatch(getCurrentUserAction(displayName, email, emailVerified, phoneNumber, uid, photoURL));
getCurrentUserAction(
displayName,
email,
emailVerified,
currentUserData.phoneNumber,
uid,
currentUserData.firstName,
currentUserData.lastName
)
);
}; };
export const updateUser = userData => async (dispatch, getState) => { export const updateUser = userData => async (dispatch, getState) => {
const { currentUserReducer } = getState(); const { currentUserReducer } = getState();
const { displayName, email, emailVerified, uid, profileImg } = currentUserReducer; const { displayName, email, emailVerified, uid, phoneNumber, photoURL } = currentUserReducer;
const ref = fireStore.collection('user').doc(uid);
const newUserData = {
firstName: userData.firstName,
lastName: userData.lastName,
phoneNumber: userData.phoneNumber,
};
try { try {
dispatch( await user.updateProfile({
getCurrentUserAction( displayName: `${userData.firstName} ${userData.lastName}`,
displayName, phoneNumber: userData.phoneNumber,
email, });
emailVerified,
userData.phoneNumber, const newDisplayName = `${userData.firstName} ${userData.lastName}`;
uid, const newPhoneNumber = userData.phoneNumber;
userData.firstName,
userData.lastName, dispatch(getCurrentUserAction(newDisplayName, email, emailVerified, uid, newPhoneNumber, photoURL));
profileImg
)
);
await ref.update(newUserData);
} catch (error) { } catch (error) {
console.log('Update user failed!', error); console.log('Update user failed!', error);
} }
......
...@@ -6,9 +6,7 @@ const initState = { ...@@ -6,9 +6,7 @@ const initState = {
emailVerified: '', emailVerified: '',
phoneNumber: '', phoneNumber: '',
uid: '', uid: '',
firstName: '', photoURL: '',
lastName: '',
profileImg: '',
}; };
const currentUserReducer = (state = initState, action) => { const currentUserReducer = (state = initState, action) => {
...@@ -21,9 +19,7 @@ const currentUserReducer = (state = initState, action) => { ...@@ -21,9 +19,7 @@ const currentUserReducer = (state = initState, action) => {
emailVerified: action.emailVerified, emailVerified: action.emailVerified,
phoneNumber: action.phoneNumber, phoneNumber: action.phoneNumber,
uid: action.uid, uid: action.uid,
firstName: action.firstName, photoURL: action.photoURL,
lastName: action.lastName,
profileImg: action.profileImg,
}; };
default: default:
return state; return state;
......
...@@ -63,6 +63,7 @@ export default class ChangePasswordScreen extends React.Component { ...@@ -63,6 +63,7 @@ export default class ChangePasswordScreen extends React.Component {
this.props.navigation.navigate('Login'); this.props.navigation.navigate('Login');
await AsyncStorage.clear(); await AsyncStorage.clear();
}; };
render() { render() {
const { changeSuccessModal, errorModal } = this.state; const { changeSuccessModal, errorModal } = this.state;
return ( return (
......
...@@ -7,6 +7,7 @@ import { theme, color } from '../../../constants/Styles'; ...@@ -7,6 +7,7 @@ import { theme, color } from '../../../constants/Styles';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { width } from '../../../constants/Layout'; import { width } from '../../../constants/Layout';
import { isIphoneX } from '../../../utils/isPhoneX'; import { isIphoneX } from '../../../utils/isPhoneX';
import AsyncStorage from '@react-native-community/async-storage';
const BUTTONS = ['Logout', 'Cancel']; const BUTTONS = ['Logout', 'Cancel'];
const DESTRUCTIVE_INDEX = 0; const DESTRUCTIVE_INDEX = 0;
...@@ -31,7 +32,7 @@ class ProfileScreen extends React.Component { ...@@ -31,7 +32,7 @@ class ProfileScreen extends React.Component {
}); });
render() { render() {
const { email, phoneNumber, firstName, lastName, profileImg } = this.props; const { email, phoneNumber, profileImg, displayName } = this.props;
return ( return (
<View style={theme.container}> <View style={theme.container}>
<View style={styles.headerStyle}> <View style={styles.headerStyle}>
...@@ -49,7 +50,7 @@ class ProfileScreen extends React.Component { ...@@ -49,7 +50,7 @@ class ProfileScreen extends React.Component {
</View> </View>
<View style={styles.infoContainer}> <View style={styles.infoContainer}>
<Text style={theme.description}>Name</Text> <Text style={theme.description}>Name</Text>
<Text style={styles.textInfo}>{firstName + ' ' + lastName || '-'}</Text> <Text style={styles.textInfo}>{displayName || '-'}</Text>
<View style={styles.seperator} /> <View style={styles.seperator} />
<Text style={theme.description}>Email</Text> <Text style={theme.description}>Email</Text>
<Text style={styles.textInfo}>{email || '-'}</Text> <Text style={styles.textInfo}>{email || '-'}</Text>
...@@ -79,7 +80,7 @@ class ProfileScreen extends React.Component { ...@@ -79,7 +80,7 @@ class ProfileScreen extends React.Component {
async buttonIndex => { async buttonIndex => {
if (buttonIndex === 0) { if (buttonIndex === 0) {
this.props.navigation.navigate('Login'); this.props.navigation.navigate('Login');
await AsyncStorage.clear(); await AsyncStorage.removeItem('RememberedLogin');
} }
} }
); );
......
...@@ -90,7 +90,7 @@ class SmartMeterScreen extends PureComponent { ...@@ -90,7 +90,7 @@ class SmartMeterScreen extends PureComponent {
<SearchBar <SearchBar
containerStyle={styles.searchBarContainer} containerStyle={styles.searchBarContainer}
inputContainerStyle={styles.searchBarInputContainer} inputContainerStyle={styles.searchBarInputContainer}
inputStyle={theme.normalText} inputStyle={styles.searchBarInput}
lightTheme lightTheme
placeholder="Search..." placeholder="Search..."
onChangeText={text => this.updateSearch(text)} onChangeText={text => this.updateSearch(text)}
...@@ -151,6 +151,8 @@ const styles = StyleSheet.create({ ...@@ -151,6 +151,8 @@ const styles = StyleSheet.create({
height: 35, height: 35,
borderColor: '#c7cad1', borderColor: '#c7cad1',
}, },
searchBarInput: { fontFamily: 'Avenir-Roman', fontSize: 14, color: color.grey, marginTop: 3 },
}); });
const mapStateToProps = state => ({ const mapStateToProps = state => ({
......
...@@ -35,6 +35,8 @@ class AuthLoadingScreen extends Component { ...@@ -35,6 +35,8 @@ class AuthLoadingScreen extends Component {
// Preload data from an external API && Preload data using AsyncStorage // Preload data from an external API && Preload data using AsyncStorage
const alreadyLaunched = await AsyncStorage.getItem('alreadyLaunched'); const alreadyLaunched = await AsyncStorage.getItem('alreadyLaunched');
const RememberedLogin = await AsyncStorage.getItem('RememberedLogin'); const RememberedLogin = await AsyncStorage.getItem('RememberedLogin');
console.log('alreadyLaunched', alreadyLaunched);
console.log('RememberedLogin', RememberedLogin);
app.auth().onAuthStateChanged(async user => { app.auth().onAuthStateChanged(async user => {
if (user) { if (user) {
......
...@@ -20,39 +20,44 @@ export default class LoginScreen extends Component { ...@@ -20,39 +20,44 @@ export default class LoginScreen extends Component {
handleSignIn = async values => { handleSignIn = async values => {
const { email, password } = values; const { email, password } = values;
try { try {
await app.auth().signInWithEmailAndPassword(email, password); const userCredential = await app.auth().signInWithEmailAndPassword(email, password);
await this.success(); if (userCredential) {
await AsyncStorage.setItem('alreadyLaunched', 'true');
if (this.state.isCheck === true) {
await AsyncStorage.setItem('RememberedLogin', 'true');
}
this.props.navigation.navigate('Main');
}
} catch (error) { } catch (error) {
alert(error); const errorCode = error.code;
} const errorMessage = error.message;
}; if (errorCode === 'auth/wrong-password') {
alert('Wrong password.');
success = async () => { } else if (errorCode === 'auth/user-not-found') {
try { alert('User not Found.');
await AsyncStorage.setItem('alreadyLaunched', 'true'); } else {
this.state.isCheck === true && (await AsyncStorage.setItem('RememberedLogin', 'true')); alert(errorMessage);
this.props.navigation.navigate('Main');
} catch (e) {
alert(e);
}
};
componentDidMount = async () => {
const currentUser = await app.auth().currentUser;
if (currentUser) {
try {
await app.auth().signOut();
} catch (error) {
alert(error);
} }
} }
}; };
// componentDidMount = async () => {
// const currentUser = await app.auth().currentUser;
// if (currentUser) {
// try {
// await app.auth().signOut();
// await AsyncStorage.removeItem('RememberedLogin');
// } catch (error) {
// alert(error);
// }
// }
// };
render() { render() {
return ( return (
<KeyboardAvoidingView style={theme.introContainer}> <KeyboardAvoidingView style={theme.introContainer}>
<Text style={[theme.title, theme.textDark]}>Login</Text> <Text style={[theme.title, theme.textDark]}>Login</Text>
<LoginForm onSubmit={this.handleSignIn}> <LoginForm onSubmit={async values => await this.handleSignIn(values)}>
<View style={[theme.rowContainer, theme.mt1, { justifyContent: 'space-between' }]}> <View style={[theme.rowContainer, theme.mt1, { justifyContent: 'space-between' }]}>
<View style={[theme.rowContainer]}> <View style={[theme.rowContainer]}>
<CheckBox <CheckBox
......
import ApolloClient from 'apollo-boost';
import gql from 'graphql-tag';
const BASE_URL = 'https://gqlv2.nexpie.io';
const READ_SHADOW = gql`
query readShadow($deviceid: String!) {
shadow(deviceid: $deviceid) {
deviceid
value
}
}
`;
const WRITE_SHADOW = gql`
mutation writeShadow($deviceid: String!, $data: String!) {
writeShadow(deviceid: $deviceid, value: $data) {
deviceid
value
}
}
`;
const client = new ApolloClient({
uri: BASE_URL,
request: async operation => {
// const token = localStorage.getItem('token');
operation.setContext({
headers: {
authorization: token ? `Bearer ${token}` : '',
},
});
},
});
export { client };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment