Update favorites query and state to work with new backend (#2520)
* wip * wip * adding favorite works in the database * favorites are showing in the left drawer * update favoorite NavItem link * wip * adding favorite works * everything seems to work * fix delete bug * fix update favorite position * update Favorite type * Fix --------- Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
@ -3,6 +3,7 @@ import { DateTime } from 'luxon';
|
||||
import { useRecoilState } from 'recoil';
|
||||
|
||||
import { ActivityTargetableEntityType } from '@/activities/types/ActivityTargetableEntity';
|
||||
import { useFavorites } from '@/favorites/hooks/useFavorites';
|
||||
import { useFindOneObjectMetadataItem } from '@/object-metadata/hooks/useFindOneObjectMetadataItem';
|
||||
import { formatFieldMetadataItemAsColumnDefinition } from '@/object-metadata/utils/formatFieldMetadataItemAsColumnDefinition';
|
||||
import { IconBuildingSkyscraper } from '@/ui/display/icon';
|
||||
@ -24,6 +25,7 @@ import { PropertyBox } from '@/ui/object/record-inline-cell/property-box/compone
|
||||
import { InlineCellHotkeyScope } from '@/ui/object/record-inline-cell/types/InlineCellHotkeyScope';
|
||||
import { PageTitle } from '@/ui/utilities/page-title/PageTitle';
|
||||
import { RecoilScope } from '@/ui/utilities/recoil-scope/components/RecoilScope';
|
||||
import { getLogoUrlFromDomainName } from '~/utils';
|
||||
|
||||
import { useFindOneObjectRecord } from '../hooks/useFindOneObjectRecord';
|
||||
import { useUpdateOneObjectRecord } from '../hooks/useUpdateOneObjectRecord';
|
||||
@ -34,6 +36,8 @@ export const RecordShowPage = () => {
|
||||
objectMetadataId: string;
|
||||
}>();
|
||||
|
||||
const { favorites, createFavorite, deleteFavorite } = useFavorites();
|
||||
|
||||
const { icons } = useLazyLoadIcons();
|
||||
|
||||
const { foundObjectMetadataItem } = useFindOneObjectMetadataItem({
|
||||
@ -76,8 +80,38 @@ export const RecordShowPage = () => {
|
||||
return [updateEntity, { loading: false }];
|
||||
};
|
||||
|
||||
const isFavorite = objectNameSingular
|
||||
? favorites.some((favorite) => favorite.recordId === object?.id)
|
||||
: false;
|
||||
|
||||
const handleFavoriteButtonClick = async () => {
|
||||
//
|
||||
if (!objectNameSingular || !object) return;
|
||||
if (isFavorite) deleteFavorite(object?.id);
|
||||
else {
|
||||
const additionalData =
|
||||
objectNameSingular === 'peopleV2'
|
||||
? {
|
||||
labelIdentifier: object.firstName + ' ' + object.lastName,
|
||||
avatarUrl: object.avatarUrl,
|
||||
avatarType: 'rounded',
|
||||
link: `/object/personV2/${object.id}`,
|
||||
recordId: object.id,
|
||||
}
|
||||
: objectNameSingular === 'companyV2'
|
||||
? {
|
||||
labelIdentifier: object.name,
|
||||
avatarUrl: getLogoUrlFromDomainName(object.domainName ?? ''),
|
||||
avatarType: 'squared',
|
||||
link: `/object/companyV2/${object.id}`,
|
||||
recordId: object.id,
|
||||
}
|
||||
: {};
|
||||
createFavorite(
|
||||
objectNameSingular.replace('V2', ''),
|
||||
object.id,
|
||||
additionalData,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
if (!object) return <></>;
|
||||
@ -91,7 +125,7 @@ export const RecordShowPage = () => {
|
||||
Icon={IconBuildingSkyscraper}
|
||||
>
|
||||
<PageFavoriteButton
|
||||
isFavorite={false}
|
||||
isFavorite={isFavorite}
|
||||
onClick={handleFavoriteButtonClick}
|
||||
/>
|
||||
<ShowPageAddButton
|
||||
|
||||
Reference in New Issue
Block a user