kopia lustrzana https://github.com/bugout-dev/moonstream
Made a few changes to fix build errors
rodzic
1ced909f15
commit
5770057838
|
@ -1,5 +1,3 @@
|
|||
|
||||
import { jsx } from "@emotion/react";
|
||||
import FourOThree from "../src/components/FourOThree";
|
||||
|
||||
const Page403 = () => {
|
||||
|
|
|
@ -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 (
|
||||
<Modal onClose={() => ui.toggleModal(null)}>
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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 (
|
||||
<Flex alignSelf="flex-start">
|
||||
<Flex flexWrap="wrap" pl={2} pr={2} spacing={2} alignItems="center">
|
||||
{displayTags?.map((tag, index) => (
|
||||
<Tag
|
||||
variant="subtle"
|
||||
colorScheme="primary"
|
||||
key={`${tag}-${index}`}
|
||||
zIndex={1}
|
||||
>
|
||||
<TagLabel>{tag}</TagLabel>
|
||||
</Tag>
|
||||
))}
|
||||
</Flex>
|
||||
</Flex>
|
||||
);
|
||||
};
|
||||
|
||||
export default Tags;
|
|
@ -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
|
||||
variant="subtle"
|
||||
colorScheme="primary"
|
||||
size="sm"
|
||||
key={`${tag}-${index}`}
|
||||
>
|
||||
<TagLabel>{tag}</TagLabel>
|
||||
</Tag>
|
||||
);
|
||||
});
|
||||
|
||||
return (
|
||||
<Flex display="flex" flexWrap="wrap" align="center" spacing={1}>
|
||||
<TagsToShow />
|
||||
{tags.length > TAGS_DISPLAY_NUM_DEF && (
|
||||
<Button
|
||||
m={1}
|
||||
onClick={() => toggleAllTags(!showAllTags)}
|
||||
size="xs"
|
||||
variant="link"
|
||||
color="primary.600"
|
||||
ml={1}
|
||||
style={{ transform: "translateY(-1px)" }}
|
||||
>
|
||||
{tagButtonText}
|
||||
</Button>
|
||||
)}
|
||||
</Flex>
|
||||
);
|
||||
};
|
||||
export default TagsList;
|
|
@ -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 (
|
||||
<Center>
|
||||
<Spinner />
|
||||
</Center>
|
||||
);
|
||||
|
||||
const handleTokenSubmit = ({ appName, appVersion }) => {
|
||||
createToken({ appName, appVersion }).then(() => toggleNewToken(false));
|
||||
};
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit(handleTokenSubmit)}>
|
||||
<Table
|
||||
variant="simple"
|
||||
colorScheme="primary"
|
||||
justifyContent="center"
|
||||
alignItems="baseline"
|
||||
h="auto"
|
||||
size="sm"
|
||||
>
|
||||
<Thead>
|
||||
<Tr>
|
||||
<Th>Token</Th>
|
||||
<Th>App Name</Th>
|
||||
<Th>App version</Th>
|
||||
<Th>Action</Th>
|
||||
</Tr>
|
||||
</Thead>
|
||||
<Tbody>
|
||||
{tokens.map((token, idx) => {
|
||||
return (
|
||||
<Tr key={`RestrictedToken-row-${idx}`}>
|
||||
<Td mr={4} p={0}>
|
||||
<CopyButton>{token.restricted_token_id}</CopyButton>
|
||||
</Td>
|
||||
<Td py={0}>{token.app_name}</Td>
|
||||
<Td py={0}>{token.app_version}</Td>
|
||||
<Td py={0}>
|
||||
<ConfirmationRequest
|
||||
bodyMessage={"please confirm"}
|
||||
header={"Delete token"}
|
||||
onConfirm={() => revoke(token.restricted_token_id)}
|
||||
>
|
||||
<IconButton
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
colorScheme="primary"
|
||||
icon={<DeleteIcon />}
|
||||
/>
|
||||
</ConfirmationRequest>
|
||||
</Td>
|
||||
</Tr>
|
||||
);
|
||||
})}
|
||||
|
||||
<NewTokenTr
|
||||
isOpen={isNewTokenOpen}
|
||||
toggleSelf={toggleNewToken}
|
||||
errors={errors}
|
||||
register={register}
|
||||
journalName={journalName}
|
||||
/>
|
||||
</Tbody>
|
||||
</Table>
|
||||
{tokens.length < 1 && (
|
||||
<Center>
|
||||
<Text my={4}>Create Usage report tokens here</Text>
|
||||
</Center>
|
||||
)}
|
||||
</form>
|
||||
);
|
||||
};
|
||||
export default TokenList;
|
|
@ -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 (
|
||||
<Box>
|
||||
<form onSubmit={handleSubmit(login)} style={formStyle}>
|
||||
<HStack>
|
||||
<Button
|
||||
variant="solid"
|
||||
colorScheme="primary"
|
||||
type="submit"
|
||||
isLoading={isLoading}
|
||||
>
|
||||
Submit
|
||||
</Button>
|
||||
|
||||
<FormControl isInvalid={errors.password}>
|
||||
<InputGroup minWidth="300px">
|
||||
<InputLeftElement onClick={togglePassword}>
|
||||
<CustomIcon icon="password" />
|
||||
</InputLeftElement>
|
||||
<Input
|
||||
colorScheme="primary"
|
||||
variant="filled"
|
||||
isDisabled={isLoading}
|
||||
autoComplete="on"
|
||||
placeholder="Your Bugout password"
|
||||
name="password"
|
||||
type={showPassword}
|
||||
ref={(e) => {
|
||||
register(e, { required: "Password is required!" });
|
||||
PasswordRef.current = e;
|
||||
}}
|
||||
/>
|
||||
<InputRightElement onClick={() => toggle(null)}>
|
||||
<CloseIcon />
|
||||
</InputRightElement>
|
||||
</InputGroup>
|
||||
<FormErrorMessage color="unsafe.400" pl="1" justifyContent="Center">
|
||||
{errors.password && errors.password.message}
|
||||
</FormErrorMessage>
|
||||
</FormControl>
|
||||
<Input
|
||||
type="hidden"
|
||||
ref={register}
|
||||
name="username"
|
||||
defaultValue={user?.username}
|
||||
/>
|
||||
</HStack>
|
||||
</form>
|
||||
</Box>
|
||||
);
|
||||
};
|
||||
export default TokenRequest;
|
|
@ -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}
|
||||
></Image>
|
||||
{caseURL && (
|
||||
// <RouterLink href={caseURL} passHref scroll={true}>
|
||||
<Link
|
||||
fontSize={["sm", null, "md", "lg"]}
|
||||
textColor="secondary.900"
|
||||
|
@ -28,7 +26,6 @@ const TrustedBadge = ({ name, caseURL, ImgURL }) => {
|
|||
>
|
||||
{`Read case study >`}
|
||||
</Link>
|
||||
// </RouterLink>
|
||||
)}
|
||||
</Flex>
|
||||
);
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
Ładowanie…
Reference in New Issue