Skip to content

Commit

Permalink
feat: add new service account based on url param id
Browse files Browse the repository at this point in the history
  • Loading branch information
nimish-ks committed Jan 31, 2025
1 parent 63d8eaf commit 7f3e3ba
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions frontend/app/[team]/apps/[app]/access/service-accounts/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ import Link from 'next/link'
import { unwrapEnvSecretsForUser, wrapEnvSecretsForAccount } from '@/utils/crypto'
import { EmptyState } from '@/components/common/EmptyState'
import Spinner from '@/components/common/Spinner'
import { useSearchParams } from 'next/navigation'

export default function ServiceAccounts({ params }: { params: { team: string; app: string } }) {
const searchParams = useSearchParams();
const preselectedAccountId = searchParams?.get('new') ?? null;

const { keyring } = useContext(KeyringContext)
const { activeOrganisation: organisation } = useContext(organisationContext)

Expand Down Expand Up @@ -129,6 +133,28 @@ export default function ServiceAccounts({ params }: { params: { team: string; ap
setIsOpen(true)
}

useEffect(() => {
if (preselectedAccountId && serviceAccountsData?.serviceAccounts) {
// Check if service account is already added to the app
const isAlreadyAdded = data?.appServiceAccounts?.some(
(account: ServiceAccountType) => account.id === preselectedAccountId
);

if (isAlreadyAdded) {
// Don't open dialog if already added
return;
}

const preselectedAccount = serviceAccountsData.serviceAccounts.find(
(account: ServiceAccountType) => account.id === preselectedAccountId
);
if (preselectedAccount) {
setSelectedAccount(preselectedAccount);
setIsOpen(true);
}
}
}, [preselectedAccountId, serviceAccountsData, data?.appServiceAccounts]);

const handleAddMember = async (e: { preventDefault: () => void }) => {
e.preventDefault()

Expand Down Expand Up @@ -195,19 +221,15 @@ export default function ServiceAccounts({ params }: { params: { team: string; ap
})

toast.success('Added account to App', { autoClose: 2000 })
}
closeModal();

useEffect(() => {
if (preselectedAccountId && serviceAccountsData?.serviceAccounts) {
const preselectedAccount = serviceAccountsData.serviceAccounts.find(
(account: ServiceAccountType) => account.id === preselectedAccountId
);
if (preselectedAccount) {
setSelectedAccount(preselectedAccount);
setIsOpen(true);
}
// Clear just the ?new parameter after successful addition of the service account. This makes sure the pop up doesn't show up again.
if (preselectedAccountId) {
const url = new URL(window.location.href);
url.searchParams.delete('new');
window.history.replaceState({}, '', url.toString());
}
}, [preselectedAccountId, serviceAccountsData]);
}

return (
<>
Expand Down

0 comments on commit 7f3e3ba

Please sign in to comment.