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 / reactions.js
Created November 16, 2021 02:29
reactions.js - routes - server - delete reaction - Instagram Clone React Node
app.post('/reactions/delete', (req, res) => {
const { postId, userId } = req.body;
if (!postId || !userId) {
res.status(200).jsonp({ message: 'Cannot create the post reaction, please try again' });
}
const deleteReactionsSql = "DELETE FROM post_reaction WHERE post_id = ? AND user_id = ?";
dbConn.query(deleteReactionsSql, [postId, userId], function (error, response) {
if (response && response.affectedRows) {
res.status(200).jsonp({ postId, userId });
} else {
@hieptl
hieptl / notifications.js
Created November 16, 2021 02:30
notifications.js - routes - server - create notification - Instagram Clone React Node
app.post('/notifications/create', (req, res) => {
const { notificationImage, notificationMessage, userId } = req.body;
if (!notificationImage || !notificationMessage || !userId) {
res.status(200).jsonp({ message: 'Cannot creat the notification, please try again' });
}
const createNotification = [[notificationImage, notificationMessage, userId]];
const createNotificationSql = "INSERT INTO user_notification (notification_image, notification_message, user_id) VALUES ?";
dbConn.query(createNotificationSql, [createNotification], function (error, insertedNotification) {
if (insertedNotification) {
res.status(200).jsonp({ id: insertedNotification.insertId, notification_image: notificationImage, notification_message: notificationMessage, user_id: userId });
@hieptl
hieptl / notifications.js
Created November 16, 2021 02:36
notifications.js - routes - server - get notification by user id - Instagram Clone React Node
app.get('/notifications/:id', (req, res) => {
const userId = req.params.id;
const getNotificationsSql = "SELECT * FROM user_notification WHERE user_id = ? ORDER BY id DESC";
dbConn.query(getNotificationsSql, [userId], function (error, notifications) {
if (notifications) {
res.status(200).jsonp(notifications);
} else {
res.status(200).jsonp({ message: 'Cannot get your notifications, please try again' });
}
});
@hieptl
hieptl / App.js
Created November 16, 2021 02:42
App.js - Initialize CometChat - Instagram Clone React Node
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();
CometChat.init(appID, appSetting).then(
@hieptl
hieptl / login.js
Last active November 22, 2021 16:34
login.js - Instagram Clone React Node
import { useEffect, useRef, useContext } from "react";
import validator from "validator";
import { useHistory } from 'react-router-dom';
import axios from 'axios';
import withModal from "../common/Modal";
import SignUp from "../register/SignUp";
import Context from "../../context";
const Login = (props) => {
const { toggleModal } = props;
@hieptl
hieptl / login.js
Created November 16, 2021 02:52
login.js - Login CometChat - Instagram Clone React Node
const loginCometChat = async (user) => {
const authKey = `${process.env.REACT_APP_COMETCHAT_AUTH_KEY}`;
return await cometChat.login(user.id, authKey);
};
@hieptl
hieptl / modal.js
Created November 16, 2021 02:54
modal.js - Instagram Clone React Node
import { useState } from 'react';
const withModal = ModalComponent => WrapperComponent => {
return function (props) {
const [isModalShown, setIsModalShown] = useState(false);
return (
<>
<WrapperComponent toggleModal={setIsModalShown} {...props} />
@hieptl
hieptl / signup.js
Last active December 3, 2021 03:03
signup.js - Instagram Clone React Node
import { useRef, useContext, useState } from "react";
import validator from "validator";
import { v4 as uuidv4 } from "uuid";
import axios from 'axios';
import Context from "../../context";
function SignUp(props) {
const { toggleModal } = props;
const { cometChat, setIsLoading } = useContext(Context);
@hieptl
hieptl / signup.js
Created November 16, 2021 02:57
signup.js - create CometChat account - Instagram Clone React Node
const createCometChatAccount = async ({ userUuid, fullname, avatar }) => {
const authKey = `${process.env.REACT_APP_COMETCHAT_AUTH_KEY}`;
const user = new cometChat.User(userUuid);
user.setName(fullname);
user.setAvatar(avatar);
return await cometChat.createUser(user, authKey);
};
@hieptl
hieptl / header.js
Last active November 22, 2021 16:31
header.js - Instagram Clone React Node
import { useContext } from 'react';
import { useHistory } from 'react-router-dom';
import withModal from '../common/Modal';
import Create from '../create/Create';
import Context from '../../context';
import logo from '../../logo.png';
const Header = (props) => {
const { toggleModal } = props;