Create Profile Hooks to fetch current user

This commit is contained in:
Charles Bochet
2023-02-02 20:40:44 +01:00
parent 0eef9871f0
commit fcdc9aaec4
17 changed files with 150 additions and 52 deletions

View File

@ -0,0 +1,33 @@
import { ApolloError, useQuery } from '@apollo/client';
import { gql } from 'graphql-tag';
import { User } from '../../interfaces/user.interface';
import { useGetUserEmailFromToken } from '../AuthenticationHooks';
const GET_USER_PROFILE = gql`
query GetUserProfile($email: String!) {
users(where: { email: { _eq: $email } }, limit: 1) {
id
email
first_name
last_name
tenant {
id
name
}
}
}
`;
type ProfileResult = {
loading: boolean;
error?: ApolloError;
user?: User;
};
export const useGetProfile = (): ProfileResult => {
const email = useGetUserEmailFromToken();
const { loading, error, data } = useQuery(GET_USER_PROFILE, {
variables: { email },
});
return { loading, error, user: data?.users[0] };
};