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;