Fix comment creation bug (#371)

This commit is contained in:
Charles Bochet 2023-06-24 11:18:13 -07:00 committed by GitHub
parent 31145c5518
commit 9c21975d2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 19 deletions

View File

@ -1,26 +1,13 @@
import { useEffect } from 'react';
import jwt from 'jwt-decode';
import { useRecoilState } from 'recoil';
import { useGetCurrentUserQuery } from '~/generated/graphql';
import { AuthTokenPair, useGetCurrentUserQuery } from '~/generated/graphql';
import { currentUserState } from '../states/currentUserState';
import { tokenPairState } from '../states/tokenPairState';
export function useFetchCurrentUser() {
const [, setCurrentUser] = useRecoilState(currentUserState);
const [tokenPair] = useRecoilState(tokenPairState);
export function useFetchCurrentUser(tokenPair: AuthTokenPair | null) {
const userId = tokenPair?.accessToken.token
? jwt<{ sub: string }>(tokenPair.accessToken.token).sub
: null;
const { data } = useGetCurrentUserQuery({
variables: { uuid: userId },
});
const user = data?.users?.[0];
useEffect(() => {
if (user) {
setCurrentUser(user);
}
}, [user, setCurrentUser]);
return data?.users?.[0];
}

View File

@ -7,7 +7,7 @@ const cookieStorageEffect =
(key: string): AtomEffect<AuthTokenPair | null> =>
({ setSelf, onSet }) => {
const savedValue = cookieStorage.getItem(key);
if (savedValue != null) {
if (savedValue != null && JSON.parse(savedValue)['accessToken']) {
setSelf(JSON.parse(savedValue));
}

View File

@ -45,7 +45,7 @@ export function DropdownMenuCheckableItem({
return (
<DropdownMenuCheckableItemContainer onClick={handleClick}>
<StyledLeftContainer>
<Checkbox onChange={onChange} id={id} name={id} checked={checked} />
<Checkbox id={id} name={id} checked={checked} />
<StyledChildrenContainer>{children}</StyledChildrenContainer>
</StyledLeftContainer>
</DropdownMenuCheckableItemContainer>

View File

@ -1,9 +1,22 @@
import { useEffect } from 'react';
import { useRecoilState } from 'recoil';
import { useFetchCurrentUser } from '@/auth/hooks/useFetchCurrentUser';
import { currentUserState } from '@/auth/states/currentUserState';
import { tokenPairState } from '@/auth/states/tokenPairState';
export const UserProvider: React.FC<React.PropsWithChildren> = ({
children,
}) => {
useFetchCurrentUser();
const [, setCurrentUser] = useRecoilState(currentUserState);
const [tokenPair] = useRecoilState(tokenPairState);
const user = useFetchCurrentUser(tokenPair);
useEffect(() => {
if (user) {
setCurrentUser(user);
}
}, [setCurrentUser, user]);
return <>{children}</>;
};

View File

@ -63,11 +63,13 @@ export class AbilityFactory {
can(AbilityAction.Read, 'Company', { workspaceId: workspace.id });
can(AbilityAction.Create, 'Company');
can(AbilityAction.Update, 'Company', { workspaceId: workspace.id });
can(AbilityAction.Delete, 'Company', { workspaceId: workspace.id });
// Person
can(AbilityAction.Read, 'Person', { workspaceId: workspace.id });
can(AbilityAction.Create, 'Person');
can(AbilityAction.Update, 'Person', { workspaceId: workspace.id });
can(AbilityAction.Delete, 'Person', { workspaceId: workspace.id });
// RefreshToken
cannot(AbilityAction.Manage, 'RefreshToken');