Skip to content

Commit

Permalink
Fix Opportunity picker and relation picker
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesBochet committed Dec 9, 2023
1 parent f2a4a95 commit 75d8c8d
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import { BoardColumnContext } from '@/object-record/record-board/contexts/BoardC
import { useCreateOpportunity } from '@/object-record/record-board/hooks/internal/useCreateOpportunity';
import { SingleEntitySelect } from '@/object-record/relation-picker/components/SingleEntitySelect';
import { useRelationPicker } from '@/object-record/relation-picker/hooks/useRelationPicker';
import { relationPickerSearchFilterScopedState } from '@/object-record/relation-picker/states/relationPickerSearchFilterScopedState';
import { RelationPickerHotkeyScope } from '@/object-record/relation-picker/types/RelationPickerHotkeyScope';
import { useFilteredSearchEntityQuery } from '@/search/hooks/useFilteredSearchEntityQuery';
import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
import { usePreviousHotkeyScope } from '@/ui/utilities/hotkey/hooks/usePreviousHotkeyScope';
import { useRecoilScopedState } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedState';

export const NewOpportunityButton = () => {
const [isCreatingCard, setIsCreatingCard] = useState(false);
Expand Down Expand Up @@ -55,9 +53,7 @@ export const NewOpportunityButton = () => {
setIsCreatingCard(false);
};

const [relationPickerSearchFilter] = useRecoilScopedState(
relationPickerSearchFilterScopedState,
);
const { relationPickerSearchFilter } = useRelationPicker();

// TODO: refactor useFilteredSearchEntityQuery
const { findManyRecordsQuery } = useObjectMetadataItem({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const useCreateOneRecord = <T>({
const createdObject = await apolloClient.mutate({
mutation: createOneRecordMutation,
variables: {
input: { ...input, id: recordId },
input: { id: recordId, ...input },
},
optimisticResponse: {
[`create${capitalize(objectMetadataItem.nameSingular)}`]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import { FieldDefinition } from '@/object-record/field/types/FieldDefinition';
import { FieldRelationMetadata } from '@/object-record/field/types/FieldMetadata';
import { SingleEntitySelect } from '@/object-record/relation-picker/components/SingleEntitySelect';
import { useRelationPicker } from '@/object-record/relation-picker/hooks/useRelationPicker';
import { relationPickerSearchFilterScopedState } from '@/object-record/relation-picker/states/relationPickerSearchFilterScopedState';
import { EntityForSelect } from '@/object-record/relation-picker/types/EntityForSelect';
import { useFilteredSearchEntityQuery } from '@/search/hooks/useFilteredSearchEntityQuery';
import { IconForbid } from '@/ui/display/icon';
import { useRecoilScopedState } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedState';

export type RelationPickerProps = {
recordId?: string;
Expand All @@ -32,8 +30,8 @@ export const RelationPicker = ({
initialSearchFilter,
fieldDefinition,
}: RelationPickerProps) => {
const [relationPickerSearchFilter, setRelationPickerSearchFilter] =
useRecoilScopedState(relationPickerSearchFilterScopedState);
const { relationPickerSearchFilter, setRelationPickerSearchFilter } =
useRelationPicker();

useEffect(() => {
setRelationPickerSearchFilter(initialSearchFilter ?? '');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import { expect } from '@storybook/jest';
import { Meta, StoryObj } from '@storybook/react';
import { userEvent, within } from '@storybook/testing-library';

import { useRelationPicker } from '@/object-record/relation-picker/hooks/useRelationPicker';
import { IconUserCircle } from '@/ui/display/icon';
import { useRecoilScopedValue } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedValue';
import { ComponentDecorator } from '~/testing/decorators/ComponentDecorator';
import { ComponentWithRecoilScopeDecorator } from '~/testing/decorators/ComponentWithRecoilScopeDecorator';
import { mockedPeopleData } from '~/testing/mock-data/people';
import { sleep } from '~/testing/sleep';

import { relationPickerSearchFilterScopedState } from '../../states/relationPickerSearchFilterScopedState';
import { EntityForSelect } from '../../types/EntityForSelect';
import { SingleEntitySelect } from '../SingleEntitySelect';

Expand Down Expand Up @@ -44,9 +43,7 @@ const meta: Meta<typeof SingleEntitySelect> = {
width,
}) => {
// eslint-disable-next-line react-hooks/rules-of-hooks
const relationPickerSearchFilter = useRecoilScopedValue(
relationPickerSearchFilterScopedState,
);
const { relationPickerSearchFilter } = useRelationPicker();

return (
<SingleEntitySelect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ export const useRelationPickerScopedStates = (args?: {
relationPickerScopedId,
);

const { identifiersMapperState } = getRelationPickerScopedStates({
relationPickerScopeId: scopeId,
});

const { searchQueryState } = getRelationPickerScopedStates({
const {
identifiersMapperState,
relationPickerSearchFilterState,
relationPickerPreselectedIdState,
searchQueryState,
} = getRelationPickerScopedStates({
relationPickerScopeId: scopeId,
});

return {
scopeId,
identifiersMapperState,
relationPickerSearchFilterState,
relationPickerPreselectedIdState,
searchQueryState,
};
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import scrollIntoView from 'scroll-into-view';
import { Key } from 'ts-key-enum';

import { useRelationPicker } from '@/object-record/relation-picker/hooks/useRelationPicker';
import { useScopedHotkeys } from '@/ui/utilities/hotkey/hooks/useScopedHotkeys';
import { useRecoilScopedState } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedState';

import { CreateButtonId } from '../constants';
import { RelationPickerRecoilScopeContext } from '../states/recoil-scope-contexts/RelationPickerRecoilScopeContext';
import { relationPickerPreselectedIdScopedState } from '../states/relationPickerPreselectedIdScopedState';
import { RelationPickerHotkeyScope } from '../types/RelationPickerHotkeyScope';
import { getPreselectedIdIndex } from '../utils/getPreselectedIdIndex';

Expand All @@ -17,15 +15,12 @@ export const useEntitySelectScroll = ({
selectableOptionIds: string[];
containerRef: React.RefObject<HTMLDivElement>;
}) => {
const [relationPickerPreselectedId, setRelationPickerPreselectedId] =
useRecoilScopedState(
relationPickerPreselectedIdScopedState,
RelationPickerRecoilScopeContext,
);
const { relationPickerPreselectedId, setRelationPickerPreselectedId } =
useRelationPicker();

const preselectedIdIndex = getPreselectedIdIndex(
selectableOptionIds,
relationPickerPreselectedId,
relationPickerPreselectedId ?? '',
);

const resetScroll = () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import debounce from 'lodash.debounce';

import { relationPickerPreselectedIdScopedState } from '@/object-record/relation-picker/states/relationPickerPreselectedIdScopedState';
import { relationPickerSearchFilterScopedState } from '@/object-record/relation-picker/states/relationPickerSearchFilterScopedState';
import { useRecoilScopedState } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedState';
import { useRelationPicker } from '@/object-record/relation-picker/hooks/useRelationPicker';

export const useEntitySelectSearch = () => {
const [, setRelationPickerPreselectedId] = useRecoilScopedState(
relationPickerPreselectedIdScopedState,
);

const [relationPickerSearchFilter, setRelationPickerSearchFilter] =
useRecoilScopedState(relationPickerSearchFilterScopedState);
const {
setRelationPickerPreselectedId,
relationPickerSearchFilter,
setRelationPickerSearchFilter,
} = useRelationPicker();

const debouncedSetSearchFilter = debounce(
setRelationPickerSearchFilter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,36 @@ export const useRelationPicker = (props?: useRelationPickeProps) => {
props?.relationPickerScopeId,
);

const { identifiersMapperState, searchQueryState } =
useRelationPickerScopedStates({
relationPickerScopedId: scopeId,
});
const {
identifiersMapperState,
searchQueryState,
relationPickerSearchFilterState,
relationPickerPreselectedIdState,
} = useRelationPickerScopedStates({
relationPickerScopedId: scopeId,
});

const [identifiersMapper, setIdentifiersMapper] = useRecoilState(
identifiersMapperState,
);

const [searchQuery, setSearchQuery] = useRecoilState(searchQueryState);

const [relationPickerSearchFilter, setRelationPickerSearchFilter] =
useRecoilState(relationPickerSearchFilterState);

const [relationPickerPreselectedId, setRelationPickerPreselectedId] =
useRecoilState(relationPickerPreselectedIdState);

return {
scopeId,
identifiersMapper,
setIdentifiersMapper,
searchQuery,
setSearchQuery,
relationPickerSearchFilter,
setRelationPickerSearchFilter,
relationPickerPreselectedId,
setRelationPickerPreselectedId,
};
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { atomFamily } from 'recoil';
import { createScopedState } from '@/ui/utilities/recoil-scope/utils/createScopedState';

export const relationPickerPreselectedIdScopedState = atomFamily<
string,
string
export const relationPickerPreselectedIdScopedState = createScopedState<
string | undefined
>({
key: 'relationPickerPreselectedIdScopedState',
default: (param) => param,
defaultValue: undefined,
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { atomFamily } from 'recoil';
import { createScopedState } from '@/ui/utilities/recoil-scope/utils/createScopedState';

export const relationPickerSearchFilterScopedState = atomFamily<string, string>(
{
key: 'relationPickerSearchFilterScopedState',
default: '',
},
);
export const relationPickerSearchFilterScopedState = createScopedState<string>({
key: 'relationPickerSearchFilterScopedState',
defaultValue: '',
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { identifiersMapperScopedState } from '@/object-record/relation-picker/states/identifiersMapperScopedState';
import { relationPickerPreselectedIdScopedState } from '@/object-record/relation-picker/states/relationPickerPreselectedIdScopedState';
import { relationPickerSearchFilterScopedState } from '@/object-record/relation-picker/states/relationPickerSearchFilterScopedState';
import { searchQueryScopedState } from '@/object-record/relation-picker/states/searchQueryScopedState';
import { getScopedState } from '@/ui/utilities/recoil-scope/utils/getScopedState';

Expand All @@ -17,8 +19,20 @@ export const getRelationPickerScopedStates = ({
relationPickerScopeId,
);

const relationPickerPreselectedIdState = getScopedState(
relationPickerPreselectedIdScopedState,
relationPickerScopeId,
);

const relationPickerSearchFilterState = getScopedState(
relationPickerSearchFilterScopedState,
relationPickerScopeId,
);

return {
identifiersMapperState,
relationPickerSearchFilterState,
relationPickerPreselectedIdState,
searchQueryState,
};
};

0 comments on commit 75d8c8d

Please sign in to comment.