Add company creation from people table (#1100)

* Add company creation from people table

* Design
This commit is contained in:
Emilien Chauvet
2023-08-09 17:17:35 +02:00
committed by GitHub
parent 3666980ccc
commit 9bd42121d3
3 changed files with 51 additions and 15 deletions

View File

@ -2,10 +2,13 @@ import { useFilteredSearchCompanyQuery } from '@/companies/queries';
import { SingleEntitySelect } from '@/ui/input/relation-picker/components/SingleEntitySelect'; import { SingleEntitySelect } from '@/ui/input/relation-picker/components/SingleEntitySelect';
import { relationPickerSearchFilterScopedState } from '@/ui/input/relation-picker/states/relationPickerSearchFilterScopedState'; import { relationPickerSearchFilterScopedState } from '@/ui/input/relation-picker/states/relationPickerSearchFilterScopedState';
import { EntityForSelect } from '@/ui/input/relation-picker/types/EntityForSelect'; import { EntityForSelect } from '@/ui/input/relation-picker/types/EntityForSelect';
import { Entity } from '@/ui/input/relation-picker/types/EntityTypeForSelect';
import { isCreateModeScopedState } from '@/ui/table/editable-cell/states/isCreateModeScopedState'; import { isCreateModeScopedState } from '@/ui/table/editable-cell/states/isCreateModeScopedState';
import { DoubleTextCellEdit } from '@/ui/table/editable-cell/type/components/DoubleTextCellEdit';
import { TableHotkeyScope } from '@/ui/table/types/TableHotkeyScope'; import { TableHotkeyScope } from '@/ui/table/types/TableHotkeyScope';
import { useSetHotkeyScope } from '@/ui/utilities/hotkey/hooks/useSetHotkeyScope'; import { useSetHotkeyScope } from '@/ui/utilities/hotkey/hooks/useSetHotkeyScope';
import { useRecoilScopedState } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedState'; import { useRecoilScopedState } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedState';
import { useInsertOneCompanyMutation } from '~/generated/graphql';
export type OwnProps = { export type OwnProps = {
companyId: string | null; companyId: string | null;
@ -22,7 +25,11 @@ export function CompanyPickerCell({
createModeEnabled, createModeEnabled,
width, width,
}: OwnProps) { }: OwnProps) {
const [, setIsCreating] = useRecoilScopedState(isCreateModeScopedState); const [isCreating, setIsCreating] = useRecoilScopedState(
isCreateModeScopedState,
);
const [insertCompany] = useInsertOneCompanyMutation();
const [searchFilter] = useRecoilScopedState( const [searchFilter] = useRecoilScopedState(
relationPickerSearchFilterScopedState, relationPickerSearchFilterScopedState,
@ -41,15 +48,43 @@ export function CompanyPickerCell({
onSubmit(entity ?? null); onSubmit(entity ?? null);
} }
function handleCreate() { function handleStartCreation() {
setIsCreating(true); setIsCreating(true);
setHotkeyScope(TableHotkeyScope.CellDoubleTextInput); setHotkeyScope(TableHotkeyScope.CellDoubleTextInput);
} }
return ( async function handleCreate(firstValue: string, secondValue: string) {
const insertCompanyRequest = await insertCompany({
variables: {
data: {
name: firstValue,
domainName: secondValue,
address: '',
},
},
});
const companyCreated = insertCompanyRequest.data?.createOneCompany;
companyCreated &&
onSubmit({
id: companyCreated.id,
name: companyCreated.name,
entityType: Entity.Company,
});
setIsCreating(false);
}
return isCreating ? (
<DoubleTextCellEdit
firstValue={searchFilter}
secondValue={''}
firstValuePlaceholder={'Name'}
secondValuePlaceholder={'Url'}
onSubmit={handleCreate}
/>
) : (
<SingleEntitySelect <SingleEntitySelect
width={width} width={width}
onCreate={createModeEnabled ? handleCreate : undefined} onCreate={createModeEnabled ? handleStartCreation : undefined}
onCancel={onCancel} onCancel={onCancel}
onEntitySelected={handleEntitySelected} onEntitySelected={handleEntitySelected}
entities={{ entities={{

View File

@ -71,22 +71,22 @@ export function SingleEntitySelect<
autoFocus autoFocus
/> />
<DropdownMenuSeparator /> <DropdownMenuSeparator />
{showCreateButton && (
<>
<DropdownMenuItemsContainer hasMaxHeight>
<DropdownMenuItem onClick={onCreate}>
<IconPlus size={theme.icon.size.md} />
Create new
</DropdownMenuItem>
</DropdownMenuItemsContainer>
<DropdownMenuSeparator />
</>
)}
<SingleEntitySelectBase <SingleEntitySelectBase
entities={entities} entities={entities}
onEntitySelected={onEntitySelected} onEntitySelected={onEntitySelected}
onCancel={onCancel} onCancel={onCancel}
/> />
{showCreateButton && (
<>
<DropdownMenuItemsContainer hasMaxHeight>
<DropdownMenuItem onClick={onCreate}>
<IconPlus size={theme.icon.size.md} />
Add New
</DropdownMenuItem>
</DropdownMenuItemsContainer>
<DropdownMenuSeparator />
</>
)}
</DropdownMenu> </DropdownMenu>
); );
} }

View File

@ -55,6 +55,7 @@ export function GenericEditableRelationCellEditMode({ viewField }: OwnProps) {
onSubmit={handleEntitySubmit} onSubmit={handleEntitySubmit}
onCancel={handleCancel} onCancel={handleCancel}
width={viewField.columnSize} width={viewField.columnSize}
createModeEnabled
/> />
); );
} }