fix stuff

This commit is contained in:
2024-10-21 16:58:41 -05:00
parent 3f477e8dfb
commit f48ec76cba
3 changed files with 437 additions and 54 deletions

View File

@ -1,5 +1,5 @@
import * as FileSystem from 'expo-file-system';
import type { User, RelationshipData } from '@/constants/Types';
import type { User, RelationshipData, Message } from '@/constants/Types';
export const getInitialDataByAppleId = async (appleId: string) => {
try {
@ -194,3 +194,52 @@ export const sendRelationshipRequest = async (userId: number, targetUserId: numb
throw error;
}
};
export const getInitialMessages = async (userId: number, limit: number = 20) => {
if (!userId || isNaN(userId)) return;
try {
const apiUrl = `${process.env.EXPO_PUBLIC_API_URL}/api/messages/getInitialMessages`;
const response = await fetch((apiUrl + `?userId=${userId}&limit=${limit}`), {
headers: {
'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '',
},
});
if (!response.ok) {
throw new Error(
`Error getting initial messages: ${response.status} ${response.statusText}`
);
}
const messages = await response.json() as Message[];
return messages;
} catch (error: unknown) {
console.error('Error getting initial messages:', error);
throw error;
}
};
export const sendMessage = async (message: Message) => {
if (!message) return;
try {
const apiUrl = `${process.env.EXPO_PUBLIC_API_URL}/api/messages/send`;
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '',
},
body: JSON.stringify({
message: message,
}),
});
if (!response.ok) {
throw new Error(
`Error sending message: ${response.status} ${response.statusText}`
);
}
const messageData = await response.json() as Message;
return messageData;
} catch (error: unknown) {
console.error('Error sending message:', error);
throw error;
}
};