Feat: On Company Show, I can create a person and add it to the company (#1256)

* On Company Show, I can create a person and add it to the company

Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: RubensRafael <rubensrafael2@live.com>

* Add minor refactors

Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: RubensRafael <rubensrafael2@live.com>

---------

Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: RubensRafael <rubensrafael2@live.com>
This commit is contained in:
gitstart-twenty 2023-08-25 22:21:57 +03:00 committed by GitHub
parent 9027a7dd1e
commit c3d4767ac4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 73 additions and 7 deletions

View File

@ -3,6 +3,7 @@ import { getOperationName } from '@apollo/client/utilities';
import styled from '@emotion/styled'; import styled from '@emotion/styled';
import { flip, offset, useFloating } from '@floating-ui/react'; import { flip, offset, useFloating } from '@floating-ui/react';
import { IconPlus } from '@tabler/icons-react'; import { IconPlus } from '@tabler/icons-react';
import { v4 } from 'uuid';
import { import {
PeoplePicker, PeoplePicker,
@ -12,14 +13,32 @@ import { GET_PEOPLE } from '@/people/graphql/queries/getPeople';
import { ButtonSize } from '@/ui/button/components/Button'; import { ButtonSize } from '@/ui/button/components/Button';
import { IconButton } from '@/ui/button/components/IconButton'; import { IconButton } from '@/ui/button/components/IconButton';
import { RelationPickerHotkeyScope } from '@/ui/input/relation-picker/types/RelationPickerHotkeyScope'; import { RelationPickerHotkeyScope } from '@/ui/input/relation-picker/types/RelationPickerHotkeyScope';
import { TextInput } from '@/ui/input/text/components/TextInput';
import { usePreviousHotkeyScope } from '@/ui/utilities/hotkey/hooks/usePreviousHotkeyScope'; import { usePreviousHotkeyScope } from '@/ui/utilities/hotkey/hooks/usePreviousHotkeyScope';
import { RecoilScope } from '@/ui/utilities/recoil-scope/components/RecoilScope'; import { RecoilScope } from '@/ui/utilities/recoil-scope/components/RecoilScope';
import { useUpdateOnePersonMutation } from '~/generated/graphql'; import {
useInsertOnePersonMutation,
useUpdateOnePersonMutation,
} from '~/generated/graphql';
const StyledContainer = styled.div` const StyledContainer = styled.div`
position: relative; position: relative;
`; `;
const StyledInputContainer = styled.div`
background-color: ${({ theme }) => theme.background.tertiary};
box-shadow: ${({ theme }) => theme.boxShadow.strong};
display: flex;
gap: ${({ theme }) => theme.spacing(0.5)};
width: ${({ theme }) => theme.spacing(62.5)};
& * input,
* div {
background-color: ${({ theme }) => theme.background.primary};
}
`;
const defaultUsername = { firstName: '', lastName: '' };
export function AddPersonToCompany({ export function AddPersonToCompany({
companyId, companyId,
peopleIds, peopleIds,
@ -28,7 +47,10 @@ export function AddPersonToCompany({
peopleIds?: string[]; peopleIds?: string[];
}) { }) {
const [isDropdownOpen, setIsDropdownOpen] = useState(false); const [isDropdownOpen, setIsDropdownOpen] = useState(false);
const [isCreationDropdownOpen, setIsCreationDropdownOpen] = useState(false);
const [username, setUsername] = useState(defaultUsername);
const [updatePerson] = useUpdateOnePersonMutation(); const [updatePerson] = useUpdateOnePersonMutation();
const [insertOnePerson] = useInsertOnePersonMutation();
const { refs, floatingStyles } = useFloating({ const { refs, floatingStyles } = useFloating({
placement: 'right-start', placement: 'right-start',
middleware: [flip(), offset({ mainAxis: -20, crossAxis: 25 })], middleware: [flip(), offset({ mainAxis: -20, crossAxis: 25 })],
@ -74,6 +96,29 @@ export function AddPersonToCompany({
} }
} }
function handleUsernameChange(type: 'firstName' | 'lastName') {
return (name: string): void =>
setUsername((prevUserName) => ({ ...prevUserName, [type]: name }));
}
async function handleInputKeyDown(e: React.KeyboardEvent<HTMLInputElement>) {
if (e.key !== 'Enter' || (!username.firstName && !username.lastName))
return;
const newPersonId = v4();
await insertOnePerson({
variables: {
data: {
company: { connect: { id: companyId } },
id: newPersonId,
...username,
},
},
refetchQueries: [getOperationName(GET_PEOPLE) ?? ''],
});
setIsCreationDropdownOpen(false);
setUsername(defaultUsername);
}
return ( return (
<RecoilScope> <RecoilScope>
<StyledContainer> <StyledContainer>
@ -88,12 +133,30 @@ export function AddPersonToCompany({
{isDropdownOpen && ( {isDropdownOpen && (
<div ref={refs.setFloating} style={floatingStyles}> <div ref={refs.setFloating} style={floatingStyles}>
<PeoplePicker {isCreationDropdownOpen ? (
personId={''} <StyledInputContainer>
onSubmit={handlePersonSelected(companyId)} <TextInput
onCancel={handleClosePicker} onKeyDown={handleInputKeyDown}
excludePersonIds={peopleIds} value={username.firstName}
/> onChange={handleUsernameChange('firstName')}
placeholder="First Name"
/>
<TextInput
onKeyDown={handleInputKeyDown}
value={username.lastName}
onChange={handleUsernameChange('lastName')}
placeholder="Last Name"
/>
</StyledInputContainer>
) : (
<PeoplePicker
personId={''}
onSubmit={handlePersonSelected(companyId)}
onCancel={handleClosePicker}
onCreate={() => setIsCreationDropdownOpen(true)}
excludePersonIds={peopleIds}
/>
)}
</div> </div>
)} )}
</StyledContainer> </StyledContainer>

View File

@ -10,6 +10,7 @@ export type OwnProps = {
personId: string | null; personId: string | null;
onSubmit: (newPersonId: PersonForSelect | null) => void; onSubmit: (newPersonId: PersonForSelect | null) => void;
onCancel?: () => void; onCancel?: () => void;
onCreate?: () => void;
excludePersonIds?: string[]; excludePersonIds?: string[];
}; };
@ -21,6 +22,7 @@ export function PeoplePicker({
personId, personId,
onSubmit, onSubmit,
onCancel, onCancel,
onCreate,
excludePersonIds, excludePersonIds,
}: OwnProps) { }: OwnProps) {
const [searchFilter] = useRecoilScopedState( const [searchFilter] = useRecoilScopedState(
@ -53,6 +55,7 @@ export function PeoplePicker({
<SingleEntitySelect <SingleEntitySelect
onEntitySelected={handleEntitySelected} onEntitySelected={handleEntitySelected}
onCancel={onCancel} onCancel={onCancel}
onCreate={onCreate}
entities={{ entities={{
loading: people.loading, loading: people.loading,
entitiesToSelect: people.entitiesToSelect, entitiesToSelect: people.entitiesToSelect,