Commit 81ccd73d by HaOuiha

update apollo

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