Fix favorites add/remove from table context menu (#2571)
* Fix favorites add/remove from table context menu * Fixed console.log --------- Co-authored-by: Charles Bochet <charlesBochet@users.noreply.github.com>
This commit is contained in:
@ -1,10 +1,16 @@
|
||||
import { useState } from 'react';
|
||||
import { useApolloClient } from '@apollo/client';
|
||||
import { OnDragEndResponder } from '@hello-pangea/dnd';
|
||||
import { useRecoilCallback, useRecoilState, useRecoilValue } from 'recoil';
|
||||
|
||||
import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState';
|
||||
import { Favorite } from '@/favorites/types/Favorite';
|
||||
import { mapFavorites } from '@/favorites/utils/mapFavorites';
|
||||
import { useFindOneObjectMetadataItem } from '@/object-metadata/hooks/useFindOneObjectMetadataItem';
|
||||
import { useFindManyObjectRecords } from '@/object-record/hooks/useFindManyObjectRecords';
|
||||
import { PaginatedObjectTypeResults } from '@/object-record/types/PaginatedObjectTypeResults';
|
||||
import { Company } from '~/generated/graphql';
|
||||
import { isDeeplyEqual } from '~/utils/isDeeplyEqual';
|
||||
|
||||
import { favoritesState } from '../states/favoritesState';
|
||||
|
||||
@ -29,11 +35,81 @@ export const useFavorites = ({
|
||||
|
||||
const apolloClient = useApolloClient();
|
||||
|
||||
const [allCompanies, setAllCompanies] = useState<
|
||||
Record<string, { name: string; domainName?: string }>
|
||||
>({});
|
||||
const [allPeople, setAllPeople] = useState<
|
||||
Record<string, { firstName: string; lastName: string; avatarUrl?: string }>
|
||||
>({});
|
||||
|
||||
// This is only temporary and will be refactored once we have main identifiers
|
||||
const { loading: companiesLoading } = useFindManyObjectRecords({
|
||||
objectNamePlural: 'companiesV2',
|
||||
onCompleted: async (
|
||||
data: PaginatedObjectTypeResults<Required<Company>>,
|
||||
) => {
|
||||
setAllCompanies(
|
||||
data.edges.reduce(
|
||||
(acc, { node: company }) => ({
|
||||
...acc,
|
||||
[company.id]: {
|
||||
name: company.name,
|
||||
domainName: company.domainName,
|
||||
},
|
||||
}),
|
||||
{},
|
||||
),
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
const { loading: peopleLoading } = useFindManyObjectRecords({
|
||||
objectNamePlural: 'peopleV2',
|
||||
onCompleted: async (data) => {
|
||||
setAllPeople(
|
||||
data.edges.reduce(
|
||||
(acc, { node: person }) => ({
|
||||
...acc,
|
||||
[person.id]: {
|
||||
firstName: person.firstName,
|
||||
lastName: person.lastName,
|
||||
avatarUrl: person.avatarUrl,
|
||||
},
|
||||
}),
|
||||
{},
|
||||
),
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
useFindManyObjectRecords({
|
||||
skip: companiesLoading || peopleLoading,
|
||||
objectNamePlural: 'favoritesV2',
|
||||
onCompleted: useRecoilCallback(
|
||||
({ snapshot, set }) =>
|
||||
async (data: PaginatedObjectTypeResults<Required<Favorite>>) => {
|
||||
const favorites = snapshot.getLoadable(favoritesState).getValue();
|
||||
|
||||
const queriedFavorites = mapFavorites(
|
||||
data.edges.map((edge) => edge.node),
|
||||
{
|
||||
...allCompanies,
|
||||
...allPeople,
|
||||
},
|
||||
);
|
||||
|
||||
if (!isDeeplyEqual(favorites, queriedFavorites)) {
|
||||
set(favoritesState, queriedFavorites);
|
||||
}
|
||||
},
|
||||
[allCompanies, allPeople],
|
||||
),
|
||||
});
|
||||
|
||||
const createFavorite = useRecoilCallback(
|
||||
({ snapshot, set }) =>
|
||||
async (favoriteTargetObjectId: string, additionalData?: any) => {
|
||||
const favoritesStateFromSnapshot = snapshot.getLoadable(favoritesState);
|
||||
const favorites = favoritesStateFromSnapshot.getValue();
|
||||
const favorites = snapshot.getLoadable(favoritesState).getValue();
|
||||
|
||||
const targetObjectName =
|
||||
favoriteTargetObjectMetadataItem?.nameSingular.replace('V2', '') ??
|
||||
@ -54,14 +130,21 @@ export const useFavorites = ({
|
||||
|
||||
const newFavorite = {
|
||||
...additionalData,
|
||||
...createdFavorite[targetObjectName],
|
||||
...createdFavorite,
|
||||
};
|
||||
|
||||
const newFavoritesMapped = mapFavorites([newFavorite], {
|
||||
...allCompanies,
|
||||
...allPeople,
|
||||
});
|
||||
|
||||
if (createdFavorite) {
|
||||
set(favoritesState, [...favorites, newFavorite]);
|
||||
set(favoritesState, [...favorites, ...newFavoritesMapped]);
|
||||
}
|
||||
},
|
||||
[
|
||||
allCompanies,
|
||||
allPeople,
|
||||
apolloClient,
|
||||
createOneMutation,
|
||||
currentWorkspaceMember,
|
||||
|
||||
Reference in New Issue
Block a user