[PersonShow] use fieldDefinition for editable fields (#1178)
* [PersonShow] use fieldDefinition for editable fields * remove unused files * fix company chip display field
This commit is contained in:
@ -1,73 +0,0 @@
|
||||
import { useFilteredSearchCompanyQuery } from '@/companies/queries';
|
||||
import { SingleEntitySelect } from '@/ui/input/relation-picker/components/SingleEntitySelect';
|
||||
import { relationPickerSearchFilterScopedState } from '@/ui/input/relation-picker/states/relationPickerSearchFilterScopedState';
|
||||
import { EntityForSelect } from '@/ui/input/relation-picker/types/EntityForSelect';
|
||||
import { useEditableCell } from '@/ui/table/editable-cell/hooks/useEditableCell';
|
||||
import { isCreateModeScopedState } from '@/ui/table/editable-cell/states/isCreateModeScopedState';
|
||||
import { TableHotkeyScope } from '@/ui/table/types/TableHotkeyScope';
|
||||
import { useSetHotkeyScope } from '@/ui/utilities/hotkey/hooks/useSetHotkeyScope';
|
||||
import { useRecoilScopedState } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedState';
|
||||
import {
|
||||
Company,
|
||||
Person,
|
||||
useUpdateOnePersonMutation,
|
||||
} from '~/generated/graphql';
|
||||
|
||||
export type OwnProps = {
|
||||
people: Pick<Person, 'id'> & { company?: Pick<Company, 'id'> | null };
|
||||
};
|
||||
|
||||
export function PeopleCompanyPicker({ people }: OwnProps) {
|
||||
const [, setIsCreating] = useRecoilScopedState(isCreateModeScopedState);
|
||||
|
||||
const [searchFilter] = useRecoilScopedState(
|
||||
relationPickerSearchFilterScopedState,
|
||||
);
|
||||
const [updatePerson] = useUpdateOnePersonMutation();
|
||||
|
||||
const { closeEditableCell } = useEditableCell();
|
||||
|
||||
const addToScopeStack = useSetHotkeyScope();
|
||||
|
||||
const companies = useFilteredSearchCompanyQuery({
|
||||
searchFilter,
|
||||
selectedIds: people.company?.id ? [people.company.id] : [],
|
||||
});
|
||||
|
||||
async function handleEntitySelected(
|
||||
entity: EntityForSelect | null | undefined,
|
||||
) {
|
||||
if (entity) {
|
||||
await updatePerson({
|
||||
variables: {
|
||||
where: {
|
||||
id: people.id,
|
||||
},
|
||||
data: {
|
||||
company: { connect: { id: entity.id } },
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
closeEditableCell();
|
||||
}
|
||||
|
||||
function handleCreate() {
|
||||
setIsCreating(true);
|
||||
addToScopeStack(TableHotkeyScope.CellDoubleTextInput);
|
||||
}
|
||||
|
||||
return (
|
||||
<SingleEntitySelect
|
||||
onCreate={handleCreate}
|
||||
onCancel={() => closeEditableCell()}
|
||||
onEntitySelected={handleEntitySelected}
|
||||
entities={{
|
||||
entitiesToSelect: companies.entitiesToSelect,
|
||||
selectedEntity: companies.selectedEntities[0],
|
||||
loading: companies.loading,
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@ -1,104 +0,0 @@
|
||||
import {
|
||||
IconCalendar,
|
||||
IconMail,
|
||||
IconMap,
|
||||
IconPhone,
|
||||
} from '@tabler/icons-react';
|
||||
|
||||
import { PropertyBox } from '@/ui/editable-field/property-box/components/PropertyBox';
|
||||
import { DateEditableField } from '@/ui/editable-field/variants/components/DateEditableField';
|
||||
import { PhoneEditableField } from '@/ui/editable-field/variants/components/PhoneEditableField';
|
||||
import { TextEditableField } from '@/ui/editable-field/variants/components/TextEditableField';
|
||||
import {
|
||||
Company,
|
||||
Person,
|
||||
useUpdateOnePersonMutation,
|
||||
} from '~/generated/graphql';
|
||||
|
||||
import { PeopleCompanyEditableField } from '../editable-field/components/PeopleCompanyEditableField';
|
||||
|
||||
type OwnProps = {
|
||||
person: Pick<
|
||||
Person,
|
||||
'id' | 'city' | 'email' | 'displayName' | 'phone' | 'createdAt'
|
||||
> & {
|
||||
company?: Pick<Company, 'id' | 'name' | 'domainName'> | null;
|
||||
};
|
||||
};
|
||||
|
||||
export function PersonPropertyBox({ person }: OwnProps) {
|
||||
const [updatePerson] = useUpdateOnePersonMutation();
|
||||
|
||||
return (
|
||||
<PropertyBox extraPadding={true}>
|
||||
<TextEditableField
|
||||
value={person.email}
|
||||
icon={<IconMail />}
|
||||
placeholder={'Email'}
|
||||
onSubmit={(newEmail) => {
|
||||
updatePerson({
|
||||
variables: {
|
||||
where: {
|
||||
id: person.id,
|
||||
},
|
||||
data: {
|
||||
email: newEmail,
|
||||
},
|
||||
},
|
||||
});
|
||||
}}
|
||||
/>
|
||||
<PhoneEditableField
|
||||
value={person.phone}
|
||||
icon={<IconPhone />}
|
||||
placeholder={'Phone'}
|
||||
onSubmit={(newPhone) => {
|
||||
updatePerson({
|
||||
variables: {
|
||||
where: {
|
||||
id: person.id,
|
||||
},
|
||||
data: {
|
||||
phone: newPhone,
|
||||
},
|
||||
},
|
||||
});
|
||||
}}
|
||||
/>
|
||||
<DateEditableField
|
||||
value={person.createdAt}
|
||||
icon={<IconCalendar />}
|
||||
onSubmit={(newDate) => {
|
||||
updatePerson({
|
||||
variables: {
|
||||
where: {
|
||||
id: person.id,
|
||||
},
|
||||
data: {
|
||||
createdAt: newDate,
|
||||
},
|
||||
},
|
||||
});
|
||||
}}
|
||||
/>
|
||||
<PeopleCompanyEditableField people={person} />
|
||||
<TextEditableField
|
||||
value={person.city}
|
||||
icon={<IconMap />}
|
||||
placeholder={'City'}
|
||||
onSubmit={(newCity) => {
|
||||
updatePerson({
|
||||
variables: {
|
||||
where: {
|
||||
id: person.id,
|
||||
},
|
||||
data: {
|
||||
city: newCity,
|
||||
},
|
||||
},
|
||||
});
|
||||
}}
|
||||
/>
|
||||
</PropertyBox>
|
||||
);
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
import { BrowserRouter } from 'react-router-dom';
|
||||
import type { Meta, StoryObj } from '@storybook/react';
|
||||
|
||||
import { mockedPeopleData } from '~/testing/mock-data/people';
|
||||
|
||||
import { PeopleCompanyEditableField } from '../../editable-field/components/PeopleCompanyEditableField';
|
||||
|
||||
const meta: Meta<typeof PeopleCompanyEditableField> = {
|
||||
title: 'Modules/People/EditableFields/PeopleCompanyEditableField',
|
||||
component: PeopleCompanyEditableField,
|
||||
};
|
||||
|
||||
export default meta;
|
||||
type Story = StoryObj<typeof PeopleCompanyEditableField>;
|
||||
|
||||
export const Default: Story = {
|
||||
render: () => (
|
||||
<BrowserRouter>
|
||||
<PeopleCompanyEditableField people={mockedPeopleData[0]} />
|
||||
</BrowserRouter>
|
||||
),
|
||||
};
|
||||
@ -1,49 +0,0 @@
|
||||
import { IconBuildingSkyscraper } from '@tabler/icons-react';
|
||||
|
||||
import { CompanyChip } from '@/companies/components/CompanyChip';
|
||||
import { EditableField } from '@/ui/editable-field/components/EditableField';
|
||||
import { FieldContext } from '@/ui/editable-field/states/FieldContext';
|
||||
import { RelationPickerHotkeyScope } from '@/ui/input/relation-picker/types/RelationPickerHotkeyScope';
|
||||
import { RecoilScope } from '@/ui/utilities/recoil-scope/components/RecoilScope';
|
||||
import { Company, Person } from '~/generated/graphql';
|
||||
import { getLogoUrlFromDomainName } from '~/utils';
|
||||
|
||||
import { PeopleCompanyEditableFieldEditMode } from './PeopleCompanyEditableFieldEditMode';
|
||||
|
||||
export type OwnProps = {
|
||||
people: Pick<Person, 'id'> & {
|
||||
company?: Pick<Company, 'id' | 'name' | 'domainName'> | null;
|
||||
};
|
||||
};
|
||||
|
||||
export function PeopleCompanyEditableField({ people }: OwnProps) {
|
||||
return (
|
||||
<RecoilScope SpecificContext={FieldContext}>
|
||||
<RecoilScope>
|
||||
<EditableField
|
||||
useEditButton
|
||||
customEditHotkeyScope={{
|
||||
scope: RelationPickerHotkeyScope.RelationPicker,
|
||||
}}
|
||||
iconLabel={<IconBuildingSkyscraper />}
|
||||
editModeContent={
|
||||
<PeopleCompanyEditableFieldEditMode people={people} />
|
||||
}
|
||||
displayModeContent={
|
||||
people.company ? (
|
||||
<CompanyChip
|
||||
id={people.company.id}
|
||||
name={people.company.name}
|
||||
pictureUrl={getLogoUrlFromDomainName(people.company.domainName)}
|
||||
/>
|
||||
) : (
|
||||
<></>
|
||||
)
|
||||
}
|
||||
isDisplayModeContentEmpty={!people.company}
|
||||
isDisplayModeFixHeight
|
||||
/>
|
||||
</RecoilScope>
|
||||
</RecoilScope>
|
||||
);
|
||||
}
|
||||
@ -1,74 +0,0 @@
|
||||
import styled from '@emotion/styled';
|
||||
|
||||
import { useFilteredSearchCompanyQuery } from '@/companies/queries';
|
||||
import { useEditableField } from '@/ui/editable-field/hooks/useEditableField';
|
||||
import { SingleEntitySelect } from '@/ui/input/relation-picker/components/SingleEntitySelect';
|
||||
import { relationPickerSearchFilterScopedState } from '@/ui/input/relation-picker/states/relationPickerSearchFilterScopedState';
|
||||
import { EntityForSelect } from '@/ui/input/relation-picker/types/EntityForSelect';
|
||||
import { useRecoilScopedState } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedState';
|
||||
import {
|
||||
Company,
|
||||
Person,
|
||||
useUpdateOnePersonMutation,
|
||||
} from '~/generated/graphql';
|
||||
|
||||
export type OwnProps = {
|
||||
people: Pick<Person, 'id'> & { company?: Pick<Company, 'id'> | null };
|
||||
};
|
||||
|
||||
const StyledContainer = styled.div`
|
||||
left: 0px;
|
||||
position: absolute;
|
||||
top: -8px;
|
||||
`;
|
||||
|
||||
export function PeopleCompanyEditableFieldEditMode({ people }: OwnProps) {
|
||||
const { closeEditableField } = useEditableField();
|
||||
|
||||
const [searchFilter] = useRecoilScopedState(
|
||||
relationPickerSearchFilterScopedState,
|
||||
);
|
||||
const [updatePerson] = useUpdateOnePersonMutation();
|
||||
|
||||
const companies = useFilteredSearchCompanyQuery({
|
||||
searchFilter,
|
||||
selectedIds: people.company?.id ? [people.company.id] : [],
|
||||
});
|
||||
|
||||
async function handleEntitySelected(
|
||||
entity: EntityForSelect | null | undefined,
|
||||
) {
|
||||
if (entity) {
|
||||
await updatePerson({
|
||||
variables: {
|
||||
where: {
|
||||
id: people.id,
|
||||
},
|
||||
data: {
|
||||
company: { connect: { id: entity.id } },
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
closeEditableField();
|
||||
}
|
||||
|
||||
function handleCancel() {
|
||||
closeEditableField();
|
||||
}
|
||||
|
||||
return (
|
||||
<StyledContainer>
|
||||
<SingleEntitySelect
|
||||
onEntitySelected={handleEntitySelected}
|
||||
entities={{
|
||||
entitiesToSelect: companies.entitiesToSelect,
|
||||
selectedEntity: companies.selectedEntities[0],
|
||||
loading: companies.loading,
|
||||
}}
|
||||
onCancel={handleCancel}
|
||||
/>
|
||||
</StyledContainer>
|
||||
);
|
||||
}
|
||||
@ -1,5 +1,7 @@
|
||||
import { gql } from '@apollo/client';
|
||||
import { useSetRecoilState } from 'recoil';
|
||||
|
||||
import { genericEntitiesFamilyState } from '@/ui/editable-field/states/genericEntitiesFamilyState';
|
||||
import { useGetPersonQuery } from '~/generated/graphql';
|
||||
|
||||
export const GET_PERSON = gql`
|
||||
@ -37,5 +39,13 @@ export const GET_PERSON = gql`
|
||||
`;
|
||||
|
||||
export function usePersonQuery(id: string) {
|
||||
return useGetPersonQuery({ variables: { id } });
|
||||
const updatePersonShowPage = useSetRecoilState(
|
||||
genericEntitiesFamilyState(id),
|
||||
);
|
||||
return useGetPersonQuery({
|
||||
variables: { id },
|
||||
onCompleted: (data) => {
|
||||
updatePersonShowPage(data?.findUniquePerson);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user