From 22d03600d6024e6bb66c419c297581a556074f71 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Mon, 18 Sep 2023 22:41:34 +0000 Subject: [PATCH 1/4] Restyled by clang-format --- src/assets/sounds/index.js | 6 +- src/components/SettingsComponents/index.js | 2 +- src/components/index.js | 5 +- src/js/deleteImg.js | 2 +- src/js/postFn.js | 82 ++++----- src/js/signIn.js | 81 ++++----- src/js/userData.js | 2 +- src/lib/firebase.js | 189 ++++++++++----------- src/pages/index.js | 6 +- src/reusableComponents/index.js | 2 +- src/reusableComponents/validation.js | 70 ++++---- vite.config.js | 6 +- 12 files changed, 224 insertions(+), 229 deletions(-) diff --git a/src/assets/sounds/index.js b/src/assets/sounds/index.js index 81ff2dc07..ac41fbc89 100644 --- a/src/assets/sounds/index.js +++ b/src/assets/sounds/index.js @@ -1,7 +1,7 @@ import backBtnSound from "./backBtn.mp3"; import errorSound from "./error.mp3"; -import successSound from "./success.mp3"; -import lightOnSound from "./lightOn.mp3"; import lightOffSound from "./lightOff.mp3"; +import lightOnSound from "./lightOn.mp3"; +import successSound from "./success.mp3"; -export { backBtnSound, errorSound, successSound, lightOnSound, lightOffSound }; +export {backBtnSound, errorSound, lightOffSound, lightOnSound, successSound}; diff --git a/src/components/SettingsComponents/index.js b/src/components/SettingsComponents/index.js index 1c7354cf6..84406e755 100644 --- a/src/components/SettingsComponents/index.js +++ b/src/components/SettingsComponents/index.js @@ -2,4 +2,4 @@ import DeleteAccount from "./DeleteAccount"; import SettingsSidebar from "./Sidebar"; import SoundSetting from "./Sounds"; -export { DeleteAccount, SettingsSidebar, SoundSetting }; +export {DeleteAccount, SettingsSidebar, SoundSetting}; diff --git a/src/components/index.js b/src/components/index.js index bbe7b92a8..deede940a 100644 --- a/src/components/index.js +++ b/src/components/index.js @@ -1,3 +1,5 @@ +import NotFound from "../pages/NotFound"; + import Caption from "./Caption"; import ChatBox from "./CommunityChat"; import EditProfile from "./EditProfile"; @@ -6,7 +8,6 @@ import GuestSignInBtn from "./Guest/GuestSignInBtn"; import GuestSignUpBtn from "./Guest/GuestSignUpBtn"; import ImgUpload from "./ImgUpload"; import Navbar from "./Navbar"; -import NotFound from "../pages/NotFound"; import Notifications from "./Notification"; import Post from "./Post"; import ReadMore from "./ReadMore"; @@ -18,6 +19,7 @@ export { Caption, ChatBox, EditProfile, + FriendsComponent, GuestSignInBtn, GuestSignUpBtn, ImgUpload, @@ -29,7 +31,6 @@ export { SideBar, StoryView, Suggestion, - FriendsComponent, }; export default Post; diff --git a/src/js/deleteImg.js b/src/js/deleteImg.js index 4b89bee1a..c68cf6f70 100644 --- a/src/js/deleteImg.js +++ b/src/js/deleteImg.js @@ -1,4 +1,4 @@ -import { storage } from "../lib/firebase"; +import {storage} from "../lib/firebase"; export default async function deleteImg(imageUrl) { if (imageUrl && imageUrl !== "") { diff --git a/src/js/postFn.js b/src/js/postFn.js index 5cebdcb8e..fe388da29 100644 --- a/src/js/postFn.js +++ b/src/js/postFn.js @@ -1,46 +1,47 @@ -import { db, storage } from "../lib/firebase"; -import { playErrorSound, playSuccessSound } from "./sounds"; - import firebase from "firebase/compat/app"; +import {db, storage} from "../lib/firebase"; + +import {playErrorSound, playSuccessSound} from "./sounds"; + export const deletePost = async ( - uid, - postId, - imageUrl, - enqueueSnackbar, - setOpen, -) => { + uid, + postId, + imageUrl, + enqueueSnackbar, + setOpen, + ) => { try { await db - .runTransaction(async (transaction) => { - //Delete doc ref from user doc - const docRef = db.collection("users").doc(uid); - transaction.update(docRef, { - posts: firebase.firestore.FieldValue.arrayRemove(postId), - }); - - // Delete the post document - const postRef = db.collection("posts").doc(postId); - transaction.delete(postRef); - }) - .then(async () => { - if (imageUrl !== "") { - const url = JSON.parse(imageUrl); - const deleteImagePromises = url.map(({ imageUrl }) => { - const imageRef = storage.refFromURL(imageUrl); - return imageRef.delete(); + .runTransaction(async (transaction) => { + // Delete doc ref from user doc + const docRef = db.collection("users").doc(uid); + transaction.update(docRef, { + posts : firebase.firestore.FieldValue.arrayRemove(postId), }); - await Promise.all(deleteImagePromises); - } - }) - .then(() => { - playSuccessSound(); - enqueueSnackbar("Post deleted successfully!", { variant: "success" }); - setOpen(false); - }); + + // Delete the post document + const postRef = db.collection("posts").doc(postId); + transaction.delete(postRef); + }) + .then(async () => { + if (imageUrl !== "") { + const url = JSON.parse(imageUrl); + const deleteImagePromises = url.map(({imageUrl}) => { + const imageRef = storage.refFromURL(imageUrl); + return imageRef.delete(); + }); + await Promise.all(deleteImagePromises); + } + }) + .then(() => { + playSuccessSound(); + enqueueSnackbar("Post deleted successfully!", {variant : "success"}); + setOpen(false); + }); } catch (error) { playErrorSound(); - enqueueSnackbar(`Error deleting post: ${error}`, { variant: "error" }); + enqueueSnackbar(`Error deleting post: ${error}`, {variant : "error"}); } }; @@ -60,10 +61,9 @@ export const savePost = async (postId) => { export const deleteComment = async (event, postId, commentId) => { event.preventDefault(); - await db - .collection("posts") - .doc(postId) - .collection("comments") - .doc(commentId) - .delete(); + await db.collection("posts") + .doc(postId) + .collection("comments") + .doc(commentId) + .delete(); }; diff --git a/src/js/signIn.js b/src/js/signIn.js index 9c6bad6fa..ae9769574 100644 --- a/src/js/signIn.js +++ b/src/js/signIn.js @@ -1,50 +1,51 @@ -import { auth, db, facebookProvider, googleProvider } from "../lib/firebase"; -import { playErrorSound, playSuccessSound } from "./sounds"; +import {auth, db, facebookProvider, googleProvider} from "../lib/firebase"; + +import {playErrorSound, playSuccessSound} from "./sounds"; const signInWithOAuth = (e, enqueueSnackbar, navigate, google = true) => { e.preventDefault(); const provider = google ? googleProvider : facebookProvider; - auth - .signInWithPopup(provider) - .then(async (val) => { - const userRef = db.collection("users").where("uid", "==", val?.user?.uid); + auth.signInWithPopup(provider) + .then(async (val) => { + const userRef = + db.collection("users").where("uid", "==", val?.user?.uid); - const docSnap = await userRef.get(); - if (docSnap.docs.length < 1) { - const usernameDoc = db.collection("users"); - await usernameDoc.doc(auth.currentUser.uid).set({ - uid: val.user.uid, - username: val.user.uid, - name: val.user.displayName, - photoURL: val.user.photoURL, - displayName: val.user.displayName, - Friends: [], - posts: [], - }); - } else if (!docSnap.docs[0].data().username) { - docSnap.docs[0].ref.update({ - username: doc.data().uid, + const docSnap = await userRef.get(); + if (docSnap.docs.length < 1) { + const usernameDoc = db.collection("users"); + await usernameDoc.doc(auth.currentUser.uid).set({ + uid : val.user.uid, + username : val.user.uid, + name : val.user.displayName, + photoURL : val.user.photoURL, + displayName : val.user.displayName, + Friends : [], + posts : [], + }); + } else if (!docSnap.docs[0].data().username) { + docSnap.docs[0].ref.update({ + username : doc.data().uid, + }); + } + playSuccessSound(); + enqueueSnackbar("Login successful!", { + variant : "success", }); - } - playSuccessSound(); - enqueueSnackbar("Login successful!", { - variant: "success", + navigate("/"); + }) + .catch((error) => { + if (error.code === "auth/account-exists-with-different-credential") { + playErrorSound(); + enqueueSnackbar("Account exists with a different credential", { + variant : "error", + }); + } else { + playErrorSound(); + enqueueSnackbar(error.message, { + variant : "error", + }); + } }); - navigate("/"); - }) - .catch((error) => { - if (error.code === "auth/account-exists-with-different-credential") { - playErrorSound(); - enqueueSnackbar("Account exists with a different credential", { - variant: "error", - }); - } else { - playErrorSound(); - enqueueSnackbar(error.message, { - variant: "error", - }); - } - }); }; export default signInWithOAuth; diff --git a/src/js/userData.js b/src/js/userData.js index e7d1607b1..d21f85124 100644 --- a/src/js/userData.js +++ b/src/js/userData.js @@ -1,4 +1,4 @@ -import { db, auth } from "../lib/firebase"; +import {auth, db} from "../lib/firebase"; export default async function getUserSessionData(getRef) { const user = auth?.currentUser; diff --git a/src/lib/firebase.js b/src/lib/firebase.js index ea6a47f95..b16794b66 100644 --- a/src/lib/firebase.js +++ b/src/lib/firebase.js @@ -3,15 +3,15 @@ import "firebase/compat/firestore"; import "firebase/compat/storage"; import firebase from "firebase/compat/app"; -import { v4 as uuid } from "uuid"; +import {v4 as uuid} from "uuid"; const firebaseApp = firebase.initializeApp({ - apiKey: import.meta.env.VITE_DUMMYGRAM_APIKEY, - authDomain: import.meta.env.VITE_DUMMYGRAM_AUTHDOMAIN, - projectId: import.meta.env.VITE_DUMMYGRAM_PROJECTID, - storageBucket: import.meta.env.VITE_DUMMYGRAM_STORAGEBUCKET, - messagingSenderId: import.meta.env.VITE_DUMMYGRAM_MESSAGINGSENDERID, - appId: import.meta.env.VITE_DUMMYGRAM_APPID, + apiKey : import.meta.env.VITE_DUMMYGRAM_APIKEY, + authDomain : import.meta.env.VITE_DUMMYGRAM_AUTHDOMAIN, + projectId : import.meta.env.VITE_DUMMYGRAM_PROJECTID, + storageBucket : import.meta.env.VITE_DUMMYGRAM_STORAGEBUCKET, + messagingSenderId : import.meta.env.VITE_DUMMYGRAM_MESSAGINGSENDERID, + appId : import.meta.env.VITE_DUMMYGRAM_APPID, }); // Use these for db & auth @@ -30,16 +30,16 @@ const storage = firebase.storage(); function handleMultiUpload(files, options = {}) { const _options = Object.assign( - { - storageLocation: "images", - /** - * - * @param {number} _percentage - */ - onUploadProgress: (_percentage) => {}, - generateThumbnails: false, - }, - options, + { + storageLocation : "images", + /** + * + * @param {number} _percentage + */ + onUploadProgress : (_percentage) => {}, + generateThumbnails : false, + }, + options, ); let totalSize = 0; @@ -48,9 +48,8 @@ function handleMultiUpload(files, options = {}) { const uploadPromises = files.map((file) => { const fileName = uuid() + "." + file.name.split(".").pop(); return new Promise((resolve, reject) => { - const task = storage - .ref(`${_options.storageLocation}/${fileName}`) - .put(file); + const task = + storage.ref(`${_options.storageLocation}/${fileName}`).put(file); /** @type {null|number} */ let currUploadTotalSize = null; @@ -58,93 +57,85 @@ function handleMultiUpload(files, options = {}) { let lastUploadedSize = null; task.on( - "state_changed", - (snapshot) => { - if (currUploadTotalSize === null) { - totalSize += snapshot.totalBytes; - currUploadTotalSize = snapshot.totalBytes; - } - - totalUploaded -= lastUploadedSize ?? 0; - totalUploaded += snapshot.bytesTransferred; - - lastUploadedSize = snapshot.bytesTransferred; - - _options.onUploadProgress( - Math.round((totalUploaded / totalSize) * 100), - ); - }, - (error) => { - if (currUploadTotalSize !== null) { - totalSize -= currUploadTotalSize; - } - - if (lastUploadedSize !== null) { - totalUploaded -= lastUploadedSize; - } - - reject(error); - }, - () => { - storage - .ref(_options.storageLocation) - .child(fileName) - .getDownloadURL() - .then((url) => { - if (_options.generateThumbnails) { - const thumbnailScale = 1 / 10; - - const image = new Image(); - image.src = URL.createObjectURL(file); - - image.addEventListener("load", () => { - const canvas = document.createElement("canvas"); - canvas.width = image.naturalWidth * thumbnailScale; - canvas.height = image.naturalHeight * thumbnailScale; - - canvas - .getContext("2d") - .drawImage(image, 0, 0, canvas.width, canvas.height); - - resolve({ - thumbnail: canvas.toDataURL(), - imageWidth: image.naturalWidth, - imageHeight: image.naturalHeight, - imageUrl: url, - }); - }); - - image.addEventListener("error", () => { - resolve({ - imageUrl: url, - thumbnail: null, - imageWidth: 0, - imageHeight: 0, - }); - }); - } else { - resolve(url); - } - }) - .catch((error) => { - reject(error); - }); - }, + "state_changed", + (snapshot) => { + if (currUploadTotalSize === null) { + totalSize += snapshot.totalBytes; + currUploadTotalSize = snapshot.totalBytes; + } + + totalUploaded -= lastUploadedSize ?? 0; + totalUploaded += snapshot.bytesTransferred; + + lastUploadedSize = snapshot.bytesTransferred; + + _options.onUploadProgress( + Math.round((totalUploaded / totalSize) * 100), + ); + }, + (error) => { + if (currUploadTotalSize !== null) { + totalSize -= currUploadTotalSize; + } + + if (lastUploadedSize !== null) { + totalUploaded -= lastUploadedSize; + } + + reject(error); + }, + () => { + storage.ref(_options.storageLocation) + .child(fileName) + .getDownloadURL() + .then((url) => { + if (_options.generateThumbnails) { + const thumbnailScale = 1 / 10; + + const image = new Image(); + image.src = URL.createObjectURL(file); + + image.addEventListener("load", () => { + const canvas = document.createElement("canvas"); + canvas.width = image.naturalWidth * thumbnailScale; + canvas.height = image.naturalHeight * thumbnailScale; + + canvas.getContext("2d").drawImage( + image, 0, 0, canvas.width, canvas.height); + + resolve({ + thumbnail : canvas.toDataURL(), + imageWidth : image.naturalWidth, + imageHeight : image.naturalHeight, + imageUrl : url, + }); + }); + + image.addEventListener("error", () => { + resolve({ + imageUrl : url, + thumbnail : null, + imageWidth : 0, + imageHeight : 0, + }); + }); + } else { + resolve(url); + } + }) + .catch((error) => { reject(error); }); + }, ); }); }); return new Promise((resolve, reject) => { Promise.all(uploadPromises) - .then((uploads) => { - resolve(uploads); - }) - .catch((error) => { - reject(error); - }); + .then((uploads) => { resolve(uploads); }) + .catch((error) => { reject(error); }); }); } -export { db, auth, storage, handleMultiUpload }; +export {db, auth, storage, handleMultiUpload}; export const googleProvider = new firebase.auth.GoogleAuthProvider(); export const facebookProvider = new firebase.auth.FacebookAuthProvider(); diff --git a/src/pages/index.js b/src/pages/index.js index 19c43dbba..282a4a248 100644 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -1,25 +1,25 @@ -import About from "./FooterPages/About"; import ChatPage from "./Chat"; +import About from "./FooterPages/About"; import Contributor from "./FooterPages/ContributorPage"; import Feedback from "./FooterPages/Feedback"; import Guidelines from "./FooterPages/Guidelines"; import HelpCenter from "./FooterPages/HelpCenter"; +import Friends from "./Friends"; import LoginScreen from "./Login"; import PostView from "./PostView"; import Profile from "./Profile"; import SignupScreen from "./Signup"; -import Friends from "./Friends"; export { About, ChatPage, Contributor, Feedback, + Friends, Guidelines, HelpCenter, LoginScreen, PostView, Profile, SignupScreen, - Friends, }; diff --git a/src/reusableComponents/index.js b/src/reusableComponents/index.js index f9190ec11..1bb4dfe41 100644 --- a/src/reusableComponents/index.js +++ b/src/reusableComponents/index.js @@ -11,8 +11,8 @@ import PostSkeleton from "./PostSkeleton"; import Scroll from "./Scroll"; import ShareModal from "./ShareModal"; import SnackBar from "./SnackBar"; -import ViewsCounter from "./Views"; import validate from "./validation"; +import ViewsCounter from "./Views"; export { AnimatedButton, diff --git a/src/reusableComponents/validation.js b/src/reusableComponents/validation.js index 68810410f..1fc1aedd2 100644 --- a/src/reusableComponents/validation.js +++ b/src/reusableComponents/validation.js @@ -2,50 +2,52 @@ const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[@$%#^&*])(?=.*[0-9]).{8,}$/; const emailRegex = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i; const validate = { - name: (value) => { - if (!value) return { name: true, nameError: "Name field cannot be empty" }; + name : (value) => { + if (!value) + return {name : true, nameError : "Name field cannot be empty"}; else { - return value.length < 6 - ? { name: true, nameError: "Name must be atleast 6 characters long." } - : { name: false, nameError: false }; + return value.length < 6 ? { + name : true, + nameError : "Name must be atleast 6 characters long." + } + : {name : false, nameError : false}; } }, - email: (value) => { - return emailRegex.test(value) - ? { email: false, emailError: false } - : { email: true, emailError: "Please enter a valid email address" }; + email : (value) => { + return emailRegex.test(value) ? {email : false, emailError : false} : { + email : true, + emailError : "Please enter a valid email address" + }; }, - password: (value) => { - return passwordRegex.test(value) - ? { password: false, passwordError: false } - : { - password: true, - passwordError: - "Minimum 8 characters, 1 uppercase, 1 lowercase, 1 symbol (@$%#^&*), 1 number (0-9).", - }; + password : (value) => { + return passwordRegex.test(value) ? {password : false, passwordError : false} : { + password : true, + passwordError : + "Minimum 8 characters, 1 uppercase, 1 lowercase, 1 symbol (@$%#^&*), 1 number (0-9).", + }; }, - confirmPassword: (value, password) => { + confirmPassword : (value, password) => { return passwordRegex.test(value) - ? value !== password - ? { - confirmPassword: true, - confirmPasswordError: "Password does not match", - } - : { confirmPassword: false, confirmPasswordError: false } - : { - confirmPassword: true, - confirmPasswordError: - "Minimum 8 characters, 1 uppercase, 1 lowercase, 1 symbol (@$%#^&*), 1 number (0-9).", - }; + ? value !== password + ? { + confirmPassword : true, + confirmPasswordError : "Password does not match", + } + : {confirmPassword : false, confirmPasswordError : false} + : { + confirmPassword : true, + confirmPasswordError : + "Minimum 8 characters, 1 uppercase, 1 lowercase, 1 symbol (@$%#^&*), 1 number (0-9).", + }; }, - initialValue: { - name: true, - email: true, - password: true, - confirmPassword: true, + initialValue : { + name : true, + email : true, + password : true, + confirmPassword : true, }, }; diff --git a/vite.config.js b/vite.config.js index 482833f6b..d84607c09 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,8 +1,8 @@ -import { defineConfig } from "vite"; import react from "@vitejs/plugin-react"; +import {defineConfig} from "vite"; // https://vitejs.dev/config/ export default defineConfig({ - base: "/dummygram", - plugins: [react()], + base : "/dummygram", + plugins : [ react() ], }); From 74475d6fef0065fd1cd179dbf2209acfc2c39048 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Mon, 18 Sep 2023 22:41:42 +0000 Subject: [PATCH 2/4] Restyled by prettier --- src/assets/sounds/index.js | 2 +- src/components/SettingsComponents/index.js | 2 +- src/js/deleteImg.js | 2 +- src/js/postFn.js | 79 ++++----- src/js/signIn.js | 80 ++++----- src/js/userData.js | 2 +- src/lib/firebase.js | 189 +++++++++++---------- src/reusableComponents/validation.js | 76 +++++---- vite.config.js | 6 +- 9 files changed, 226 insertions(+), 212 deletions(-) diff --git a/src/assets/sounds/index.js b/src/assets/sounds/index.js index ac41fbc89..db08c1724 100644 --- a/src/assets/sounds/index.js +++ b/src/assets/sounds/index.js @@ -4,4 +4,4 @@ import lightOffSound from "./lightOff.mp3"; import lightOnSound from "./lightOn.mp3"; import successSound from "./success.mp3"; -export {backBtnSound, errorSound, lightOffSound, lightOnSound, successSound}; +export { backBtnSound, errorSound, lightOffSound, lightOnSound, successSound }; diff --git a/src/components/SettingsComponents/index.js b/src/components/SettingsComponents/index.js index 84406e755..1c7354cf6 100644 --- a/src/components/SettingsComponents/index.js +++ b/src/components/SettingsComponents/index.js @@ -2,4 +2,4 @@ import DeleteAccount from "./DeleteAccount"; import SettingsSidebar from "./Sidebar"; import SoundSetting from "./Sounds"; -export {DeleteAccount, SettingsSidebar, SoundSetting}; +export { DeleteAccount, SettingsSidebar, SoundSetting }; diff --git a/src/js/deleteImg.js b/src/js/deleteImg.js index c68cf6f70..4b89bee1a 100644 --- a/src/js/deleteImg.js +++ b/src/js/deleteImg.js @@ -1,4 +1,4 @@ -import {storage} from "../lib/firebase"; +import { storage } from "../lib/firebase"; export default async function deleteImg(imageUrl) { if (imageUrl && imageUrl !== "") { diff --git a/src/js/postFn.js b/src/js/postFn.js index fe388da29..d7450df0b 100644 --- a/src/js/postFn.js +++ b/src/js/postFn.js @@ -1,47 +1,47 @@ import firebase from "firebase/compat/app"; -import {db, storage} from "../lib/firebase"; +import { db, storage } from "../lib/firebase"; -import {playErrorSound, playSuccessSound} from "./sounds"; +import { playErrorSound, playSuccessSound } from "./sounds"; export const deletePost = async ( - uid, - postId, - imageUrl, - enqueueSnackbar, - setOpen, - ) => { + uid, + postId, + imageUrl, + enqueueSnackbar, + setOpen, +) => { try { await db - .runTransaction(async (transaction) => { - // Delete doc ref from user doc - const docRef = db.collection("users").doc(uid); - transaction.update(docRef, { - posts : firebase.firestore.FieldValue.arrayRemove(postId), - }); - - // Delete the post document - const postRef = db.collection("posts").doc(postId); - transaction.delete(postRef); - }) - .then(async () => { - if (imageUrl !== "") { - const url = JSON.parse(imageUrl); - const deleteImagePromises = url.map(({imageUrl}) => { - const imageRef = storage.refFromURL(imageUrl); - return imageRef.delete(); - }); - await Promise.all(deleteImagePromises); - } - }) - .then(() => { - playSuccessSound(); - enqueueSnackbar("Post deleted successfully!", {variant : "success"}); - setOpen(false); + .runTransaction(async (transaction) => { + // Delete doc ref from user doc + const docRef = db.collection("users").doc(uid); + transaction.update(docRef, { + posts: firebase.firestore.FieldValue.arrayRemove(postId), }); + + // Delete the post document + const postRef = db.collection("posts").doc(postId); + transaction.delete(postRef); + }) + .then(async () => { + if (imageUrl !== "") { + const url = JSON.parse(imageUrl); + const deleteImagePromises = url.map(({ imageUrl }) => { + const imageRef = storage.refFromURL(imageUrl); + return imageRef.delete(); + }); + await Promise.all(deleteImagePromises); + } + }) + .then(() => { + playSuccessSound(); + enqueueSnackbar("Post deleted successfully!", { variant: "success" }); + setOpen(false); + }); } catch (error) { playErrorSound(); - enqueueSnackbar(`Error deleting post: ${error}`, {variant : "error"}); + enqueueSnackbar(`Error deleting post: ${error}`, { variant: "error" }); } }; @@ -61,9 +61,10 @@ export const savePost = async (postId) => { export const deleteComment = async (event, postId, commentId) => { event.preventDefault(); - await db.collection("posts") - .doc(postId) - .collection("comments") - .doc(commentId) - .delete(); + await db + .collection("posts") + .doc(postId) + .collection("comments") + .doc(commentId) + .delete(); }; diff --git a/src/js/signIn.js b/src/js/signIn.js index ae9769574..e3a5e29fe 100644 --- a/src/js/signIn.js +++ b/src/js/signIn.js @@ -1,51 +1,51 @@ -import {auth, db, facebookProvider, googleProvider} from "../lib/firebase"; +import { auth, db, facebookProvider, googleProvider } from "../lib/firebase"; -import {playErrorSound, playSuccessSound} from "./sounds"; +import { playErrorSound, playSuccessSound } from "./sounds"; const signInWithOAuth = (e, enqueueSnackbar, navigate, google = true) => { e.preventDefault(); const provider = google ? googleProvider : facebookProvider; - auth.signInWithPopup(provider) - .then(async (val) => { - const userRef = - db.collection("users").where("uid", "==", val?.user?.uid); + auth + .signInWithPopup(provider) + .then(async (val) => { + const userRef = db.collection("users").where("uid", "==", val?.user?.uid); - const docSnap = await userRef.get(); - if (docSnap.docs.length < 1) { - const usernameDoc = db.collection("users"); - await usernameDoc.doc(auth.currentUser.uid).set({ - uid : val.user.uid, - username : val.user.uid, - name : val.user.displayName, - photoURL : val.user.photoURL, - displayName : val.user.displayName, - Friends : [], - posts : [], - }); - } else if (!docSnap.docs[0].data().username) { - docSnap.docs[0].ref.update({ - username : doc.data().uid, - }); - } - playSuccessSound(); - enqueueSnackbar("Login successful!", { - variant : "success", + const docSnap = await userRef.get(); + if (docSnap.docs.length < 1) { + const usernameDoc = db.collection("users"); + await usernameDoc.doc(auth.currentUser.uid).set({ + uid: val.user.uid, + username: val.user.uid, + name: val.user.displayName, + photoURL: val.user.photoURL, + displayName: val.user.displayName, + Friends: [], + posts: [], }); - navigate("/"); - }) - .catch((error) => { - if (error.code === "auth/account-exists-with-different-credential") { - playErrorSound(); - enqueueSnackbar("Account exists with a different credential", { - variant : "error", - }); - } else { - playErrorSound(); - enqueueSnackbar(error.message, { - variant : "error", - }); - } + } else if (!docSnap.docs[0].data().username) { + docSnap.docs[0].ref.update({ + username: doc.data().uid, + }); + } + playSuccessSound(); + enqueueSnackbar("Login successful!", { + variant: "success", }); + navigate("/"); + }) + .catch((error) => { + if (error.code === "auth/account-exists-with-different-credential") { + playErrorSound(); + enqueueSnackbar("Account exists with a different credential", { + variant: "error", + }); + } else { + playErrorSound(); + enqueueSnackbar(error.message, { + variant: "error", + }); + } + }); }; export default signInWithOAuth; diff --git a/src/js/userData.js b/src/js/userData.js index d21f85124..bd50ae698 100644 --- a/src/js/userData.js +++ b/src/js/userData.js @@ -1,4 +1,4 @@ -import {auth, db} from "../lib/firebase"; +import { auth, db } from "../lib/firebase"; export default async function getUserSessionData(getRef) { const user = auth?.currentUser; diff --git a/src/lib/firebase.js b/src/lib/firebase.js index b16794b66..ea6a47f95 100644 --- a/src/lib/firebase.js +++ b/src/lib/firebase.js @@ -3,15 +3,15 @@ import "firebase/compat/firestore"; import "firebase/compat/storage"; import firebase from "firebase/compat/app"; -import {v4 as uuid} from "uuid"; +import { v4 as uuid } from "uuid"; const firebaseApp = firebase.initializeApp({ - apiKey : import.meta.env.VITE_DUMMYGRAM_APIKEY, - authDomain : import.meta.env.VITE_DUMMYGRAM_AUTHDOMAIN, - projectId : import.meta.env.VITE_DUMMYGRAM_PROJECTID, - storageBucket : import.meta.env.VITE_DUMMYGRAM_STORAGEBUCKET, - messagingSenderId : import.meta.env.VITE_DUMMYGRAM_MESSAGINGSENDERID, - appId : import.meta.env.VITE_DUMMYGRAM_APPID, + apiKey: import.meta.env.VITE_DUMMYGRAM_APIKEY, + authDomain: import.meta.env.VITE_DUMMYGRAM_AUTHDOMAIN, + projectId: import.meta.env.VITE_DUMMYGRAM_PROJECTID, + storageBucket: import.meta.env.VITE_DUMMYGRAM_STORAGEBUCKET, + messagingSenderId: import.meta.env.VITE_DUMMYGRAM_MESSAGINGSENDERID, + appId: import.meta.env.VITE_DUMMYGRAM_APPID, }); // Use these for db & auth @@ -30,16 +30,16 @@ const storage = firebase.storage(); function handleMultiUpload(files, options = {}) { const _options = Object.assign( - { - storageLocation : "images", - /** - * - * @param {number} _percentage - */ - onUploadProgress : (_percentage) => {}, - generateThumbnails : false, - }, - options, + { + storageLocation: "images", + /** + * + * @param {number} _percentage + */ + onUploadProgress: (_percentage) => {}, + generateThumbnails: false, + }, + options, ); let totalSize = 0; @@ -48,8 +48,9 @@ function handleMultiUpload(files, options = {}) { const uploadPromises = files.map((file) => { const fileName = uuid() + "." + file.name.split(".").pop(); return new Promise((resolve, reject) => { - const task = - storage.ref(`${_options.storageLocation}/${fileName}`).put(file); + const task = storage + .ref(`${_options.storageLocation}/${fileName}`) + .put(file); /** @type {null|number} */ let currUploadTotalSize = null; @@ -57,85 +58,93 @@ function handleMultiUpload(files, options = {}) { let lastUploadedSize = null; task.on( - "state_changed", - (snapshot) => { - if (currUploadTotalSize === null) { - totalSize += snapshot.totalBytes; - currUploadTotalSize = snapshot.totalBytes; - } - - totalUploaded -= lastUploadedSize ?? 0; - totalUploaded += snapshot.bytesTransferred; - - lastUploadedSize = snapshot.bytesTransferred; - - _options.onUploadProgress( - Math.round((totalUploaded / totalSize) * 100), - ); - }, - (error) => { - if (currUploadTotalSize !== null) { - totalSize -= currUploadTotalSize; - } - - if (lastUploadedSize !== null) { - totalUploaded -= lastUploadedSize; - } - - reject(error); - }, - () => { - storage.ref(_options.storageLocation) - .child(fileName) - .getDownloadURL() - .then((url) => { - if (_options.generateThumbnails) { - const thumbnailScale = 1 / 10; - - const image = new Image(); - image.src = URL.createObjectURL(file); - - image.addEventListener("load", () => { - const canvas = document.createElement("canvas"); - canvas.width = image.naturalWidth * thumbnailScale; - canvas.height = image.naturalHeight * thumbnailScale; - - canvas.getContext("2d").drawImage( - image, 0, 0, canvas.width, canvas.height); - - resolve({ - thumbnail : canvas.toDataURL(), - imageWidth : image.naturalWidth, - imageHeight : image.naturalHeight, - imageUrl : url, - }); - }); - - image.addEventListener("error", () => { - resolve({ - imageUrl : url, - thumbnail : null, - imageWidth : 0, - imageHeight : 0, - }); - }); - } else { - resolve(url); - } - }) - .catch((error) => { reject(error); }); - }, + "state_changed", + (snapshot) => { + if (currUploadTotalSize === null) { + totalSize += snapshot.totalBytes; + currUploadTotalSize = snapshot.totalBytes; + } + + totalUploaded -= lastUploadedSize ?? 0; + totalUploaded += snapshot.bytesTransferred; + + lastUploadedSize = snapshot.bytesTransferred; + + _options.onUploadProgress( + Math.round((totalUploaded / totalSize) * 100), + ); + }, + (error) => { + if (currUploadTotalSize !== null) { + totalSize -= currUploadTotalSize; + } + + if (lastUploadedSize !== null) { + totalUploaded -= lastUploadedSize; + } + + reject(error); + }, + () => { + storage + .ref(_options.storageLocation) + .child(fileName) + .getDownloadURL() + .then((url) => { + if (_options.generateThumbnails) { + const thumbnailScale = 1 / 10; + + const image = new Image(); + image.src = URL.createObjectURL(file); + + image.addEventListener("load", () => { + const canvas = document.createElement("canvas"); + canvas.width = image.naturalWidth * thumbnailScale; + canvas.height = image.naturalHeight * thumbnailScale; + + canvas + .getContext("2d") + .drawImage(image, 0, 0, canvas.width, canvas.height); + + resolve({ + thumbnail: canvas.toDataURL(), + imageWidth: image.naturalWidth, + imageHeight: image.naturalHeight, + imageUrl: url, + }); + }); + + image.addEventListener("error", () => { + resolve({ + imageUrl: url, + thumbnail: null, + imageWidth: 0, + imageHeight: 0, + }); + }); + } else { + resolve(url); + } + }) + .catch((error) => { + reject(error); + }); + }, ); }); }); return new Promise((resolve, reject) => { Promise.all(uploadPromises) - .then((uploads) => { resolve(uploads); }) - .catch((error) => { reject(error); }); + .then((uploads) => { + resolve(uploads); + }) + .catch((error) => { + reject(error); + }); }); } -export {db, auth, storage, handleMultiUpload}; +export { db, auth, storage, handleMultiUpload }; export const googleProvider = new firebase.auth.GoogleAuthProvider(); export const facebookProvider = new firebase.auth.FacebookAuthProvider(); diff --git a/src/reusableComponents/validation.js b/src/reusableComponents/validation.js index 1fc1aedd2..59bc91522 100644 --- a/src/reusableComponents/validation.js +++ b/src/reusableComponents/validation.js @@ -2,52 +2,56 @@ const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[@$%#^&*])(?=.*[0-9]).{8,}$/; const emailRegex = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i; const validate = { - name : (value) => { - if (!value) - return {name : true, nameError : "Name field cannot be empty"}; + name: (value) => { + if (!value) return { name: true, nameError: "Name field cannot be empty" }; else { - return value.length < 6 ? { - name : true, - nameError : "Name must be atleast 6 characters long." - } - : {name : false, nameError : false}; + return value.length < 6 + ? { + name: true, + nameError: "Name must be atleast 6 characters long.", + } + : { name: false, nameError: false }; } }, - email : (value) => { - return emailRegex.test(value) ? {email : false, emailError : false} : { - email : true, - emailError : "Please enter a valid email address" - }; + email: (value) => { + return emailRegex.test(value) + ? { email: false, emailError: false } + : { + email: true, + emailError: "Please enter a valid email address", + }; }, - password : (value) => { - return passwordRegex.test(value) ? {password : false, passwordError : false} : { - password : true, - passwordError : - "Minimum 8 characters, 1 uppercase, 1 lowercase, 1 symbol (@$%#^&*), 1 number (0-9).", - }; + password: (value) => { + return passwordRegex.test(value) + ? { password: false, passwordError: false } + : { + password: true, + passwordError: + "Minimum 8 characters, 1 uppercase, 1 lowercase, 1 symbol (@$%#^&*), 1 number (0-9).", + }; }, - confirmPassword : (value, password) => { + confirmPassword: (value, password) => { return passwordRegex.test(value) - ? value !== password - ? { - confirmPassword : true, - confirmPasswordError : "Password does not match", - } - : {confirmPassword : false, confirmPasswordError : false} - : { - confirmPassword : true, - confirmPasswordError : - "Minimum 8 characters, 1 uppercase, 1 lowercase, 1 symbol (@$%#^&*), 1 number (0-9).", - }; + ? value !== password + ? { + confirmPassword: true, + confirmPasswordError: "Password does not match", + } + : { confirmPassword: false, confirmPasswordError: false } + : { + confirmPassword: true, + confirmPasswordError: + "Minimum 8 characters, 1 uppercase, 1 lowercase, 1 symbol (@$%#^&*), 1 number (0-9).", + }; }, - initialValue : { - name : true, - email : true, - password : true, - confirmPassword : true, + initialValue: { + name: true, + email: true, + password: true, + confirmPassword: true, }, }; diff --git a/vite.config.js b/vite.config.js index d84607c09..1660e1143 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,8 +1,8 @@ import react from "@vitejs/plugin-react"; -import {defineConfig} from "vite"; +import { defineConfig } from "vite"; // https://vitejs.dev/config/ export default defineConfig({ - base : "/dummygram", - plugins : [ react() ], + base: "/dummygram", + plugins: [react()], }); From 88e7b0a5d3d4d5432dac453a1b15cf2d5c33aae7 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Mon, 18 Sep 2023 22:42:06 +0000 Subject: [PATCH 3/4] Restyled by shfmt --- .husky/commit-msg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.husky/commit-msg b/.husky/commit-msg index 42a70e424..b56767669 100755 --- a/.husky/commit-msg +++ b/.husky/commit-msg @@ -1,4 +1,4 @@ #!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" -npx --no -- commitlint --edit "$1" \ No newline at end of file +npx --no -- commitlint --edit "$1" From e4a81470263c2e87053897dfc49212396d04640c Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Mon, 18 Sep 2023 22:42:07 +0000 Subject: [PATCH 4/4] Restyled by whitespace --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d9ad60ef3..45d6ee0c2 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -60,7 +60,7 @@ Open Source projects always have something to workon and improves with each new You have forked the project you want to contribute to your github account. To get this project on your development machine we use clone command of git. -`$ git clone https://github.com//.git` +`$ git clone https://github.com//.git` Now you have the project on your local machine. ### ADD A REMOTE (UPSTREAM) TO ORIGINAL PROJECT REPOSITORY