-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserProfileSettings.tsx
105 lines (86 loc) · 3.67 KB
/
UserProfileSettings.tsx
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
import React, {useContext, useState} from 'react';
import {
Grid,
Button,
TextField,
useMediaQuery,
Autocomplete
} from '@mui/material';
import EditIcon from '@mui/icons-material/Edit';
import SaveIcon from '@mui/icons-material/Save';
import {useTheme} from "@mui/system";
import countries from "i18n-iso-countries";
// Import the languages you want to use
import enLocale from "i18n-iso-countries/langs/en.json";
import itLocale from "i18n-iso-countries/langs/it.json";
import {UserContext} from "../MyProviders/MyProviders";
import {useAuth} from "../AuthContext/AuthContext";
const UserProfileSettings = React.memo(() => {
const {currentUser} = useContext(UserContext);
const {updateProfile} = useAuth();
const [isEditing, setIsEditing] = useState(false);
const [userName, setUserName] = useState(currentUser ? currentUser.user_name : "");
const [email, setEmail] = useState(currentUser ? currentUser.email : "");
const [selectedCountry, setSelectedCountry] = useState({label: "Ukrainian", value: "UK"});
const theme = useTheme();
const mdUp = useMediaQuery(theme.breakpoints.up('md'))
const handleEditClick = () => {
setIsEditing(true);
};
const handleSaveClick = async () => {
setIsEditing(false);
await updateProfile(userName, email, selectedCountry.value);
};
const selectCountryHandler = (value: { label: string; value: string } | null) => {
if (value) {
setSelectedCountry(value);
}
};
const countryObj = countries.getNames("en", {select: "official"});
const countryArr = Object.entries(countryObj).map(([key, value]) => {
return {
label: value,
value: key
};
});
return (
<React.Fragment>
<div className={"field_container"}>
<TextField label={"UserName:"} disabled={!isEditing} fullWidth value={userName}
onChange={(e) => setUserName(e.target.value)}/>
<TextField label={"Email:"} disabled={!isEditing} fullWidth value={email}
onChange={(e) => setEmail(e.target.value)}/>
{/*<TextField label={"Password:"} disabled={!isEditing} fullWidth type="password" value={password}*/}
{/* onChange={(e) => setPassword(e.target.value)}/>*/}
<Autocomplete
disabled={!isEditing}
fullWidth
disablePortal
id="combo-box-demo"
options={countryArr}
value={selectedCountry}
onChange={(event: any, newValue: { label: string; value: string } | null) => {
selectCountryHandler(newValue);
}}
renderInput={(params) => <TextField {...params} fullWidth label="Country:"/>}
/>
</div>
<Grid container justifyContent={!mdUp ? "flex-end" : "flex-start"}>
<Grid item xs={6} lg={12}>
{isEditing ? (
<Button fullWidth variant={"contained"} color={"primary"} onClick={handleSaveClick}
startIcon={<SaveIcon/>}>
Save
</Button>
) : (
<Button fullWidth variant={"contained"} color={"primary"} onClick={handleEditClick}
startIcon={<EditIcon/>}>
Edit
</Button>
)}
</Grid>
</Grid>
</React.Fragment>
);
});
export default UserProfileSettings;