Skip to content

Instantly share code, notes, and snippets.

View hieptl's full-sized avatar
🎯
Focusing

Hiep Le hieptl

🎯
Focusing
View GitHub Profile
@hieptl
hieptl / RemoveGroupMembers.tsx
Last active October 27, 2021 05:21
RemoveGroupMembers.tsx - Ionic Chat App
import React, { useState, useEffect, useContext } from 'react';
import { IonToolbar, IonTitle, IonHeader, IonButtons, IonBackButton } from '@ionic/react';
import { useHistory } from 'react-router';
import { v4 as uuidv4 } from "uuid";
import Context from '../context';
const RemoveGroupMembers: React.FC = () => {
@hieptl
hieptl / Home.tsx
Created October 18, 2021 16:05
Home.tsx - Logout - Ionic Chat App
...
const logout = () => {
const shouldLogout = window.confirm('Do you want to logout');
if (shouldLogout) {
setIsLoading(true);
cometChat.logout().then(
() => {
setUser(null);
setSelectedConversation(null);
localStorage.removeItem('auth');
@hieptl
hieptl / PrivateRoute.tsx
Created October 20, 2021 15:47
PrivateRoute.tsx - Ionic Chat App
// import react.
import React from 'react';
// import react router.
import { Route, Redirect } from 'react-router-dom';
const PrivateRoute: React.FC<any> = ({ component: Component, ...rest }: any) => {
return (
// Show the component only when the user is logged in
// Otherwise, redirect the user to /signin page
<Route {...rest} render={props => (
@hieptl
hieptl / App.tsx
Last active October 27, 2021 05:13
App.tsx - Listen for Group Changes
...
useEffect(() => {
...
return () => {
...
if (cometChat) {
...
cometChat.removeGroupListener(groupListenerId);
}
}
@hieptl
hieptl / ManageGroup.tsx
Last active October 27, 2021 10:09
ManageGroup.tsx - Delete Group & Leave Group - Ionic Chat App
...
const handleDeleteGroup = () => {
if (selectedConversation && selectedConversation.name && selectedConversation.guid) {
setIsLoading(true);
cometChat.deleteGroup(selectedConversation.guid).then(
(response: any) => {
setIsLoading(false);
alert(`${selectedConversation.name} was deleted successfully`);
history.push('/');
},
@hieptl
hieptl / App.js
Created October 27, 2021 06:44
App.js - Init CometChat - E2E Encrypted Chat App
...
useEffect(() => {
initCometChat();
}, []);
const initCometChat = async () => {
const { CometChat } = await import('@cometchat-pro/chat');
const appID = `${process.env.REACT_APP_COMETCHAT_APP_ID}`;
const region = `${process.env.REACT_APP_COMETCHAT_REGION}`;
const appSetting = new CometChat.AppSettingsBuilder().subscribePresenceForAllUsers().setRegion(region).build();
@hieptl
hieptl / firebase.js
Created October 27, 2021 06:45
firebase.js - E2E Encrypted Chat App
import firebase from "firebase";
import "firebase/storage";
const firebaseConfig = {
apiKey: `${process.env.REACT_APP_FIREBASE_API_KEY}`,
authDomain: `${process.env.REACT_APP_FIREBASE_AUTH_DOMAIN}`,
databaseURL: `${process.env.REACT_APP_FIREBASE_DATABASE_URL}`,
projectId: `${process.env.REACT_APP_FIREBASE_PROJECT_ID}`,
storageBucket: `${process.env.REACT_APP_FIREBASE_STORAGE_BUCKET}`,
messagingSenderId: `${process.env.REACT_APP_FIREABSE_MESSAGING_SENDER_ID}`,
@hieptl
hieptl / context.js
Created October 27, 2021 06:56
context.js - E2E Encrypted Chat App
import React from 'react';
export default React.createContext(null);
@hieptl
hieptl / login.js
Last active November 8, 2021 08:02
login.js - Encrypted Chat App
import { useRef, useContext, useEffect } from "react";
import Context from "../context";
import { auth, realTimeDb } from "../firebase";
import validator from "validator";
import withModal from "./Modal";
import SignUp from "./SignUp";
import { useHistory } from 'react-router-dom';
import { v4 as uuidv4 } from "uuid";
const Login = (props) => {
@hieptl
hieptl / login.js
Created October 27, 2021 07:01
login.js - Log in to CometChat - Encrypted Chat App
...
const keys = Object.keys(val);
const user = val[keys[0]];
cometChat.login(user.id, `${process.env.REACT_APP_COMETCHAT_AUTH_KEY}`).then(
User => {
...
},
error => {
}
);