Skip to content

Commit

Permalink
restrict user to chat in the old chanel
Browse files Browse the repository at this point in the history
  • Loading branch information
Irirwanirira committed Jul 20, 2024
1 parent 318a4bb commit a6c2115
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
13 changes: 8 additions & 5 deletions app/(app)/Appointments/MessagingAppointment/ChannelList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import React, { useContext, useEffect, useState } from 'react'
import { fetchPatientData } from '@/utils/LoggedInUser';
import { AuthContext } from '@/ctx/AuthContext';
import {
ChannelList
ChannelList,
DefaultStreamChatGenerics
} from 'stream-chat-expo'
import { useAppContext } from '@/ctx/ChatContext';
import { PatientTypes } from '@/constants/Types';
import {useRouter } from 'expo-router';
import { ChannelSort } from 'stream-chat';

const ChannelLists = ({appointmentId}:{appointmentId:any}) => {

const ChannelLists = ({appointmentId, loggedInUserId}:{appointmentId:any, loggedInUserId:string}) => {
const { channel, setChannel } = useAppContext();
const [patientData, setPatientData] = useState<PatientTypes[] | null>(null);
const { userId, } = useContext(AuthContext);
Expand All @@ -28,13 +31,13 @@ const ChannelLists = ({appointmentId}:{appointmentId:any}) => {
};
}

const sort = {last_message_at: -1,};
const sort : ChannelSort<DefaultStreamChatGenerics> = { last_message_at: -1 }

const handleNavigateToChannel = (channel: any) => {
setChannel(channel);
router.push({
pathname: "(app)/Appointments/MessagingAppointment/ChannelScreen",
params: { id: channel?.id, appointmentId },
params: { chanelId: channel?.id, appointmentId, loggedInUserId},
});
};

Expand All @@ -45,7 +48,7 @@ const ChannelLists = ({appointmentId}:{appointmentId:any}) => {
filters={filters}
sort={sort}
/>

)
}

Expand Down
24 changes: 16 additions & 8 deletions app/(app)/Appointments/MessagingAppointment/ChannelScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,31 @@ import { supabase } from "@/lib/supabase";
import { useGlobalSearchParams } from "expo-router";
import Typography from "@/constants/Typography";
import { useModal } from "@/ctx/ModalContext";
import { StreamChat } from "stream-chat";

const ChannelScreen = () => {
const { theme, changeTheme } = useContext(ThemeContext);
const { theme} = useContext(ThemeContext);
const [isloading, setIsLoading] = useState(false);
const ios = Platform.OS === "ios";
const modal = useModal();
const {id,appointmentId} = useGlobalSearchParams();
const {chanelId,appointmentId, loggedInUserId} = useGlobalSearchParams();
const { channel } = useAppContext();
const API_KEY = process.env.EXPO_PUBLIC_STREAM_API_KEY;

const chatClient = StreamChat.getInstance(`${API_KEY}`);

const disableUser = async () => {
try{
await chatClient.channel('messaging', `${chanelId}`).removeMembers([`${loggedInUserId}`])
}catch(error){
console.log("user not deleted",error);
}
}


// const deleteChannel = async () => {
// try{
// await client.deleteChannels([id], {hard_delete: true});
// }catch(error){}
// }
const endAppointment = async() => {
try {
disableUser();
setIsLoading(true);
const { error } = await supabase
.from("appointment")
Expand Down Expand Up @@ -131,7 +140,6 @@ const ChannelScreen = () => {
>
<Channel
channel={channel}
// MessageText={MessageStyle}
audioRecordingEnabled={true}
enforceUniqueReaction={true}
giphyEnabled={true}
Expand Down
5 changes: 3 additions & 2 deletions app/(app)/Appointments/MessagingAppointment/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import uuid from "react-native-uuid";
import { useGlobalSearchParams } from "expo-router";

const API_KEY = process.env.EXPO_PUBLIC_STREAM_API_KEY;
const SECRETE_KEY = process.env.EXPO_PUBLIC_STREAM_API_SECRET;

export function Home() {
const [clientIsReady, setClientIsReady] = useState<boolean>(false);
const [patientData, setPatientData] = useState<PatientTypes[] | null>(null);
const [client, setClient] = useState<StreamChat | null>(null);
const { channel, setChannel } = useAppContext();
const [loggedInUserId , setLoggedInUserId] = useState<string>("");
const { userId } = useContext(AuthContext);
const CDNURL =
"https://vbwbfflzxuhktdvpbspd.supabase.co/storage/v1/object/public/patients/";
Expand All @@ -35,6 +35,7 @@ export function Home() {
const setupClient = async () => {
try {
if (patientData && patientData[0]) {
setLoggedInUserId(patientData[0].id);
const user = {
id: patientData[0].id,
name: `${patientData[0].first_name} ${patientData[0].last_name}`,
Expand Down Expand Up @@ -66,7 +67,7 @@ export function Home() {
// if(client){return async()=> await client.disconnectUser()}
}, [patientData]);

return <ChannelLists appointmentId={appointmentId} />;
return <ChannelLists appointmentId={appointmentId} loggedInUserId={loggedInUserId }/>;
}

export default Home;
1 change: 0 additions & 1 deletion utils/LoggedInUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export async function getPatientData(supabase: any, userInfo: any): Promise<any>
return;
}
patentInfo(data);
console.log(data)
} catch (error) {
console.error("Error while fetching data:", error);
return;
Expand Down

0 comments on commit a6c2115

Please sign in to comment.