Skip to content

Commit

Permalink
Align with the other PR
Browse files Browse the repository at this point in the history
  • Loading branch information
tchojnacki committed Oct 15, 2023
1 parent 6825b34 commit cfc1dae
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 67 deletions.
12 changes: 10 additions & 2 deletions app/dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Text } from "react-native";
import { Button } from "react-native-paper";

import { useIdentity, RedirectIfNoProfile } from "@/common/identity";
import { MonoText } from "@/components/StyledText";
import { View } from "@/components/Themed";

const Page = () => {
const identity = useIdentity();
Expand All @@ -9,7 +11,13 @@ const Page = () => {
return <RedirectIfNoProfile identity={identity} />;
}

return <Text>Dashboard</Text>;
return (
<View>
{/* TODO: This shouldn't be here */}
<MonoText>{JSON.stringify(identity.profile, null, 2)}</MonoText>
<Button onPress={() => identity.logOut()}>LOG OUT</Button>
</View>
);
};

export default Page;
7 changes: 0 additions & 7 deletions app/profile/interfaces.ts

This file was deleted.

44 changes: 28 additions & 16 deletions app/profile/list.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
import { Link, Stack } from "expo-router";
import { Link, Stack, router } from "expo-router";
import { ScrollView, StyleSheet, View } from "react-native";
import { Avatar, Button, List } from "react-native-paper";

import { IProfile } from "./interfaces";

import { useI18n } from "@/common/i18n";
import {
Profile,
isSenior,
isCaretaker,
useIdentity,
RedirectIfLoggedOut,
} from "@/common/identity";

const mockApiResponse = {
profiles: [
{ type: "caretaker", seniorId: "2137", seniorAlias: "Jan Kowalski" },
{ type: "caretaker", seniorId: "123", seniorAlias: "Grzegorz Floryda" },
{ type: "senior", seniorId: "789" },
] as IProfile[],
{ type: "caretaker", seniorId: 2137, seniorAlias: "Jan Kowalski" },
{ type: "caretaker", seniorId: 123, seniorAlias: "Grzegorz Floryda" },
{ type: "senior", seniorId: 789 },
] as Profile[],
};

const ProfilesList = () => {
const { t } = useI18n();
const identity = useIdentity();

if (!identity.isLoggedIn) {
return <RedirectIfLoggedOut identity={identity} />;
}

const profiles = mockApiResponse.profiles;
const seniorProfile = profiles.find(({ type }) => type === "senior");
const caretakerProfiles = profiles.filter(({ type }) => type === "caretaker");
const seniorProfile = profiles.find(isSenior);
const caretakerProfiles = profiles.filter(isCaretaker);

const handleItemPress = (seniorId: string) => {
console.log(seniorId); // TODO: navigate to profile
const handleItemPress = (profile: Profile) => {
identity.selectProfile(profile);
router.push("/dashboard");
};

return (
Expand All @@ -34,13 +46,13 @@ const ProfilesList = () => {
</List.Subheader>
)}
<ScrollView style={styles.scrollView}>
{caretakerProfiles.map(({ seniorId, seniorAlias }) => {
{caretakerProfiles.map((p) => {
return (
<List.Item
key={seniorId}
key={p.seniorId}
title={t("profileList.caretakerNameFallback")}
description={`Senior: ${seniorAlias || seniorId}`}
onPress={() => handleItemPress(seniorId)}
description={`Senior: ${p.seniorAlias}`}
onPress={() => handleItemPress(p)}
style={styles.listItem}
titleStyle={styles.listItemTitle}
descriptionStyle={styles.listItemDescription}
Expand All @@ -63,7 +75,7 @@ const ProfilesList = () => {
<List.Item
title={t("profileList.seniorNameFallback")}
description={t("profileList.seniorDescription")}
onPress={() => handleItemPress(seniorProfile.seniorId)}
onPress={() => handleItemPress(seniorProfile)}
style={styles.listItem}
titleStyle={styles.listItemTitle}
descriptionStyle={styles.listItemDescription}
Expand Down
23 changes: 0 additions & 23 deletions app/profiles/list.tsx

This file was deleted.

5 changes: 5 additions & 0 deletions common/identity/guards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Profile, SeniorProfile, CaretakerProfile } from "./types";

export const isSenior = (p: Profile): p is SeniorProfile => p.type === "senior";
export const isCaretaker = (p: Profile): p is CaretakerProfile =>
p.type === "caretaker";
8 changes: 7 additions & 1 deletion common/identity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ export {
RedirectIfLoggedOut,
RedirectIfLoggedIn,
} from "./redirects";
export type { Identity, Profile } from "./types";
export type {
Identity,
Profile,
SeniorProfile,
CaretakerProfile,
} from "./types";
export { isSenior, isCaretaker } from "./guards";
4 changes: 2 additions & 2 deletions common/identity/redirects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const RedirectIfNoProfile = ({ identity }: RedirectProps) => {
return <Redirect href="/auth/login" />;
}
if (!identity.hasProfile) {
return <Redirect href="/profiles/list" />;
return <Redirect href="/profile/list" />;
}
throw new InvalidRedirectUsageError(RedirectIfNoProfile);
};
Expand Down Expand Up @@ -76,7 +76,7 @@ export const RedirectIfLoggedIn = ({ identity }: RedirectProps) => {
return <Redirect href="/dashboard" />;
}
if (identity.isLoggedIn) {
return <Redirect href="/profiles/list" />;
return <Redirect href="/profile/list" />;
}
throw new InvalidRedirectUsageError(RedirectIfLoggedIn);
};
28 changes: 15 additions & 13 deletions common/identity/types.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
export type Profile =
| {
type: "senior";
/** For `type` `"senior"`, this is the account ID of the current user. */
seniorId: number;
}
| {
type: "caretaker";
/** For `type` `"caretaker"`, this is the account ID of the senior which is taken care of (another user). */
seniorId: number;
/** Custom alias for a given senior's account, set by the current caretaker. */
seniorAlias: string;
};
export type SeniorProfile = {
type: "senior";
/** For `type` `"senior"`, this is the account ID of the current user. */
seniorId: number;
};

export type CaretakerProfile = {
type: "caretaker";
/** For `type` `"caretaker"`, this is the account ID of the senior which is taken care of (another user). */
seniorId: number;
/** Custom alias for a given senior's account, set by the current caretaker. */
seniorAlias: string;
};

export type Profile = SeniorProfile | CaretakerProfile;

/**
* State machine representing the current state of the user's identity.
Expand Down
6 changes: 3 additions & 3 deletions tests/common/identity.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const testRedirect = async (
redirect: ReactNode,
targetRoute: string,
) => {
const routes = ["/dashboard", "/auth/login", "/profiles/list"];
const routes = ["/dashboard", "/auth/login", "/profile/list"];

render(
<MockRouter
Expand Down Expand Up @@ -117,7 +117,7 @@ describe("Identity", () => {
testRedirect(
"/dashboard",
<RedirectIfNoProfile identity={mockIdentityLoggedIn} />,
"/profiles/list",
"/profile/list",
));
});

Expand All @@ -142,7 +142,7 @@ describe("Identity", () => {
testRedirect(
"/auth/login",
<RedirectIfLoggedIn identity={mockIdentityLoggedIn} />,
"/profiles/list",
"/profile/list",
));
});
});
Expand Down

0 comments on commit cfc1dae

Please sign in to comment.