Gracefully handle backend failures in Expo app

This commit is contained in:
Adolfo Reyna
2026-02-20 19:25:38 -05:00
parent fe9fc8e3e4
commit 009f1ec792
14 changed files with 205 additions and 97 deletions

View File

@@ -29,12 +29,16 @@ const getName = async (key) => {
let CourseCard = ({ profileid, hideIcon, profileObj, twoCols }) => {
let [profile, setProfile] = useState(profileObj || {});
const safeProfile = profile || {};
const safeProfileInfo = safeProfile.profile || {};
const safeProfileObj = profileObj || {};
const safeProfileObjInfo = safeProfileObj.profile || {};
const navigation = useNavigation();
useEffect(() => {
let subscribed = true;
const getData = async () => {
if (profileObj._id) return 0;
if (safeProfileObj._id) return 0;
let cacheProfile = await getName(profileid);
if (cacheProfile && cacheProfile.profile) setProfile(cacheProfile);
let p = await API.getUserProfile(profileid).catch(() => { return {} });
@@ -47,13 +51,13 @@ let CourseCard = ({ profileid, hideIcon, profileObj, twoCols }) => {
}
}, [profileid]);
let icon = profile._id ? (!profile.isGroup ? "person-outline" : "group") : '';
icon = icon === "person-outline" && profile.subscription && profile.subscription > (new Date() - 0) ? "assignment-ind" : icon;
icon = icon === "group" && profile.isCourse ? "subscriptions" : icon;
let photoUrl = profile.data && profile.data.profileImg ? profile.data.profileImg : DefaultPhoto;
let icon = safeProfile._id ? (!safeProfile.isGroup ? "person-outline" : "group") : '';
icon = icon === "person-outline" && safeProfile.subscription && safeProfile.subscription > (new Date() - 0) ? "assignment-ind" : icon;
icon = icon === "group" && safeProfile.isCourse ? "subscriptions" : icon;
let photoUrl = safeProfile.data && safeProfile.data.profileImg ? safeProfile.data.profileImg : DefaultPhoto;
const onPress = () => {
return navigation.navigate('Profile', { profileid: profile._id })
return navigation.navigate('Profile', { profileid: safeProfile._id })
}
return (
@@ -64,28 +68,28 @@ let CourseCard = ({ profileid, hideIcon, profileObj, twoCols }) => {
{!hideIcon ? <Icon name={icon} size={18} /> : <></>}
</Text>
<Text>
{profile.profile && profile.profile.firstName} {profile.profile && profile.profile.lastName}
{safeProfileInfo.firstName} {safeProfileInfo.lastName}
</Text>
</Title>
<Paragraph numberOfLines={2}>
{profileObj.profile.description ? profileObj.profile.description : "We are working on this course description, soon to come!"}
{safeProfileObjInfo.description ? safeProfileObjInfo.description : "We are working on this course description, soon to come!"}
</Paragraph>
{profile.data ?
{safeProfile.data ?
<View style={{flexDirection: "row", marginTop:5}}>
<Avatar.Image size={64} source={{ uri: photoUrl }} />
<View>
<Text>By: {profile.data.author ? profile.data.author : 'Working on this'}</Text>
<Text>By: {safeProfile.data.author ? safeProfile.data.author : 'Working on this'}</Text>
<Text>
<Icon name="date-range" />
{profile.data.year ? profile.data.year : 'XXXX'}
{safeProfile.data.year ? safeProfile.data.year : 'XXXX'}
</Text>
<Text>
<Icon name="timer" />
{profile.data.duration ? profile.data.duration : '??'}
{safeProfile.data.duration ? safeProfile.data.duration : '??'}
</Text>
<Text>
<Icon name="language" />
{profile.data.language}
{safeProfile.data.language}
</Text>
</View>
</View>