* Wip * WIP * Removed concole log * Add relations to workspace init (#2511) * Add relations to workspace init * remove logs * update prefill * add missing isSystem * comment relation fields * Migrate v2 core models to graphql schema (#2509) * migrate v2 core models to graphql schema * Migrate to new workspace member schema * Continue work * migrated-main * Finished accountOwner nested field integration on companies * Introduce bug * Fix --------- Co-authored-by: Lucas Bordeau <bordeau.lucas@gmail.com> Co-authored-by: Weiko <corentin@twenty.com>
87 lines
3.4 KiB
TypeScript
87 lines
3.4 KiB
TypeScript
import { getOperationName } from '@apollo/client/utilities';
|
|
import { useRecoilState, useRecoilValue } from 'recoil';
|
|
import { v4 } from 'uuid';
|
|
|
|
import { currentUserState } from '@/auth/states/currentUserState';
|
|
import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState';
|
|
import { GET_COMPANIES } from '@/companies/graphql/queries/getCompanies';
|
|
import { GET_PEOPLE } from '@/people/graphql/queries/getPeople';
|
|
import { useRightDrawer } from '@/ui/layout/right-drawer/hooks/useRightDrawer';
|
|
import { RightDrawerHotkeyScope } from '@/ui/layout/right-drawer/types/RightDrawerHotkeyScope';
|
|
import { RightDrawerPages } from '@/ui/layout/right-drawer/types/RightDrawerPages';
|
|
import { useSetHotkeyScope } from '@/ui/utilities/hotkey/hooks/useSetHotkeyScope';
|
|
import { ActivityType, useCreateActivityMutation } from '~/generated/graphql';
|
|
|
|
import { GET_ACTIVITIES } from '../graphql/queries/getActivities';
|
|
import { GET_ACTIVITIES_BY_TARGETS } from '../graphql/queries/getActivitiesByTarget';
|
|
import { GET_ACTIVITY } from '../graphql/queries/getActivity';
|
|
import { activityTargetableEntityArrayState } from '../states/activityTargetableEntityArrayState';
|
|
import { viewableActivityIdState } from '../states/viewableActivityIdState';
|
|
import { ActivityTargetableEntity } from '../types/ActivityTargetableEntity';
|
|
import { getRelationData } from '../utils/getRelationData';
|
|
|
|
export const useOpenCreateActivityDrawer = () => {
|
|
const { openRightDrawer } = useRightDrawer();
|
|
const [createActivityMutation] = useCreateActivityMutation();
|
|
const currentUser = useRecoilValue(currentUserState);
|
|
const currentWorkspaceMember = useRecoilValue(currentWorkspaceMemberState);
|
|
const setHotkeyScope = useSetHotkeyScope();
|
|
|
|
const [, setActivityTargetableEntityArray] = useRecoilState(
|
|
activityTargetableEntityArrayState,
|
|
);
|
|
const [, setViewableActivityId] = useRecoilState(viewableActivityIdState);
|
|
|
|
return ({
|
|
type,
|
|
targetableEntities,
|
|
assigneeId,
|
|
}: {
|
|
type: ActivityType;
|
|
targetableEntities?: ActivityTargetableEntity[];
|
|
assigneeId?: string;
|
|
}) => {
|
|
const now = new Date().toISOString();
|
|
|
|
return createActivityMutation({
|
|
variables: {
|
|
data: {
|
|
id: v4(),
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
author: { connect: { id: currentUser?.id ?? '' } },
|
|
workspaceMemberAuthor: {
|
|
connect: { id: currentWorkspaceMember?.id ?? '' },
|
|
},
|
|
assignee: { connect: { id: assigneeId ?? currentUser?.id ?? '' } },
|
|
workspaceMemberAssignee: {
|
|
connect: { id: currentWorkspaceMember?.id ?? '' },
|
|
},
|
|
type: type,
|
|
activityTargets: {
|
|
createMany: {
|
|
data: targetableEntities
|
|
? getRelationData(targetableEntities)
|
|
: [],
|
|
skipDuplicates: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
refetchQueries: [
|
|
getOperationName(GET_COMPANIES) ?? '',
|
|
getOperationName(GET_PEOPLE) ?? '',
|
|
getOperationName(GET_ACTIVITY) ?? '',
|
|
getOperationName(GET_ACTIVITIES_BY_TARGETS) ?? '',
|
|
getOperationName(GET_ACTIVITIES) ?? '',
|
|
],
|
|
onCompleted: (data) => {
|
|
setHotkeyScope(RightDrawerHotkeyScope.RightDrawer, { goto: false });
|
|
setViewableActivityId(data.createOneActivity.id);
|
|
setActivityTargetableEntityArray(targetableEntities ?? []);
|
|
openRightDrawer(RightDrawerPages.CreateActivity);
|
|
},
|
|
});
|
|
};
|
|
};
|