Refactor people query into separate file.

This commit is contained in:
Anders Borch
2023-04-24 11:10:56 +02:00
parent a5bfeef2d6
commit 47bdabd5aa
3 changed files with 34 additions and 26 deletions

View File

@ -3,37 +3,16 @@ import WithTopBarContainer from '../../layout/containers/WithTopBarContainer';
import Table from '../../components/table/Table';
import styled from '@emotion/styled';
import { peopleColumns } from './people-table';
import { gql, useQuery } from '@apollo/client';
import { GraphqlPerson, mapPerson } from '../../interfaces/person.interface';
import { mapPerson } from '../../interfaces/person.interface';
import { useState } from 'react';
import { SortType } from '../../components/table/table-header/SortAndFilterBar';
import { OrderBy, usePeopleQuery } from '../../services/people';
const StyledPeopleContainer = styled.div`
display: flex;
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[] = [
{
created_at: 'desc',
@ -57,9 +36,7 @@ function People() {
setOrderBy(sorts.length ? reduceSortsToOrderBy(sorts) : defaultOrderBy);
};
const { data } = useQuery<{ people: GraphqlPerson[] }>(GET_PEOPLE, {
variables: { orderBy: orderBy },
});
const { data } = usePeopleQuery(orderBy);
return (
<WithTopBarContainer title="People" icon={faUser}>

View File

@ -0,0 +1 @@
export * from './select';

View 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 },
});
}