Skip to content

Commit

Permalink
PR: Store user's accountId on frontend, adjust to breaking changes fr…
Browse files Browse the repository at this point in the history
…om backend #80  (#85)
  • Loading branch information
tchojnacki authored Dec 2, 2023
1 parent 0de54e5 commit 9dd6ae5
Show file tree
Hide file tree
Showing 23 changed files with 639 additions and 468 deletions.
4 changes: 2 additions & 2 deletions app/auth/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const Page = () => {
const identity = useIdentity();
const theme = useTheme();
const { t } = useI18n();
const obtainToken = useMutation("post", "/api/v1/token");
const obtainToken = useMutation("post", "/api/v1/account/token");

const [status, setStatus] = useState<"idle" | "pending" | "error">("idle");

Expand All @@ -30,7 +30,7 @@ const Page = () => {
if (res.error) {
setStatus("error");
} else {
identity.logIn(res.data.token);
identity.logIn(res.data);
}
};

Expand Down
4 changes: 2 additions & 2 deletions app/auth/register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const Page = () => {
const theme = useTheme();
const { t } = useI18n();
const createAccount = useMutation("post", "/api/v1/account");
const obtainToken = useMutation("post", "/api/v1/token");
const obtainToken = useMutation("post", "/api/v1/account/token");

const [status, setStatus] = useState<"idle" | "pending" | "error">("idle");

Expand All @@ -51,7 +51,7 @@ const Page = () => {
if (accountRes.error || tokenRes.error) {
setStatus("error");
} else {
identity.logIn(tokenRes.data.token);
identity.logIn(tokenRes.data);
}
};

Expand Down
7 changes: 7 additions & 0 deletions app/dashboard/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Redirect } from "expo-router";
import { FlatList, View } from "react-native";

import { actions } from "@/common/actions";
import { AppRoutes } from "@/common/constants";
import { useI18n } from "@/common/i18n";
import { useIdentity, RedirectIfNoProfile } from "@/common/identity";
import { sty } from "@/common/styles";
Expand All @@ -22,6 +24,11 @@ const Page = () => {
return <LoadingScreen title={t("dashboard.view.pageTitle")} />;
}

// Ensure backend's enum changes don't break the app
if (gadgets.some((action) => !(action in actions))) {
return <Redirect href={AppRoutes.EditDashboard} />;
}

return (
<View style={sty.full}>
<Header left={actions.openMenu} title={t("dashboard.view.pageTitle")} />
Expand Down
1 change: 1 addition & 0 deletions app/games/wordle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ const useStyles = sty.themedHook(({ colors }) => ({
flexDirection: "row",
justifyContent: "space-around",
width: "100%",
padding: 16,
},
}));

Expand Down
2 changes: 1 addition & 1 deletion app/medication/history.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const Page = () => {
<PaginatedScrollView
renderer={(data) => <IntakeEntry intake={Intake.fromData(data)} />}
query={{
url: "/api/v1/reminders/senior/{seniorId}/intakes",
url: "/api/v1/intakes/senior/{seniorId}",
params: { path: { seniorId } },
}}
itemsPerPage={5}
Expand Down
2 changes: 1 addition & 1 deletion app/profile/caretaker/scan-qr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function Page() {
const identity = useIdentity();
const createCaretakerProfile = useMutation(
"post",
"/api/v1/account/profiles/caretaker",
"/api/v1/profiles/caretaker",
);

const [hasPermission, setHasPermission] = useState<boolean | null>(null);
Expand Down
26 changes: 12 additions & 14 deletions app/profile/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,21 @@ const ProfilesList = () => {
const identity = useIdentity();

const { data: profileData, mutate } = useQuery({
url: "/api/v1/account/profiles",
url: "/api/v1/profiles",
});
const { data: caretakersData, isLoading: caretakersLoading } = useQuery({
url: "/api/v1/account/profiles/senior/caretakers",
url: "/api/v1/profiles/senior/caretakers",
});
const createSeniorProfile = useMutation(
"post",
"/api/v1/account/profiles/senior",
);
const createSeniorProfile = useMutation("post", "/api/v1/profiles/senior");
const editCaretakerProfile = useMutation(
"put",
"/api/v1/account/profiles/caretaker/{seniorId}",
"/api/v1/profiles/caretaker/{seniorId}",
);
// const deleteCaretakerProfile = useMutation(
// "delete",
// "/api/v1/account/profiles/caretaker/{seniorId}/{caretakerId}",
// );
const deleteSeniorProfile = useMutation(
const deleteCaretakerProfile = useMutation(
"delete",
"/api/v1/account/profiles/senior",
"/api/v1/profiles/caretaker/{seniorId}/{caretakerId}",
);
const deleteSeniorProfile = useMutation("delete", "/api/v1/profiles/senior");

const [isCreatingSeniorProfile, setIsCreatingSeniorProfile] = useState(false);
const { dialogState, dismissDialog, showEditPrompt, showDeleteAlert } =
Expand Down Expand Up @@ -228,7 +222,11 @@ const ProfilesList = () => {
if (dialogState.profile.type === "senior") {
await deleteSeniorProfile({});
} else if (dialogState.profile.type === "caretaker") {
// TODO: Fix after backend changes
const seniorId = identity.accountId;
const caretakerId = dialogState.profile.seniorId;
await deleteCaretakerProfile({
params: { path: { seniorId, caretakerId } },
});
}
await mutate();
}}
Expand Down
12 changes: 9 additions & 3 deletions app/profile/senior/caretakers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ import { ScrollView, View } from "react-native";
import { IconButton, List, Menu } from "react-native-paper";

import { actions } from "@/common/actions";
import { useMutation, useQuery, useQueryInvalidation } from "@/common/api";
import {
useMutation,
useQuery,
useQueryInvalidation,
type MethodPath,
} from "@/common/api";
import { type Translator, useI18n } from "@/common/i18n";
import { RedirectIfNoProfile, isSenior, useIdentity } from "@/common/identity";
import type { IdentityProfileKnown } from "@/common/identity/types";
import { useRefreshControl } from "@/common/refresh";
import { CaretakerBanner, Header, LoadingScreen } from "@/components";

const endpoint = "/api/v1/account/profiles/senior/caretakers";
const endpoint =
"/api/v1/profiles/senior/caretakers" satisfies MethodPath<"get">;

type ItemProps = {
profile: {
Expand All @@ -29,7 +35,7 @@ const Item = ({ profile, invalidateProfiles, identity, t }: ItemProps) => {

const unlink = useMutation(
"delete",
"/api/v1/account/profiles/caretaker/{seniorId}/{caretakerId}",
"/api/v1/profiles/caretaker/{seniorId}/{caretakerId}",
);

return (
Expand Down
2 changes: 1 addition & 1 deletion app/profile/senior/display-qr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Header, LoadingScreen } from "@/components";
const CreateSeniorProfile = () => {
const { t } = useI18n();
const { data, isLoading, mutate } = useQuery({
url: "/api/v1/account/profiles/senior",
url: "/api/v1/profiles/senior/pairing",
});
const [secondsLeft, setSecondsLeft] = useState<number>(data?.validFor || 10);

Expand Down
8 changes: 4 additions & 4 deletions assets/i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@
"pl": "Gry"
},
"actions.playMemoryGame": {
"en": "Memory game",
"pl": "Gra Memory"
"en": "Memory",
"pl": "Memory"
},
"actions.playWordleGame": {
"en": "Graydle",
Expand Down Expand Up @@ -470,8 +470,8 @@
"pl_2,3,4": "{count} minuty temu"
},
"games.memory.pageTitle": {
"en": "Memory game",
"pl": "Gra Memory"
"en": "Memory",
"pl": "Memory"
},
"games.memory.startButton": {
"en": "Start game",
Expand Down
1 change: 1 addition & 0 deletions common/api/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export type { MethodPath } from "./client";
export { baseUrl } from "./consts";
export { useQueryInvalidation } from "./invalidation";
export { useQuery } from "./query";
Expand Down
Loading

0 comments on commit 9dd6ae5

Please sign in to comment.