Skip to content

Commit

Permalink
messaging done.
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorpfiz committed Dec 31, 2023
1 parent 81569a1 commit de1f0f4
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 199 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
"version": 1,
"author": "expo"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "expo"
"info": {
"version": 1,
"author": "expo"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
"version": 1,
"author": "expo"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
"version": 1,
"author": "expo"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,47 +26,51 @@ export default function ChatPage() {
});

const chatMsgsQuery = api.communication.chatMsgs.useQuery({
sender: `Patient/${patientId}`,
recipient: `Practitioner/${practitionerId}`,
query: {
sender: `Patient/${patientId}`,
recipient: `Practitioner/${practitionerId}`,
},
});

const isLoading = practitionerQuery.isLoading || chatMsgsQuery.isLoading;
const isError = practitionerQuery.isError || chatMsgsQuery.isError;
const error = practitionerQuery.error ?? chatMsgsQuery.error;

const createMsg = api.communication.createMsg.useMutation({
const createMsg = api.communication.createCommunication.useMutation({
onSuccess: (data) => {
// Invalidate the senderMsgs query so that it will be refetched
void utils.communication.senderMsgs.invalidate();
// Invalidate the patientChats query so that it will be refetched
void utils.communication.patientChats.invalidate();
},
});

// Synchronize local state with API data
useEffect(() => {
const newMessages =
chatMsgsQuery.data
?.map((msg) => ({
_id: msg._id,
text: msg.text,
createdAt: new Date(msg.createdAt),
user: {
_id: msg.user._id,
name: msg.user.name,
// avatar: msg.user.avatar,
},
}))
.reverse() ?? [];
setMessages(newMessages);
if (chatMsgsQuery.data) {
setMessages(chatMsgsQuery.data);
}
}, [chatMsgsQuery.data]);

// This function will be called when a new message is sent
const onSend = useCallback(
async (newMessages: IMessage[] = []) => {
if (newMessages.length > 0) {
await createMsg.mutateAsync({
payload: newMessages?.[0]?.text ?? "",
recipient: `Practitioner/${practitionerId}`,
sender: `Patient/${patientId}`,
body: {
status: "unknown",
payload: [
{
contentString: newMessages?.[0]?.text ?? "",
},
],
recipient: [
{
reference: `Practitioner/${practitionerId}`,
},
],
sender: {
reference: `Patient/${patientId}`,
},
},
});

setMessages((previousMessages) =>
Expand Down
22 changes: 9 additions & 13 deletions apps/expo/src/app/(main)/portal/(messages)/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Text, View } from "react-native";
import { router } from "expo-router";
import { FlashList } from "@shopify/flash-list";
import { useAtom } from "jotai";
import { Loader2 } from "lucide-react-native";

import { patientIdAtom } from "~/app/(main)";
import ChatPreviewCard from "~/components/ui/cards/chat-preview-card";
Expand All @@ -14,18 +13,18 @@ export default function MessagesPage() {
const [patientId] = useAtom(patientIdAtom);

const { isLoading, isError, data, error } =
api.communication.senderMsgs.useQuery({
api.communication.patientChats.useQuery({
query: {
sender: `Patient/${patientId}`,
},
});

const chats = useMemo(() => {
return (
data?.map((msg) => ({
title: msg.recipient.name,
preview: msg?.messages[msg?.messages?.length - 1] ?? "",
onPress: () => router.push(`/portal/chat/${msg.recipient.id}`),
data?.map((chat) => ({
title: chat.recipient.name,
preview: chat.latestMessage,
onPress: () => router.push(`/portal/chat/${chat.recipient.id}`),
})) ?? []
);
}, [data]);
Expand All @@ -41,13 +40,10 @@ export default function MessagesPage() {
return (
<View className="flex-1 bg-gray-100">
{chats.length === 0 ? (
<View className="mb-36 flex-1 items-center justify-center bg-white">
<Loader2
size={48}
color="black"
strokeWidth={2}
className="animate-spin"
/>
<View className="flex-1 flex-col items-center justify-center">
<Text className="mt-4 text-xl font-bold">
You have no messages yet
</Text>
</View>
) : (
<FlashList
Expand Down
13 changes: 7 additions & 6 deletions apps/expo/src/app/(main)/portal/(modals)/tasks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,13 @@ export default function TasksPage() {
}}
renderItem={(item, isFirst) => (
<TouchableOpacity
className={`my-4 flex flex-col gap-4 rounded px-4 py-2 ${JSON.parse(item.name).status === "requested"
? "bg-red-500"
: JSON.parse(item.name).status === "cancelled"
? "bg-yellow-500"
: "bg-green-500"
}`}
className={`my-4 flex flex-col gap-4 rounded px-4 py-2 ${
JSON.parse(item.name).status === "requested"
? "bg-red-500"
: JSON.parse(item.name).status === "cancelled"
? "bg-yellow-500"
: "bg-green-500"
}`}
>
<Text className="font-medium text-white">
{format(new Date(item.day), "h:mm a")}
Expand Down
2 changes: 1 addition & 1 deletion apps/expo/src/components/tasks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default function Tasks() {
color={"#1d4ed8"}
/>
</View>
{!hasTasks ? (
{hasTasks ? (
<FlatList
horizontal={true}
showsHorizontalScrollIndicator={false}
Expand Down
7 changes: 2 additions & 5 deletions apps/expo/src/components/ui/headers/tasks-header.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Text, TouchableOpacity } from "react-native";
import { useRouter } from "expo-router";

import { Button } from "../rn-ui/components/ui/button";

export function LeftHeaderDone() {
Expand All @@ -13,9 +14,5 @@ export function LeftHeaderDone() {
}

export function RightHeaderCreate() {
return (
<Button>
Add Task
</Button>
)
return <Button>Add Task</Button>;
}
1 change: 1 addition & 0 deletions packages/api/src/canvas/canvas-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,7 @@ export const get_SearchCommunicationSender = {
path: z.literal("/Communication"),
parameters: z.object({
query: z.object({
patient: z.string().optional(),
sender: z.string().optional(),
recipient: z.string().optional(),
_id: z.string().optional(),
Expand Down
36 changes: 0 additions & 36 deletions packages/api/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,44 +92,8 @@ function validateApiResponse<Z extends z.ZodTypeAny>(
return validatedData;
}

// messages helper functions
async function fetchPersonDetails(api, reference) {
if (!reference) return null;
const [type, id] = reference.split("/");
const response = await api.get(`/${type}/{id}`, { path: { id } });
return response; // Assuming response has the structure { id, name }
}

const getAvatarUrl = () => {
return `https://files.oaiusercontent.com/file-qn1PnhbqEv2cNvNw6N6LPAN0?se=2023-12-17T02%3A15%3A29Z&sp=r&sv=2021-08-06&sr=b&rscc=max-age%3D31536000%2C%20immutable&rscd=attachment%3B%20filename%3D76788262-6f59-4406-88cb-6f38c74327c9.webp&sig=hAOqj/xKCiKa%2BiJFNn53F5F3TUXScoEZtdxuc9tT7w8%3D`; // Placeholder
};

async function processSingleMessage(api, msg, isSender): Promise<IMessage> {
const personData = await fetchPersonDetails(
api,
isSender
? msg.resource.sender?.reference
: msg.resource.recipient[0]?.reference,
);

// Assuming the avatar URL logic is implemented in getAvatarUrl function
const avatarUrl = getAvatarUrl();

return {
_id: msg.resource.id,
text: msg.resource.payload[0]?.contentString,
createdAt: new Date(msg.resource.sent),
user: {
_id: isSender ? 1 : 2, // Use actual IDs if available
name: personData?.name[0]?.use ?? (isSender ? "Sender" : "Recipient"),
avatar: avatarUrl,
},
};
}

export {
handleCanvasApiResponse,
handleOperationOutcomeError,
validateApiResponse,
processSingleMessage,
};
Loading

0 comments on commit de1f0f4

Please sign in to comment.