From 7bb38fe7fbf1d32795816fd20b0da690f1cc2c90 Mon Sep 17 00:00:00 2001 From: Mallepally Lokeshwar Reddy Date: Tue, 13 Aug 2024 16:29:42 +0530 Subject: [PATCH 1/2] fix: Correct author data and follow functionality --- .../TutorialPage/components/PostDetails.jsx | 6 +----- .../TutorialPage/components/UserDetails.jsx | 13 +++++++++---- src/store/actions/profileActions.js | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/components/TutorialPage/components/PostDetails.jsx b/src/components/TutorialPage/components/PostDetails.jsx index 1c46ac63..9d0256e5 100644 --- a/src/components/TutorialPage/components/PostDetails.jsx +++ b/src/components/TutorialPage/components/PostDetails.jsx @@ -100,11 +100,7 @@ const PostDetails = ({ details }) => { - + diff --git a/src/components/TutorialPage/components/UserDetails.jsx b/src/components/TutorialPage/components/UserDetails.jsx index 0438b2f9..2310956f 100644 --- a/src/components/TutorialPage/components/UserDetails.jsx +++ b/src/components/TutorialPage/components/UserDetails.jsx @@ -7,6 +7,7 @@ import { useFirebase, useFirestore } from "react-redux-firebase"; import { getUserProfileData } from "../../../store/actions"; import { isUserFollower } from "../../../store/actions/profileActions"; import { addUserFollower } from "../../../store/actions"; + const useStyles = makeStyles(() => ({ container: { padding: "20px", @@ -19,7 +20,8 @@ const useStyles = makeStyles(() => ({ fontWeight: "600" } })); -const User = ({ id, timestamp, showFollowButton, size }) => { + +const User = ({ id, timestamp, size }) => { const classes = useStyles(); const dispatch = useDispatch(); const firebase = useFirebase(); @@ -55,13 +57,16 @@ const User = ({ id, timestamp, showFollowButton, size }) => { return () => {}; }, [profileData, user]); - const followUser = () => { - addUserFollower(profileData, user, firestore); + const followUser = async () => { + await addUserFollower(profileData, user, firestore); }; const getTime = timestamp => { return timestamp.toDate().toDateString(); }; + + const showFollowButton = profileData?.uid !== user?.uid; + return ( <> { padding: "1px 10px" }} > - Follow + + {isFollowed ? "Following" : "Follow +"} )} diff --git a/src/store/actions/profileActions.js b/src/store/actions/profileActions.js index c3a96c31..fd1e4db7 100644 --- a/src/store/actions/profileActions.js +++ b/src/store/actions/profileActions.js @@ -161,7 +161,7 @@ export const getUserProfileData = handle => async (firebase, firestore, dispatch) => { try { dispatch({ type: actions.GET_USER_DATA_START }); - const isUserExists = await checkUserHandleExists(handle)(firestore); + const isUserExists = checkUserHandleExists(handle)(firestore); if (isUserExists) { const docs = await firestore .collection("cl_user") From 0ab17e3edd3627a52243abf0ff156dde11fe706c Mon Sep 17 00:00:00 2001 From: Mallepally Lokeshwar Reddy Date: Sat, 24 Aug 2024 16:16:19 +0530 Subject: [PATCH 2/2] Refactor: Remove unused dispatch parameter and ensure correct usage of firebase and firestore parameters --- src/components/Dashboard/index.jsx | 1 - .../OrgUsersCard/addOrgUserModal.jsx | 5 +- src/helpers/validations.jsx | 3 +- src/store/actions/authActions.js | 10 +- src/store/actions/profileActions.js | 7 +- src/store/actions/tutorialsActions.js | 302 +++++++++--------- 6 files changed, 160 insertions(+), 168 deletions(-) diff --git a/src/components/Dashboard/index.jsx b/src/components/Dashboard/index.jsx index 072dfb21..c4147673 100644 --- a/src/components/Dashboard/index.jsx +++ b/src/components/Dashboard/index.jsx @@ -215,7 +215,6 @@ const Dashboard = ({ background = "white", textColor = "black" }) => { validateHandle( checkUserHandleExists, firebase, - dispatch, handle, setHandleValidateError, setHandleValidateErrorMessage, diff --git a/src/components/Organization/OrgUsersCard/addOrgUserModal.jsx b/src/components/Organization/OrgUsersCard/addOrgUserModal.jsx index 3a08fb46..376a863d 100644 --- a/src/components/Organization/OrgUsersCard/addOrgUserModal.jsx +++ b/src/components/Organization/OrgUsersCard/addOrgUserModal.jsx @@ -74,10 +74,7 @@ const AddOrgUserModal = ({ currentOrgHandle }) => { }, [userProps]); const onFinish = async () => { - const handleExists = await checkUserHandleExists(handle)( - firebase, - dispatch - ); + const handleExists = await checkUserHandleExists(handle)(firebase); if (handle.length < 1) { setHandleValidateError(true); diff --git a/src/helpers/validations.jsx b/src/helpers/validations.jsx index b1aee239..5a310eb3 100644 --- a/src/helpers/validations.jsx +++ b/src/helpers/validations.jsx @@ -25,7 +25,6 @@ export const validateName = ( export const validateHandle = async ( checkUserHandleExists, firebase, - dispatch, handle, setHandleValidateError, setHandleValidateErrorMessage, @@ -34,7 +33,7 @@ export const validateHandle = async ( lengthMsg, takenMsg ) => { - const handleExists = await checkUserHandleExists(handle)(firebase, dispatch); + const handleExists = await checkUserHandleExists(handle)(firebase); if (validator.isEmpty(handle)) { setHandleValidateError(true); setHandleValidateErrorMessage(emptyMsg); diff --git a/src/store/actions/authActions.js b/src/store/actions/authActions.js index 5b748dee..8017fa25 100644 --- a/src/store/actions/authActions.js +++ b/src/store/actions/authActions.js @@ -187,10 +187,9 @@ export const checkUserHandleExists = userHandle => async firebase => { } }; -export const checkOrgHandleExists = orgHandle => async firebase => { +export const checkOrgHandleExists = orgHandle => async firestore => { try { - const organizationHandle = await firebase - .firestore() + const organizationHandle = await firestore .collection("cl_org_general") .doc(orgHandle) .get(); @@ -229,9 +228,8 @@ export const setUpInitialData = } if (orgData) { - const isOrgHandleExists = await checkOrgHandleExists(org_handle)( - firebase - ); + const isOrgHandleExists = + await checkOrgHandleExists(org_handle)(firestore); if (isOrgHandleExists) { dispatch({ diff --git a/src/store/actions/profileActions.js b/src/store/actions/profileActions.js index fd1e4db7..00a1c5d1 100644 --- a/src/store/actions/profileActions.js +++ b/src/store/actions/profileActions.js @@ -61,9 +61,8 @@ export const createOrganization = dispatch({ type: actions.PROFILE_EDIT_START }); const userData = firebase.auth().currentUser; const { org_name, org_handle, org_country, org_website } = orgData; - const isOrgHandleExists = await checkOrgHandleExists(org_handle)( - firebase - ); + const isOrgHandleExists = + await checkOrgHandleExists(org_handle)(firestore); if (isOrgHandleExists) { dispatch({ @@ -161,7 +160,7 @@ export const getUserProfileData = handle => async (firebase, firestore, dispatch) => { try { dispatch({ type: actions.GET_USER_DATA_START }); - const isUserExists = checkUserHandleExists(handle)(firestore); + const isUserExists = checkUserHandleExists(handle)(firebase); if (isUserExists) { const docs = await firestore .collection("cl_user") diff --git a/src/store/actions/tutorialsActions.js b/src/store/actions/tutorialsActions.js index 7d273c1d..1094bd4c 100644 --- a/src/store/actions/tutorialsActions.js +++ b/src/store/actions/tutorialsActions.js @@ -173,8 +173,8 @@ export const createTutorial = } }; -const checkUserOrOrgHandle = handle => async firestore => { - const userHandleExists = await checkUserHandleExists(handle)(firestore); +export const checkUserOrOrgHandle = handle => async (firebase, firestore) => { + const userHandleExists = await checkUserHandleExists(handle)(firebase); const orgHandleExists = await checkOrgHandleExists(handle)(firestore); if (userHandleExists && !orgHandleExists) { @@ -230,36 +230,36 @@ export const getCurrentTutorialData = export const addNewTutorialStep = ({ owner, tutorial_id, title, time, id }) => - async (firebase, firestore, dispatch) => { - try { - dispatch({ type: actions.CREATE_TUTORIAL_STEP_START }); + async (firebase, firestore, dispatch) => { + try { + dispatch({ type: actions.CREATE_TUTORIAL_STEP_START }); - await firestore - .collection("tutorials") - .doc(tutorial_id) - .collection("steps") - .doc(id) - .set({ - content: `Switch to editor mode to begin ${title} step`, - id, - time, - title, - visibility: true, - deleted: false - }); + await firestore + .collection("tutorials") + .doc(tutorial_id) + .collection("steps") + .doc(id) + .set({ + content: `Switch to editor mode to begin ${title} step`, + id, + time, + title, + visibility: true, + deleted: false + }); - await getCurrentTutorialData(owner, tutorial_id)( - firebase, - firestore, - dispatch - ); + await getCurrentTutorialData(owner, tutorial_id)( + firebase, + firestore, + dispatch + ); - dispatch({ type: actions.CREATE_TUTORIAL_STEP_SUCCESS }); - } catch (e) { - console.log("CREATE_TUTORIAL_STEP_FAIL", e.message); - dispatch({ type: actions.CREATE_TUTORIAL_STEP_FAIL, payload: e.message }); - } - }; + dispatch({ type: actions.CREATE_TUTORIAL_STEP_SUCCESS }); + } catch (e) { + console.log("CREATE_TUTORIAL_STEP_FAIL", e.message); + dispatch({ type: actions.CREATE_TUTORIAL_STEP_FAIL, payload: e.message }); + } + }; export const clearCreateTutorials = () => dispatch => dispatch({ type: actions.CLEAR_CREATE_TUTORIALS_STATE }); @@ -305,78 +305,78 @@ export const setCurrentStepContent = export const hideUnHideStep = (owner, tutorial_id, step_id, visibility) => - async (firebase, firestore, dispatch) => { - try { - /* not being used */ - // const type = await checkUserOrOrgHandle(owner)(firebase); - await firestore - .collection("tutorials") - .doc(tutorial_id) - .collection("steps") - .doc(step_id) - .update({ - [`visibility`]: !visibility, - updatedAt: firestore.FieldValue.serverTimestamp() - }); + async (firebase, firestore, dispatch) => { + try { + /* not being used */ + // const type = await checkUserOrOrgHandle(owner)(firebase); + await firestore + .collection("tutorials") + .doc(tutorial_id) + .collection("steps") + .doc(step_id) + .update({ + [`visibility`]: !visibility, + updatedAt: firestore.FieldValue.serverTimestamp() + }); - await getCurrentTutorialData(owner, tutorial_id)( - firebase, - firestore, - dispatch - ); - } catch (e) { - console.log(e.message); - } - }; + await getCurrentTutorialData(owner, tutorial_id)( + firebase, + firestore, + dispatch + ); + } catch (e) { + console.log(e.message); + } + }; export const publishUnpublishTutorial = (owner, tutorial_id, isPublished) => - async (firebase, firestore, dispatch) => { - try { - await firestore - .collection("tutorials") - .doc(tutorial_id) - .update({ - ["isPublished"]: !isPublished - }); + async (firebase, firestore, dispatch) => { + try { + await firestore + .collection("tutorials") + .doc(tutorial_id) + .update({ + ["isPublished"]: !isPublished + }); - getCurrentTutorialData(owner, tutorial_id)(firebase, firestore, dispatch); - } catch (e) { - console.log(e.message); - } - }; + getCurrentTutorialData(owner, tutorial_id)(firebase, firestore, dispatch); + } catch (e) { + console.log(e.message); + } + }; export const removeStep = (owner, tutorial_id, step_id, current_step_no) => - async (firebase, firestore, dispatch) => { - try { - await firestore - .collection("tutorials") - .doc(tutorial_id) - .collection("steps") - .doc(step_id) - .delete() - - // const data = await firestore - // .collection("tutorials") - // .doc(tutorial_id) - // .collection("steps") - // .doc(step_id) - // .get(); - - await setCurrentStepNo( - current_step_no > 0 ? current_step_no - 1 : current_step_no - )(dispatch); - - await getCurrentTutorialData(owner, tutorial_id)( - firebase, - firestore, - dispatch - ); - } catch (e) { - console.log(e.message); - } - }; + async (firebase, firestore, dispatch) => { + try { + await firestore + .collection("tutorials") + .doc(tutorial_id) + .collection("steps") + .doc(step_id) + .delete(); + + // const data = await firestore + // .collection("tutorials") + // .doc(tutorial_id) + // .collection("steps") + // .doc(step_id) + // .get(); + + await setCurrentStepNo( + current_step_no > 0 ? current_step_no - 1 : current_step_no + )(dispatch); + + await getCurrentTutorialData(owner, tutorial_id)( + firebase, + firestore, + dispatch + ); + } catch (e) { + console.log(e.message); + } + }; export const setCurrentStep = data => async dispatch => dispatch({ type: actions.SET_EDITOR_DATA, payload: data }); @@ -388,7 +388,7 @@ export const uploadTutorialImages = (owner, tutorial_id, files) => async (firebase, firestore, dispatch) => { try { dispatch({ type: actions.TUTORIAL_IMAGE_UPLOAD_START }); - const type = await checkUserOrOrgHandle(owner)(firestore); + const type = await checkUserOrOrgHandle(owner)(firebase, firestore); const storagePath = `tutorials/${type}/${owner}/${tutorial_id}`; const dbPath = `tutorials`; @@ -430,7 +430,7 @@ export const remoteTutorialImages = dispatch({ type: actions.TUTORIAL_IMAGE_DELETE_START }); - const type = await checkUserOrOrgHandle(owner)(firestore); + const type = await checkUserOrOrgHandle(owner)(firebase, firestore); const storagePath = `tutorials/${type}/${owner}/${tutorial_id}/${name}`; const dbPath = `tutorials`; @@ -465,69 +465,69 @@ export const remoteTutorialImages = export const updateStepTitle = (owner, tutorial_id, step_id, step_title) => - async (firebase, firestore, dispatch) => { - try { - const dbPath = `tutorials/${tutorial_id}/steps`; - await firestore - .collection(dbPath) - .doc(step_id) - .update({ - [`title`]: step_title, - updatedAt: firestore.FieldValue.serverTimestamp() - }); + async (firebase, firestore, dispatch) => { + try { + const dbPath = `tutorials/${tutorial_id}/steps`; + await firestore + .collection(dbPath) + .doc(step_id) + .update({ + [`title`]: step_title, + updatedAt: firestore.FieldValue.serverTimestamp() + }); - await getCurrentTutorialData(owner, tutorial_id)( - firebase, - firestore, - dispatch - ); - } catch (e) { - console.log(e); - } - }; + await getCurrentTutorialData(owner, tutorial_id)( + firebase, + firestore, + dispatch + ); + } catch (e) { + console.log(e); + } + }; export const updateStepTime = (owner, tutorial_id, step_id, step_time) => - async (firebase, firestore, dispatch) => { - try { - const dbPath = `tutorials/${tutorial_id}/steps`; - - await firestore - .collection(dbPath) - .doc(step_id) - .update({ - [`time`]: step_time, - updatedAt: firestore.FieldValue.serverTimestamp() - }); + async (firebase, firestore, dispatch) => { + try { + const dbPath = `tutorials/${tutorial_id}/steps`; - await getCurrentTutorialData(owner, tutorial_id)( - firebase, - firestore, - dispatch - ); - } catch (e) { - console.log(e.message); - } - }; + await firestore + .collection(dbPath) + .doc(step_id) + .update({ + [`time`]: step_time, + updatedAt: firestore.FieldValue.serverTimestamp() + }); + + await getCurrentTutorialData(owner, tutorial_id)( + firebase, + firestore, + dispatch + ); + } catch (e) { + console.log(e.message); + } + }; export const setTutorialTheme = ({ tutorial_id, owner, bgColor, textColor }) => - async (firebase, firestore, dispatch) => { - try { - const dbPath = `tutorials`; + async (firebase, firestore, dispatch) => { + try { + const dbPath = `tutorials`; - await firestore.collection(dbPath).doc(tutorial_id).update({ - text_color: textColor, - background_color: bgColor, - updatedAt: firestore.FieldValue.serverTimestamp() - }); + await firestore.collection(dbPath).doc(tutorial_id).update({ + text_color: textColor, + background_color: bgColor, + updatedAt: firestore.FieldValue.serverTimestamp() + }); - await getCurrentTutorialData(owner, tutorial_id)( - firebase, - firestore, - dispatch - ); - } catch (e) { - console.log(e.message); - } - }; + await getCurrentTutorialData(owner, tutorial_id)( + firebase, + firestore, + dispatch + ); + } catch (e) { + console.log(e.message); + } + };