-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
better folder structure and some optimizations
- Loading branch information
1 parent
8592d05
commit fe9cc0d
Showing
26 changed files
with
187 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as Location from "expo-location"; | ||
|
||
import { SOS } from "@/types"; | ||
import { axios_ as axios } from "@/utils/axios"; | ||
|
||
export const sosKeys = { | ||
sos: (user_id: number) => [user_id, "sos"] as const | ||
}; | ||
|
||
export const closeSOS = async (user_id: number) => { | ||
console.log(`Closing SOS for user_id: ${user_id}.`); | ||
try { | ||
await axios.patch(`/sos/close/${user_id}`); | ||
} catch (error) { | ||
console.error("Error closing SOS:", error); | ||
throw new Error(`Error closing SOS: ${error}`); | ||
} | ||
}; | ||
|
||
export const createSOS = async ({ | ||
user_id, | ||
location | ||
}: { | ||
user_id: number; | ||
location: Location.LocationObject; | ||
}) => { | ||
console.log(`Creating SOS for user_id: ${user_id}. Location: ${location}`); | ||
try { | ||
await axios.post("/sos/create", { | ||
user_id: user_id, | ||
lat: location.coords.latitude, | ||
long: location.coords.longitude | ||
}); | ||
return; | ||
} catch (error) { | ||
console.error("Error creating SOS:", error); | ||
throw new Error(`Error creating SOS: ${error}`); | ||
} | ||
}; | ||
|
||
export const fetchNotifications = async () => { | ||
try { | ||
const { data: notifications } = await axios.get<SOS[]>("/sos/"); | ||
return notifications; | ||
} catch (error) { | ||
throw new Error("Error fetching notifications"); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { IMessage } from "react-native-gifted-chat"; | ||
|
||
import { Ticket } from "@/types"; | ||
import { TicketMsg } from "@/types"; | ||
import { axios_ as axios } from "@/utils/axios"; | ||
|
||
export const ticketKeys = { | ||
tickets: (user_id: number) => [user_id, "tickets"] as const, | ||
ticket_msgs: (user_id: number, ticket_id: string) => | ||
[...ticketKeys.tickets(user_id), ticket_id, "ticket_msgs"] as const | ||
}; | ||
|
||
export const createTicket = async (user_id: number, ticket: Ticket) => { | ||
delete ticket.rating; | ||
|
||
try { | ||
const { data } = await axios.post<Ticket>("/tickets/create/", { | ||
...ticket, | ||
user_id: user_id | ||
}); | ||
return data; | ||
} catch (error) { | ||
throw new Error(`Error Creating ticket`); | ||
} | ||
}; | ||
|
||
export const fetchTickets = async (user_id: number) => { | ||
try { | ||
const { data: tickets } = await axios.get<Ticket[]>(`/tickets/${user_id}`); | ||
return tickets; | ||
} catch (error) { | ||
throw new Error(`Error fetching tickets`); | ||
} | ||
}; | ||
export const closeTicket = async (ticketId: string) => { | ||
try { | ||
await axios.patch(`/tickets/close/${ticketId}`); | ||
} catch (error) { | ||
throw new Error(`Error closing ticket: ${ticketId}`); | ||
} | ||
}; | ||
|
||
export const fetchTicketMsgs = async (ticketId: string) => { | ||
try { | ||
const { data } = await axios.get<TicketMsg[]>( | ||
`/tickets/messages/${ticketId}` | ||
); | ||
|
||
const messages: IMessage[] = data.map((message: TicketMsg) => ({ | ||
_id: message.message_id, | ||
createdAt: new Date(message.created_at), | ||
text: message.message_text, | ||
user: { | ||
_id: message.user_id, | ||
name: `User ${message.user_id}` | ||
} | ||
})); | ||
|
||
return messages; | ||
} catch (error) { | ||
throw new Error(`Error fetching ticket: ${ticketId} messages`); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,36 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { useLocalSearchParams } from "expo-router"; | ||
import React from "react"; | ||
import { H6 } from "tamagui"; | ||
|
||
import { fetchTicketMsgs, ticketKeys } from "@/api/ticket"; | ||
import { ChatRoom } from "@/components"; | ||
import { useWebSocket } from "@/hooks"; | ||
import { type TicketParams } from "@/types"; | ||
import { fetchTicketMsgs } from "@/utils/fetchTicketMessages"; | ||
|
||
const Chat = () => { | ||
const { ticketId, userId } = useLocalSearchParams<TicketParams>(); | ||
const query = useQuery({ | ||
queryKey: ["ticket_msgs", ticketId], | ||
queryKey: ticketKeys.ticket_msgs(Number(userId!), ticketId!), | ||
queryFn: async () => await fetchTicketMsgs(ticketId!) | ||
}); | ||
|
||
const sendMessage = useWebSocket({ | ||
queryKey: ["ticket_msgs", ticketId!], | ||
queryKey: ticketKeys.ticket_msgs(Number(userId!), ticketId!), | ||
url: `/${ticketId}/${userId}` | ||
}); | ||
|
||
return <ChatRoom messages={query.data!} onSend={sendMessage} />; | ||
return ( | ||
<> | ||
{query.isError ? ( | ||
<H6>Error... {query.error.message}</H6> | ||
) : query.isLoading ? ( | ||
<H6>Loading Ticket Chat...</H6> | ||
) : ( | ||
<ChatRoom messages={query.data!} onSend={sendMessage} /> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export default Chat; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.