From 7d6a1594716ba57ea84c3c4c3fc5f9ccd98ef29c Mon Sep 17 00:00:00 2001 From: Dita Aji Pratama Date: Thu, 19 Jun 2025 10:39:04 +0700 Subject: [PATCH] Pertemuan ke-11: Membuat halaman profile (Username: tester, Password: tester) --- screens/ScreenProfile.js | 108 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 105 insertions(+), 3 deletions(-) diff --git a/screens/ScreenProfile.js b/screens/ScreenProfile.js index 7d3b512..6e934b5 100644 --- a/screens/ScreenProfile.js +++ b/screens/ScreenProfile.js @@ -71,11 +71,113 @@ const ScreenProfile = ({ navigation }) => { }; HttpRequest(url, body, config, handleLogout); }; + + const handleProfileDetail = (data) => { + if (data.status === 'success') { + setProfileId(data.data.id); + setProfileName(data.data.name); + setProfileSex(data.data.sex); + setProfileDob(data.data.dob); + if (data.data.dob !== null) { + const tanggalLahir = new Date(data.data.dob); + const today = new Date(); + const umur = today.getFullYear() - tanggalLahir.getFullYear(); + const bulanSekarang = today.getMonth() - tanggalLahir.getMonth(); + let umurTerupdate; + if (bulanSekarang < 0 || (bulanSekarang === 0 && today.getDate() < tanggalLahir.getDate())) { umurTerupdate = umur - 1; } + else { umurTerupdate = umur; } + setProfileAge(umurTerupdate); + } + } + } + const reqProfileDetail = async () => { + try { + const url = 'https://asrul.costafuture.com/api/checkcare/profile/detail'; + const body = {}; + const auth_token = await AsyncStorage.getItem('auth_token'); + const config = { + headers: { + Authorization: `Bearer ${auth_token}` + } + }; + if (auth_token !== null) { HttpRequest(url, body, config, handleProfileDetail); } + }catch(error){ + console.log(error.stack); + } + }; + + const handleProfileEdit = (data) => { + reqProfileDetail(); + }; + const reqProfileEdit = async () => { + try { + const url = 'https://asrul.costafuture.com/api/checkcare/profile/edit'; + const body = { + id:profileId, + name:profileName, + dob:profileDob, + sex:profileSex + }; + const auth_token = await AsyncStorage.getItem('auth_token'); + const config = { + headers: { + Authorization: `Bearer ${auth_token}` + } + }; + if (auth_token !== null) { HttpRequest(url, body, config, handleProfileEdit); } + }catch(error){ + console.log(error.stack); + } + }; + + useEffect(() => { + getToken(); + reqProfileDetail(); + }, []); return ( - - Ini halaman Profile - + + {token ? ( + + ID: {profileId} + + setProfileName(text)} + value={profileName} + /> + {profileSex} + + {profileAge} Tahun + {profileDob} + +