Skip to content

Commit

Permalink
chore: improve app mutations so that we don't have to wait for the we…
Browse files Browse the repository at this point in the history
…bsocket message
  • Loading branch information
bnussman committed Oct 26, 2024
1 parent 3d19303 commit fa01b81
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 55 deletions.
38 changes: 16 additions & 22 deletions app/components/ActionButton.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import React, { useState } from "react";
import React from "react";
import { Button } from "@/components/Button";
import { useEffect } from "react";
import { RouterOutput, trpc } from "@/utils/trpc";
import { TRPCClientError } from "@trpc/client";

type Status = RouterOutput['beeper']['queue'][number]['status'];

Expand All @@ -25,9 +23,15 @@ interface Props {

export function ActionButton(props: Props) {
const { beep } = props;
const [isLoading, setIsLoading] = useState(false);
const { mutateAsync: update } = trpc.beeper.updateBeep.useMutation();
const utils = trpc.useUtils();
const { mutate, isPending } = trpc.beeper.updateBeep.useMutation({
onSuccess(data) {
utils.beeper.queue.setData(undefined, data);
},
onError(error) {
alert(error.message);
}
});

const getMessage = () => {
switch (beep.status) {
Expand All @@ -46,27 +50,17 @@ export function ActionButton(props: Props) {
}
};

useEffect(() => {
setIsLoading(false);
}, [beep]);

const onPress = () => {
setIsLoading(true);
update({
beepId: beep.id,
data: {
status: nextStatusMap[beep.status as InProgressStatuses],
}
}).then((data) => {
utils.beeper.queue.setData(undefined, data);
}).catch((error: TRPCClientError<any>) => {
setIsLoading(false);
alert(error.message);
});
mutate({
beepId: beep.id,
data: {
status: nextStatusMap[beep.status as InProgressStatuses],
}
})
};

return (
<Button isLoading={isLoading} onPress={onPress} size="lg">
<Button isLoading={isPending} onPress={onPress} size="lg">
{getMessage()}
</Button>
);
Expand Down
31 changes: 13 additions & 18 deletions app/components/CancelButton.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useState } from "react";
import { isMobile, Unpacked } from "../utils/constants";
import React from "react";
import { isMobile } from "../utils/constants";
import { Button } from "@/components/Button";
import { useEffect } from "react";
import { Alert } from "react-native";
import { RouterOutput, trpc } from "@/utils/trpc";
import { TRPCClientError } from "@trpc/client";
Expand All @@ -11,12 +10,15 @@ interface Props {
}

export function CancelButton({ beep }: Props) {
const [isLoading, setIsLoading] = useState(false);
const { mutateAsync: cancel } = trpc.beeper.updateBeep.useMutation();

useEffect(() => {
setIsLoading(false);
}, [beep]);
const utils = trpc.useUtils();
const { mutate: cancel, isPending } = trpc.beeper.updateBeep.useMutation({
onSuccess(data) {
utils.beeper.queue.setData(undefined, data);
},
onError(error) {
alert(error.message);
}
});

const onPress = () => {
if (isMobile) {
Expand All @@ -26,9 +28,6 @@ export function CancelButton({ beep }: Props) {
[
{
text: "No",
onPress: () => {
setIsLoading(false);
},
style: "cancel",
},
{
Expand All @@ -44,16 +43,12 @@ export function CancelButton({ beep }: Props) {
};

const onCancel = () => {
setIsLoading(true);
cancel({ beepId: beep.id, data: { status: "canceled" } }).catch((error: TRPCClientError<any>) => {
setIsLoading(false);
alert(error.message);
});
cancel({ beepId: beep.id, data: { status: "canceled" } });
};

return (
<Button
isLoading={isLoading}
isLoading={isPending}
onPress={onPress}
className="text-white bg-red-400 dark:bg-red-400 dark:active:bg-red-500 active:bg-red-500"
>
Expand Down
28 changes: 13 additions & 15 deletions app/routes/ride/LeaveButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ interface Props {
}

export function LeaveButton(props: Props) {
const { beepersId, ...rest } = props;
const { mutateAsync: leave } = trpc.rider.leaveQueue.useMutation();
const [isLoading, setIsLoading] = useState<boolean>(false);
const { beepersId } = props;
const utils = trpc.useUtils();
const { mutate, isPending } = trpc.rider.leaveQueue.useMutation({
onSuccess() {
utils.rider.currentRide.setData(undefined, null);
},
onError(error) {
alert(error.message);
}
});

function leaveQueueWrapper(): void {
if (isMobile) {
Expand All @@ -23,30 +30,21 @@ export function LeaveButton(props: Props) {
text: "No",
style: "cancel",
},
{ text: "Yes", onPress: () => leaveQueue() },
{ text: "Yes", onPress: () => mutate({ beeperId: beepersId }) },
],
{ cancelable: true },
);
} else {
leaveQueue();
mutate({ beeperId: beepersId });
}
}

async function leaveQueue(): Promise<void> {
setIsLoading(true);
leave({ beeperId: beepersId }).catch((error) => {
alert(error.message);
setIsLoading(false);
});
}

return (
<Button
isLoading={isLoading}
isLoading={isPending}
onPress={() => leaveQueueWrapper()}
activityIndicatorProps={{ color: "white" }}
className="min-w-48 !text-white !bg-red-400 active:!bg-red-500"
{...rest}
>
Leave Queue
</Button>
Expand Down

0 comments on commit fa01b81

Please sign in to comment.