fix: team switching

This commit is contained in:
Andras Bacsai 2022-08-26 18:44:51 +00:00
parent 4c5e71f33c
commit 570b286ef9
2 changed files with 29 additions and 21 deletions

View File

@ -160,6 +160,7 @@ export async function getTeam(request: FastifyRequest<OnlyId>, reply: FastifyRep
const invitations = await prisma.teamInvitation.findMany({ where: { teamId: team.id } }); const invitations = await prisma.teamInvitation.findMany({ where: { teamId: team.id } });
const { teams } = await prisma.user.findUnique({ where: { id: userId }, include: { teams: true } }) const { teams } = await prisma.user.findUnique({ where: { id: userId }, include: { teams: true } })
return { return {
currentTeam: teamId,
team, team,
teams, teams,
permissions, permissions,

View File

@ -25,6 +25,8 @@
</script> </script>
<script lang="ts"> <script lang="ts">
export let team: any;
export let currentTeam: string;
export let teams: any; export let teams: any;
import { page } from '$app/stores'; import { page } from '$app/stores';
import { errorNotification, handlerNotFoundLoad } from '$lib/common'; import { errorNotification, handlerNotFoundLoad } from '$lib/common';
@ -40,7 +42,8 @@
if (sure) { if (sure) {
try { try {
await del(`/iam/team/${id}`, { id }); await del(`/iam/team/${id}`, { id });
const switchTeam = teams.find((team: any) => team.id !== id) if (currentTeam === id) {
const switchTeam = teams.find((team: any) => team.id !== id);
const payload = await get(`/user?teamId=${switchTeam.id}`); const payload = await get(`/user?teamId=${switchTeam.id}`);
if (payload.token) { if (payload.token) {
Cookies.set('token', payload.token, { Cookies.set('token', payload.token, {
@ -52,6 +55,8 @@
$appSession.isAdmin = payload.isAdmin; $appSession.isAdmin = payload.isAdmin;
return window.location.assign('/iam'); return window.location.assign('/iam');
} }
}
return await goto('/iam', { replaceState: true }); return await goto('/iam', { replaceState: true });
} catch (error) { } catch (error) {
return errorNotification(error); return errorNotification(error);
@ -62,6 +67,7 @@
{#if id !== 'new'} {#if id !== 'new'}
<nav class="nav-side"> <nav class="nav-side">
{#if team.id !== '0'}
<button <button
on:click={deleteTeam} on:click={deleteTeam}
type="submit" type="submit"
@ -72,6 +78,7 @@
? 'Delete' ? 'Delete'
: $t('destination.permission_denied_delete_destination')}><DeleteIcon /></button : $t('destination.permission_denied_delete_destination')}><DeleteIcon /></button
> >
{/if}
</nav> </nav>
{/if} {/if}
<slot /> <slot />