Skip to content

Commit

Permalink
fix: primaryselecion
Browse files Browse the repository at this point in the history
  • Loading branch information
scopsy committed Dec 20, 2024
1 parent 31094fc commit b97f518
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ export function useIntegrationPrimaryModal({

const existingPrimaryIntegration = filteredIntegrations.find((el) => el.primary);
const hasOtherProviders = filteredIntegrations.length;
const hasSameChannelActiveIntegration = filteredIntegrations.find((el) => el.active);

const shouldShowPrimaryModal = (data: IntegrationFormData) => {
if (!channel && !integration) return false;
if (!isChannelSupportPrimary) return false;

const hasSameChannelActiveIntegration = filteredIntegrations.find((el) => el.active);

return data.active && data.primary && hasSameChannelActiveIntegration && existingPrimaryIntegration;
};

Expand Down Expand Up @@ -97,5 +96,6 @@ export function useIntegrationPrimaryModal({
handlePrimaryConfirm,
existingPrimaryIntegration,
hasOtherProviders,
hasSameChannelActiveIntegration,
};
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,70 @@
import { ConfirmationModal } from '@/components/confirmation-modal';
import { SelectPrimaryIntegrationModal } from './select-primary-integration-modal';
import { IIntegration } from '@novu/shared';
import { useState } from 'react';

export type DeleteIntegrationModalProps = {
isOpen: boolean;
onOpenChange: (open: boolean) => void;
onConfirm: () => void;
onConfirm: (newPrimaryIntegrationId?: string) => void;
isPrimary?: boolean;
otherIntegrations?: IIntegration[];
};

export function DeleteIntegrationModal({ isOpen, onOpenChange, onConfirm, isPrimary }: DeleteIntegrationModalProps) {
export function DeleteIntegrationModal({
isOpen,
onOpenChange,
onConfirm,
isPrimary,
otherIntegrations = [],
}: DeleteIntegrationModalProps) {
const [isSelectPrimaryModalOpen, setIsSelectPrimaryModalOpen] = useState(false);
const hasOtherIntegrations = otherIntegrations.length > 0;

const description = isPrimary ? (
<>
<p>Are you sure you want to delete this primary integration?</p>
<p>This will disable the channel until you set up a new integration.</p>
<p>
{hasOtherIntegrations
? 'You will need to select a new primary integration for this channel.'
: 'This will disable the channel until you set up a new integration.'}
</p>
</>
) : (
<p>Are you sure you want to delete this integration?</p>
);

const handleConfirm = () => {
if (isPrimary && hasOtherIntegrations) {
setIsSelectPrimaryModalOpen(true);

return;
}

onConfirm();
};

return (
<ConfirmationModal
open={isOpen}
onOpenChange={onOpenChange}
onConfirm={onConfirm}
title={`Delete ${isPrimary ? 'Primary ' : ''}Integration`}
description={description}
confirmButtonText="Delete Integration"
/>
<>
<ConfirmationModal
open={isOpen}
onOpenChange={onOpenChange}
onConfirm={handleConfirm}
title={`Delete ${isPrimary ? 'Primary ' : ''}Integration`}
description={description}
confirmButtonText="Delete Integration"
/>

<SelectPrimaryIntegrationModal
isOpen={isSelectPrimaryModalOpen}
onOpenChange={setIsSelectPrimaryModalOpen}
onConfirm={(newPrimaryIntegrationId) => {
setIsSelectPrimaryModalOpen(false);
onConfirm(newPrimaryIntegrationId);
}}
otherIntegrations={otherIntegrations}
mode="select"
/>
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export function UpdateIntegrationSidebar({ isOpened }: UpdateIntegrationSidebarP
existingPrimaryIntegration,
isChannelSupportPrimary,
hasOtherProviders,
hasSameChannelActiveIntegration,
} = useIntegrationPrimaryModal({
onSubmit,
integrations,
Expand All @@ -63,15 +64,7 @@ export function UpdateIntegrationSidebar({ isOpened }: UpdateIntegrationSidebarP

// If the integration was primary and is being unmarked or deactivated
if (!skipPrimaryCheck && integration.primary && ((!data.primary && data.active) || !data.active)) {
const otherActiveIntegrationsInChannel = integrations?.filter(
(i) =>
i._id !== integration._id &&
i.channel === integration.channel &&
i.active &&
i._environmentId === integration._environmentId
);

if (otherActiveIntegrationsInChannel && otherActiveIntegrationsInChannel.length > 0) {
if (hasSameChannelActiveIntegration) {
setIsPrimaryModalOpen(true);
setPendingData(data);
return;
Expand Down Expand Up @@ -103,10 +96,14 @@ export function UpdateIntegrationSidebar({ isOpened }: UpdateIntegrationSidebarP
}
}

const onDelete = async () => {
const onDelete = async (newPrimaryIntegrationId?: string) => {
if (!integration) return;

try {
if (newPrimaryIntegrationId) {
await setPrimaryIntegration({ integrationId: newPrimaryIntegrationId });
}

await deleteIntegration({ id: integration._id });

showSuccessToast('Integration deleted successfully');
Expand Down Expand Up @@ -165,6 +162,13 @@ export function UpdateIntegrationSidebar({ isOpened }: UpdateIntegrationSidebarP
onOpenChange={setIsDeleteDialogOpen}
onConfirm={onDelete}
isPrimary={integration.primary}
otherIntegrations={integrations?.filter(
(i) =>
i._id !== integration?._id &&
i.channel === integration?.channel &&
i.active &&
i._environmentId === integration?._environmentId
)}
/>

<SelectPrimaryIntegrationModal
Expand Down

0 comments on commit b97f518

Please sign in to comment.