Untitled
unknown
plain_text
4 months ago
7.2 kB
3
Indexable
import { FIREBASE_DB } from "@/firebase/firebase"; import { arrayUnion, collection, doc, getDoc, getDocs, updateDoc, Timestamp, onSnapshot, } from "firebase/firestore"; import { useEffect, useState } from "react"; type MessageDataProps = { sentAt: { seconds: number; nanoseconds: number; }; userId: string; lastMessage: { event: MessageEventTypes; content: { data: { variant_details: { additionalFee: Array<{ [key: string]: string }>; roomType: string; _id: string; updatedAt: string; paymentOption: string; title: string; createdAt: string; feedbacks: any[]; // or specify if there's a specific structure for feedbacks amenities: string[]; variantImages: string[]; __v: number; pricing: number; disable: boolean; description: string; units: number; status: string; listing: { securityMeasures: string[]; user: string; city: string; _id: string; houseRules: string[]; state: string; address: string; cleaningMaintenance: string; gender: string; houseType: string; }; }; user_info: { profile_picture: string; role: string; firstName: string; _id: string; lastName: string; email: string; }; }; message: string; }; }; }; export interface SecurityMeasureType { securityMeasures: string[]; } export interface HouseRuleType { houseRules: string[]; } export interface ListingType extends SecurityMeasureType, HouseRuleType { _id: string; houseType: string; state: string; city: string; gender: string; name: string; id: string; ownerId?: string; user?: string; } export interface AdditionalFeeType { [key: string]: string; } export interface VariantType { _id: string; listing: ListingType; roomType: string; units: number; title: string; user: string; description: string; amenities: string[]; paymentOption: string; pricing: number; additionalFee: AdditionalFeeType[]; variantImages: string[]; status: string; feedbacks: any[]; // Assuming feedbacks is an array, replace `any` with the appropriate type if known __v: number; createdAt: string; updatedAt: string; name: string; isFavorite?: boolean; } export type ExploreVariantsResponseData = { docs: VariantType[]; totalDoc: number; page: number; }; export type MessageContent = | { event: "VARIANT"; content: { message: string; data: VariantDataType }; } | { event: "AUTOMATED_RESPONSE"; content: { message: string; data: { video_link: string } }; } | { event: "MOBILIZATION_FEE"; content: { message: string; data: { cost: string } }; } | { event: "MOBILIZATION_PAYMENT_CONFIRMATION"; content: { message: string }; } | { event: "MOBILIZATION_PAYMENT_SUCCESS"; content: { message: string; data: { payment_status: "paid"; transaction_id: string }; }; } | { event: "RECEIPT"; content: { message: string; data: { receipt_data: object /* Object to be gotten from the backend */; }; }; } | { event: "APPOINTMENT"; content: { message: string; data: { appointment_title: string; location: string; date_time: Timestamp; instruction: string; }; }; } | { event: "VARIANT_SUGGESTIONS"; content: { message: string; data: { suggestions: VariantType[]; }; }; } | { event: "PLAIN_TEXT"; content: { message: string; }; } | { event: "SUPPORTED"; content: { message: string; }; }; export type MessageEventTypes = | "VARIANT" | "AUTOMATED_RESPONSE" | "MOBILIZATION_FEE" | "MOBILIZATION_PAYMENT_CONFIRMATION" | "MOBILIZATION_PAYMENT_SUCCESS" | "RECEIPT" | "APPOINTMENT" | "SUPPORTED" | "VARIANT_SUGGESTIONS" | "PLAIN_TEXT"; export type VariantDataType = { variant_details: VariantType; user_info: { _id: string; firstName: string; lastName: string; email: string; role: any; profile_picture: string; }; }; export const useGetAllChatHistory = (customerSupportId: string) => { // NOTE : THIS IS TO GET ALL THE CHAT HISTORY FOR THE CUSTOMER SUPPORT const [messageHistoryData, setMessageHistoryData] = useState< MessageDataProps[] >([]); useEffect(() => { const messageHistory = () => { try { const docRef = doc(FIREBASE_DB, "CUSTOMER_SUPPORTS", customerSupportId); // Set up a real-time listener const unsubscribe = onSnapshot(docRef, (docSnap) => { console.log("use effect is calling") if (docSnap.exists()) { const fieldValue = docSnap.data()["message_history"]; // console.log("Field Value:", fieldValue); setMessageHistoryData(fieldValue); } else { // console.log("Document does not exist"); setMessageHistoryData([]); } }); // Return the unsubscribe function to allow cleanup return unsubscribe; } catch (error) { console.log("Error setting up real-time listener:", error); } }; return messageHistory() }, [customerSupportId]); return { messageHistoryData }; }; export const useGetChatBetweenUsers = ( chatId: string, customerSupportId: string ) => { const [chatsData, setChatsData] = useState([]); //NOTE : THIS IS TO GET CHAT HISTORY BETWEEN 2 USERS useEffect(() => { const checkData = async () => { try { const subcollectionRef = collection( FIREBASE_DB, "CUSTOMER_SUPPORTS", customerSupportId, chatId ); const querySnapshot = await getDocs(subcollectionRef); const subcollectionData: any = []; querySnapshot.forEach((doc) => { subcollectionData.push({ id: doc.id, ...doc.data() }); }); console.log("Subcollection Data:", subcollectionData); setChatsData(subcollectionData); } catch (error) { console.log("Error checking value in array:", error); } }; checkData(); }, [customerSupportId, chatId]); //NOTE : THIS IS TO ADD A NEW USER WITH MESSAGE HISTORY TO THE CUSTOMER SUPPORT const sendMessage = async () => { try { const docRef = doc(FIREBASE_DB, "CUSTOMER_SUPPORTS", customerSupportId); const newMessage = { timestamp: new Date().toISOString(), sender: "User", message: "Hello, this is a new message!", }; await updateDoc(docRef, { message_history: arrayUnion(newMessage), }); console.log(newMessage, "--updatedDoc"); } catch (error) { console.log("Error checking value in array:", error); } }; return { sendMessage, chatsData }; };
Editor is loading...
Leave a Comment