Skip to content

Commit

Permalink
Fix comment creation bug (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesBochet authored Jun 24, 2023
1 parent 31145c5 commit 9c21975
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
19 changes: 3 additions & 16 deletions front/src/modules/auth/hooks/useFetchCurrentUser.ts
Original file line number Diff line number Diff line change
@@ -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];
}
2 changes: 1 addition & 1 deletion front/src/modules/auth/states/tokenPairState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
15 changes: 14 additions & 1 deletion front/src/providers/user/UserProvider.tsx
Original file line number Diff line number Diff line change
@@ -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}</>;
};
2 changes: 2 additions & 0 deletions server/src/ability/ability.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 9c21975

Please sign in to comment.