implemented identity, updated session, added 'workings' list
added paginated querying, added 'common'
This commit is contained in:
parent
027deba422
commit
6ada519e06
36 changed files with 1422 additions and 279 deletions
|
|
@ -31,7 +31,7 @@ export function useAuth(): UseAuthOutput {
|
|||
successMsg: 'Logged out successfully.'
|
||||
});
|
||||
|
||||
if (!authContext) throw new Error('useAuth must be used within an AuthProvider');
|
||||
if (authContext == null) throw new Error('useAuth must be used within an AuthProvider');
|
||||
|
||||
return {
|
||||
...authContext,
|
||||
|
|
@ -45,7 +45,6 @@ export function useAuth(): UseAuthOutput {
|
|||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (data != null) {
|
||||
authContext.setHeaders(data.login);
|
||||
return data.login;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,52 @@
|
|||
// interface UseIdentityProps {}
|
||||
import { ApolloError, ApolloQueryResult, gql, OperationVariables, useQuery } from '@apollo/client';
|
||||
import { Identity, IdentityRootQuery } from '@generated/types';
|
||||
import { createContext, useContext } from 'react';
|
||||
|
||||
// export function useIdentity(props: UseIdentityProps) {}
|
||||
export interface IdentityContext {
|
||||
data: Identity;
|
||||
refetch: () => void;
|
||||
}
|
||||
|
||||
export const IdentityContext = createContext<IdentityContext | null>(null);
|
||||
export const IdentityProvider = IdentityContext.Provider;
|
||||
|
||||
export function useIdentity(): IdentityContext | null {
|
||||
const identityContext = useContext(IdentityContext);
|
||||
if (identityContext == null) return null;
|
||||
return identityContext;
|
||||
}
|
||||
|
||||
export function useIdentityQuery({ skip }: { skip?: boolean }): {
|
||||
data: IdentityRootQuery | undefined;
|
||||
loading: boolean;
|
||||
error: ApolloError | undefined;
|
||||
refetch: (
|
||||
variables?: Partial<OperationVariables> | undefined
|
||||
) => Promise<ApolloQueryResult<IdentityRootQuery>>;
|
||||
} {
|
||||
const { data, loading, error, refetch } = useQuery<IdentityRootQuery>(query, {
|
||||
skip
|
||||
});
|
||||
return {
|
||||
data,
|
||||
loading,
|
||||
error,
|
||||
refetch
|
||||
};
|
||||
}
|
||||
|
||||
const query = gql`
|
||||
query IdentityRoot {
|
||||
identity {
|
||||
id
|
||||
email
|
||||
isDarkTheme
|
||||
user {
|
||||
id
|
||||
username
|
||||
createdAt
|
||||
updatedAt
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
|
|
|||
40
app/client/hooks/usePaginatedQuery.tsx
Normal file
40
app/client/hooks/usePaginatedQuery.tsx
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import {
|
||||
ApolloError,
|
||||
ApolloQueryResult,
|
||||
MaybeMasked,
|
||||
OperationVariables,
|
||||
useQuery
|
||||
} from '@apollo/client';
|
||||
import { DocumentNode } from 'graphql';
|
||||
|
||||
interface UsePaginatedQueryProps {
|
||||
query: DocumentNode;
|
||||
input: {
|
||||
filter: {
|
||||
terms?: string | null;
|
||||
offset?: number | null;
|
||||
size?: number | null;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
interface UsePaginatedQueryReturn<T> {
|
||||
data?: MaybeMasked<T> | undefined;
|
||||
loading: boolean;
|
||||
error: ApolloError | undefined;
|
||||
refetch: (
|
||||
variables?: Partial<OperationVariables> | undefined
|
||||
) => Promise<ApolloQueryResult<MaybeMasked<T>>>;
|
||||
}
|
||||
|
||||
export function usePaginatedQuery<T>(props: UsePaginatedQueryProps): UsePaginatedQueryReturn<T> {
|
||||
const { data, loading, error, refetch } = useQuery<T>(props.query, {
|
||||
variables: { input: props.input }
|
||||
});
|
||||
return {
|
||||
data,
|
||||
loading,
|
||||
error,
|
||||
refetch
|
||||
};
|
||||
}
|
||||
33
app/client/hooks/useUserOneQuery.tsx
Normal file
33
app/client/hooks/useUserOneQuery.tsx
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import { ApolloError, ApolloQueryResult, gql, OperationVariables, useQuery } from '@apollo/client';
|
||||
import { UserOneQuery } from '@generated/types';
|
||||
|
||||
export function useUserOneQuery({ id, skip }: { id?: string; skip?: boolean }): {
|
||||
data: UserOneQuery | undefined;
|
||||
loading: boolean;
|
||||
error: ApolloError | undefined;
|
||||
refetch: (
|
||||
variables?: Partial<OperationVariables> | undefined
|
||||
) => Promise<ApolloQueryResult<UserOneQuery>>;
|
||||
} {
|
||||
const { data, loading, error, refetch } = useQuery<UserOneQuery>(query, {
|
||||
variables: { id },
|
||||
skip
|
||||
});
|
||||
return {
|
||||
data,
|
||||
loading,
|
||||
error,
|
||||
refetch
|
||||
};
|
||||
}
|
||||
|
||||
const query = gql`
|
||||
query UserOne($id: ID!) {
|
||||
userOne(id: $id) {
|
||||
id
|
||||
username
|
||||
createdAt
|
||||
updatedAt
|
||||
}
|
||||
}
|
||||
`;
|
||||
Loading…
Add table
Add a link
Reference in a new issue