Refactor people query into separate file.
This commit is contained in:
@ -3,37 +3,16 @@ import WithTopBarContainer from '../../layout/containers/WithTopBarContainer';
|
|||||||
import Table from '../../components/table/Table';
|
import Table from '../../components/table/Table';
|
||||||
import styled from '@emotion/styled';
|
import styled from '@emotion/styled';
|
||||||
import { peopleColumns } from './people-table';
|
import { peopleColumns } from './people-table';
|
||||||
import { gql, useQuery } from '@apollo/client';
|
import { mapPerson } from '../../interfaces/person.interface';
|
||||||
import { GraphqlPerson, mapPerson } from '../../interfaces/person.interface';
|
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { SortType } from '../../components/table/table-header/SortAndFilterBar';
|
import { SortType } from '../../components/table/table-header/SortAndFilterBar';
|
||||||
|
import { OrderBy, usePeopleQuery } from '../../services/people';
|
||||||
|
|
||||||
const StyledPeopleContainer = styled.div`
|
const StyledPeopleContainer = styled.div`
|
||||||
display: flex;
|
display: flex;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
export const GET_PEOPLE = gql`
|
|
||||||
query GetPeople($orderBy: [people_order_by!]) {
|
|
||||||
people(order_by: $orderBy) {
|
|
||||||
id
|
|
||||||
phone
|
|
||||||
email
|
|
||||||
city
|
|
||||||
firstname
|
|
||||||
lastname
|
|
||||||
created_at
|
|
||||||
company {
|
|
||||||
company_name
|
|
||||||
company_domain
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
// @TODO get those types from generated-code person-order-by
|
|
||||||
type OrderBy = Record<string, 'asc' | 'desc'>;
|
|
||||||
|
|
||||||
const defaultOrderBy: OrderBy[] = [
|
const defaultOrderBy: OrderBy[] = [
|
||||||
{
|
{
|
||||||
created_at: 'desc',
|
created_at: 'desc',
|
||||||
@ -57,9 +36,7 @@ function People() {
|
|||||||
setOrderBy(sorts.length ? reduceSortsToOrderBy(sorts) : defaultOrderBy);
|
setOrderBy(sorts.length ? reduceSortsToOrderBy(sorts) : defaultOrderBy);
|
||||||
};
|
};
|
||||||
|
|
||||||
const { data } = useQuery<{ people: GraphqlPerson[] }>(GET_PEOPLE, {
|
const { data } = usePeopleQuery(orderBy);
|
||||||
variables: { orderBy: orderBy },
|
|
||||||
});
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<WithTopBarContainer title="People" icon={faUser}>
|
<WithTopBarContainer title="People" icon={faUser}>
|
||||||
|
|||||||
1
front/src/services/people/index.ts
Normal file
1
front/src/services/people/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './select';
|
||||||
30
front/src/services/people/select.ts
Normal file
30
front/src/services/people/select.ts
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import { QueryResult, gql, useQuery } from '@apollo/client';
|
||||||
|
import { GraphqlPerson } from '../../interfaces/person.interface';
|
||||||
|
|
||||||
|
export type OrderBy = Record<string, 'asc' | 'desc'>;
|
||||||
|
|
||||||
|
export const GET_PEOPLE = gql`
|
||||||
|
query GetPeople($orderBy: [people_order_by!]) {
|
||||||
|
people(order_by: $orderBy) {
|
||||||
|
id
|
||||||
|
phone
|
||||||
|
email
|
||||||
|
city
|
||||||
|
firstname
|
||||||
|
lastname
|
||||||
|
created_at
|
||||||
|
company {
|
||||||
|
company_name
|
||||||
|
company_domain
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
export function usePeopleQuery(
|
||||||
|
orderBy: OrderBy[],
|
||||||
|
): QueryResult<{ people: GraphqlPerson[] }> {
|
||||||
|
return useQuery<{ people: GraphqlPerson[] }>(GET_PEOPLE, {
|
||||||
|
variables: { orderBy },
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user