forked from crowdbotics-apps/haileyshealthyhango-48467
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit.js
143 lines (136 loc) · 4.03 KB
/
edit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import React, { useState } from "react";
import {
TextInput,
View,
Alert,
Text,
TouchableOpacity,
ActivityIndicator
} from "react-native";
import { Avatar } from "react-native-elements";
import { inputStyles, Color, buttonStyles, styles } from "./styles";
import { getInitials, transformLabel } from "./utils";
import { updateUserById } from "./store";
import { useSelector, useDispatch } from "react-redux";
export const Button = props => (
<TouchableOpacity onPress={props.onPress} disabled={props.loading}>
<View style={[buttonStyles.view, props.buttonStyle]}>
{props.loading
? (
<ActivityIndicator color={Color.white} />
)
: (
<Text style={[buttonStyles.text, props.buttonTextStyle]}>{props.title}</Text>
)}
</View>
</TouchableOpacity>
);
export const InputContainer = props => (
<View>
<Text style={inputStyles.label}>{transformLabel(props.label)}</Text>
<View>
<TextInput
autoCapitalize="none"
style={[inputStyles.input, props.textInputStyle]}
placeholderTextColor={Color.steel}
underlineColorAndroid={"transparent"}
{...props}
/>
{!!props.error && <Text style={inputStyles.error}>{props.error}</Text>}
</View>
</View>
);
export const EditUser = props => {
const { user } = props;
const initials = getInitials(user);
const [form, setForm] = useState({
first_name: user.first_name,
last_name: user.last_name,
email: user.email,
bio: user.bio
});
// code below depends on the existence of any login module - update as needed.
const login = useSelector(state => {
return state?.login;
});
const api = useSelector(state => state.userProfile.api);
const dispatch = useDispatch();
const onUpdateForm = (key, value) => {
setForm({
...form,
[key]: value
});
};
const onSaveProfile = async () => {
const payload = { data: { ...form, id: user.id }, token: login?.token };
dispatch(updateUserById(payload))
.then(() =>
Alert.alert(
"User Updated",
"User information was successfully updated."
)
)
.catch(e => {
// handle your custom error message here
console.log(e.message);
Alert.alert(
"User Update Failed",
`An unexpected error happened: ${e.message}`
);
});
};
return (
<View style={props.editContainerStyle}>
<View style={styles.profileIcon}>
<Avatar
size="large"
rounded
icon={{ name: "user", type: "font-awesome" }}
title={initials}
containerStyle={[{ backgroundColor: Color.pink }, props.avatarStyle]}
/>
</View>
<InputContainer
keyboardType="default"
label="First Name"
placeholder="John"
onChangeText={value => onUpdateForm("first_name", value)}
value={form.first_name}
textInputStyle={props.textInputStyle}
/>
<InputContainer
keyboardType="default"
label="Last Name"
placeholder="Doe"
onChangeText={value => onUpdateForm("last_name", value)}
value={form.last_name}
textInputStyle={props.textInputStyle}
/>
<InputContainer
keyboardType="email-address"
label="Email Address"
placeholder="[email protected]"
onChangeText={value => onUpdateForm("email", value)}
value={form.email}
error={form.email ? "" : "E-mail address field is required."}
textInputStyle={props.textInputStyle}
/>
<InputContainer
label="Bio"
multiline={true}
numberOfLines={2}
placeholder="Write something about yourself."
onChangeText={value => onUpdateForm("bio", value)}
value={form.bio}
textInputStyle={props.textInputStyle}
/>
<Button
title="Save"
loading={api.loading === "pending"}
onPress={onSaveProfile}
buttonStyle = {props.buttonStyle}
buttonTextStyle = {props.buttonTextStyle}
/>
</View>
);
};