Skip to content

Commit

Permalink
Restyled by prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
restyled-commits committed Sep 18, 2023
1 parent 22d0360 commit 74475d6
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 212 deletions.
2 changes: 1 addition & 1 deletion src/assets/sounds/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
2 changes: 1 addition & 1 deletion src/components/SettingsComponents/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import DeleteAccount from "./DeleteAccount";
import SettingsSidebar from "./Sidebar";
import SoundSetting from "./Sounds";

export {DeleteAccount, SettingsSidebar, SoundSetting};
export { DeleteAccount, SettingsSidebar, SoundSetting };
2 changes: 1 addition & 1 deletion src/js/deleteImg.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {storage} from "../lib/firebase";
import { storage } from "../lib/firebase";

export default async function deleteImg(imageUrl) {
if (imageUrl && imageUrl !== "") {
Expand Down
79 changes: 40 additions & 39 deletions src/js/postFn.js
Original file line number Diff line number Diff line change
@@ -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" });
}
};

Expand All @@ -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();
};
80 changes: 40 additions & 40 deletions src/js/signIn.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion src/js/userData.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Loading

0 comments on commit 74475d6

Please sign in to comment.