Skip to content

Commit

Permalink
more refatoring and code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
ehconitin committed Aug 18, 2024
1 parent 7d5ecfa commit 1679dac
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import styled from '@emotion/styled';
import { useCallback, useContext, useRef } from 'react';

import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { RecordBoardContext } from '@/object-record/record-board/contexts/RecordBoardContext';
import { RecordBoardColumnContext } from '@/object-record/record-board/record-board-column/contexts/RecordBoardColumnContext';
import { useAddNewCard } from '@/object-record/record-board/record-board-column/hooks/useAddNewCard';
import { useAddNewOpportunity } from '@/object-record/record-board/record-board-column/hooks/useAddNewOpportunity';
import { SingleEntitySelect } from '@/object-record/relation-picker/components/SingleEntitySelect';
import { DropdownMenu } from '@/ui/layout/dropdown/components/DropdownMenu';
Expand All @@ -28,7 +30,7 @@ export const RecordBoardColumnDropdownMenu = ({
onClose,
}: RecordBoardColumnDropdownMenuProps) => {
const boardColumnMenuRef = useRef<HTMLDivElement>(null);

const { objectMetadataItem } = useContext(RecordBoardContext);
const closeMenu = useCallback(() => {
onClose();
}, [onClose]);
Expand All @@ -40,8 +42,23 @@ export const RecordBoardColumnDropdownMenu = ({

const { columnDefinition } = useContext(RecordBoardColumnContext);

const { isCreatingCard, handleNewClick, handleCancel, handleEntitySelect } =
useAddNewOpportunity();
const {
isCreatingCard,
handleAddNewOpportunityClick,
handleCancel,
handleEntitySelect,
} = useAddNewOpportunity();
const { handleAddNewCardClick } = useAddNewCard();

const isOpportunity =
objectMetadataItem.nameSingular === CoreObjectNameSingular.Opportunity;

const handleClick = isOpportunity
? handleAddNewOpportunityClick
: () => {
handleAddNewCardClick();
closeMenu();
};

return (
<StyledMenuContainer ref={boardColumnMenuRef}>
Expand Down Expand Up @@ -72,7 +89,7 @@ export const RecordBoardColumnDropdownMenu = ({
/>
))}
<MenuItem
onClick={handleNewClick}
onClick={handleClick}
LeftIcon={IconPlus}
text="Add Record"
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { useTheme } from '@emotion/react';
import styled from '@emotion/styled';
import { useContext } from 'react';
import { IconPlus } from 'twenty-ui';

import { RecordBoardContext } from '@/object-record/record-board/contexts/RecordBoardContext';
import { RecordBoardColumnContext } from '@/object-record/record-board/record-board-column/contexts/RecordBoardColumnContext';
import { useAddNewCard } from '@/object-record/record-board/record-board-column/hooks/useAddNewCard';

const StyledButton = styled.button`
align-items: center;
Expand All @@ -25,21 +23,11 @@ const StyledButton = styled.button`

export const RecordBoardColumnNewButton = () => {
const theme = useTheme();
const { columnDefinition } = useContext(RecordBoardColumnContext);
const { createOneRecord, selectFieldMetadataItem } =
useContext(RecordBoardContext);

const onNewClick = () => {
createOneRecord({
[selectFieldMetadataItem.name]: columnDefinition.value,
position: 'last',
});
};

const { handleAddNewCardClick } = useAddNewCard();
return (
<StyledButton onClick={onNewClick}>
<StyledButton onClick={handleAddNewCardClick}>
<IconPlus size={theme.icon.size.md} />
New agaga
New
</StyledButton>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ const StyledButton = styled.button`

export const RecordBoardColumnNewOpportunityButton = () => {
const theme = useTheme();
const { isCreatingCard, handleNewClick, handleCancel, handleEntitySelect } =
useAddNewOpportunity();
const {
isCreatingCard,
handleAddNewOpportunityClick,
handleCancel,
handleEntitySelect,
} = useAddNewOpportunity();
return (
<>
{isCreatingCard ? (
Expand All @@ -39,7 +43,7 @@ export const RecordBoardColumnNewOpportunityButton = () => {
selectedRelationRecordIds={[]}
/>
) : (
<StyledButton onClick={handleNewClick}>
<StyledButton onClick={handleAddNewOpportunityClick}>
<IconPlus size={theme.icon.size.md} />
New
</StyledButton>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { RecordBoardContext } from '@/object-record/record-board/contexts/RecordBoardContext';
import { RecordBoardColumnContext } from '@/object-record/record-board/record-board-column/contexts/RecordBoardColumnContext';
import { useContext } from 'react';

export const useAddNewCard = () => {
const { columnDefinition } = useContext(RecordBoardColumnContext);
const { createOneRecord, selectFieldMetadataItem } =
useContext(RecordBoardContext);

const handleAddNewCardClick = () => {
createOneRecord({
[selectFieldMetadataItem.name]: columnDefinition.value,
position: 'last',
});
};

return {
handleAddNewCardClick,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const useAddNewOpportunity = () => {
],
);

const handleNewClick = useCallback(() => {
const handleAddNewOpportunityClick = useCallback(() => {
setIsCreatingCard(true);
setHotkeyScopeAndMemorizePreviousScope(
RelationPickerHotkeyScope.RelationPicker,
Expand All @@ -61,7 +61,7 @@ export const useAddNewOpportunity = () => {
return {
isCreatingCard,
handleEntitySelect,
handleNewClick,
handleAddNewOpportunityClick,
handleCancel,
};
};

0 comments on commit 1679dac

Please sign in to comment.