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 / context.ts
Created October 18, 2021 13:51
context.ts - Ionic Chat App
import React from 'react';
export default React.createContext<any>(null);
@hieptl
hieptl / Login.tsx
Created October 18, 2021 13:57
Login.tsx - Ionic Chat App
import React, { useRef, useContext } from 'react';
import { useHistory } from 'react-router';
import validator from "validator";
import { auth } from "../firebase";
import Context from '../context';
const Login: React.FC = () => {
// get shared data from context.
@hieptl
hieptl / Login.tsx
Created October 18, 2021 13:59
Login.tsx - Log in to CometChat - Ionic Chat App
...
cometChat.login(firebaseUid, `${process.env.REACT_APP_COMETCHAT_AUTH_KEY}`).then(
(User: any) => {
// User loged in successfully.
// save authenticated user to local storage.
localStorage.setItem('auth', JSON.stringify(User));
// save authenticated user to context.
setUser(User);
// hide loading.
setIsLoading(false);
@hieptl
hieptl / SignUp.tsx
Created October 18, 2021 14:07
SignUp.tsx - Ionic Chat App
import React, { useRef, useContext } from 'react';
import { IonToolbar, IonTitle, IonButtons, IonBackButton, IonHeader } from '@ionic/react';
import validator from "validator";
import { auth } from '../firebase';
import Context from '../context';
const SignUp: React.FC<any> = () => {
@hieptl
hieptl / SignUp.tsx
Created October 18, 2021 14:08
SignUp.tsx - Register CometChat Account - Ionic Chat App
...
// cometchat auth key
const authKey = `${process.env.REACT_APP_COMETCHAT_AUTH_KEY}`;
// call cometchat service to register a new account.
const user = new cometChat.User(firebaseUid);
user.setName(fullname);
user.setAvatar(userAvatar);
cometChat.createUser(user, authKey).then(
(user: any) => {
@hieptl
hieptl / Home.tsx
Last active October 27, 2021 12:16
Home.tsx - Ionic Chat App
import React, { useRef, useContext, useState, useEffect, useCallback } from 'react';
import { IonToolbar, IonTitle, IonHeader, IonButtons, IonButton, IonIcon } from '@ionic/react';
import { add, exit } from 'ionicons/icons';
import { useHistory } from 'react-router';
import { v4 as uuidv4 } from "uuid";
import Context from '../context';
@hieptl
hieptl / Home.tsx
Last active October 27, 2021 12:17
Home.tsx - Search Users, Search Groups - Ionic Chat App
...
useEffect(() => {
if (cometChat) {
listenForMessages();
}
return () => {
if (cometChat) {
cometChat.removeMessageListener(listenerID);
}
setData(null);
@hieptl
hieptl / Home.tsx
Last active October 27, 2021 10:13
Home.tsx - Join Group, Select Item - Ionic Chat App
...
const joinGroup = (item: any) => {
if (item && item.guid && !item.hasJoined) {
setIsLoading(true);
const GUID = item.guid;
const password = "";
const groupType = cometChat.GROUP_TYPE.PUBLIC;
cometChat.joinGroup(GUID, groupType, password).then(
(group: any) => {
@hieptl
hieptl / CreateGroup.tsx
Created October 18, 2021 14:20
CreateGroup.tsx - Ionic Chat App
import React, { useRef, useContext } from 'react';
import { IonToolbar, IonTitle, IonHeader, IonButtons, IonBackButton } from '@ionic/react';
import validator from "validator";
import { v4 as uuidv4 } from "uuid";
import Context from '../context';
const CreateGroup: React.FC = () => {
@hieptl
hieptl / CreateGroup.tsx
Created October 18, 2021 14:21
CreateGroup.tsx - Create Group CometChat - Ionic Chat App
...
const createGroup = () => {
const groupName = groupNameRef.current.value;
if (isGroupValid(groupName)) {
setIsLoading(true);
const GUID = uuidv4();
const groupType = cometChat.GROUP_TYPE.PUBLIC;
const groupIcon = generateAvatar();
const password = "";