-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2487db2
commit f9a8e1d
Showing
7 changed files
with
165 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,39 +13,74 @@ import { | |
IonSelectOption, | ||
} from '@ionic/react'; | ||
import { folder } from 'ionicons/icons'; | ||
import { useEffect, useState } from 'react'; | ||
import { useContext, useEffect, useRef, useState } from 'react'; | ||
|
||
import Layout from 'components/layout'; | ||
import { AuthContext } from 'contexts/auth'; | ||
import { getUserData, updateUserData } from 'services/firebase'; | ||
import { UserData } from 'types/userData'; | ||
import { useHistory } from 'react-router'; | ||
|
||
interface User { | ||
name: string; | ||
bio: string; | ||
gender: string; | ||
email: string; | ||
phone: string; | ||
address: string; | ||
} | ||
const initialData: UserData = { | ||
id: '1', | ||
fullName: 'John Doe', | ||
gender: 'male', | ||
email: '[email protected]', | ||
phoneNumber: '12345', | ||
address: 'USA', | ||
}; | ||
|
||
const EditProfile: React.FC = () => { | ||
const [userData, setUserData] = useState<User>({ | ||
name: 'John Doe', | ||
bio: 'Hello World', | ||
gender: 'male', | ||
email: '[email protected]', | ||
phone: '08123456789', | ||
address: 'Jl. Mawar No. 1', | ||
}); | ||
const [userData, setUserData] = useState<UserData>(initialData); | ||
const [gender, setGender] = useState<'male' | 'female'>('male'); | ||
|
||
const fullNameRef = useRef<HTMLIonInputElement>(null); | ||
const emailRef = useRef<HTMLIonInputElement>(null); | ||
const phoneNumberRef = useRef<HTMLIonInputElement>(null); | ||
const addressRef = useRef<HTMLIonInputElement>(null); | ||
|
||
const { currentUser } = useContext(AuthContext); | ||
|
||
const history = useHistory(); | ||
|
||
useEffect(() => { | ||
setUserData({ | ||
name: 'John Doe', | ||
bio: 'Hello World', | ||
gender: 'male', | ||
email: '[email protected]', | ||
phone: '08123456789', | ||
address: 'Jl. Mawar No. 1', | ||
}); | ||
}, []); | ||
const fetchUserData = async () => { | ||
try { | ||
const data = await getUserData(currentUser); | ||
|
||
if (!data) return; | ||
|
||
setUserData(data); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}; | ||
|
||
fetchUserData(); | ||
}, [currentUser]); | ||
|
||
const handleEditUserData = async () => { | ||
const fullName = fullNameRef.current?.value as string; | ||
const email = emailRef.current?.value as string; | ||
const phoneNumber = phoneNumberRef.current?.value as string; | ||
const address = addressRef.current?.value as string; | ||
|
||
const updatedUser = { | ||
fullName: fullName ?? userData.fullName, | ||
gender: gender ?? userData.gender, | ||
email: email ?? userData.email, | ||
phoneNumber: phoneNumber ?? userData.phoneNumber, | ||
address: address ?? userData.address, | ||
}; | ||
|
||
try { | ||
await updateUserData(currentUser, updatedUser); | ||
|
||
history.replace('/main/profile'); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}; | ||
|
||
return ( | ||
<Layout title="Edit Profil"> | ||
|
@@ -76,14 +111,19 @@ const EditProfile: React.FC = () => { | |
<IonLabel position="fixed" color="primary"> | ||
Nama | ||
</IonLabel> | ||
<IonInput value={userData.name} inputMode="text" clearInput /> | ||
<IonInput | ||
ref={fullNameRef} | ||
value={userData.fullName} | ||
inputMode="text" | ||
clearInput | ||
/> | ||
</IonItem> | ||
|
||
<IonItem> | ||
<IonLabel position="fixed" color="primary"> | ||
Bio | ||
</IonLabel> | ||
<IonInput value={userData.bio} inputMode="text" clearInput /> | ||
<IonInput value="Hello World" inputMode="text" clearInput /> | ||
</IonItem> | ||
|
||
<IonItem> | ||
|
@@ -92,7 +132,7 @@ const EditProfile: React.FC = () => { | |
</IonLabel> | ||
<IonSelect | ||
value={userData.gender} | ||
onIonChange={(e) => e.detail.value} | ||
onIonChange={(e: CustomEvent) => setGender(e.detail.value)} | ||
> | ||
<IonSelectOption value="male">Laki-Laki</IonSelectOption> | ||
<IonSelectOption value="female">Perempuan</IonSelectOption> | ||
|
@@ -103,15 +143,20 @@ const EditProfile: React.FC = () => { | |
<IonLabel position="fixed" color="primary"> | ||
</IonLabel> | ||
<IonInput value={userData.email} inputMode="email" clearInput /> | ||
<IonInput | ||
ref={emailRef} | ||
value={userData.email} | ||
inputMode="email" | ||
clearInput | ||
/> | ||
</IonItem> | ||
|
||
<IonItem> | ||
<IonLabel position="fixed" color="primary"> | ||
No. Telp | ||
</IonLabel> | ||
<IonInput | ||
value={userData.phone} | ||
value={userData.phoneNumber} | ||
inputMode="tel" | ||
maxlength={12} | ||
clearInput | ||
|
@@ -123,6 +168,7 @@ const EditProfile: React.FC = () => { | |
Alamat | ||
</IonLabel> | ||
<IonInput | ||
ref={addressRef} | ||
value={userData.address} | ||
inputMode="text" | ||
clearInput | ||
|
@@ -138,7 +184,7 @@ const EditProfile: React.FC = () => { | |
shape="round" | ||
expand="block" | ||
color="primary" | ||
routerLink="/main/profile" | ||
onClick={handleEditUserData} | ||
> | ||
Simpan | ||
</IonButton> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import { | |
IonList, | ||
IonRow, | ||
IonText, | ||
useIonToast, | ||
} from '@ionic/react'; | ||
import { | ||
callOutline, | ||
|
@@ -23,26 +24,50 @@ import Layout from 'components/layout'; | |
import { AuthContext } from 'contexts/auth'; | ||
import { getUserData } from 'services/firebase'; | ||
import { UserData } from 'types/userData'; | ||
import { useHistory } from 'react-router'; | ||
|
||
const initialData: UserData = { | ||
id: '1', | ||
fullName: 'John Doe', | ||
gender: 'male', | ||
email: '[email protected]', | ||
phoneNumber: '12345', | ||
address: 'USA', | ||
}; | ||
|
||
const Profile: React.FC = () => { | ||
const [userData, setUserData] = useState<UserData>(); | ||
const [isFetchData, setIsFetchData] = useState<boolean>(false); | ||
const authCtx = useContext(AuthContext); | ||
const [userData, setUserData] = useState<UserData>(initialData); | ||
const { currentUser, logout } = useContext(AuthContext); | ||
const [presentToast] = useIonToast(); | ||
const history = useHistory(); | ||
|
||
useEffect(() => { | ||
const fetchUserData = async () => { | ||
try { | ||
const data = await getUserData(authCtx.currentUser); | ||
const data = await getUserData(currentUser); | ||
|
||
if (!data) return; | ||
|
||
setUserData(data); | ||
console.log(userData); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}; | ||
|
||
fetchUserData(); | ||
}, []); | ||
}, [currentUser]); | ||
|
||
const handleLogout = async () => { | ||
await logout(); | ||
|
||
presentToast({ | ||
message: 'Anda telah keluar.', | ||
duration: 2000, | ||
color: 'danger', | ||
}); | ||
|
||
history.replace('/'); | ||
}; | ||
|
||
return ( | ||
<Layout title="Profil Saya"> | ||
|
@@ -67,7 +92,7 @@ const Profile: React.FC = () => { | |
}} | ||
> | ||
<h3 style={{ fontWeight: 'bold' }}> | ||
<IonText color="danger">full name</IonText> | ||
<IonText color="danger">{userData.fullName}</IonText> | ||
</h3> | ||
<p>Hello World</p> | ||
</IonText> | ||
|
@@ -93,22 +118,24 @@ const Profile: React.FC = () => { | |
<IonList> | ||
<IonItem> | ||
<IonIcon icon={maleOutline} slot="start" color="primary" /> | ||
<IonLabel>gender</IonLabel> | ||
<IonLabel> | ||
{userData.gender === 'male' ? 'Laki-Laki' : 'Perempuan'} | ||
</IonLabel> | ||
</IonItem> | ||
|
||
<IonItem> | ||
<IonIcon icon={mailOutline} slot="start" color="primary" /> | ||
<IonLabel>email</IonLabel> | ||
<IonLabel>{userData.email}</IonLabel> | ||
</IonItem> | ||
|
||
<IonItem> | ||
<IonIcon icon={callOutline} slot="start" color="primary" /> | ||
<IonLabel>nohp</IonLabel> | ||
<IonLabel>{userData.phoneNumber}</IonLabel> | ||
</IonItem> | ||
|
||
<IonItem> | ||
<IonIcon icon={mapOutline} slot="start" color="primary" /> | ||
<IonLabel>address</IonLabel> | ||
<IonLabel>{userData.address}</IonLabel> | ||
</IonItem> | ||
</IonList> | ||
</IonCol> | ||
|
@@ -121,7 +148,7 @@ const Profile: React.FC = () => { | |
shape="round" | ||
expand="block" | ||
color="danger" | ||
routerLink="/" | ||
onClick={handleLogout} | ||
> | ||
<IonIcon icon={logOut} slot="start" /> | ||
<IonLabel>Keluar</IonLabel> | ||
|
Oops, something went wrong.