2018-08-19 20:02:43 -05:00
|
|
|
// @flow
|
2019-09-21 15:45:07 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import { View, Text, Switch, TextInput, AsyncStorage } from "react-native";
|
|
|
|
|
import IconButton from "../components/IconButton";
|
|
|
|
|
import { palette } from "../ui-theme";
|
|
|
|
|
import { compose, withState, withHandlers, lifecycle } from "recompose";
|
|
|
|
|
import { withRouterContext } from "../enhancers/routeEnhancers";
|
|
|
|
|
import { getLocation } from "../apis/PositionApi";
|
|
|
|
|
import Snackbar from "react-native-snackbar";
|
2018-08-19 20:02:43 -05:00
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
zipcode: ?string,
|
|
|
|
|
loading: boolean,
|
|
|
|
|
setLoading: (l: boolean) => void,
|
|
|
|
|
setZip: (zip: string | null) => void,
|
|
|
|
|
saveZip: (zip: number) => Promise<void>,
|
|
|
|
|
error: string,
|
|
|
|
|
setError: (e: string) => void,
|
|
|
|
|
toggleGPS: (a: boolean) => Promise<void>,
|
|
|
|
|
router: {
|
|
|
|
|
history: {
|
2019-09-21 15:45:07 +00:00
|
|
|
replace: (route: string) => void
|
|
|
|
|
}
|
2018-08-19 20:02:43 -05:00
|
|
|
},
|
2019-09-21 15:45:07 +00:00
|
|
|
isLoggedIn: boolean
|
2018-08-19 20:02:43 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const ProfilePage = (props: Props) => {
|
|
|
|
|
const {
|
|
|
|
|
zipcode,
|
|
|
|
|
loading,
|
|
|
|
|
setZip,
|
|
|
|
|
saveZip,
|
|
|
|
|
error,
|
|
|
|
|
toggleGPS,
|
|
|
|
|
router: { history },
|
2019-09-21 15:45:07 +00:00
|
|
|
isLoggedIn
|
2018-08-19 20:02:43 -05:00
|
|
|
} = props;
|
2019-09-21 15:45:07 +00:00
|
|
|
const usingGPS = zipcode === "usegps";
|
2018-08-19 20:02:43 -05:00
|
|
|
return (
|
2019-09-21 15:45:07 +00:00
|
|
|
<View style={{ flex: 1, flexDirection: "column", padding: 15 }}>
|
|
|
|
|
<Text style={{ fontSize: 32, marginBottom: 20, color: "#555555" }}>Profile</Text>
|
|
|
|
|
<View style={{ flexDirection: "row", justifyContent: "space-between" }}>
|
2018-08-19 20:02:43 -05:00
|
|
|
<Text style={{ fontSize: 18 }}>Use My Location</Text>
|
|
|
|
|
<Switch value={usingGPS} onValueChange={toggleGPS} />
|
|
|
|
|
</View>
|
|
|
|
|
{!usingGPS && (
|
2019-09-21 15:45:07 +00:00
|
|
|
<View style={{ flexDirection: "row" }}>
|
2018-08-19 20:02:43 -05:00
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
flex: 1,
|
2019-09-21 15:45:07 +00:00
|
|
|
flexDirection: "column",
|
|
|
|
|
justifyContent: "flex-end",
|
|
|
|
|
height: 60
|
|
|
|
|
}}
|
|
|
|
|
>
|
2018-08-19 20:02:43 -05:00
|
|
|
<TextInput
|
|
|
|
|
editable={!loading}
|
|
|
|
|
value={zipcode}
|
|
|
|
|
placeholder="zipcode"
|
|
|
|
|
onChangeText={setZip}
|
|
|
|
|
onEndEditing={saveZip}
|
|
|
|
|
keyboardType="numeric"
|
|
|
|
|
style={{
|
2019-09-21 15:45:07 +00:00
|
|
|
fontSize: 18
|
2018-08-19 20:02:43 -05:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</View>
|
|
|
|
|
</View>
|
|
|
|
|
)}
|
2019-09-21 15:45:07 +00:00
|
|
|
{!!error && <Text style={{ fontSize: 16, color: "red" }}>{error}</Text>}
|
2018-08-19 20:02:43 -05:00
|
|
|
<View style={{ marginTop: 20 }}>
|
|
|
|
|
{isLoggedIn && (
|
|
|
|
|
<IconButton
|
|
|
|
|
glyph="exit-to-app"
|
|
|
|
|
text="Logout"
|
2019-09-21 15:45:07 +00:00
|
|
|
onPress={() => history.push("/logout")}
|
2018-08-19 20:02:43 -05:00
|
|
|
color={palette.accentColor}
|
|
|
|
|
textStyle={{ color: palette.accentColor }}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
{!isLoggedIn && (
|
|
|
|
|
<IconButton
|
|
|
|
|
glyph="exit-to-app"
|
|
|
|
|
text="Login"
|
2019-09-21 15:45:07 +00:00
|
|
|
onPress={() => history.push("/login")}
|
2018-08-19 20:02:43 -05:00
|
|
|
color={palette.accentColor}
|
|
|
|
|
textStyle={{ color: palette.accentColor }}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</View>
|
|
|
|
|
</View>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
|
withRouterContext,
|
2019-09-21 15:45:07 +00:00
|
|
|
withState("loading", "setLoading", false),
|
|
|
|
|
withState("zipcode", "setZip", null),
|
|
|
|
|
withState("error", "setError", ""),
|
2018-08-19 20:02:43 -05:00
|
|
|
withHandlers({
|
|
|
|
|
saveZip: (props: Props) => async () => {
|
2019-09-21 15:45:07 +00:00
|
|
|
props.setError("");
|
2018-08-19 20:02:43 -05:00
|
|
|
if (!props.zipcode || props.zipcode.length !== 5) {
|
2019-09-21 15:45:07 +00:00
|
|
|
props.setError("Zipcode must be five digits");
|
2018-08-19 20:02:43 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
props.setLoading(true);
|
2019-09-21 15:45:07 +00:00
|
|
|
await AsyncStorage.setItem("zipcode", props.zipcode);
|
2018-08-19 20:02:43 -05:00
|
|
|
props.setLoading(false);
|
|
|
|
|
getLocation();
|
2019-09-21 15:45:07 +00:00
|
|
|
Snackbar.show({ title: "Zipcode updated." });
|
|
|
|
|
}
|
2018-09-16 09:58:13 -05:00
|
|
|
}),
|
|
|
|
|
withHandlers({
|
2018-08-19 20:02:43 -05:00
|
|
|
toggleGPS: (props: Props) => async (useGPS: boolean) => {
|
2018-09-16 09:58:13 -05:00
|
|
|
try {
|
|
|
|
|
props.setLoading(true);
|
2019-09-21 15:45:07 +00:00
|
|
|
props.setZip(useGPS ? "usegps" : "");
|
|
|
|
|
await AsyncStorage.setItem("zipcode", "usegps");
|
2018-09-16 09:58:13 -05:00
|
|
|
} catch (error) {
|
2019-09-21 15:45:07 +00:00
|
|
|
console.error(error); //eslint-disable-line no-console
|
|
|
|
|
props.setZip(props.zipcode || "");
|
2018-09-16 09:58:13 -05:00
|
|
|
} finally {
|
|
|
|
|
props.setLoading(false);
|
|
|
|
|
}
|
2018-08-19 20:02:43 -05:00
|
|
|
},
|
2018-11-18 12:07:16 -06:00
|
|
|
getZipcode: ({ setLoading, setZip }) => async () => {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
try {
|
2019-09-21 15:45:07 +00:00
|
|
|
setZip(await AsyncStorage.getItem("zipcode"));
|
2018-11-18 12:07:16 -06:00
|
|
|
} catch (error) {
|
2019-09-21 15:45:07 +00:00
|
|
|
console.error(error); //eslint-disable-line no-console
|
2018-11-18 12:07:16 -06:00
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
2019-09-21 15:45:07 +00:00
|
|
|
}
|
2018-11-18 12:07:16 -06:00
|
|
|
}),
|
|
|
|
|
lifecycle({
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
this.props.getZipcode();
|
2019-09-21 15:45:07 +00:00
|
|
|
}
|
2018-08-19 20:02:43 -05:00
|
|
|
})
|
|
|
|
|
)(ProfilePage);
|