From 1ced909f15125347392d56fb553084fb7031b747 Mon Sep 17 00:00:00 2001 From: Neeraj Kashyap Date: Sat, 31 Jul 2021 10:35:58 -0700 Subject: [PATCH 1/6] Updated frontend user auth service Now, instead of using Brood, it uses the Moonstream API. --- backend/moonstream/routes/users.py | 13 ++- frontend/sample.env | 8 +- frontend/src/core/hooks/useLogin.js | 2 +- frontend/src/core/hooks/useLogout.js | 2 +- frontend/src/core/hooks/useSignUp.js | 2 +- .../src/core/providers/UserProvider/index.js | 2 +- frontend/src/core/services/auth.service.js | 87 ++++++------------- frontend/src/core/services/search.service.js | 4 +- frontend/src/core/utils/http.js | 2 +- 9 files changed, 40 insertions(+), 82 deletions(-) diff --git a/backend/moonstream/routes/users.py b/backend/moonstream/routes/users.py index 276fcbb1..3cbb95ce 100644 --- a/backend/moonstream/routes/users.py +++ b/backend/moonstream/routes/users.py @@ -56,8 +56,8 @@ whitelist_paths.update( { "/users": "POST", "/users/token": "POST", - "/users/password/restore": "POST", - "/users/password/reset": "POST", + "/users/password/reset_initiate": "POST", + "/users/password/reset_complete": "POST", } ) app.add_middleware(BroodAuthMiddleware, whitelist=whitelist_paths) @@ -87,11 +87,10 @@ async def get_user_handler(request: Request) -> BugoutUser: return user -@app.post("/password/restore", tags=["users"], response_model=Dict[str, Any]) -async def restore_password_handler(request: Request) -> Dict[str, Any]: - user = request.state.user +@app.post("/password/reset_initiate", tags=["users"], response_model=Dict[str, Any]) +async def restore_password_handler(email: str = Form(...)) -> Dict[str, Any]: try: - response = bc.restore_password(email=user.email) + response = bc.restore_password(email=email) except BugoutResponseException as e: raise HTTPException(status_code=e.status_code, detail=e.detail) except Exception as e: @@ -99,7 +98,7 @@ async def restore_password_handler(request: Request) -> Dict[str, Any]: return response -@app.post("/password/reset", tags=["users"], response_model=BugoutUser) +@app.post("/password/reset_complete", tags=["users"], response_model=BugoutUser) async def reset_password_handler( reset_id: str = Form(...), new_password: str = Form(...) ) -> BugoutUser: diff --git a/frontend/sample.env b/frontend/sample.env index e295fc84..d2031fd0 100644 --- a/frontend/sample.env +++ b/frontend/sample.env @@ -1,9 +1,3 @@ -export NEXT_PUBLIC_SIMIOTICS_SEARCH_URL=http://localhost:5000 export NEXT_PUBLIC_MIXPANEL_TOKEN="" -export NEXT_PUBLIC_SIMIOTICS_AUTH_URL=http://localhost:7474 -export NEXT_PUBLIC_SIMIOTICS_JOURNALS_URL=http://localhost:7475 -export NEXT_PUBLIC_BUGOUT_CONTACTUS_TOKEN="" -export NEXT_PUBLIC_BUGOUT_CONTACTUS_JOURNAL_ID="" export NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY="" -export NEXT_PUBLIC_MOONSTREAM_API_URL=http://localhost:7481 - +export NEXT_PUBLIC_MOONSTREAM_API_URL=http://localhost:7481 \ No newline at end of file diff --git a/frontend/src/core/hooks/useLogin.js b/frontend/src/core/hooks/useLogin.js index cbb6a8ca..f9ca27a6 100644 --- a/frontend/src/core/hooks/useLogin.js +++ b/frontend/src/core/hooks/useLogin.js @@ -28,7 +28,7 @@ const useLogin = (loginType) => { if (!data) { return; } - localStorage.setItem("BUGOUT_ACCESS_TOKEN", data.data.access_token); + localStorage.setItem("MOONSTREAM_ACCESS_TOKEN", data.data.access_token); const invite_code = window.sessionStorage.getItem("invite_code"); if (invite_code) { inviteAccept(invite_code); diff --git a/frontend/src/core/hooks/useLogout.js b/frontend/src/core/hooks/useLogout.js index a8c7cec9..e883b10a 100644 --- a/frontend/src/core/hooks/useLogout.js +++ b/frontend/src/core/hooks/useLogout.js @@ -34,7 +34,7 @@ const useLogout = () => { return; } - localStorage.removeItem("BUGOUT_ACCESS_TOKEN"); + localStorage.removeItem("MOONSTREAM_ACCESS_TOKEN"); cache.clear(); }, [data, cache]); diff --git a/frontend/src/core/hooks/useSignUp.js b/frontend/src/core/hooks/useSignUp.js index f1212268..56e38dba 100644 --- a/frontend/src/core/hooks/useSignUp.js +++ b/frontend/src/core/hooks/useSignUp.js @@ -18,7 +18,7 @@ const useSignUp = (source) => { isSuccess } = useMutation(AuthService.register(), { onSuccess: (response) => { - localStorage.setItem("BUGOUT_ACCESS_TOKEN", response.data.access_token); + localStorage.setItem("MOONSTREAM_ACCESS_TOKEN", response.data.access_token); const invite_code = window.sessionStorage.getItem("invite_code"); if (invite_code) { inviteAccept(invite_code); diff --git a/frontend/src/core/providers/UserProvider/index.js b/frontend/src/core/providers/UserProvider/index.js index 84836d11..edd5bdc4 100644 --- a/frontend/src/core/providers/UserProvider/index.js +++ b/frontend/src/core/providers/UserProvider/index.js @@ -8,7 +8,7 @@ const UserProvider = ({ children }) => { const [isInit, setInit] = useState(false); const getUser = useCallback(() => { - const token = localStorage.getItem("BUGOUT_ACCESS_TOKEN"); + const token = localStorage.getItem("MOONSTREAM_ACCESS_TOKEN"); if (!token) { setInit(true); return setUser(null); diff --git a/frontend/src/core/services/auth.service.js b/frontend/src/core/services/auth.service.js index a29778fc..e5b9c257 100644 --- a/frontend/src/core/services/auth.service.js +++ b/frontend/src/core/services/auth.service.js @@ -1,9 +1,9 @@ import { http } from "../utils"; -const AUTH_URL = process.env.NEXT_PUBLIC_SIMIOTICS_AUTH_URL; +const API_URL = process.env.NEXT_PUBLIC_MOONSTREAM_API_URL; +const AUTH_URL = `${API_URL}/users`; export const login = ({ username, password }) => { - console.log('login',username, password) const data = new FormData(); data.append("username", username); data.append("password", password); @@ -17,66 +17,38 @@ export const login = ({ username, password }) => { export const revoke = () => { return http({ - method: "POST", - url: `${AUTH_URL}/revoke/${localStorage.getItem("BUGOUT_ACCESS_TOKEN")}`, - }); -}; - -export const register = () => ({ username, email, password }) => { - const data = new FormData(); - data.append("username", username); - data.append("email", email); - data.append("password", password); - - return http({ - method: "POST", - url: `${AUTH_URL}/user`, - data, - }).then(() => - http({ - method: "POST", - url: `${AUTH_URL}/token`, - data, - }) - ); -}; - -export const verify = ({ code }) => { - const data = new FormData(); - data.append("verification_code", code); - return http({ - method: "POST", - url: `${AUTH_URL}/confirm`, - data, - }); -}; - -export const getTokenList = () => { - const data = new FormData(); - return http({ - method: "GET", - url: `${AUTH_URL}/tokens`, - data, - }); -}; - -export const updateToken = ({ note, token }) => { - const data = new FormData(); - data.append("token_note", note); - data.append("access_token", token); - return http({ - method: "PUT", + method: "DELETE", url: `${AUTH_URL}/token`, - data, }); }; +export const register = + () => + ({ username, email, password }) => { + const data = new FormData(); + data.append("username", username); + data.append("email", email); + data.append("password", password); + + return http({ + method: "POST", + url: `${AUTH_URL}/`, + data, + }).then(() => + http({ + method: "POST", + url: `${AUTH_URL}/token`, + data, + }) + ); + }; + export const forgotPassword = ({ email }) => { const data = new FormData(); data.append("email", email); return http({ method: "POST", - url: `${AUTH_URL}/reset`, + url: `${AUTH_URL}/password/reset_initiate`, data, }); }; @@ -87,18 +59,11 @@ export const resetPassword = ({ newPassword, resetId }) => { data.append("new_password", newPassword); return http({ method: "POST", - url: `${AUTH_URL}/password/reset`, + url: `${AUTH_URL}/password/reset_complete`, data, }); }; -export const revokeToken = (token) => { - return http({ - method: "POST", - url: `${AUTH_URL}/revoke/${token}`, - }); -}; - export const changePassword = ({ currentPassword, newPassword }) => { const data = new FormData(); data.append("current_password", currentPassword); diff --git a/frontend/src/core/services/search.service.js b/frontend/src/core/services/search.service.js index a967a2bf..a1e23b03 100644 --- a/frontend/src/core/services/search.service.js +++ b/frontend/src/core/services/search.service.js @@ -22,9 +22,9 @@ export const getResultsByEndpoint = async (query, endpoint, clientID) => { // myself, I would have to implement the logic to handle multiple origins (since the // Access-Control-Allow-Origins only takes one origin). // At that point, uncomment the following: - // const token = localStorage.getItem('BUGOUT_ACCESS_TOKEN') + // const token = localStorage.getItem('MOONSTREAM_ACCESS_TOKEN') // if (token) { - // headers.Authorization = `Bearer ${localStorage.getItem('BUGOUT_ACCESS_TOKEN')}` + // headers.Authorization = `Bearer ${localStorage.getItem('MOONSTREAM_ACCESS_TOKEN')}` // } const response = await fetch(requestURL, { method, headers }); diff --git a/frontend/src/core/utils/http.js b/frontend/src/core/utils/http.js index ce2cc85b..aa5c68f0 100644 --- a/frontend/src/core/utils/http.js +++ b/frontend/src/core/utils/http.js @@ -4,7 +4,7 @@ let axios = require("axios"); enableMockupRequests(axios); const http = (config) => { - const token = localStorage.getItem("BUGOUT_ACCESS_TOKEN"); + const token = localStorage.getItem("MOONSTREAM_ACCESS_TOKEN"); const authorization = token ? { Authorization: `Bearer ${token}` } : {}; const defaultHeaders = config.headers ?? {}; const options = { From 57700578385b70969fe6c8f8d1a797f74d0476f2 Mon Sep 17 00:00:00 2001 From: Neeraj Kashyap Date: Sat, 31 Jul 2021 10:48:36 -0700 Subject: [PATCH 2/6] Made a few changes to fix build errors --- frontend/pages/403.js | 2 - frontend/src/components/SignUp.js | 3 +- frontend/src/components/SubscriptionsList.js | 4 +- frontend/src/components/Tags.js | 31 --- frontend/src/components/TagsList.js | 46 ---- frontend/src/components/TokenList.js | 103 -------- frontend/src/components/TokenRequest.js | 109 --------- .../{TrustedBadge.js => TrustedBadge.jsx} | 5 +- frontend/src/core/hooks/useCreateEntry.js | 36 --- frontend/src/core/hooks/useCreateJournal.js | 27 --- frontend/src/core/hooks/useDeleteEntry.js | 33 --- frontend/src/core/hooks/useDeleteJournal.js | 29 --- frontend/src/core/hooks/useEntriesSearch.js | 31 --- frontend/src/core/hooks/useForgotPassword.js | 26 -- frontend/src/core/hooks/useGroup.js | 223 ------------------ frontend/src/core/hooks/useGroups.js | 118 --------- frontend/src/core/hooks/useHumbug.js | 26 -- frontend/src/core/hooks/useHumbugTokens.js | 90 ------- frontend/src/core/hooks/useHumbugs.js | 99 -------- frontend/src/core/hooks/useInviteAccept.js | 24 -- frontend/src/core/hooks/useJournal.js | 34 --- frontend/src/core/hooks/useJournalEntries.js | 86 ------- frontend/src/core/hooks/useJournalEntry.js | 28 --- .../src/core/hooks/useJournalPermissions.js | 178 -------------- frontend/src/core/hooks/useJournalStats.js | 36 --- frontend/src/core/hooks/useJournals.js | 58 ----- frontend/src/core/hooks/useJournalsScopes.js | 25 -- frontend/src/core/hooks/usePreferences.js | 40 ---- frontend/src/core/hooks/useTokens.js | 33 --- frontend/src/core/hooks/useUpdateEntry.js | 73 ------ frontend/src/core/hooks/useUpdateTag.js | 100 -------- 31 files changed, 4 insertions(+), 1752 deletions(-) delete mode 100644 frontend/src/components/Tags.js delete mode 100644 frontend/src/components/TagsList.js delete mode 100644 frontend/src/components/TokenList.js delete mode 100644 frontend/src/components/TokenRequest.js rename frontend/src/components/{TrustedBadge.js => TrustedBadge.jsx} (85%) delete mode 100644 frontend/src/core/hooks/useCreateEntry.js delete mode 100644 frontend/src/core/hooks/useCreateJournal.js delete mode 100644 frontend/src/core/hooks/useDeleteEntry.js delete mode 100644 frontend/src/core/hooks/useDeleteJournal.js delete mode 100644 frontend/src/core/hooks/useEntriesSearch.js delete mode 100644 frontend/src/core/hooks/useForgotPassword.js delete mode 100644 frontend/src/core/hooks/useGroup.js delete mode 100644 frontend/src/core/hooks/useGroups.js delete mode 100644 frontend/src/core/hooks/useHumbug.js delete mode 100644 frontend/src/core/hooks/useHumbugTokens.js delete mode 100644 frontend/src/core/hooks/useHumbugs.js delete mode 100644 frontend/src/core/hooks/useInviteAccept.js delete mode 100644 frontend/src/core/hooks/useJournal.js delete mode 100644 frontend/src/core/hooks/useJournalEntries.js delete mode 100644 frontend/src/core/hooks/useJournalEntry.js delete mode 100644 frontend/src/core/hooks/useJournalPermissions.js delete mode 100644 frontend/src/core/hooks/useJournalStats.js delete mode 100644 frontend/src/core/hooks/useJournals.js delete mode 100644 frontend/src/core/hooks/useJournalsScopes.js delete mode 100644 frontend/src/core/hooks/usePreferences.js delete mode 100644 frontend/src/core/hooks/useTokens.js delete mode 100644 frontend/src/core/hooks/useUpdateEntry.js delete mode 100644 frontend/src/core/hooks/useUpdateTag.js diff --git a/frontend/pages/403.js b/frontend/pages/403.js index f5263be5..3aa6d031 100644 --- a/frontend/pages/403.js +++ b/frontend/pages/403.js @@ -1,5 +1,3 @@ - -import { jsx } from "@emotion/react"; import FourOThree from "../src/components/FourOThree"; const Page403 = () => { diff --git a/frontend/src/components/SignUp.js b/frontend/src/components/SignUp.js index 23364f9e..2a2ec413 100644 --- a/frontend/src/components/SignUp.js +++ b/frontend/src/components/SignUp.js @@ -26,12 +26,11 @@ const SignUp = ({ toggleModal }) => { const { signUp, isLoading, isSuccess } = useSignUp(); const ui = useContext(UIContext); - useEffect(() => { if (isSuccess) { ui.toggleModal(null); } - }, [isSuccess, toggleModal]); + }, [isSuccess, toggleModal, ui]); return ( ui.toggleModal(null)}> diff --git a/frontend/src/components/SubscriptionsList.js b/frontend/src/components/SubscriptionsList.js index f34a89e8..c097a499 100644 --- a/frontend/src/components/SubscriptionsList.js +++ b/frontend/src/components/SubscriptionsList.js @@ -18,7 +18,7 @@ import CopyButton from "./CopyButton"; import { useSubscriptions } from "../core/hooks"; import ConfirmationRequest from "./ConfirmationRequest"; -const List = (data) => { +const SubscriptionsList = () => { const { subscriptionsCache, changeNote, deleteSubscription } = useSubscriptions(); @@ -127,4 +127,4 @@ const List = (data) => { return ""; } }; -export default List; +export default SubscriptionsList; diff --git a/frontend/src/components/Tags.js b/frontend/src/components/Tags.js deleted file mode 100644 index 194470e6..00000000 --- a/frontend/src/components/Tags.js +++ /dev/null @@ -1,31 +0,0 @@ -import React from "react"; -import { Tag, TagLabel, Flex } from "@chakra-ui/react"; -const Tags = ({ tags }) => { - const displayTags = tags?.filter( - (tag) => - tag.startsWith("from") || - tag.startsWith("client") || - tag.startsWith("network") || - tag.startsWith("to") || - tag.startsWith("source") || - tag.startsWith("node") - ); - return ( - - - {displayTags?.map((tag, index) => ( - - {tag} - - ))} - - - ); -}; - -export default Tags; diff --git a/frontend/src/components/TagsList.js b/frontend/src/components/TagsList.js deleted file mode 100644 index 26a717f2..00000000 --- a/frontend/src/components/TagsList.js +++ /dev/null @@ -1,46 +0,0 @@ - -import { jsx } from "@emotion/react"; -import { Tag, TagLabel, Flex, Button } from "@chakra-ui/react"; -import { useState } from "react"; - -const TAGS_DISPLAY_NUM_DEF = 5; -const TagsList = ({ tags }) => { - const [showAllTags, toggleAllTags] = useState(false); - const tagButtonText = showAllTags ? "Show less" : "Show all"; - - const TagsToShow = () => - tags - .filter((tag, i) => (showAllTags ? true : i < TAGS_DISPLAY_NUM_DEF)) - .map((tag, index) => { - return ( - - {tag} - - ); - }); - - return ( - - - {tags.length > TAGS_DISPLAY_NUM_DEF && ( - - )} - - ); -}; -export default TagsList; diff --git a/frontend/src/components/TokenList.js b/frontend/src/components/TokenList.js deleted file mode 100644 index 936d4f64..00000000 --- a/frontend/src/components/TokenList.js +++ /dev/null @@ -1,103 +0,0 @@ -import React from "react"; -import { IconButton } from "@chakra-ui/react"; -import { - Table, - Th, - Td, - Tr, - Thead, - Tbody, - Text, - Center, - Spinner, -} from "@chakra-ui/react"; -import { DeleteIcon } from "@chakra-ui/icons"; -import CopyButton from "./CopyEntryButton"; -import ConfirmationRequest from "./ConfirmationRequest"; -import NewTokenTr from "./NewTokenTr"; -import { useForm } from "react-hook-form"; - -const TokenList = ({ - tokens, - revoke, - isLoading, - isNewTokenOpen, - toggleNewToken, - createToken, - journalName, -}) => { - const { register, handleSubmit, errors } = useForm(); - if (isLoading) - return ( -
- -
- ); - - const handleTokenSubmit = ({ appName, appVersion }) => { - createToken({ appName, appVersion }).then(() => toggleNewToken(false)); - }; - - return ( -
- - - - - - - - - - - {tokens.map((token, idx) => { - return ( - - - - - - - ); - })} - - - -
TokenApp NameApp versionAction
- {token.restricted_token_id} - {token.app_name}{token.app_version} - revoke(token.restricted_token_id)} - > - } - /> - -
- {tokens.length < 1 && ( -
- Create Usage report tokens here -
- )} -
- ); -}; -export default TokenList; diff --git a/frontend/src/components/TokenRequest.js b/frontend/src/components/TokenRequest.js deleted file mode 100644 index b6dd42ee..00000000 --- a/frontend/src/components/TokenRequest.js +++ /dev/null @@ -1,109 +0,0 @@ - -import { jsx } from "@emotion/react"; -import { - Box, - InputGroup, - InputLeftElement, - FormControl, - FormErrorMessage, - HStack, - Button, - InputRightElement, - Input, -} from "@chakra-ui/react"; -import { CloseIcon } from "@chakra-ui/icons"; -import { useEffect, useState, useRef } from "react"; -import CustomIcon from "./CustomIcon" -import { useForm } from "react-hook-form"; -import { useUser, useLogin } from "../core/hooks"; - -const TokenRequest = ({ newToken, toggle }) => { - const { user } = useUser(); - const { login, isLoading, data } = useLogin("SendLoginProp"); - const { handleSubmit, errors, register } = useForm(); - const [showPassword, setShowPassword] = useState("password"); - - const togglePassword = () => { - if (showPassword === "password") { - setShowPassword("text"); - } else { - setShowPassword("password"); - } - }; - - const PasswordRef = useRef(); - - useEffect(() => { - if (PasswordRef.current) { - PasswordRef.current.focus(); - } - }, [PasswordRef]); - - useEffect(() => { - if (data) { - newToken(data.data.access_token); - toggle(null); - } - }, [data, newToken, toggle]); - - const formStyle = { - display: "flex", - flexWrap: "wrap", - minWidth: "100px", - flexFlow: "row wrap-reverse", - aligntContent: "flex-end", - }; - - if (!user) return ""; //loading... - - return ( - -
- - - - - - - - - { - register(e, { required: "Password is required!" }); - PasswordRef.current = e; - }} - /> - toggle(null)}> - - - - - {errors.password && errors.password.message} - - - - -
-
- ); -}; -export default TokenRequest; diff --git a/frontend/src/components/TrustedBadge.js b/frontend/src/components/TrustedBadge.jsx similarity index 85% rename from frontend/src/components/TrustedBadge.js rename to frontend/src/components/TrustedBadge.jsx index 12fbf675..5e36f8be 100644 --- a/frontend/src/components/TrustedBadge.js +++ b/frontend/src/components/TrustedBadge.jsx @@ -1,5 +1,4 @@ - -import { jsx } from "@emotion/react"; +import { React } from "react"; import { Flex, Image, Link } from "@chakra-ui/react"; const TrustedBadge = ({ name, caseURL, ImgURL }) => { @@ -20,7 +19,6 @@ const TrustedBadge = ({ name, caseURL, ImgURL }) => { alt={name} > {caseURL && ( - // { > {`Read case study >`} - // )} ); diff --git a/frontend/src/core/hooks/useCreateEntry.js b/frontend/src/core/hooks/useCreateEntry.js deleted file mode 100644 index 19ee50a1..00000000 --- a/frontend/src/core/hooks/useCreateEntry.js +++ /dev/null @@ -1,36 +0,0 @@ -import { useMutation, useQueryCache } from "react-query"; -import { EntryService } from "../services"; -import { useToast } from "."; - -const useCreateEntry = (journalId) => { - const cache = useQueryCache(); - const toast = useToast(); - - const [createEntry, { isLoading, data }] = useMutation( - EntryService.create(journalId), - { - onSuccess: (newEntry) => { - const EntriesPages = cache.getQueryData([ - "journal-entries", - { journalId }, - ]); - EntriesPages[0].data.unshift(newEntry.data); - EntriesPages.map((page, idx) => { - if (idx + 1 < EntriesPages.length) { - const ShiftedEntry = EntriesPages[idx].data.pop(); - EntriesPages[idx + 1].data.unshift(ShiftedEntry); - } - return page; - }); - cache.setQueryData(["journal-entries", { journalId }], EntriesPages); - }, - onError: (error) => { - toast(error, "error"); - }, - } - ); - - return { createEntry, isLoading, data }; -}; - -export default useCreateEntry; diff --git a/frontend/src/core/hooks/useCreateJournal.js b/frontend/src/core/hooks/useCreateJournal.js deleted file mode 100644 index 4750147d..00000000 --- a/frontend/src/core/hooks/useCreateJournal.js +++ /dev/null @@ -1,27 +0,0 @@ -import { useMutation, useQueryCache } from "react-query"; -import { JournalService } from "../services"; - -const useCreateJournal = () => { - const cache = useQueryCache(); - - const createJournal = useMutation(JournalService.create, { - onSuccess: (newJournal) => { - const previousJournals = cache.getQueryData(["journals-list"]); - const newJournals = [...previousJournals]; - newJournals.push(newJournal.data); - newJournals.sort(function (a, b) { - var aName = a.name.toUpperCase(); - var bName = b.name.toUpperCase(); - return aName < bName ? -1 : aName < bName ? 1 : 0; - }); - - cache.setQueryData(["journals-list"], newJournals); - - return () => cache.setQueryData(["journals-list"], previousJournals); - }, - }); - - return createJournal; -}; - -export default useCreateJournal; diff --git a/frontend/src/core/hooks/useDeleteEntry.js b/frontend/src/core/hooks/useDeleteEntry.js deleted file mode 100644 index e213cb99..00000000 --- a/frontend/src/core/hooks/useDeleteEntry.js +++ /dev/null @@ -1,33 +0,0 @@ -import { useMutation, useQueryCache } from "react-query"; -import { EntryService } from "../services"; -import { useToast } from "."; - -const useDeleteEntry = ({ entryId, journalId }) => { - const cache = useQueryCache(); - const toast = useToast(); - - const [deleteEntry] = useMutation( - EntryService.deleteEntry(journalId, entryId), - { - onSuccess: () => { - const previousEntriesPages = cache.getQueryData([ - "journal-entries", - { journalId }, - ]); - const newEntriesPages = [...previousEntriesPages]; - newEntriesPages.map((page) => { - page.data = page.data.filter((entry) => entry.id !== entryId); - return page; - }); - - cache.setQueryData(["journal-entries", { journalId }], newEntriesPages); - }, - onError: (error) => { - toast(error, "error"); - }, - } - ); - return deleteEntry; -}; - -export default useDeleteEntry; diff --git a/frontend/src/core/hooks/useDeleteJournal.js b/frontend/src/core/hooks/useDeleteJournal.js deleted file mode 100644 index 23a3f049..00000000 --- a/frontend/src/core/hooks/useDeleteJournal.js +++ /dev/null @@ -1,29 +0,0 @@ -import { useMutation, useQueryCache } from "react-query"; -import { JournalService } from "../services"; -import { useToast } from "."; - -const useDeleteJournal = (id) => { - const cache = useQueryCache(); - const toast = useToast(); - - const [deleteJournal] = useMutation(JournalService.deleteJournal(id), { - onMutate: () => { - const previousJournals = [...cache.getQueryData(["journals-list"])]; - - const newJournals = previousJournals.filter( - (journal) => journal.id !== id - ); - cache.setQueryData(["journals-list"], newJournals); - - return { previousJournals }; - }, - onError: (error, variables, context) => { - cache.setQueryData(["journals-list"], context.previousJournals); - toast("Not enough permisions to delete this Journal", "error"); - }, - }); - - return { deleteJournal }; -}; - -export default useDeleteJournal; diff --git a/frontend/src/core/hooks/useEntriesSearch.js b/frontend/src/core/hooks/useEntriesSearch.js deleted file mode 100644 index 46bedb07..00000000 --- a/frontend/src/core/hooks/useEntriesSearch.js +++ /dev/null @@ -1,31 +0,0 @@ -import { useMutation } from "react-query"; -import { JournalService } from "../services"; -import { useToast } from "."; - -const useEntriesSearch = ({ journalId }) => { - const toast = useToast(); - - const { - mutateAsync: entriesSearch, - isLoading, - error, - data, - } = useMutation( - JournalService.searchEntries({ journalId }), - - { - onError: (error) => { - toast(error, "error"); - }, - } - ); - - return { - entriesSearch, - data, - isLoading, - error, - }; -}; - -export default useEntriesSearch; diff --git a/frontend/src/core/hooks/useForgotPassword.js b/frontend/src/core/hooks/useForgotPassword.js deleted file mode 100644 index ad6dae19..00000000 --- a/frontend/src/core/hooks/useForgotPassword.js +++ /dev/null @@ -1,26 +0,0 @@ -import { useEffect } from "react"; -import { useMutation } from "react-query"; -import { AuthService } from "../../core/services"; -import { useAuthResultHandler } from "./"; -import { useToast } from "."; - -const useForgotPassword = () => { - const toast = useToast(); - const [forgotPassword, { isLoading, error, data }] = useMutation( - AuthService.forgotPassword - ); - useAuthResultHandler( - data, - error, - "Please check your inbox for verification URL." - ); - useEffect(() => { - if (error?.response?.data?.detail) { - toast(error.response.data.detail, "error"); - } - }, [error, toast, data]); - - return { forgotPassword, isLoading, data }; -}; - -export default useForgotPassword; diff --git a/frontend/src/core/hooks/useGroup.js b/frontend/src/core/hooks/useGroup.js deleted file mode 100644 index 0b22fb07..00000000 --- a/frontend/src/core/hooks/useGroup.js +++ /dev/null @@ -1,223 +0,0 @@ -import { useQuery, useQueryCache, useMutation } from "react-query"; -import { GroupService, UserService } from "../services"; -import { useToast, useUser } from "."; -import { queryCacheProps } from "./hookCommon"; - -const useGroup = (groupId) => { - const { user } = useUser(); - const cache = useQueryCache(); - const toast = useToast(); - - const { data: GroupUsersResponse, isLoading, refetch: getUsers } = useQuery( - ["group-users", groupId], - GroupService.getGroupUsers, - queryCacheProps - ); - - const getInvites = async (key, groupId) => { - var data; - data = await GroupService.getInvites(groupId); - const newInvites = data.data.invites; - - return [...newInvites]; - }; - - const invitesQueryCache = useQuery( - ["group-invites", groupId], - getInvites, - queryCacheProps - ); - - const [addExistingUser, addUserStatus] = useMutation( - GroupService.setGroupUser(groupId), - { - onMutate: (newUser) => { - const NewGroupResponse = cache.getQueryData(["group-users", groupId]); - const previousGroupResponse = JSON.parse( - JSON.stringify(NewGroupResponse) - ); - NewGroupResponse.data.users = [ - ...NewGroupResponse.data.users, - { email: newUser.email, user_type: newUser.role }, - ]; - cache.setQueryData(["group-users", groupId], { - ...NewGroupResponse, - }); - - return previousGroupResponse; - }, - onError: (error, variables, context) => { - cache.setQueryData(["group-users", groupId], context); - toast(error, "error"); - }, - - //fetch data from backend again to fill missing fields of newly added - //user - onSuccess: () => { - getUsers(); - }, - } - ); - - const [sendInvite, sendInviteStatus] = useMutation( - GroupService.sendInvite(groupId), - { - onSuccess: () => { - invitesQueryCache.refetch(); - }, - onError: (error) => { - toast(error, "error"); - }, - } - ); - - /** - * addToGroup adds to group. - * - * If `email` is specified and found our users DB - add user. - * - * If `email` is specified and not found in users DB - send invite link - * - * If `email` not specified - return public invite code - */ - const addToGroup = async (invitee) => { - if (invitee?.email) { - const query = `email=${invitee.email}`; - - await UserService.findUser(query).then( - (response) => { - if (response.data.user_id) { - addExistingUser(invitee); - } - }, - () => { - if (invitee.email) { - sendInvite(invitee); - } else { - toast("user not found", "error"); - } - } - ); - } else { - sendInvite(); - } - }; - - //ToDo: const addUserMutation = useMutation(.. when upgrading to React Query 3 - const addUserMutation = { - addUser: addToGroup, - isLoading: addUserStatus.isLoading, - }; - - const [removeUser, removeUserStatus] = useMutation( - GroupService.deleteGroupUser(groupId), - { - onMutate: (removedUsername) => { - const NewGroupResponse = cache.getQueryData(["group-users", groupId]); - const previousGroupResponse = JSON.parse( - JSON.stringify(NewGroupResponse) - ); - NewGroupResponse.data.users = NewGroupResponse.data.users.filter( - (user) => user.username !== removedUsername - ); - cache.setQueryData(["group-users", groupId], { - ...NewGroupResponse, - }); - if (user.username === removedUsername) { - const NewGroupsResponse = cache.getQueryData(["groups"]); - const previousGroupsResponse = JSON.parse( - JSON.stringify(NewGroupsResponse) - ); - NewGroupsResponse.data[groupId].user_type = "none"; - - cache.setQueryData(["groups"], { - ...NewGroupsResponse, - }); - - return { previousGroupResponse, previousGroupsResponse }; - } else { - return { previousGroupResponse }; - } - }, - onError: (error, variables, context) => { - cache.setQueryData( - ["group-users", groupId], - context.previousGroupResponse - ); - if (context.previousGroupsResponse) { - cache.setQueryData(["groups"], context.previousGroupsResponse); - } - - toast(error, "error"); - }, - - onSuccess: (response, username) => { - if (user.username === username) { - const NewGroupsResponse = cache.getQueryData(["groups"]); - delete NewGroupsResponse.data[groupId]; - - cache.setQueryData(["groups"], { - ...NewGroupsResponse, - }); - } - }, - } - ); - - //ToDo: const removeUserMutation = useMutation(.. when upgrading to React Query 3 - const removeUserMutation = { - removeUser, - isLoading: removeUserStatus.isLoading, - }; - - const [revokeInvite, activatePublicInviteStatus] = useMutation( - GroupService.deleteInvite(groupId), - { - onSuccess: () => { - invitesQueryCache.refetch(); - }, - } - ); - - const users = { - isLoading: isLoading, - data: GroupUsersResponse?.data?.users, - refetch: getUsers, - }; - - const readInvites = ({ isPublic, isPersonal }) => { - const allInvites = cache.getQueryData(["group-invites", groupId]); - if (allInvites) { - if (isPublic && isPersonal) { - return allInvites; - } else if (isPersonal) { - return allInvites.filter((item) => item.email && item.active); - } else { - return allInvites.filter((item) => !item.email && item.active); - } - } - }; - - const invites = { - personal: readInvites({ isPublic: false, isPersonal: true }), - public: readInvites({ isPublic: true, isPersonal: false }), - all: readInvites({ isPublic: true, isPersonal: true }), - isLoading: invitesQueryCache.isLoading, - get: () => invitesQueryCache.refetch(), - - createPersonal: addToGroup, - createPublic: () => addToGroup(), - isLoadingCreate: sendInviteStatus.isLoading, - revokeInvite: revokeInvite, - isLoadingRevoke: activatePublicInviteStatus.isLoading, - }; - - return { - users, - addUserMutation, - removeUserMutation, - invites, - }; -}; - -export default useGroup; diff --git a/frontend/src/core/hooks/useGroups.js b/frontend/src/core/hooks/useGroups.js deleted file mode 100644 index 9147a8f7..00000000 --- a/frontend/src/core/hooks/useGroups.js +++ /dev/null @@ -1,118 +0,0 @@ -import { useQuery, useQueryCache, useMutation } from "react-query"; -import { GroupService } from "../services"; -import { useToast } from "."; -import { queryCacheProps } from "./hookCommon"; -import { useUser } from "../hooks"; - -const useGroups = () => { - const cache = useQueryCache(); - const toast = useToast(); - const { user } = useUser(); - - const fetchGroups = async (query) => { - const response = await GroupService.getGroups(query); - - return response?.data?.groups; - }; - - const { data, isLoading, refetch: getGroups } = useQuery( - "groups", - fetchGroups, - queryCacheProps - ); - - const [createGroup, createStatus] = useMutation( - (groupName) => GroupService.createGroup(groupName), - { - onSuccess: (response) => { - const currentGroups = cache.getQueryData(["groups"]); - currentGroups.push({ - autogenerated: false, - group_id: response.data.id, - group_name: response.data.group_name, - user_id: user.user_id, - user_type: "owner", - }); - cache.setQueryData(["groups"], currentGroups); - cache.refetchQueries(["groups"]); - }, - onError: (error) => { - toast(error, "error"); - }, - } - ); - - //ToDo: const createGroupMutation = useMutation(.. when upgrading to React Query 3 - const createGroupMutation = { - createGroup, - isLoading: createStatus.isLoading, - }; - - const [deleteGroup, deleteStatus] = useMutation(GroupService.deleteGroup, { - onMutate: (groupId) => { - const previousGroups = cache.getQueryData(["groups"]); - - const newGroups = previousGroups.filter( - (group) => group.group_id !== groupId - ); - - cache.setQueryData(["groups"], [...newGroups]); - - return previousGroups; - }, - onError: (error, variables, context) => { - cache.setQueryData(["groups"], context); - toast(error, "error"); - }, - }); - - //ToDo: const createGroupMutation = useMutation(.. when upgrading to React Query 3 - const deleteGroupMutation = { - deleteGroup, - isLoading: deleteStatus.isLoading, - }; - - const [renameGroup, renameStatus] = useMutation(GroupService.setGroupName, { - onMutate: (data) => { - const newGroups = cache.getQueryData(["groups"]); - const previousGroups = [...newGroups]; - - newGroups.forEach((group) => { - if (group.group_id === data.groupId) { - group.group_name = data.name; - } - }); - - cache.setQueryData(["groups"], [...newGroups]); - - return previousGroups; - }, - - onError: (error, variables, context) => { - cache.setQueryData(["groups"], context); - toast(error, "error"); - }, - - onSuccess: () => { - getGroups(); - }, - }); - - //ToDo: const renameGroupMutation = useMutation(.. when upgrading to React Query 3 - const renameGroupMutation = { - renameGroup, - isLoading: renameStatus.isLoading, - status: { ...renameStatus }, - }; - - return { - data, - isLoading, - getGroups, - createGroupMutation, - deleteGroupMutation, - renameGroupMutation, - }; -}; - -export default useGroups; diff --git a/frontend/src/core/hooks/useHumbug.js b/frontend/src/core/hooks/useHumbug.js deleted file mode 100644 index de19567f..00000000 --- a/frontend/src/core/hooks/useHumbug.js +++ /dev/null @@ -1,26 +0,0 @@ -import { HumbugService } from "../services"; -import { useToast } from "."; -import { useQuery } from "react-query"; -import { queryCacheProps } from "./hookCommon"; - -const useHumbug = (humbugId) => { - const toast = useToast(); - - const { data, isLoading, refetch } = useQuery( - ["humbug", { humbugId }], - HumbugService.getHumbug, - { - ...queryCacheProps, - onError: (error) => { - toast(error, "error"); - }, - } - ); - - return { - data, - isLoading, - refetch, - }; -}; -export default useHumbug; diff --git a/frontend/src/core/hooks/useHumbugTokens.js b/frontend/src/core/hooks/useHumbugTokens.js deleted file mode 100644 index f2dd4a6e..00000000 --- a/frontend/src/core/hooks/useHumbugTokens.js +++ /dev/null @@ -1,90 +0,0 @@ -import { HumbugService } from "../services"; -import { useToast } from "."; -import { useMutation, useQuery, useQueryCache } from "react-query"; -import { queryCacheProps } from "./hookCommon"; - -const useHumbugTokens = (humbugId) => { - const toast = useToast(); - const cache = useQueryCache(); - - const getTokens = async (key, { humbugId }) => { - var data; - data = await HumbugService.getTokens(humbugId); - const newHumbugTokens = data.data.tokens; - return [...newHumbugTokens]; - }; - - const humbugTokensCache = useQuery( - [`Humbug-Tokens`, { humbugId }], - getTokens, - queryCacheProps - ); - - const [ - createRestrictedToken, - { - isLoading: isLoadingRestrictedToken, - error: errorRestrictedToken, - data: dataRestrictedToken, - }, - ] = useMutation(HumbugService.createRestrictedToken(humbugId), { - onMutate: () => {}, - onError: (error) => { - toast(error, "error"); - }, - - onSuccess: (response) => { - const oldData = cache.getQueryData([`Humbug-Tokens`, { humbugId }]); - const newData = [...oldData, ...response.data.tokens]; - cache.setQueryData([`Humbug-Tokens`, { humbugId }], newData); - }, - }); - - const createRestrictedTokenMutation = { - createRestrictedToken, - isLoading: isLoadingRestrictedToken, - error: errorRestrictedToken, - data: dataRestrictedToken, - }; - - const [ - deleteRestrictedToken, - { - isLoading: isLoadingDeleteRestrictedToken, - error: errorDeleteRestrictedToken, - data: dataDeleteRestrictedToken, - }, - ] = useMutation(HumbugService.deleteRestrictedToken(humbugId), { - onMutate: (tokenId) => { - var newTokens = cache.getQueryData([`Humbug-Tokens`, { humbugId }]); - const previousTokens = [...newTokens]; - - newTokens = newTokens.filter( - (token) => token.restricted_token_id !== tokenId - ); - - cache.setQueryData([`Humbug-Tokens`, { humbugId }], newTokens); - - return previousTokens; - }, - onError: (error, variables, context) => { - cache.setQueryData([`Humbug-Tokens`, { humbugId }], context); - toast(error, "error"); - }, - }); - - const deleteRestrictedTokenMutation = { - deleteRestrictedToken, - isLoading: isLoadingDeleteRestrictedToken, - error: errorDeleteRestrictedToken, - data: dataDeleteRestrictedToken, - }; - - return { - humbugTokensCache, - createRestrictedTokenMutation, - deleteRestrictedTokenMutation, - }; -}; - -export default useHumbugTokens; diff --git a/frontend/src/core/hooks/useHumbugs.js b/frontend/src/core/hooks/useHumbugs.js deleted file mode 100644 index 1e1b1ceb..00000000 --- a/frontend/src/core/hooks/useHumbugs.js +++ /dev/null @@ -1,99 +0,0 @@ -import { HumbugService } from "../services"; -import { useToast } from "."; -import { useMutation, useQuery, useQueryCache } from "react-query"; -import { queryCacheProps } from "./hookCommon"; - -const useHumbugs = (query) => { - const toast = useToast(); - const cache = useQueryCache(); - - const getHumbugItegrations = async (key, { query }) => { - var data; - if (!query) { - data = await HumbugService.getHumbugItegrations(); - } else { - data = await HumbugService.getHumbugItegrations(query); - } - const newHumbugIntegrations = data.data.integrations; - - return [...newHumbugIntegrations]; - }; - - const { data: humbugList } = useQuery( - ["humbugs", { query }], - getHumbugItegrations, - queryCacheProps - ); - - const [ - createHumbug, - { - isLoading: isLoadingCreateHumbug, - error: errorCreateHumbug, - data: dataCreateHumbug, - }, - ] = useMutation(HumbugService.createHumbug, { - onSuccess: (response) => { - var oldData = cache.getQueryData(["humbugs", { query }]); - var newData = oldData ? [...oldData, response.data] : [response.data]; - cache.setQueryData(["humbugs", { query }], newData); - cache.refetchQueries(["journals-list"]); - - cache.refetchQueries(["humbugs"], newData); - }, - onError: (error) => { - toast(error, "error"); - }, - }); - - const [ - deleteHumbug, - { - isLoading: isLoadingDeleteHumbug, - error: errorDeleteHumbug, - data: dataDeleteHumbug, - }, - ] = useMutation(HumbugService.deleteHumbug, { - onMutate: (humbugId) => { - var newHumbugs = cache.getQueryData(["humbugs", { query }]); - const previousHumbugs = [...newHumbugs]; - newHumbugs = newHumbugs.filter((humbug) => humbug.id !== humbugId); - - cache.setQueryData(["humbugs", { query }], newHumbugs); - - var newHumbugsAll = cache.getQueryData(["humbugs", {}]); - const prevHumbugsAll = [...newHumbugsAll]; - newHumbugsAll = newHumbugs.filter((humbug) => humbug.id !== humbugId); - - cache.setQueryData(["humbugs", {}], newHumbugsAll); - - return { previousHumbugs, prevHumbugsAll }; - }, - onError: (error, variables, context) => { - cache.setQueryData(["humbugs", { query }], context.previousHumbugs); - cache.setQueryData(["humbugs"], context.prevHumbugsAll); - toast(error, "error"); - }, - }); - - const createHumbugMutation = { - createHumbug, - isLoading: isLoadingCreateHumbug, - errorCreateHumbug, - dataCreateHumbug, - }; - - const deleteHumbugMutation = { - deleteHumbug, - isLoading: isLoadingDeleteHumbug, - errorDeleteHumbug, - dataDeleteHumbug, - }; - - return { - humbugList, - createHumbugMutation, - deleteHumbugMutation, - }; -}; -export default useHumbugs; diff --git a/frontend/src/core/hooks/useInviteAccept.js b/frontend/src/core/hooks/useInviteAccept.js deleted file mode 100644 index c8a0bcdf..00000000 --- a/frontend/src/core/hooks/useInviteAccept.js +++ /dev/null @@ -1,24 +0,0 @@ -import { InvitesService } from "../services"; -import { useToast } from "."; - -const useInviteAccept = () => { - const toast = useToast(); - - const inviteAccept = (invite_code) => { - InvitesService.accept(invite_code) - .then( - () => toast("You were successfully added to the team", "success"), - (reason) => { - toast(reason, "error"); - } - ) - .finally(window.sessionStorage.clear("invite_code")) - .catch((error) => { - console.error("Error during sending an invite link:", error); - }); - }; - - return { inviteAccept }; -}; - -export default useInviteAccept; diff --git a/frontend/src/core/hooks/useJournal.js b/frontend/src/core/hooks/useJournal.js deleted file mode 100644 index 35914dd2..00000000 --- a/frontend/src/core/hooks/useJournal.js +++ /dev/null @@ -1,34 +0,0 @@ -import { useQuery } from "react-query"; -import { JournalService } from "../services"; -import { queryCacheProps } from "./hookCommon"; -import { useToast } from "."; - -const useJournal = (journalId, journalScope) => { - const toast = useToast(); - - const getJournal = async (query, key) => { - const journalEndpoint = - journalScope === "public" - ? JournalService.getPublicJournal - : JournalService.getJournal; - const data = await journalEndpoint(key, { journalId }); - - const entry = data.data; - return entry; - }; - - const { data, isLoading, refetch } = useQuery("journal", getJournal, { - ...queryCacheProps, - onError: (error) => { - toast(error, "error"); - }, - }); - - return { - data, - isLoading, - refetch, - }; -}; - -export default useJournal; diff --git a/frontend/src/core/hooks/useJournalEntries.js b/frontend/src/core/hooks/useJournalEntries.js deleted file mode 100644 index 90baaabb..00000000 --- a/frontend/src/core/hooks/useJournalEntries.js +++ /dev/null @@ -1,86 +0,0 @@ -import { useInfiniteQuery } from "react-query"; -import { useEntriesSearch } from "."; -import { queryCacheProps } from "./hookCommon"; - -const useJournalEntries = ({ - journalId, - journalType, - isContent, - pageSize, - searchQuery, -}) => { - const limit = pageSize ? pageSize : 25; - - const { entriesSearch } = useEntriesSearch({ - journalId, - }); - - const getEntries = - (searchTerm) => - async ({ pageParam = 0 }) => { - if (!pageParam) { - pageParam = 0; - } - - const searchTags = searchTerm.split(" ").filter(function (n) { - if (n.startsWith("#")) return n; - else { - return null; - } - }); - - const data = await entriesSearch({ - searchTerm, - journalType, - isContent, - limit, - offset: pageParam, - searchTags, - }); - const newEntryList = data.data.results.map((entry) => ({ - ...entry, - id: entry.entry_url.split("/").pop(), - })); - return { - data: [...newEntryList], - pageParams: { - pageParam: pageParam + 1, - next_offset: data.data.next_offset, - total_results: data.data.total_results, - offset: data.data.offset, - }, - }; - }; - - const { - data: EntriesPages, - isFetchingMore, - isLoading, - canFetchMore, - fetchMore, - refetch, - } = useInfiniteQuery( - ["journal-entries", { journalId }], - getEntries(searchQuery), - { - refetchInterval: 1000, - ...queryCacheProps, - // getNextPageParam: (lastPage) => lastPage.next_offset ?? false, - getNextPageParam: (lastGroup) => { - return lastGroup.next_offset === null ? false : lastGroup.next_offset; - }, - enabled: !!journalId, - } - ); - - return { - EntriesPages, - fetchMore, - isFetchingMore, - canFetchMore, - refetch, - isLoading, - }; -}; - -export default useJournalEntries; diff --git a/frontend/src/core/hooks/useJournalEntry.js b/frontend/src/core/hooks/useJournalEntry.js deleted file mode 100644 index e46eac98..00000000 --- a/frontend/src/core/hooks/useJournalEntry.js +++ /dev/null @@ -1,28 +0,0 @@ -import { useQuery } from "react-query"; -import { JournalService } from "../services"; -import { queryCacheProps } from "./hookCommon"; -import { useToast } from "."; - -const useJournalEntry = (journalId, entryId, journalScope) => { - const toast = useToast(); - const getEntry = async (query, key) => { - const endpoint = - journalScope === "personal" - ? JournalService.getEntry - : JournalService.getPublicEntry; - const data = await endpoint(key, { journalId, entryId }); - - const entry = data.data; - return entry; - }; - - const { data, isLoading, isFetchedAfterMount, refetch, isError, error } = - useQuery(["journal-entry", { journalId, entryId }], getEntry, { - ...queryCacheProps, - onError: (error) => toast(error, "error"), - }); - - return { data, isFetchedAfterMount, isLoading, refetch, isError, error }; -}; - -export default useJournalEntry; diff --git a/frontend/src/core/hooks/useJournalPermissions.js b/frontend/src/core/hooks/useJournalPermissions.js deleted file mode 100644 index 441df7ff..00000000 --- a/frontend/src/core/hooks/useJournalPermissions.js +++ /dev/null @@ -1,178 +0,0 @@ -import { useQuery, useMutation, useQueryClient } from "react-query"; -import { JournalService } from "../services"; -import { useToast } from "."; -import { queryCacheProps } from "./hookCommon"; - -const useJournalPermissions = (journalId, journalScope) => { - const cache = useQueryClient(); - const toast = useToast(); - const { - data, - isLoading, - refetch: getPermissions, - error, - } = useQuery( - ["journal-permissions", { journalId }], - async () => { - if (journalId) { - if (journalScope === "personal") { - const response = await JournalService.getJournalPermissions( - journalId - ); - if (!response.data || !response.data.permissions) { - return []; - } - return response.data.permissions; - } else { - return []; - } - } else { - const response = { data: { scopes: ["public"] } }; - return response; - } - }, - queryCacheProps - ); - - const { - data: currentUserPermissions, - refetch: getCurrentUserPermissions, - isLoading: currentUserPermissionsIsLoading, - } = useQuery( - ["journal-permissions-current-user", { journalId }], - async () => { - if (journalId) { - if (journalScope === "personal") { - let response = { data: {} }; - try { - response = await JournalService.getCurrentUserJournalPermissions( - journalId - ); - } catch (error) { - console.warn("error retrieving scopes:", error); - } - if (!response.data || !response.data.scopes) { - return []; - } - return response.data.scopes.map((scope) => scope.permission); - } else { - return []; - } - } else { - const response = { data: { scopes: ["public"] } }; - return response; - } - }, - { - ...queryCacheProps, - staleTime: 720000, // 12 hours - } - ); - - const setJournalPermissionMutation = useMutation( - JournalService.setJournalPermission(journalId), - { - onMutate: (data) => { - const newJournalPermissionResponse = cache.getQueryData([ - "journal-permissions", - { journalId }, - ]); - const previousJournalPermissionResponse = JSON.parse( - JSON.stringify(newJournalPermissionResponse) - ); - const index = previousJournalPermissionResponse.findIndex( - (i) => i.holder_id === data.holder_id - ); - - if (index === -1) { - newJournalPermissionResponse.push({ - permissions: [...data.permission_list], - holder_id: data.holder_id, - holder_type: data.holder_type, - }); - } else { - newJournalPermissionResponse[index].permissions = [ - ...newJournalPermissionResponse[index].permissions, - ...data.permission_list, - ]; - } - - cache.setQueryData( - ["journal-permissions", { journalId }], - newJournalPermissionResponse - ); - - return previousJournalPermissionResponse; - }, - onError: (error, value, context) => { - cache.setQueryData(["journal-permissions", { journalId }], context); - - toast(error, "error"); - }, - - onSuccess: () => { - getCurrentUserPermissions(); - }, - } - ); - - const removeJournalPermissionMutation = useMutation( - JournalService.deleteJournalPermission(journalId), - { - onMutate: (data) => { - const newJournalPermissionResponse = cache.getQueryData([ - "journal-permissions", - { journalId }, - ]); - const previousJournalPermissionResponse = JSON.parse( - JSON.stringify(newJournalPermissionResponse) - ); - const index = previousJournalPermissionResponse.findIndex( - (i) => i.holder_id === data.holder_id - ); - newJournalPermissionResponse[index].permissions = - newJournalPermissionResponse[index].permissions.filter( - (value) => !data.permission_list.includes(value) - ); - - if (newJournalPermissionResponse[index].permissions.length < 1) { - newJournalPermissionResponse.splice(index, 1); - } - - cache.setQueryData( - ["journal-permissions", { journalId }], - newJournalPermissionResponse - ); - - return previousJournalPermissionResponse; - }, - onError: (error, value, context) => { - cache.setQueryData(["journal-permissions", { journalId }], context); - - toast(error, "error"); - }, - - onSuccess: () => { - getCurrentUserPermissions(); - }, - } - ); - - - - - - const holders = data; - return { - holders, - isLoading, - getPermissions, - error, - setJournalPermissionMutation, - removeJournalPermissionMutation, - currentUserPermissions, - currentUserPermissionsIsLoading, - }; -}; - -export default useJournalPermissions; diff --git a/frontend/src/core/hooks/useJournalStats.js b/frontend/src/core/hooks/useJournalStats.js deleted file mode 100644 index 370e6eff..00000000 --- a/frontend/src/core/hooks/useJournalStats.js +++ /dev/null @@ -1,36 +0,0 @@ -import { useQuery } from "react-query"; -import { JournalService } from "../services"; -import { queryCacheProps } from "./hookCommon"; -import { useToast } from "."; - -const useJournalStats = (journalId) => { - const toast = useToast(); - - const getStats = async (query, key) => { - const response = await JournalService.getJournalStats(key, { journalId })( - query - ); - - return response.data; - }; - - const { data, isLoading, refetch } = useQuery( - ["journal-stats", { journalId }], - getStats, - { - ...queryCacheProps, - onError: (error) => { - toast(error, "error"); - }, - onSuccess: () => {}, - } - ); - - return { - data, - isLoading, - refetch, - }; -}; - -export default useJournalStats; diff --git a/frontend/src/core/hooks/useJournals.js b/frontend/src/core/hooks/useJournals.js deleted file mode 100644 index e12cfbce..00000000 --- a/frontend/src/core/hooks/useJournals.js +++ /dev/null @@ -1,58 +0,0 @@ -import { useQuery } from "react-query"; -import { JournalService } from "../services"; -import { queryCacheProps } from "./hookCommon"; -import { useToast, useUser } from "."; - -const useJournals = () => { - const toast = useToast(); - const { user } = useUser(); - - const getAllJournals = async () => { - const response = await JournalService.getAll(); - const newAllJournals = [...response.data.journals]; - newAllJournals.sort(function (a, b) { - var aName = a.name.toUpperCase(); - var bName = b.name.toUpperCase(); - return aName < bName ? -1 : aName < bName ? 1 : 0; - }); - return [...newAllJournals]; - }; - - const journalsCache = useQuery("journals-list", getAllJournals, { - ...queryCacheProps, - placeholderData: [], - enabled: !!user, - onError: (error) => { - toast(error, "error"); - }, - }); - - const getPublicJournals = async () => { - const response = await JournalService.getPublicJournals(); - - const newPublicJournals = [...response.data.journals]; - newPublicJournals.sort(function (a, b) { - var aName = a.name.toUpperCase(); - var bName = b.name.toUpperCase(); - return aName < bName ? -1 : aName < bName ? 1 : 0; - }); - - return [...newPublicJournals]; - }; - - const publicJournalsCache = useQuery(["journals-public"], getPublicJournals, { - placeholderData: [], - ...queryCacheProps, - - onError: (error) => { - toast(error, "error"); - }, - }); - - return { - journalsCache, - publicJournalsCache, - }; -}; - -export default useJournals; diff --git a/frontend/src/core/hooks/useJournalsScopes.js b/frontend/src/core/hooks/useJournalsScopes.js deleted file mode 100644 index d8171d0b..00000000 --- a/frontend/src/core/hooks/useJournalsScopes.js +++ /dev/null @@ -1,25 +0,0 @@ -import { useQuery } from "react-query"; -import { JournalService } from "../services"; -import { queryCacheProps } from "./hookCommon"; - -const useJournals = () => { - const getJournalsScopes = async () => { - var data; - - data = await JournalService.getJournalsScopes(); - const scopes = data.data.scopes; - - return [...scopes]; - }; - - const scopesCache = useQuery("journals-scopes", getJournalsScopes, { - ...queryCacheProps, - enabled: true, - }); - - return { - scopesCache, - }; -}; - -export default useJournals; diff --git a/frontend/src/core/hooks/usePreferences.js b/frontend/src/core/hooks/usePreferences.js deleted file mode 100644 index 611f454b..00000000 --- a/frontend/src/core/hooks/usePreferences.js +++ /dev/null @@ -1,40 +0,0 @@ -import { useQuery, useQueryCache } from "react-query"; -import { PreferencesService } from "../services"; -import { queryCacheProps } from "./hookCommon"; - -const getPreferences = async () => { - let preferences = {}; - try { - const defaultJournalResponse = await PreferencesService.getDefaultJournal(); - preferences.defaultJournal = defaultJournalResponse.data?.id; - } catch { - preferences.defaultJournal = null; - } - return preferences; -}; - -const usePreferences = () => { - const preferencesKey = "preferences-default-journal"; - const cache = useQueryCache(); - const { data, refetch } = useQuery(preferencesKey, getPreferences, { - ...queryCacheProps, - staleTime: 300000, - }); - - const invalidateAfter = (modifierFn) => { - return async function (...args) { - await modifierFn(...args); - cache.invalidateQueries(preferencesKey); - }; - }; - - const setPreference = { - defaultJournal: invalidateAfter(PreferencesService.setDefaultJournal), - }; - const unsetPreference = { - defaultJournal: invalidateAfter(PreferencesService.unsetDefaultJournal), - }; - return { data, refetch, setPreference, unsetPreference }; -}; - -export default usePreferences; diff --git a/frontend/src/core/hooks/useTokens.js b/frontend/src/core/hooks/useTokens.js deleted file mode 100644 index 70bbcfe9..00000000 --- a/frontend/src/core/hooks/useTokens.js +++ /dev/null @@ -1,33 +0,0 @@ -import { useMutation } from "react-query"; -import { AuthService } from "../services"; - -const useTokens = () => { - const { - mutate: list, - isLoading, - error, - data, - } = useMutation(AuthService.getTokenList); - const { mutate: revoke } = useMutation(AuthService.revokeToken, { - onSuccess: () => { - list(); - }, - }); - - const { mutate: update } = useMutation(AuthService.updateToken, { - onSuccess: () => { - list(); - }, - }); - - return { - list, - update, - revoke, - isLoading, - data, - error, - }; -}; - -export default useTokens; diff --git a/frontend/src/core/hooks/useUpdateEntry.js b/frontend/src/core/hooks/useUpdateEntry.js deleted file mode 100644 index c43e88ab..00000000 --- a/frontend/src/core/hooks/useUpdateEntry.js +++ /dev/null @@ -1,73 +0,0 @@ -import { useMutation, useQueryClient } from "react-query"; -import { EntryService } from "../services"; -import { useToast } from "."; - -const useUpdateEntry = (journalId, entryId) => { - const entriesCache = useQueryClient(); - const entryCache = useQueryClient(); - const toast = useToast(); - - const handleError = (error, variables, context) => { - entriesCache.setQueryData( - ["journal-entries", { journalId }], - context.prevEntriesPages - ); - entryCache.setQueryData( - ["journal-entry", { journalId, entryId }], - context.prevEntry - ); - - toast(error, "error"); - }; - - const { mutate: updateEntry } = useMutation( - EntryService.update(journalId, entryId), - { - onMutate: (newData) => { - const prevEntriesPages = entriesCache.getQueryData([ - "journal-entries", - { journalId }, - ]); - - const newEntriesPages = JSON.parse(JSON.stringify(prevEntriesPages)); - const prevEntry = entryCache.getQueryData([ - "journal-entry", - { journalId, entryId }, - ]); - - const newEntry = { ...prevEntry, ...newData }; - - newEntriesPages.map((page) => { - page.data = page.data.map((entry) => { - if (entry.id === entryId) { - return { - ...entry, - ...newData, - // for tags useUpdateTag instead - }; - } - return entry; - }); - return page; - }); - - entriesCache.setQueryData( - ["journal-entries", { journalId }], - newEntriesPages - ); - entryCache.setQueryData( - ["journal-entry", { journalId, entryId }], - newEntry - ); - - return { prevEntriesPages, prevEntry }; - }, - onError: (error, variables, context) => - handleError(error, variables, context), - } - ); - - return updateEntry; -}; - -export default useUpdateEntry; diff --git a/frontend/src/core/hooks/useUpdateTag.js b/frontend/src/core/hooks/useUpdateTag.js deleted file mode 100644 index 3d399154..00000000 --- a/frontend/src/core/hooks/useUpdateTag.js +++ /dev/null @@ -1,100 +0,0 @@ -import { useCallback } from "react"; -import { useMutation, useQueryCache } from "react-query"; -import { TagService } from "../services"; -import { useToast } from "."; - -const useUpdateTag = (journalId, entryId) => { - const cache = useQueryCache(); - const entryCache = useQueryCache(); - const toast = useToast(); - - const updateCache = (tagUpdate) => { - const prevEntriesPages = cache.getQueryData([ - "journal-entries", - { journalId }, - ]); - const newEntriesPages = JSON.parse(JSON.stringify(prevEntriesPages)); - const prevEntry = entryCache.getQueryData([ - "journal-entry", - { journalId, entryId }, - ]); - const newEntry = JSON.parse(JSON.stringify(prevEntry)); - - newEntriesPages.map((page) => { - page.data = page.data.map((entry) => { - if (entry.id === entryId) { - var newTags; - if (tagUpdate.action === "add") { - newTags = [...entry.tags, tagUpdate.tag]; - entry.tags = newTags; - } else { - newTags = entry.tags.filter((item) => item !== tagUpdate.tag); - entry.tags = newTags; - } - newEntry.tags = newTags; - entryCache.setQueryData( - ["journal-entry", { journalId, entryId }], - newEntry - ); - } - - return entry; - }); - return page; - }); - cache.setQueryData(["journal-entries", { journalId }], newEntriesPages); - - return { prevEntriesPages, prevEntry }; - }; - - const handleError = (error, variables, context) => { - if (context) { - cache.setQueryData( - ["journal-entries", { journalId }], - context.prevEntriesPages - ); - entryCache.setQueryData( - ["journal-entry", { journalId, entryId }], - context.prevEntry - ); - } - - toast(error, "error"); - }; - const [addTag] = useMutation(TagService.createTag(journalId, entryId), { - onMutate: (data) => { - let retval = updateCache({ tag: data.tags[0], action: "add" }); - return retval; - }, - onError: (error, variables, context) => - handleError(error, variables, context), - }); - - const [deleteTag] = useMutation(TagService.deleteTag(journalId, entryId), { - onMutate: (data) => { - let retval = updateCache(data); - return retval; - }, - onError: (error, variables, context) => - handleError(error, variables, context), - }); - - const updateTag = useCallback( - (tagUpdate) => { - switch (tagUpdate.action) { - case "add": - addTag({ tags: [tagUpdate.tag] }); - break; - case "delete": - deleteTag({ tag: tagUpdate.tag }); - break; - default: - return ""; - } - }, - [addTag, deleteTag] - ); - - return updateTag; -}; -export default useUpdateTag; From c4ce547a1aeb7d3f2abc24a787844b1f72557433 Mon Sep 17 00:00:00 2001 From: Neeraj Kashyap Date: Sat, 31 Jul 2021 10:49:41 -0700 Subject: [PATCH 3/6] Prettier fixes --- frontend/src/components/AccountIconButton.js | 2 +- frontend/src/components/AccountNavbar.js | 1 - frontend/src/components/ChangePassword.js | 1 - frontend/src/components/ContentBlock.js | 1 - frontend/src/components/CopyButton.js | 1 - frontend/src/components/CopyEntryButton.js | 1 - frontend/src/components/Footer.js | 5 +- frontend/src/components/ForgotPassword.js | 4 +- frontend/src/components/FourOFour.js | 1 - frontend/src/components/FourOThree.js | 1 - frontend/src/components/HeadLinks.js | 1 - frontend/src/components/HeadSEO.js | 1 - frontend/src/components/IconButton.js | 1 - frontend/src/components/LoadingDots.js | 1 - frontend/src/components/Modal/index.js | 2 +- frontend/src/components/Navbar.js | 1 - frontend/src/components/PasswordInput.js | 2 +- frontend/src/components/RadioCard.js | 2 +- frontend/src/components/SignIn.js | 4 +- frontend/src/core/hooks/useAnalytics.js | 5 +- frontend/src/core/hooks/useLogin.js | 6 +- frontend/src/core/hooks/useLogout.js | 2 +- frontend/src/core/hooks/useSignUp.js | 7 +- frontend/src/core/hooks/useStream.js | 3 +- frontend/src/core/hooks/useToast.js | 1 - frontend/src/core/services/humbug.service.js | 25 ++-- frontend/src/core/services/journal.service.js | 112 +++++++++--------- frontend/src/layouts/AccountLayout.js | 1 - frontend/src/layouts/AppLayout.js | 1 - frontend/src/layouts/LandingLayout.js | 1 - frontend/src/layouts/RootLayout.js | 1 - frontend/src/layouts/index.js | 4 +- 32 files changed, 91 insertions(+), 111 deletions(-) diff --git a/frontend/src/components/AccountIconButton.js b/frontend/src/components/AccountIconButton.js index 4daf4b6f..08101a3f 100644 --- a/frontend/src/components/AccountIconButton.js +++ b/frontend/src/components/AccountIconButton.js @@ -24,7 +24,7 @@ const AccountIconButton = (props) => { {...props} as={IconButton} aria-label="Account menu" - icon={} + icon={} // variant="outline" color="gray.100" /> diff --git a/frontend/src/components/AccountNavbar.js b/frontend/src/components/AccountNavbar.js index 534cd901..54deb3b7 100644 --- a/frontend/src/components/AccountNavbar.js +++ b/frontend/src/components/AccountNavbar.js @@ -1,4 +1,3 @@ - import { jsx } from "@emotion/react"; import { useUser, useRouter } from "../core/hooks"; import { useEffect, Fragment, useState } from "react"; diff --git a/frontend/src/components/ChangePassword.js b/frontend/src/components/ChangePassword.js index c90e31a6..6d6259dc 100644 --- a/frontend/src/components/ChangePassword.js +++ b/frontend/src/components/ChangePassword.js @@ -1,4 +1,3 @@ - import { jsx } from "@emotion/react"; import { useState, useEffect } from "react"; import { useForm } from "react-hook-form"; diff --git a/frontend/src/components/ContentBlock.js b/frontend/src/components/ContentBlock.js index 3fd0f38e..4899388a 100644 --- a/frontend/src/components/ContentBlock.js +++ b/frontend/src/components/ContentBlock.js @@ -1,4 +1,3 @@ - import { jsx } from "@emotion/react"; import { GridItem } from "@chakra-ui/react"; import { Heading, Text, Image } from "@chakra-ui/react"; diff --git a/frontend/src/components/CopyButton.js b/frontend/src/components/CopyButton.js index bf18d57c..be21e9a3 100644 --- a/frontend/src/components/CopyButton.js +++ b/frontend/src/components/CopyButton.js @@ -1,4 +1,3 @@ - import { Fragment } from "react"; import { jsx } from "@emotion/react"; import { diff --git a/frontend/src/components/CopyEntryButton.js b/frontend/src/components/CopyEntryButton.js index 275db74f..9fc3972b 100644 --- a/frontend/src/components/CopyEntryButton.js +++ b/frontend/src/components/CopyEntryButton.js @@ -1,4 +1,3 @@ - import { jsx } from "@emotion/react"; import { Button, diff --git a/frontend/src/components/Footer.js b/frontend/src/components/Footer.js index d700dcdf..a0e8252b 100644 --- a/frontend/src/components/Footer.js +++ b/frontend/src/components/Footer.js @@ -2,7 +2,7 @@ /** @jsx jsx */ import { jsx } from "@emotion/react"; import { Flex, Heading, Text, Link } from "@chakra-ui/react"; -import CustomIcon from "../components/CustomIcon" +import CustomIcon from "../components/CustomIcon"; import RouterLink from "next/link"; const ICONS = [ @@ -14,8 +14,7 @@ const ICONS = [ { social: "twit", link: "https://twitter.com/Bugout_dev" }, { social: "slack", - link: - "https://join.slack.com/t/bugout-dev/shared_invite/zt-fhepyt87-5XcJLy0iu702SO_hMFKNhQ", + link: "https://join.slack.com/t/bugout-dev/shared_invite/zt-fhepyt87-5XcJLy0iu702SO_hMFKNhQ", }, ]; diff --git a/frontend/src/components/ForgotPassword.js b/frontend/src/components/ForgotPassword.js index 616d6183..45410b16 100644 --- a/frontend/src/components/ForgotPassword.js +++ b/frontend/src/components/ForgotPassword.js @@ -13,8 +13,8 @@ import { Input, InputRightElement, } from "@chakra-ui/react"; -import CustomIcon from "./CustomIcon" -import Modal from "./Modal" +import CustomIcon from "./CustomIcon"; +import Modal from "./Modal"; const ForgotPassword = ({ toggleModal }) => { const toast = useToast(); diff --git a/frontend/src/components/FourOFour.js b/frontend/src/components/FourOFour.js index e2716fc1..9ef16487 100644 --- a/frontend/src/components/FourOFour.js +++ b/frontend/src/components/FourOFour.js @@ -1,4 +1,3 @@ - import { jsx } from "@emotion/react"; import { Heading, Box, Text, Center, VStack } from "@chakra-ui/react"; const Page404 = () => ( diff --git a/frontend/src/components/FourOThree.js b/frontend/src/components/FourOThree.js index 541ac5d2..c62df467 100644 --- a/frontend/src/components/FourOThree.js +++ b/frontend/src/components/FourOThree.js @@ -1,4 +1,3 @@ - import { jsx } from "@emotion/react"; import { Heading, Box, Text, VStack, Center } from "@chakra-ui/react"; const Page403 = ({ location }) => ( diff --git a/frontend/src/components/HeadLinks.js b/frontend/src/components/HeadLinks.js index bf28c4c1..b8b63fd1 100644 --- a/frontend/src/components/HeadLinks.js +++ b/frontend/src/components/HeadLinks.js @@ -1,4 +1,3 @@ - import { jsx } from "@emotion/react"; import Head from "next/head"; import propTypes from "prop-types"; diff --git a/frontend/src/components/HeadSEO.js b/frontend/src/components/HeadSEO.js index c068b472..8a32563c 100644 --- a/frontend/src/components/HeadSEO.js +++ b/frontend/src/components/HeadSEO.js @@ -1,4 +1,3 @@ - import { jsx } from "@emotion/react"; import Head from "next/head"; import propTypes from "prop-types"; diff --git a/frontend/src/components/IconButton.js b/frontend/src/components/IconButton.js index 3e45e2a0..f20b0722 100644 --- a/frontend/src/components/IconButton.js +++ b/frontend/src/components/IconButton.js @@ -1,4 +1,3 @@ - import { jsx } from "@emotion/react"; import { IconButton as IconButtonChakra } from "@chakra-ui/react"; import { CheckIcon } from "@chakra-ui/icons"; diff --git a/frontend/src/components/LoadingDots.js b/frontend/src/components/LoadingDots.js index cb5d291a..89db8451 100644 --- a/frontend/src/components/LoadingDots.js +++ b/frontend/src/components/LoadingDots.js @@ -1,4 +1,3 @@ - import { jsx } from "@emotion/react"; import { useState, useEffect } from "react"; import { Text } from "@chakra-ui/react"; diff --git a/frontend/src/components/Modal/index.js b/frontend/src/components/Modal/index.js index feb8a155..22969e3e 100644 --- a/frontend/src/components/Modal/index.js +++ b/frontend/src/components/Modal/index.js @@ -2,7 +2,7 @@ /** @jsx jsx */ import { jsx } from "@emotion/react"; import { Flex } from "@chakra-ui/react"; -import CustomIcon from "../CustomIcon" +import CustomIcon from "../CustomIcon"; import styles from "./styles"; const Modal = ({ children, onClose }) => ( diff --git a/frontend/src/components/Navbar.js b/frontend/src/components/Navbar.js index a1350120..8868e1f6 100644 --- a/frontend/src/components/Navbar.js +++ b/frontend/src/components/Navbar.js @@ -13,7 +13,6 @@ const AppNavbar = React.lazy(() => import("./AppNavbar")); const Navbar = () => { const { modal, toggleModal, isAppView, isLoggedIn } = useContext(UIContext); - return ( { const [showPassword, togglePassword] = useState(false); diff --git a/frontend/src/components/RadioCard.js b/frontend/src/components/RadioCard.js index 669779f6..fddee4e8 100644 --- a/frontend/src/components/RadioCard.js +++ b/frontend/src/components/RadioCard.js @@ -8,7 +8,7 @@ const RadioCard = (props) => { const checkbox = getCheckboxProps(); return ( - console.log('hello2')}> + console.log("hello2")}> { colorScheme="primary" placeholder="Your Bugout username" name="username" - {...register('username', { required: true })} + {...register("username", { required: true })} ref={register({ required: "Username is required!" })} /> diff --git a/frontend/src/core/hooks/useAnalytics.js b/frontend/src/core/hooks/useAnalytics.js index ce7fd37e..fee7e9eb 100644 --- a/frontend/src/core/hooks/useAnalytics.js +++ b/frontend/src/core/hooks/useAnalytics.js @@ -2,9 +2,8 @@ import AnalyticsContext from "../providers/AnalyticsProvider/context"; import { useContext } from "react"; import { useState, useEffect, useCallback } from "react"; const useAnalytics = () => { - const { mixpanel, isLoaded, MIXPANEL_EVENTS, MIXPANEL_PROPS } = useContext( - AnalyticsContext - ); + const { mixpanel, isLoaded, MIXPANEL_EVENTS, MIXPANEL_PROPS } = + useContext(AnalyticsContext); const [trackProps, setTrackProps] = useState({ event: null, props: null, diff --git a/frontend/src/core/hooks/useLogin.js b/frontend/src/core/hooks/useLogin.js index f9ca27a6..29bfafc3 100644 --- a/frontend/src/core/hooks/useLogin.js +++ b/frontend/src/core/hooks/useLogin.js @@ -36,10 +36,12 @@ const useLogin = (loginType) => { getUser(); if (analytics.isLoaded) { analytics.mixpanel.people.set_once({ - [`${analytics.MIXPANEL_EVENTS.FIRST_LOGIN_DATE}`]: new Date().toISOString(), + [`${analytics.MIXPANEL_EVENTS.FIRST_LOGIN_DATE}`]: + new Date().toISOString(), }); analytics.mixpanel.people.set({ - [`${analytics.MIXPANEL_EVENTS.LAST_LOGIN_DATE}`]: new Date().toISOString(), + [`${analytics.MIXPANEL_EVENTS.LAST_LOGIN_DATE}`]: + new Date().toISOString(), }); analytics.mixpanel.track( `${analytics.MIXPANEL_EVENTS.USER_LOGS_IN}`, diff --git a/frontend/src/core/hooks/useLogout.js b/frontend/src/core/hooks/useLogout.js index e883b10a..ce71984f 100644 --- a/frontend/src/core/hooks/useLogout.js +++ b/frontend/src/core/hooks/useLogout.js @@ -8,7 +8,7 @@ const useLogout = () => { const { setLoggingOut } = useContext(UIContext); const router = useRouter(); const analytics = useAnalytics(); - const {mutate: revoke, data } = useMutation(AuthService.revoke, { + const { mutate: revoke, data } = useMutation(AuthService.revoke, { onSuccess: () => { if (analytics.isLoaded) { analytics.mixpanel.track( diff --git a/frontend/src/core/hooks/useSignUp.js b/frontend/src/core/hooks/useSignUp.js index 56e38dba..1419ba16 100644 --- a/frontend/src/core/hooks/useSignUp.js +++ b/frontend/src/core/hooks/useSignUp.js @@ -15,10 +15,13 @@ const useSignUp = (source) => { isLoading, error, data, - isSuccess + isSuccess, } = useMutation(AuthService.register(), { onSuccess: (response) => { - localStorage.setItem("MOONSTREAM_ACCESS_TOKEN", response.data.access_token); + localStorage.setItem( + "MOONSTREAM_ACCESS_TOKEN", + response.data.access_token + ); const invite_code = window.sessionStorage.getItem("invite_code"); if (invite_code) { inviteAccept(invite_code); diff --git a/frontend/src/core/hooks/useStream.js b/frontend/src/core/hooks/useStream.js index 6920f9b5..26e067c9 100644 --- a/frontend/src/core/hooks/useStream.js +++ b/frontend/src/core/hooks/useStream.js @@ -58,8 +58,7 @@ const useJournalEntries = ({ getNextPageParam: (lastGroup) => { return lastGroup.next_offset === null ? false : lastGroup.next_offset; }, - onSuccess: (data) => { - }, + onSuccess: (data) => {}, enabled: !!enabled, }); diff --git a/frontend/src/core/hooks/useToast.js b/frontend/src/core/hooks/useToast.js index 7d30315e..0bfc2765 100644 --- a/frontend/src/core/hooks/useToast.js +++ b/frontend/src/core/hooks/useToast.js @@ -1,4 +1,3 @@ - import { jsx } from "@emotion/react"; import { useToast as useChakraToast, Box } from "@chakra-ui/react"; import { useCallback } from "react"; diff --git a/frontend/src/core/services/humbug.service.js b/frontend/src/core/services/humbug.service.js index 77dc7b5c..766ca34d 100644 --- a/frontend/src/core/services/humbug.service.js +++ b/frontend/src/core/services/humbug.service.js @@ -50,20 +50,19 @@ export const getTokens = (humbugId) => { }); }; -export const createRestrictedToken = (humbugId) => ({ - appName, - appVersion, -}) => { - const data = new FormData(); - data.append("app_name", appName); - data.append("app_version", appVersion); +export const createRestrictedToken = + (humbugId) => + ({ appName, appVersion }) => { + const data = new FormData(); + data.append("app_name", appName); + data.append("app_version", appVersion); - return http({ - method: "POST", - url: `${API}/humbug/${humbugId}/tokens`, - data, - }); -}; + return http({ + method: "POST", + url: `${API}/humbug/${humbugId}/tokens`, + data, + }); + }; export const deleteRestrictedToken = (humbugId) => (tokenId) => { const data = new FormData(); diff --git a/frontend/src/core/services/journal.service.js b/frontend/src/core/services/journal.service.js index 9115d5eb..eefcc074 100644 --- a/frontend/src/core/services/journal.service.js +++ b/frontend/src/core/services/journal.service.js @@ -62,35 +62,31 @@ export const getJournalsScopes = () => { }); }; -export const setJournalPermission = (journalId) => ({ - holder_type, - holder_id, - permission_list, -}) => { - const data = new FormData(); - data.append("holder_type", holder_type); - data.append("holder_id", holder_id); - data.append("permission_list", permission_list); +export const setJournalPermission = + (journalId) => + ({ holder_type, holder_id, permission_list }) => { + const data = new FormData(); + data.append("holder_type", holder_type); + data.append("holder_id", holder_id); + data.append("permission_list", permission_list); - return http({ - method: "POST", - url: `${API}/journals/${journalId}/scopes`, - data: { holder_type, holder_id, permission_list }, - }); -}; + return http({ + method: "POST", + url: `${API}/journals/${journalId}/scopes`, + data: { holder_type, holder_id, permission_list }, + }); + }; -export const deleteJournalPermission = (journalId) => ({ - holder_type, - holder_id, - permission_list, -}) => { - return http({ - method: "DELETE", - url: `${API}/journals/${journalId}/scopes`, - data: { holder_type, holder_id, permission_list }, - // permission_list: ["read"] - }); -}; +export const deleteJournalPermission = + (journalId) => + ({ holder_type, holder_id, permission_list }) => { + return http({ + method: "DELETE", + url: `${API}/journals/${journalId}/scopes`, + data: { holder_type, holder_id, permission_list }, + // permission_list: ["read"] + }); + }; export const getPublicJournals = () => http({ @@ -98,32 +94,30 @@ export const getPublicJournals = () => url: `${API}/public/`, }); -export const searchEntries = ({ journalId }) => ({ - searchTerm, - limit, - offset, - isContent, - journalType, -}) => { - const journalScope = journalType === "personal" ? "journals" : "public"; - return http({ - method: "GET", - url: `${API}/${journalScope}/${journalId}/search`, - params: { - // filters: searchTags, - q: searchTerm, - limit: encodeURIComponent(limit), - offset: encodeURIComponent(offset), - content: encodeURIComponent(isContent), - }, - }); -}; +export const searchEntries = + ({ journalId }) => + ({ searchTerm, limit, offset, isContent, journalType }) => { + const journalScope = journalType === "personal" ? "journals" : "public"; + return http({ + method: "GET", + url: `${API}/${journalScope}/${journalId}/search`, + params: { + // filters: searchTags, + q: searchTerm, + limit: encodeURIComponent(limit), + offset: encodeURIComponent(offset), + content: encodeURIComponent(isContent), + }, + }); + }; -export const publicSearchEntries = ({ journalId }) => (query) => - http({ - method: "GET", - url: `${API}/public/${journalId}/search?q=${query}`, - }); +export const publicSearchEntries = + ({ journalId }) => + (query) => + http({ + method: "GET", + url: `${API}/public/${journalId}/search?q=${query}`, + }); export const getPublicJournal = (key, { journalId }) => http({ @@ -131,9 +125,11 @@ export const getPublicJournal = (key, { journalId }) => url: `${API}/public/${journalId}`, }); -export const getJournalStats = (key, { journalId }) => () => - http({ - method: "GET", - url: `${API}/journals/${journalId}/stats`, - params: { stats_version: 5 }, - }); +export const getJournalStats = + (key, { journalId }) => + () => + http({ + method: "GET", + url: `${API}/journals/${journalId}/stats`, + params: { stats_version: 5 }, + }); diff --git a/frontend/src/layouts/AccountLayout.js b/frontend/src/layouts/AccountLayout.js index 2c80794a..eaf84c15 100644 --- a/frontend/src/layouts/AccountLayout.js +++ b/frontend/src/layouts/AccountLayout.js @@ -1,4 +1,3 @@ - import { jsx } from "@emotion/react"; import { Box } from "@chakra-ui/react"; import { getLayout as getSiteLayout } from "./AppLayout"; diff --git a/frontend/src/layouts/AppLayout.js b/frontend/src/layouts/AppLayout.js index fdfbe133..c4bf88ba 100644 --- a/frontend/src/layouts/AppLayout.js +++ b/frontend/src/layouts/AppLayout.js @@ -1,4 +1,3 @@ - import { Flex } from "@chakra-ui/react"; import { getLayout as getSiteLayout } from "./RootLayout"; import React, { useContext } from "react"; diff --git a/frontend/src/layouts/LandingLayout.js b/frontend/src/layouts/LandingLayout.js index fe0aceee..693fe557 100644 --- a/frontend/src/layouts/LandingLayout.js +++ b/frontend/src/layouts/LandingLayout.js @@ -1,4 +1,3 @@ - import { jsx } from "@emotion/react"; import { Scrollable, Footer } from "../components"; import { getLayout as getSiteLayout } from "./index"; diff --git a/frontend/src/layouts/RootLayout.js b/frontend/src/layouts/RootLayout.js index c13d3757..cbdcae27 100644 --- a/frontend/src/layouts/RootLayout.js +++ b/frontend/src/layouts/RootLayout.js @@ -1,4 +1,3 @@ - import { jsx } from "@emotion/react"; import { Flex, Spinner } from "@chakra-ui/react"; import React, { Suspense, useContext, useState, useEffect } from "react"; diff --git a/frontend/src/layouts/index.js b/frontend/src/layouts/index.js index 26bd3c8e..2f98557f 100644 --- a/frontend/src/layouts/index.js +++ b/frontend/src/layouts/index.js @@ -1,5 +1,5 @@ -import Footer from "../components/Footer" -import Scrollable from "../components/Scrollable" +import Footer from "../components/Footer"; +import Scrollable from "../components/Scrollable"; import RootLayout from "./RootLayout"; const LayoutWrapper = ({ children }) => { From 5b1de53e0458baab0ec0632e6df0b9b5610255b0 Mon Sep 17 00:00:00 2001 From: Neeraj Kashyap Date: Sat, 31 Jul 2021 10:58:45 -0700 Subject: [PATCH 4/6] Continuing to fix "yarn build" A long way to go. --- frontend/src/components/EntriesNavigation.js | 42 ++++++++++++------- .../{FourOFour.js => FourOFour.jsx} | 2 +- .../{FourOThree.js => FourOThree.jsx} | 2 +- .../{HeadLinks.js => HeadLinks.jsx} | 2 +- .../components/{HeadSEO.js => HeadSEO.jsx} | 2 +- .../{IconButton.js => IconButton.jsx} | 2 +- .../{LoadingDots.js => LoadingDots.jsx} | 3 +- frontend/src/components/NewSubscription.js | 2 +- .../{SearchBar.js => SearchBar.jsx} | 2 +- 9 files changed, 35 insertions(+), 24 deletions(-) rename frontend/src/components/{FourOFour.js => FourOFour.jsx} (88%) rename frontend/src/components/{FourOThree.js => FourOThree.jsx} (90%) rename frontend/src/components/{HeadLinks.js => HeadLinks.jsx} (95%) rename frontend/src/components/{HeadSEO.js => HeadSEO.jsx} (98%) rename frontend/src/components/{IconButton.js => IconButton.jsx} (92%) rename frontend/src/components/{LoadingDots.js => LoadingDots.jsx} (88%) rename frontend/src/components/{SearchBar.js => SearchBar.jsx} (99%) diff --git a/frontend/src/components/EntriesNavigation.js b/frontend/src/components/EntriesNavigation.js index c3f627e9..09c20d95 100644 --- a/frontend/src/components/EntriesNavigation.js +++ b/frontend/src/components/EntriesNavigation.js @@ -1,4 +1,10 @@ -import React, { useRef, useEffect, useContext, useState } from "react"; +import React, { + useRef, + useEffect, + useContext, + useState, + useCallback, +} from "react"; import { Flex, Spinner, @@ -67,14 +73,17 @@ const EntriesNavigation = () => { ]); const [filterState, setFilterState] = useState([]); - const setNewFilterState = (props) => { - console.log( - "setNewFilterState", - props, - subscriptionsCache.data.subscriptions[0].id - ); - _setNewFilterState(props); - }; + const setNewFilterState = useCallback( + (props) => { + console.log( + "setNewFilterState", + props, + subscriptionsCache.data.subscriptions[0].id + ); + _setNewFilterState(props); + }, + [subscriptionsCache.data.subscriptions] + ); const loadMoreButtonRef = useRef(null); const { fetchMore, isFetchingMore, canFetchMore, EntriesPages, isLoading } = @@ -97,11 +106,14 @@ const EntriesNavigation = () => { } }; - const setFilterProps = (filterIdx, props) => { - const newFilterProps = [...newFilterState]; - newFilterProps[filterIdx] = { ...newFilterProps[filterIdx], ...props }; - setNewFilterState(newFilterProps); - }; + const setFilterProps = useCallback( + (filterIdx, props) => { + const newFilterProps = [...newFilterState]; + newFilterProps[filterIdx] = { ...newFilterProps[filterIdx], ...props }; + setNewFilterState(newFilterProps); + }, + [newFilterState, setNewFilterState] + ); useEffect(() => { if ( @@ -112,7 +124,7 @@ const EntriesNavigation = () => { value: subscriptionsCache.data.subscriptions[0].address, }); } - }, [subscriptionsCache.isLoading]); + }, [subscriptionsCache, newFilterState, setFilterProps]); const entriesPagesData = EntriesPages ? EntriesPages.pages.map((page) => { diff --git a/frontend/src/components/FourOFour.js b/frontend/src/components/FourOFour.jsx similarity index 88% rename from frontend/src/components/FourOFour.js rename to frontend/src/components/FourOFour.jsx index 9ef16487..614ab1fe 100644 --- a/frontend/src/components/FourOFour.js +++ b/frontend/src/components/FourOFour.jsx @@ -1,4 +1,4 @@ -import { jsx } from "@emotion/react"; +import { React } from "react"; import { Heading, Box, Text, Center, VStack } from "@chakra-ui/react"; const Page404 = () => ( diff --git a/frontend/src/components/FourOThree.js b/frontend/src/components/FourOThree.jsx similarity index 90% rename from frontend/src/components/FourOThree.js rename to frontend/src/components/FourOThree.jsx index c62df467..fe6747fa 100644 --- a/frontend/src/components/FourOThree.js +++ b/frontend/src/components/FourOThree.jsx @@ -1,4 +1,4 @@ -import { jsx } from "@emotion/react"; +import { React } from "react"; import { Heading, Box, Text, VStack, Center } from "@chakra-ui/react"; const Page403 = ({ location }) => ( diff --git a/frontend/src/components/HeadLinks.js b/frontend/src/components/HeadLinks.jsx similarity index 95% rename from frontend/src/components/HeadLinks.js rename to frontend/src/components/HeadLinks.jsx index b8b63fd1..7b5048fd 100644 --- a/frontend/src/components/HeadLinks.js +++ b/frontend/src/components/HeadLinks.jsx @@ -1,4 +1,4 @@ -import { jsx } from "@emotion/react"; +import { React } from "react"; import Head from "next/head"; import propTypes from "prop-types"; diff --git a/frontend/src/components/HeadSEO.js b/frontend/src/components/HeadSEO.jsx similarity index 98% rename from frontend/src/components/HeadSEO.js rename to frontend/src/components/HeadSEO.jsx index 8a32563c..20215c20 100644 --- a/frontend/src/components/HeadSEO.js +++ b/frontend/src/components/HeadSEO.jsx @@ -1,4 +1,4 @@ -import { jsx } from "@emotion/react"; +import { React } from "react"; import Head from "next/head"; import propTypes from "prop-types"; diff --git a/frontend/src/components/IconButton.js b/frontend/src/components/IconButton.jsx similarity index 92% rename from frontend/src/components/IconButton.js rename to frontend/src/components/IconButton.jsx index f20b0722..3844a684 100644 --- a/frontend/src/components/IconButton.js +++ b/frontend/src/components/IconButton.jsx @@ -1,4 +1,4 @@ -import { jsx } from "@emotion/react"; +import { React } from "react"; import { IconButton as IconButtonChakra } from "@chakra-ui/react"; import { CheckIcon } from "@chakra-ui/icons"; diff --git a/frontend/src/components/LoadingDots.js b/frontend/src/components/LoadingDots.jsx similarity index 88% rename from frontend/src/components/LoadingDots.js rename to frontend/src/components/LoadingDots.jsx index 89db8451..b1156ec7 100644 --- a/frontend/src/components/LoadingDots.js +++ b/frontend/src/components/LoadingDots.jsx @@ -1,5 +1,4 @@ -import { jsx } from "@emotion/react"; -import { useState, useEffect } from "react"; +import { React, useState, useEffect } from "react"; import { Text } from "@chakra-ui/react"; const LoadingDots = (props) => { diff --git a/frontend/src/components/NewSubscription.js b/frontend/src/components/NewSubscription.js index 2f068188..9fc08aac 100644 --- a/frontend/src/components/NewSubscription.js +++ b/frontend/src/components/NewSubscription.js @@ -22,7 +22,7 @@ const NewSubscription = ({ isFreeOption, onClose }) => { const { typesCache, createSubscription } = useSubscriptions(); const { handleSubmit, errors, register } = useForm(); const [radioState, setRadioState] = useState("ethereum_blockchain"); - let { getRootProps, getRadioProps, ref } = useRadioGroup({ + let { getRootProps, getRadioProps } = useRadioGroup({ name: "type", defaultValue: radioState, onChange: setRadioState, diff --git a/frontend/src/components/SearchBar.js b/frontend/src/components/SearchBar.jsx similarity index 99% rename from frontend/src/components/SearchBar.js rename to frontend/src/components/SearchBar.jsx index 6549d563..6f48829a 100644 --- a/frontend/src/components/SearchBar.js +++ b/frontend/src/components/SearchBar.jsx @@ -1,5 +1,5 @@ -import { jsx } from "@emotion/react"; import { + React, useState, useContext, useRef, From 12e3d576ebea5474590c7e7f4c92d4be77693ab5 Mon Sep 17 00:00:00 2001 From: Neeraj Kashyap Date: Sat, 31 Jul 2021 11:19:44 -0700 Subject: [PATCH 5/6] "yarn build" finally working --- frontend/pages/403.js | 1 + frontend/pages/404.js | 3 +- frontend/pages/_document.js | 4 +- frontend/pages/api/hello.ts | 13 --- frontend/pages/index.jsx | 2 +- frontend/pages/pricing.js | 16 +--- frontend/pages/subscriptions.js | 13 ++- frontend/src/components/AccountNavbar.js | 3 +- frontend/src/components/AppNavbar.js | 2 - .../{ChangePassword.js => ChangePassword.jsx} | 3 +- frontend/src/components/ContentBlock.js | 66 ------------- .../{CopyButton.js => CopyButton.jsx} | 3 +- frontend/src/components/CopyEntryButton.js | 94 ------------------- frontend/src/components/Tags.js | 31 ++++++ frontend/src/core/hooks/index.js | 22 +---- frontend/src/core/hooks/useForgotPassword.js | 26 +++++ frontend/src/core/hooks/useInviteAccept.js | 24 +++++ frontend/src/core/hooks/useJournalEntry.js | 28 ++++++ 18 files changed, 130 insertions(+), 224 deletions(-) delete mode 100644 frontend/pages/api/hello.ts rename frontend/src/components/{ChangePassword.js => ChangePassword.jsx} (98%) delete mode 100644 frontend/src/components/ContentBlock.js rename frontend/src/components/{CopyButton.js => CopyButton.jsx} (95%) delete mode 100644 frontend/src/components/CopyEntryButton.js create mode 100644 frontend/src/components/Tags.js create mode 100644 frontend/src/core/hooks/useForgotPassword.js create mode 100644 frontend/src/core/hooks/useInviteAccept.js create mode 100644 frontend/src/core/hooks/useJournalEntry.js diff --git a/frontend/pages/403.js b/frontend/pages/403.js index 3aa6d031..72f75af1 100644 --- a/frontend/pages/403.js +++ b/frontend/pages/403.js @@ -1,3 +1,4 @@ +import { React } from "react"; import FourOThree from "../src/components/FourOThree"; const Page403 = () => { diff --git a/frontend/pages/404.js b/frontend/pages/404.js index bd902deb..0a3db06e 100644 --- a/frontend/pages/404.js +++ b/frontend/pages/404.js @@ -1,5 +1,4 @@ - -import { jsx } from "@emotion/react"; +import { React } from "react"; import FourOFour from "../src/components/FourOFour"; const Page404 = () => { diff --git a/frontend/pages/_document.js b/frontend/pages/_document.js index 004ffbef..ce1a8cc8 100644 --- a/frontend/pages/_document.js +++ b/frontend/pages/_document.js @@ -1,6 +1,4 @@ - -import { jsx } from "@emotion/react"; - +import { React } from "react"; import Document, { Html, Head, Main, NextScript } from "next/document"; export default class MyDocument extends Document { diff --git a/frontend/pages/api/hello.ts b/frontend/pages/api/hello.ts deleted file mode 100644 index f8bcc7e5..00000000 --- a/frontend/pages/api/hello.ts +++ /dev/null @@ -1,13 +0,0 @@ -// Next.js API route support: https://nextjs.org/docs/api-routes/introduction -import type { NextApiRequest, NextApiResponse } from 'next' - -type Data = { - name: string -} - -export default function handler( - req: NextApiRequest, - res: NextApiResponse -) { - res.status(200).json({ name: 'John Doe' }) -} diff --git a/frontend/pages/index.jsx b/frontend/pages/index.jsx index bc86b85e..82d77856 100644 --- a/frontend/pages/index.jsx +++ b/frontend/pages/index.jsx @@ -17,7 +17,7 @@ import { import { Grid, GridItem } from "@chakra-ui/react"; import { useUser, useAnalytics, useModals, useRouter } from "../src/core/hooks"; import { openPopupWidget, InlineWidget } from "react-calendly"; -import TrustedBadge from "../src/components/TrustedBadge" +import TrustedBadge from "../src/components/TrustedBadge"; import { getLayout } from "../src/layouts"; const TEXT_PROPS = { diff --git a/frontend/pages/pricing.js b/frontend/pages/pricing.js index 0c87c052..c8e59984 100644 --- a/frontend/pages/pricing.js +++ b/frontend/pages/pricing.js @@ -30,7 +30,7 @@ function PriceWrapper({ children }) { ); } -const Pricing = (props) => { +const Pricing = () => { return ( @@ -225,20 +225,6 @@ export async function getStaticProps() { "https://s3.amazonaws.com/static.simiotics.com/landing/aviator-2.svg", }; -// const assetPreload = Object.keys(assets).map((key) => { -// return { -// rel: "preload", -// href: assets[key], -// as: "image", -// }; -// }); -// const preconnects = [ -// { rel: "preconnect", href: "https://s3.amazonaws.com" }, -// { rel: "preconnect", href: "https://assets.calendly.com/" }, -// ]; - -// const preloads = assetPreload.concat(preconnects); - return { props: { metaTags }, }; diff --git a/frontend/pages/subscriptions.js b/frontend/pages/subscriptions.js index a2afea4f..867bf8ea 100644 --- a/frontend/pages/subscriptions.js +++ b/frontend/pages/subscriptions.js @@ -15,7 +15,6 @@ import { ModalOverlay, ModalContent, } from "@chakra-ui/react"; -import { headingStyle } from "./index"; import NewSubscription from "../src/components/NewSubscription"; import { AiOutlinePlusCircle } from "react-icons/ai"; @@ -26,6 +25,18 @@ const Subscriptions = () => { document.title = `My Subscriptions`; + // TODO(zomglings): This should be imported from some common location. For now, copied from + // pages/account/security.js. It was attempting to get imported from "./index", but is not defined + // there. + const headingStyle = { + as: "h2", + pt: 2, + mb: 4, + borderBottom: "solid", + borderColor: "primary.50", + borderBottomWidth: "2px", + }; + const newSubscriptionClicked = (isForFree) => { setIsAddingFreeSubscription(isForFree); onOpen(); diff --git a/frontend/src/components/AccountNavbar.js b/frontend/src/components/AccountNavbar.js index 54deb3b7..80e56a01 100644 --- a/frontend/src/components/AccountNavbar.js +++ b/frontend/src/components/AccountNavbar.js @@ -1,6 +1,5 @@ -import { jsx } from "@emotion/react"; import { useUser, useRouter } from "../core/hooks"; -import { useEffect, Fragment, useState } from "react"; +import { React, useEffect, Fragment, useState } from "react"; import { Heading, Center, Spinner, Link, Button } from "@chakra-ui/react"; import RouterLink from "next/link"; const ACCOUNT_SCREEN_WIDGETS = { diff --git a/frontend/src/components/AppNavbar.js b/frontend/src/components/AppNavbar.js index 19346734..e85796f8 100644 --- a/frontend/src/components/AppNavbar.js +++ b/frontend/src/components/AppNavbar.js @@ -21,9 +21,7 @@ import { } from "@chakra-ui/react"; import { HamburgerIcon, - PlusSquareIcon, QuestionOutlineIcon, - BellIcon, ArrowLeftIcon, ArrowRightIcon, } from "@chakra-ui/icons"; diff --git a/frontend/src/components/ChangePassword.js b/frontend/src/components/ChangePassword.jsx similarity index 98% rename from frontend/src/components/ChangePassword.js rename to frontend/src/components/ChangePassword.jsx index 6d6259dc..ef2b372e 100644 --- a/frontend/src/components/ChangePassword.js +++ b/frontend/src/components/ChangePassword.jsx @@ -1,5 +1,4 @@ -import { jsx } from "@emotion/react"; -import { useState, useEffect } from "react"; +import { React, useState, useEffect } from "react"; import { useForm } from "react-hook-form"; import { useChangePassword, useRouter } from "../core/hooks"; import { diff --git a/frontend/src/components/ContentBlock.js b/frontend/src/components/ContentBlock.js deleted file mode 100644 index 4899388a..00000000 --- a/frontend/src/components/ContentBlock.js +++ /dev/null @@ -1,66 +0,0 @@ -import { jsx } from "@emotion/react"; -import { GridItem } from "@chakra-ui/react"; -import { Heading, Text, Image } from "@chakra-ui/react"; -import { Fragment } from "react"; - -const Block = (content) => { - const TitleRef = content.PrevTitle - ? `#${content.PrevTitle}-${content.title}` - : `#${content.title}`; - - var HeaderStyle = content.PrevTitle - ? { as: "h2", fontSize: "3xl" } - : { as: "h1", fontSize: "4xl" }; - - return ( - - {content.title && ( - - - {content.title} - - - )} - {content.body.map((element, idx) => { - if ("text" in element) { - return ( - - {element.text.map((paragraph, idx) => { - return ( - - {paragraph} - - ); - })} - - ); - } - if ("image" in element) { - return ( - - {element.image.annotation} - - ); - } - if ("title" in element) { - element.PrevTitle = content.title; - return ; - } - return ""; - })} - - ); -}; -export default Block; diff --git a/frontend/src/components/CopyButton.js b/frontend/src/components/CopyButton.jsx similarity index 95% rename from frontend/src/components/CopyButton.js rename to frontend/src/components/CopyButton.jsx index be21e9a3..a8f69912 100644 --- a/frontend/src/components/CopyButton.js +++ b/frontend/src/components/CopyButton.jsx @@ -1,5 +1,4 @@ -import { Fragment } from "react"; -import { jsx } from "@emotion/react"; +import { Fragment, React } from "react"; import { useClipboard, IconButton, diff --git a/frontend/src/components/CopyEntryButton.js b/frontend/src/components/CopyEntryButton.js deleted file mode 100644 index 9fc3972b..00000000 --- a/frontend/src/components/CopyEntryButton.js +++ /dev/null @@ -1,94 +0,0 @@ -import { jsx } from "@emotion/react"; -import { - Button, - Menu, - MenuButton, - MenuList, - MenuItem, - MenuGroup, - MenuDivider, -} from "@chakra-ui/react"; -import { - useJournalEntry, - useJournals, - useRouter, - useToast, -} from "../core/hooks"; -import { EntryService } from "../core/services"; -import { useQueryCache } from "react-query"; - -const CopyEntryButton = ({ id, journalId }) => { - const router = useRouter(); - const cache = useQueryCache(); - const { appScope } = router.params; - const { data: entryToCopy, isLoading: sourceIsLoading } = useJournalEntry( - journalId, - id, - appScope - ); - const toast = useToast(); - const { journalsCache } = useJournals(); - const copyEntry = async (targetJournalId) => { - try { - const newEntry = { ...entryToCopy }; - newEntry.title = "Copy of " + newEntry.title; - await EntryService.create(targetJournalId)(newEntry).then((response) => { - journalsCache.refetch(); - setTimeout( - () => cache.refetchQueries(["journal-entries", { journalId }]), - 500 - ); - if (response.status === 200) { - toast("Copied!", "success"); - } - }); - } catch (e) { - console.error( - "Error copying entry. Please email engineering@bugout.dev if you encounter this issue.", - e - ); - } - }; - - if (journalsCache.isLoading || sourceIsLoading) return ""; - - return ( - - - Copy - - - - copyEntry(journalId)}> - { - journalsCache?.data?.data?.journals?.filter( - (journal) => journal.id === journalId - )[0]?.name - } - - - {journalsCache?.data?.data?.journals?.map((journal) => { - if (journal.id === journalId) return ""; - return ( - copyEntry(journal.id)} - > - {journal.name} - - ); - })} - - - - ); -}; - -export default CopyEntryButton; diff --git a/frontend/src/components/Tags.js b/frontend/src/components/Tags.js new file mode 100644 index 00000000..194470e6 --- /dev/null +++ b/frontend/src/components/Tags.js @@ -0,0 +1,31 @@ +import React from "react"; +import { Tag, TagLabel, Flex } from "@chakra-ui/react"; +const Tags = ({ tags }) => { + const displayTags = tags?.filter( + (tag) => + tag.startsWith("from") || + tag.startsWith("client") || + tag.startsWith("network") || + tag.startsWith("to") || + tag.startsWith("source") || + tag.startsWith("node") + ); + return ( + + + {displayTags?.map((tag, index) => ( + + {tag} + + ))} + + + ); +}; + +export default Tags; diff --git a/frontend/src/core/hooks/index.js b/frontend/src/core/hooks/index.js index cc2149db..8c56f0fc 100644 --- a/frontend/src/core/hooks/index.js +++ b/frontend/src/core/hooks/index.js @@ -1,32 +1,15 @@ -export { default as hookCommon } from "./hookCommon"; +export { queryCacheProps as hookCommon } from "./hookCommon"; export { default as useAnalytics } from "./useAnalytics"; export { default as useAuthResultHandler } from "./useAuthResultHandler"; export { default as useChangePassword } from "./useChangePassword"; export { default as useClientID } from "./useClientID"; export { default as useCodeVerification } from "./useCodeVerification"; -export { default as useCreateEntry } from "./useCreateEntry"; -export { default as useCreateJournal } from "./useCreateJournal"; -export { default as useDeleteEntry } from "./useDeleteEntry"; -export { default as useDeleteJournal } from "./useDeleteJournal"; -export { default as useEntriesSearch } from "./useEntriesSearch"; export { default as useForgotPassword } from "./useForgotPassword"; -export { default as useGroup } from "./useGroup"; -export { default as useGroups } from "./useGroups"; -export { default as useHumbug } from "./useHumbug"; -export { default as useHumbugTokens } from "./useHumbugTokens"; -export { default as useHumbugs } from "./useHumbugs"; export { default as useInviteAccept } from "./useInviteAccept"; -export { default as useJournal } from "./useJournal"; -export { default as useJournalEntries } from "./useJournalEntries"; export { default as useJournalEntry } from "./useJournalEntry"; -export { default as useJournalPermissions } from "./useJournalPermissions"; -export { default as useJournalStats } from "./useJournalStats"; -export { default as useJournals } from "./useJournals"; -export { default as useJournalsScopes } from "./useJournalsScopes"; export { default as useLogin } from "./useLogin"; export { default as useLogout } from "./useLogout"; export { default as useModals } from "./useModals"; -export { default as usePreferences } from "./usePreferences"; export { default as usePresignedURL } from "./usePresignedURL"; export { default as useQuery } from "./useQuery"; export { default as useResetPassword } from "./useResetPassword"; @@ -36,7 +19,4 @@ export { default as useStorage } from "./useStorage"; export { default as useStripe } from "./useStripe"; export { default as useSubscriptions } from "./useSubscriptions"; export { default as useToast } from "./useToast"; -export { default as useTokens } from "./useTokens"; -export { default as useUpdateEntry } from "./useUpdateEntry"; -export { default as useUpdateTag } from "./useUpdateTag"; export { default as useUser } from "./useUser"; diff --git a/frontend/src/core/hooks/useForgotPassword.js b/frontend/src/core/hooks/useForgotPassword.js new file mode 100644 index 00000000..ad6dae19 --- /dev/null +++ b/frontend/src/core/hooks/useForgotPassword.js @@ -0,0 +1,26 @@ +import { useEffect } from "react"; +import { useMutation } from "react-query"; +import { AuthService } from "../../core/services"; +import { useAuthResultHandler } from "./"; +import { useToast } from "."; + +const useForgotPassword = () => { + const toast = useToast(); + const [forgotPassword, { isLoading, error, data }] = useMutation( + AuthService.forgotPassword + ); + useAuthResultHandler( + data, + error, + "Please check your inbox for verification URL." + ); + useEffect(() => { + if (error?.response?.data?.detail) { + toast(error.response.data.detail, "error"); + } + }, [error, toast, data]); + + return { forgotPassword, isLoading, data }; +}; + +export default useForgotPassword; diff --git a/frontend/src/core/hooks/useInviteAccept.js b/frontend/src/core/hooks/useInviteAccept.js new file mode 100644 index 00000000..c8a0bcdf --- /dev/null +++ b/frontend/src/core/hooks/useInviteAccept.js @@ -0,0 +1,24 @@ +import { InvitesService } from "../services"; +import { useToast } from "."; + +const useInviteAccept = () => { + const toast = useToast(); + + const inviteAccept = (invite_code) => { + InvitesService.accept(invite_code) + .then( + () => toast("You were successfully added to the team", "success"), + (reason) => { + toast(reason, "error"); + } + ) + .finally(window.sessionStorage.clear("invite_code")) + .catch((error) => { + console.error("Error during sending an invite link:", error); + }); + }; + + return { inviteAccept }; +}; + +export default useInviteAccept; diff --git a/frontend/src/core/hooks/useJournalEntry.js b/frontend/src/core/hooks/useJournalEntry.js new file mode 100644 index 00000000..e46eac98 --- /dev/null +++ b/frontend/src/core/hooks/useJournalEntry.js @@ -0,0 +1,28 @@ +import { useQuery } from "react-query"; +import { JournalService } from "../services"; +import { queryCacheProps } from "./hookCommon"; +import { useToast } from "."; + +const useJournalEntry = (journalId, entryId, journalScope) => { + const toast = useToast(); + const getEntry = async (query, key) => { + const endpoint = + journalScope === "personal" + ? JournalService.getEntry + : JournalService.getPublicEntry; + const data = await endpoint(key, { journalId, entryId }); + + const entry = data.data; + return entry; + }; + + const { data, isLoading, isFetchedAfterMount, refetch, isError, error } = + useQuery(["journal-entry", { journalId, entryId }], getEntry, { + ...queryCacheProps, + onError: (error) => toast(error, "error"), + }); + + return { data, isFetchedAfterMount, isLoading, refetch, isError, error }; +}; + +export default useJournalEntry; From 30d905b68f2541991d4a024230191a60ffd5cb5e Mon Sep 17 00:00:00 2001 From: Neeraj Kashyap Date: Sat, 31 Jul 2021 12:40:22 -0700 Subject: [PATCH 6/6] Got user login functional using Moonstream application on Brood --- frontend/src/components/EntriesNavigation.js | 2 +- frontend/src/core/hooks/useLogin.js | 2 +- frontend/src/core/providers/UserProvider/constants.js | 1 - frontend/src/core/providers/UserProvider/index.js | 4 ++-- frontend/src/core/services/auth.service.js | 2 +- frontend/src/core/services/index.js | 2 -- frontend/src/core/services/user.service.js | 10 ---------- 7 files changed, 5 insertions(+), 18 deletions(-) delete mode 100644 frontend/src/core/providers/UserProvider/constants.js delete mode 100644 frontend/src/core/services/user.service.js diff --git a/frontend/src/components/EntriesNavigation.js b/frontend/src/components/EntriesNavigation.js index 09c20d95..e3872555 100644 --- a/frontend/src/components/EntriesNavigation.js +++ b/frontend/src/components/EntriesNavigation.js @@ -82,7 +82,7 @@ const EntriesNavigation = () => { ); _setNewFilterState(props); }, - [subscriptionsCache.data.subscriptions] + [subscriptionsCache?.data?.subscriptions] ); const loadMoreButtonRef = useRef(null); diff --git a/frontend/src/core/hooks/useLogin.js b/frontend/src/core/hooks/useLogin.js index 29bfafc3..c8dd3bfc 100644 --- a/frontend/src/core/hooks/useLogin.js +++ b/frontend/src/core/hooks/useLogin.js @@ -28,7 +28,7 @@ const useLogin = (loginType) => { if (!data) { return; } - localStorage.setItem("MOONSTREAM_ACCESS_TOKEN", data.data.access_token); + localStorage.setItem("MOONSTREAM_ACCESS_TOKEN", data.data.id); const invite_code = window.sessionStorage.getItem("invite_code"); if (invite_code) { inviteAccept(invite_code); diff --git a/frontend/src/core/providers/UserProvider/constants.js b/frontend/src/core/providers/UserProvider/constants.js deleted file mode 100644 index dc2dd352..00000000 --- a/frontend/src/core/providers/UserProvider/constants.js +++ /dev/null @@ -1 +0,0 @@ -export const AUTH_URL = process.env.NEXT_PUBLIC_SIMIOTICS_AUTH_URL; diff --git a/frontend/src/core/providers/UserProvider/index.js b/frontend/src/core/providers/UserProvider/index.js index edd5bdc4..7dc268c8 100644 --- a/frontend/src/core/providers/UserProvider/index.js +++ b/frontend/src/core/providers/UserProvider/index.js @@ -1,7 +1,7 @@ import React, { useState, useEffect, useCallback } from "react"; import http from "axios"; -import { AUTH_URL } from "./constants"; import UserContext from "./context"; +import { AUTH_URL } from "../../services/auth.service"; const UserProvider = ({ children }) => { const [user, setUser] = useState(); @@ -16,7 +16,7 @@ const UserProvider = ({ children }) => { const headers = { Authorization: `Bearer ${token}` }; http - .get(`${AUTH_URL}/user`, { headers }) + .get(`${AUTH_URL}/`, { headers }) .then((response) => { setUser(response.data); }) diff --git a/frontend/src/core/services/auth.service.js b/frontend/src/core/services/auth.service.js index e5b9c257..bdc34303 100644 --- a/frontend/src/core/services/auth.service.js +++ b/frontend/src/core/services/auth.service.js @@ -1,7 +1,7 @@ import { http } from "../utils"; const API_URL = process.env.NEXT_PUBLIC_MOONSTREAM_API_URL; -const AUTH_URL = `${API_URL}/users`; +export const AUTH_URL = `${API_URL}/users`; export const login = ({ username, password }) => { const data = new FormData(); diff --git a/frontend/src/core/services/index.js b/frontend/src/core/services/index.js index d311e414..cc1ce3d2 100644 --- a/frontend/src/core/services/index.js +++ b/frontend/src/core/services/index.js @@ -7,7 +7,6 @@ import * as GroupService from "./group.service"; import * as PreferencesService from "./preferences.service"; import * as HumbugService from "./humbug.service"; import * as InvitesService from "./invites.service"; -import * as UserService from "./user.service"; import * as SubscriptionsService from "./subscriptions.service"; export { @@ -20,6 +19,5 @@ export { PreferencesService, HumbugService, InvitesService, - UserService, SubscriptionsService, }; diff --git a/frontend/src/core/services/user.service.js b/frontend/src/core/services/user.service.js deleted file mode 100644 index 5581811f..00000000 --- a/frontend/src/core/services/user.service.js +++ /dev/null @@ -1,10 +0,0 @@ -import { http } from "../utils"; - -const AUTH_URL = process.env.NEXT_PUBLIC_SIMIOTICS_AUTH_URL; - -export const findUser = (query) => { - return http({ - method: "GET", - url: `${AUTH_URL}/user/find?${query}`, - }); -};