Feat/account owner picker (#359)

* Added account owner picker

* Regenerated graphql files

* Fixed pickers staying in edit mode with a new generic hook

* Fixed lint
This commit is contained in:
Lucas Bordeau
2023-06-22 19:47:04 +02:00
committed by GitHub
parent cd70209502
commit 4a2797c491
8 changed files with 147 additions and 66 deletions

View File

@ -0,0 +1,26 @@
import { PersonChip } from '@/people/components/PersonChip';
import { EditableCellV2 } from '@/ui/components/editable-cell/EditableCellV2';
import { Company, User } from '~/generated/graphql';
import { CompanyAccountOwnerPicker } from './CompanyAccountOwnerPicker';
export type OwnProps = {
company: Pick<Company, 'id'> & {
accountOwner?: Pick<User, 'id' | 'displayName'> | null;
};
};
export function CompanyAccountOwnerCell({ company }: OwnProps) {
return (
<EditableCellV2
editModeContent={<CompanyAccountOwnerPicker company={company} />}
nonEditModeContent={
company.accountOwner?.displayName ? (
<PersonChip name={company.accountOwner?.displayName ?? ''} />
) : (
<></>
)
}
/>
);
}

View File

@ -0,0 +1,67 @@
import { SingleEntitySelect } from '@/relation-picker/components/SingleEntitySelect';
import { useFilteredSearchEntityQuery } from '@/relation-picker/hooks/useFilteredSearchEntityQuery';
import { relationPickerSearchFilterScopedState } from '@/relation-picker/states/relationPickerSearchFilterScopedState';
import { EntityForSelect } from '@/relation-picker/types/EntityForSelect';
import { Entity } from '@/relation-picker/types/EntityTypeForSelect';
import { useCloseEditableCell } from '@/ui/components/editable-cell/hooks/useCloseEditableCell';
import { useRecoilScopedState } from '@/ui/hooks/useRecoilScopedState';
import {
Company,
User,
useSearchUserQuery,
useUpdateCompanyMutation,
} from '~/generated/graphql';
export type OwnProps = {
company: Pick<Company, 'id'> & {
accountOwner?: Pick<User, 'id' | 'displayName'> | null;
};
};
type UserForSelect = EntityForSelect & {
entityType: Entity.User;
};
export function CompanyAccountOwnerPicker({ company }: OwnProps) {
const [searchFilter] = useRecoilScopedState(
relationPickerSearchFilterScopedState,
);
const [updateCompany] = useUpdateCompanyMutation();
const closeEditableCell = useCloseEditableCell();
const companies = useFilteredSearchEntityQuery({
queryHook: useSearchUserQuery,
selectedIds: [company?.accountOwner?.id ?? ''],
searchFilter: searchFilter,
mappingFunction: (user) => ({
entityType: Entity.User,
id: user.id,
name: user.displayName,
avatarType: 'rounded',
}),
orderByField: 'displayName',
searchOnFields: ['displayName'],
});
async function handleEntitySelected(selectedUser: UserForSelect) {
await updateCompany({
variables: {
...company,
accountOwnerId: selectedUser.id,
},
});
closeEditableCell();
}
return (
<SingleEntitySelect
onEntitySelected={handleEntitySelected}
entities={{
entitiesToSelect: companies.entitiesToSelect,
selectedEntity: companies.selectedEntities[0],
}}
/>
);
}

View File

@ -1,11 +1,9 @@
import { useRecoilState } from 'recoil';
import { SingleEntitySelect } from '@/relation-picker/components/SingleEntitySelect';
import { useFilteredSearchEntityQuery } from '@/relation-picker/hooks/useFilteredSearchEntityQuery';
import { relationPickerSearchFilterScopedState } from '@/relation-picker/states/relationPickerSearchFilterScopedState';
import { useCloseEditableCell } from '@/ui/components/editable-cell/hooks/useCloseEditableCell';
import { isCreateModeScopedState } from '@/ui/components/editable-cell/states/isCreateModeScopedState';
import { useRecoilScopedState } from '@/ui/hooks/useRecoilScopedState';
import { isSomeInputInEditModeState } from '@/ui/tables/states/isSomeInputInEditModeState';
import { getLogoUrlFromDomainName } from '@/utils/utils';
import {
CommentableType,
@ -26,9 +24,8 @@ export function PeopleCompanyPicker({ people }: OwnProps) {
relationPickerSearchFilterScopedState,
);
const [updatePeople] = useUpdatePeopleMutation();
const [, setIsSomeInputInEditMode] = useRecoilState(
isSomeInputInEditModeState,
);
const closeEditableCell = useCloseEditableCell();
const companies = useFilteredSearchEntityQuery({
queryHook: useSearchCompanyQuery,
@ -46,14 +43,14 @@ export function PeopleCompanyPicker({ people }: OwnProps) {
});
async function handleEntitySelected(entity: any) {
setIsSomeInputInEditMode(false);
await updatePeople({
variables: {
...people,
companyId: entity.id,
},
});
closeEditableCell();
}
function handleCreate() {

View File

@ -1,3 +1,12 @@
import { CommentableType, PipelineProgressableType } from '~/generated/graphql';
export type EntityTypeForSelect = CommentableType | PipelineProgressableType;
export enum Entity {
Company = 'Company',
Person = 'Person',
User = 'User',
}
export type EntityTypeForSelect =
| CommentableType
| PipelineProgressableType
| Entity;

View File

@ -28,8 +28,16 @@ export const SEARCH_PEOPLE_QUERY = gql`
`;
export const SEARCH_USER_QUERY = gql`
query SearchUser($where: UserWhereInput, $limit: Int) {
searchResults: findManyUser(where: $where, take: $limit) {
query SearchUser(
$where: UserWhereInput
$limit: Int
$orderBy: [UserOrderByWithRelationInput!]
) {
searchResults: findManyUser(
where: $where
take: $limit
orderBy: $orderBy
) {
id
email
displayName

View File

@ -0,0 +1,19 @@
import { useCallback } from 'react';
import { useRecoilState } from 'recoil';
import { useRecoilScopedState } from '@/ui/hooks/useRecoilScopedState';
import { isSomeInputInEditModeState } from '@/ui/tables/states/isSomeInputInEditModeState';
import { isEditModeScopedState } from '../states/isEditModeScopedState';
export function useCloseEditableCell() {
const [, setIsSomeInputInEditMode] = useRecoilState(
isSomeInputInEditModeState,
);
const [, setIsEditMode] = useRecoilScopedState(isEditModeScopedState);
return useCallback(() => {
setIsSomeInputInEditMode(false);
setIsEditMode(false);
}, [setIsEditMode, setIsSomeInputInEditMode]);
}