Files
twenty/front/src/modules/pipeline/components/PipelineProgressPointOfContactPicker.tsx
Lucas Bordeau a2ccb643ff Optimize table loading (#866)
* wip

* wip

* Ok

* Deleted unused code

* Fixed lint

* Minor fixes

* Minor fixes

* Minor Fixes

* Minor merge fixes

* Ok

* Fix storybook tests

* Removed console.log

* Fix login

* asd

* Fixed storybook

* Added await

* Fixed await

* Added sleep for failing test

* Fix sleep

* Fix test

* Fix tests

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
2023-07-25 11:00:15 -07:00

94 lines
2.6 KiB
TypeScript

import { getOperationName } from '@apollo/client/utilities';
import { Key } from 'ts-key-enum';
import { useFilteredSearchPeopleQuery } from '@/people/queries';
import { useScopedHotkeys } from '@/ui/hotkey/hooks/useScopedHotkeys';
import { useRecoilScopedState } from '@/ui/recoil-scope/hooks/useRecoilScopedState';
import { SingleEntitySelect } from '@/ui/relation-picker/components/SingleEntitySelect';
import { relationPickerSearchFilterScopedState } from '@/ui/relation-picker/states/relationPickerSearchFilterScopedState';
import { RelationPickerHotkeyScope } from '@/ui/relation-picker/types/RelationPickerHotkeyScope';
import { isCreateModeScopedState } from '@/ui/table/editable-cell/states/isCreateModeScopedState';
import {
Person,
PipelineProgress,
useUpdateOnePipelineProgressMutation,
} from '~/generated/graphql';
import { EntityForSelect } from '../../ui/relation-picker/types/EntityForSelect';
import { GET_PIPELINE_PROGRESS, GET_PIPELINES } from '../queries';
export type OwnProps = {
pipelineProgress: Pick<PipelineProgress, 'id'> & {
pointOfContact?: Pick<Person, 'id'> | null;
};
onSubmit?: () => void;
onCancel?: () => void;
};
export function PipelineProgressPointOfContactPicker({
pipelineProgress,
onSubmit,
onCancel,
}: OwnProps) {
const [, setIsCreating] = useRecoilScopedState(isCreateModeScopedState);
const [searchFilter] = useRecoilScopedState(
relationPickerSearchFilterScopedState,
);
const [updatePipelineProgress] = useUpdateOnePipelineProgressMutation();
const people = useFilteredSearchPeopleQuery({
searchFilter,
selectedIds: pipelineProgress.pointOfContact?.id
? [pipelineProgress.pointOfContact.id]
: [],
});
async function handleEntitySelected(
entity: EntityForSelect | null | undefined,
) {
if (!entity) {
return;
}
await updatePipelineProgress({
variables: {
...pipelineProgress,
pointOfContactId: entity.id,
},
refetchQueries: [
getOperationName(GET_PIPELINE_PROGRESS) ?? '',
getOperationName(GET_PIPELINES) ?? '',
],
});
onSubmit?.();
}
function handleCreate() {
setIsCreating(true);
onSubmit?.();
}
useScopedHotkeys(
Key.Escape,
() => {
onCancel && onCancel();
},
RelationPickerHotkeyScope.RelationPicker,
[],
);
return (
<SingleEntitySelect
onCreate={handleCreate}
onEntitySelected={handleEntitySelected}
entities={{
entitiesToSelect: people.entitiesToSelect,
selectedEntity: people.selectedEntities[0],
loading: people.loading,
}}
/>
);
}