Reorganize frontend and install Craco to alias modules (#190)

This commit is contained in:
Charles Bochet
2023-06-04 11:23:09 +02:00
committed by GitHub
parent bbc80cd543
commit 7b858fd7c9
149 changed files with 3441 additions and 1158 deletions

View File

@ -0,0 +1,24 @@
import { reduceSortsToOrderBy } from '@/filters-and-sorts/helpers';
import { PeopleSelectedSortType } from '../select';
describe('reduceSortsToOrderBy', () => {
it('should return an array of objects with the id as key and the order as value', () => {
const sorts = [
{
key: 'firstname',
label: 'firstname',
order: 'asc',
_type: 'default_sort',
},
{
key: 'lastname',
label: 'lastname',
order: 'desc',
_type: 'default_sort',
},
] satisfies PeopleSelectedSortType[];
const result = reduceSortsToOrderBy(sorts);
expect(result).toEqual([{ firstname: 'asc' }, { lastname: 'desc' }]);
});
});

View File

@ -0,0 +1,50 @@
import {
GraphqlMutationPerson,
GraphqlQueryPerson,
} from '../../interfaces/person.interface';
import { updatePerson } from '../update';
jest.mock('~/apollo', () => {
const personInterface = jest.requireActual(
'@/people/interfaces/person.interface',
);
return {
apiClient: {
mutate: (arg: {
mutation: unknown;
variables: GraphqlMutationPerson;
}) => {
const gqlPerson = arg.variables as unknown as GraphqlQueryPerson;
return { data: personInterface.mapToPerson(gqlPerson) };
},
},
};
});
it('updates a person', async () => {
const result = await updatePerson({
firstname: 'John',
lastname: 'Doe',
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6c',
email: 'john@example.com',
company: {
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6b',
name: 'ACME',
domainName: 'example.com',
__typename: 'companies',
},
phone: '+1 (555) 123-4567',
pipes: [
{
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6d',
name: 'Customer',
icon: '!',
},
],
createdAt: new Date(),
city: 'San Francisco',
__typename: 'people',
});
expect(result.data).toBeDefined();
result.data && expect(result.data.email).toBe('john@example.com');
});

View File

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

View File

@ -0,0 +1,50 @@
import { gql, QueryResult, useQuery } from '@apollo/client';
import { SelectedSortType } from '@/filters-and-sorts/interfaces/sorts/interface';
import {
PersonOrderByWithRelationInput as People_Order_By,
PersonWhereInput as People_Bool_Exp,
SortOrder,
} from '~/generated/graphql';
import { GraphqlQueryPerson } from '../interfaces/person.interface';
export type PeopleSelectedSortType = SelectedSortType<People_Order_By>;
export const GET_PEOPLE = gql`
query GetPeople(
$orderBy: [PersonOrderByWithRelationInput!]
$where: PersonWhereInput
$limit: Int
) {
people: findManyPerson(orderBy: $orderBy, where: $where, take: $limit) {
id
phone
email
city
firstname
lastname
createdAt
company {
id
name
domainName
}
}
}
`;
export function usePeopleQuery(
orderBy: People_Order_By[],
where: People_Bool_Exp,
): QueryResult<{ people: GraphqlQueryPerson[] }> {
return useQuery<{ people: GraphqlQueryPerson[] }>(GET_PEOPLE, {
variables: { orderBy, where },
});
}
export const defaultOrderBy: People_Order_By[] = [
{
createdAt: SortOrder.Desc,
},
];

View File

@ -0,0 +1,122 @@
import { FetchResult, gql } from '@apollo/client';
import { apiClient } from '../../../apollo';
import { mapToGqlPerson, Person } from '../interfaces/person.interface';
export const UPDATE_PERSON = gql`
mutation UpdatePeople(
$id: String
$firstname: String
$lastname: String
$phone: String
$city: String
$companyId: String
$email: String
$createdAt: DateTime
) {
updateOnePerson(
where: { id: $id }
data: {
city: { set: $city }
company: { connect: { id: $companyId } }
email: { set: $email }
firstname: { set: $firstname }
id: { set: $id }
lastname: { set: $lastname }
phone: { set: $phone }
createdAt: { set: $createdAt }
}
) {
city
company {
domainName
name
id
}
email
firstname
id
lastname
phone
createdAt
}
}
`;
export const INSERT_PERSON = gql`
mutation InsertPerson(
$id: String!
$firstname: String!
$lastname: String!
$phone: String!
$city: String!
$email: String!
$createdAt: DateTime
) {
createOnePerson(
data: {
id: $id
firstname: $firstname
lastname: $lastname
phone: $phone
city: $city
email: $email
createdAt: $createdAt
}
) {
city
company {
domainName
name
id
}
email
firstname
id
lastname
phone
createdAt
}
}
`;
export const DELETE_PEOPLE = gql`
mutation DeletePeople($ids: [String!]) {
deleteManyPerson(where: { id: { in: $ids } }) {
count
}
}
`;
export async function updatePerson(
person: Person,
): Promise<FetchResult<Person>> {
const result = await apiClient.mutate({
mutation: UPDATE_PERSON,
variables: mapToGqlPerson(person),
});
return result;
}
export async function insertPerson(
person: Person,
): Promise<FetchResult<Person>> {
const result = await apiClient.mutate({
mutation: INSERT_PERSON,
variables: mapToGqlPerson(person),
refetchQueries: ['GetPeople'],
});
return result;
}
export async function deletePeople(
peopleIds: string[],
): Promise<FetchResult<Person>> {
const result = await apiClient.mutate({
mutation: DELETE_PEOPLE,
variables: { ids: peopleIds },
});
return result;
}