240729 TIL

웅웅·2024년 7월 29일

채팅 목록 불러오기

  const getChatList = async () => {
    //유저의 대화구독목록 불러오기
  const user_id = userdata.id
  console.log(user_id)
    const { data, error } = await supabase
      .from('chat_subscribe')
      .select('*')
      .eq('user_id', user_id)
    if (data) {
      const channelListData = data.map((channel) => {
        return channel.channel_id
      })
      setChannelList(channelListData)
    }
  }
  
  const getChatListData = async (chatlist: number[] | null) => {
    //구독리스트의 정보 불러오기
    // const user_id = userdata.id
    // console.log(user_id)
    if (channelList) {
      const { data, error } = await supabase
        .from('chat_channels')
        .select('*')
        .in('channel_id', [...channelList])
      if (data) {
        console.log(data)
        const channelListDatas = data.map((channel) => {
          return {
            channel_id: channel.channel_id,
            channel_name: channel.channel_name,
            created_at: channel.created_at,
            owner_id: channel.owner_id
          }
        })
        setChatListData(channelListDatas)
      }
    }
  }
  
  useEffect(() => {
    if (userdata != undefined) getChatList();
  }, [userdata])
  useEffect(()=>{
    getChatListData(channelList);
  },[channelList])
  
  
  return (
    <>
      {chatListData?.map((channel) => (
        <div key={channel.channel_id} className="category-item text-center p-1  min-w-[100px]">
          <p className="text-sm font-normal">{channel.channel_name}</p>
          <button>{channel.channel_name}의 채팅방 입장하기</button>
        </div>
      ))}
    </>
  )

유저가 구독하고 있는 채팅방을 불러와서 리스트를 화면에 출력하였다.

채팅 메세지 기록 불러오기

  const getChatMessages = async () => {

    const user_id = userdata.id
    const approve = userdata.approve
    if (approve) {
      const { data, error } = await supabase
        .from('chat_messages')
        .select('*')
        .eq('channel_id', 1)
      console.log(data)
    } else {
      const influ_id = 'e717cc1d-16de-43c6-a90b-a567022b48e0'
      const { data, error } = await supabase
        .from('chat_messages')
        .select('*')
        .in('user_id', [user_id, influ_id])
        .eq('channel_id', 1)
      console.log(data)
    }
  }

0개의 댓글