Skip to content

Commit

Permalink
Persistent Data
Browse files Browse the repository at this point in the history
  • Loading branch information
MengTo committed May 14, 2019
1 parent db251fe commit 2243424
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 18 deletions.
14 changes: 7 additions & 7 deletions App.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ const initialState = {
const reducer = (state = initialState, action) => {
switch (action.type) {
case "OPEN_MENU":
return { action: "openMenu" };
return { ...state, action: "openMenu" };
case "CLOSE_MENU":
return { action: "closeMenu" };
return { ...state, action: "closeMenu" };
case "UPDATE_NAME":
return { name: action.name };
return { ...state, name: action.name };
case "OPEN_CARD":
return { action: "openCard" };
return { ...state, action: "openCard" };
case "CLOSE_CARD":
return { action: "closeCard" };
return { ...state, action: "closeCard" };
case "OPEN_LOGIN":
return { action: "openLogin" };
return { ...state, action: "openLogin" };
case "CLOSE_LOGIN":
return { action: "closeLogin" };
return { ...state, action: "closeLogin" };
default:
return state;
}
Expand Down
4 changes: 2 additions & 2 deletions components/Avatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ class Avatar extends React.Component {
})
.then(response => response.json())
.then(response => {
console.log(response);
// console.log(response);

this.setState({
photo: response[0].photo
});

this.props.updateName(response[0].name);
// this.props.updateName(response[0].name);
});
}

Expand Down
26 changes: 21 additions & 5 deletions components/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Animated, TouchableOpacity, Dimensions } from "react-native";
import { Icon } from "expo";
import MenuItem from "./MenuItem";
import { connect } from "react-redux";
import { AsyncStorage } from "react-native";

const screenWidth = Dimensions.get("window").width;
var cardWidth = screenWidth;
Expand All @@ -20,6 +21,11 @@ function mapDispatchToProps(dispatch) {
closeMenu: () =>
dispatch({
type: "CLOSE_MENU"
}),
updateName: name =>
dispatch({
type: "UPDATE_NAME",
name
})
};
}
Expand Down Expand Up @@ -53,6 +59,14 @@ class Menu extends React.Component {
}
};

handleMenu = index => {
if (index === 3) {
this.props.closeMenu();
this.props.updateName();
AsyncStorage.clear();
}
};

render() {
return (
<AnimatedContainer style={{ top: this.state.top }}>
Expand All @@ -77,12 +91,14 @@ class Menu extends React.Component {
</TouchableOpacity>
<Content>
{items.map((item, index) => (
<MenuItem
<TouchableOpacity
key={index}
icon={item.icon}
title={item.title}
text={item.text}
/>
onPress={() => {
this.handleMenu(index);
}}
>
<MenuItem icon={item.icon} title={item.title} text={item.text} />
</TouchableOpacity>
))}
</Content>
</AnimatedContainer>
Expand Down
33 changes: 31 additions & 2 deletions components/ModalLogin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Loading from "./Loading";
import { Alert, Animated, Dimensions } from "react-native";
import { connect } from "react-redux";
import firebase from "./Firebase";
import { AsyncStorage } from "react-native";

const screenHeight = Dimensions.get("window").height;

Expand All @@ -23,6 +24,11 @@ function mapDispatchToProps(dispatch) {
closeLogin: () =>
dispatch({
type: "CLOSE_LOGIN"
}),
updateName: name =>
dispatch({
type: "UPDATE_NAME",
name
})
};
}
Expand All @@ -40,6 +46,10 @@ class ModalLogin extends React.Component {
translateY: new Animated.Value(0)
};

componentDidMount() {
this.retrieveName();
}

componentDidUpdate() {
if (this.props.action === "openLogin") {
Animated.timing(this.state.top, {
Expand Down Expand Up @@ -69,8 +79,24 @@ class ModalLogin extends React.Component {
}
}

storeName = async name => {
try {
await AsyncStorage.setItem("name", name);
} catch (error) {}
};

retrieveName = async () => {
try {
const name = await AsyncStorage.getItem("name");
if (name !== null) {
console.log(name);
this.props.updateName(name);
}
} catch (error) {}
};

handleLogin = () => {
console.log(this.state.email, this.state.password);
// console.log(this.state.email, this.state.password);

this.setState({ isLoading: true });

Expand All @@ -84,7 +110,7 @@ class ModalLogin extends React.Component {
Alert.alert("Error", error.message);
})
.then(response => {
console.log(response);
// console.log(response);

this.setState({ isLoading: false });

Expand All @@ -93,6 +119,9 @@ class ModalLogin extends React.Component {

Alert.alert("Congrats", "You've logged successfully!");

this.storeName(response.user.email);
this.props.updateName(response.user.email);

setTimeout(() => {
this.props.closeLogin();
this.setState({ isSuccessful: false });
Expand Down
12 changes: 10 additions & 2 deletions screens/HomeScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ class HomeScreen extends React.Component {
}
};

handleAvatar = () => {
if (this.props.name) {
this.props.openMenu();
} else {
this.props.openLogin();
}
};

render() {
return (
<RootView>
Expand All @@ -135,7 +143,7 @@ class HomeScreen extends React.Component {
<ScrollView style={{ height: "100%" }}>
<TitleBar>
<TouchableOpacity
onPress={this.props.openLogin}
onPress={this.handleAvatar}
style={{ position: "absolute", top: 0, left: 20 }}
>
<Avatar />
Expand Down Expand Up @@ -171,7 +179,7 @@ class HomeScreen extends React.Component {
if (loading) return <Message>Loading...</Message>;
if (error) return <Message>Error...</Message>;

console.log(data.cardsCollection.items);
// console.log(data.cardsCollection.items);

return (
<CardsContainer>
Expand Down

0 comments on commit 2243424

Please sign in to comment.