Skip to content

Instantly share code, notes, and snippets.

@parvezmrobin
Last active March 24, 2020 13:42
Show Gist options
  • Select an option

  • Save parvezmrobin/4cfdb63c2a094bebfba1e13ec0539a59 to your computer and use it in GitHub Desktop.

Select an option

Save parvezmrobin/4cfdb63c2a094bebfba1e13ec0539a59 to your computer and use it in GitHub Desktop.

Revisions

  1. parvezmrobin revised this gist Mar 24, 2020. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions ChatContext.js
    Original file line number Diff line number Diff line change
    @@ -15,16 +15,16 @@ const ChatContextProvider = (props) => {

    //Method to fetch chat list from Firebase
    const getChatList= () => {
    let chatListCopy = [];

    if('chats' in localStorage) {
    const chats = JSON.parse(localStorage.getItem('chats'));
    setChatList([chats]);
    }

    chatRef
    .doc('JyNeS95OnZ3qLDno3EVd')
    .onSnapshot((querySnapshot) => {
    chatListCopy.push(querySnapshot.data().c);
    //console.log(chatListCopy);
    setChatList(chatListCopy);
    chatListCopy = [];

    setChatList([querySnapshot.data().c]);
    localStorage.setItem('chats', JSON.stringify(querySnapshot.data().c));
    });
    }

  2. @DimaMirana DimaMirana created this gist Mar 24, 2020.
    39 changes: 39 additions & 0 deletions ChatContext.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    import React, {createContext, useState} from 'react';
    import firebase from '../firebase';

    export const ChatContext = createContext();

    const ChatContextProvider = (props) => {

    //Firebase settings
    const DB = firebase.firestore();
    const chatRef = DB.collection("chats");

    //States
    const [chatList, setChatList] = useState([]);
    //console.log(chatList);

    //Method to fetch chat list from Firebase
    const getChatList= () => {
    let chatListCopy = [];

    chatRef
    .doc('JyNeS95OnZ3qLDno3EVd')
    .onSnapshot((querySnapshot) => {
    chatListCopy.push(querySnapshot.data().c);
    //console.log(chatListCopy);
    setChatList(chatListCopy);
    chatListCopy = [];

    });
    }

    return (
    <ChatContext.Provider value={{chatList, getChatList}}>
    {props.children}
    </ChatContext.Provider>

    );
    }

    export default ChatContextProvider;