Skip to content

Commit 03f92c9

Browse files
author
Lasim
committed
feat: implement team selection event handling and UI updates in Teams and AppSidebar components
1 parent 2e7efca commit 03f92c9

File tree

4 files changed

+114
-6
lines changed

4 files changed

+114
-6
lines changed

services/frontend/src/components/AppSidebar.vue

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@ const fetchTeams = async (forceRefresh = false) => {
137137
} catch (error) { console.error('Error fetching teams:', error); teamsError.value = error instanceof Error ? error.message : 'Failed to load teams'; } finally { teamsLoading.value = false; }
138138
}
139139
140-
const selectTeam = (team: Team) => { selectedTeam.value = team }
140+
const selectTeam = (team: Team) => {
141+
selectedTeam.value = team
142+
// Emit global event for team selection
143+
eventBus.emit('team-selected', { teamId: team.id, teamName: team.name })
144+
}
141145
const navigateTo = (url: string) => { router.push(url) }
142146
const goToAccount = () => { router.push('/user/account') }
143147
const logout = async () => {
@@ -153,6 +157,16 @@ const logout = async () => {
153157
}
154158
const getUserInitials = (name: string) => { return name.split(' ').map(word => word.charAt(0)).join('').toUpperCase().slice(0, 2) }
155159
160+
// Handle team selection from other components (like Teams page)
161+
const handleTeamSelectedFromOtherComponents = (data: { teamId: string; teamName: string }) => {
162+
// Find the team in our teams list and update selectedTeam
163+
const team = teams.value.find(t => t.id === data.teamId)
164+
if (team) {
165+
selectedTeam.value = team
166+
console.log('Team selected from other component:', data.teamName)
167+
}
168+
}
169+
156170
onMounted(() => {
157171
fetchUserData()
158172
fetchTeams()
@@ -162,11 +176,15 @@ onMounted(() => {
162176
console.log('Teams updated event received, refreshing teams...')
163177
fetchTeams(true) // Force refresh to get latest data
164178
})
179+
180+
// Listen for team selection from other components
181+
eventBus.on('team-selected', handleTeamSelectedFromOtherComponents)
165182
})
166183
167184
onUnmounted(() => {
168185
// Clean up event listeners
169186
eventBus.off('teams-updated')
187+
eventBus.off('team-selected', handleTeamSelectedFromOtherComponents)
170188
})
171189
</script>
172190

services/frontend/src/composables/useEventBus.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type EventBusEvents = {
55
'teams-updated': void
66
'team-created': void
77
'team-deleted': void
8+
'team-selected': { teamId: string; teamName: string }
89
}
910

1011
export function useEventBus() {

services/frontend/src/views/Teams.vue

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { ref, onMounted, computed } from 'vue'
2+
import { ref, onMounted, onUnmounted, computed } from 'vue'
33
import { useI18n } from 'vue-i18n'
44
import {
55
FlexRender,
@@ -25,7 +25,7 @@ import { Button } from '@/components/ui/button'
2525
import { Plus } from 'lucide-vue-next'
2626
import DashboardLayout from '@/components/DashboardLayout.vue'
2727
import AddTeamModal from '@/components/teams/AddTeamModal.vue'
28-
import { TeamService, type TeamWithRole } from '@/services/teamService'
28+
import { TeamService, type TeamWithRole, type Team } from '@/services/teamService'
2929
import { UserService } from '@/services/userService'
3030
import { useEventBus } from '@/composables/useEventBus'
3131
import { createColumns } from './teams/columns'
@@ -51,11 +51,39 @@ const canCreateTeams = ref(false)
5151
const userPermissions = ref<string[]>([])
5252
const deleteSuccessMessage = ref<string | null>(null)
5353
54+
// Team switching state
55+
const selectedTeam = ref<Team | null>(null)
56+
5457
// Handle manage team navigation
5558
const handleManageTeam = (teamId: string) => {
5659
router.push(`/teams/manage/${teamId}`)
5760
}
5861
62+
// Handle team switching
63+
const handleSwitchTeam = (teamId: string) => {
64+
const team = teams.value.find(t => t.id === teamId)
65+
if (team) {
66+
selectedTeam.value = team
67+
// Emit global event for team selection to update sidebar
68+
eventBus.emit('team-selected', { teamId: team.id, teamName: team.name })
69+
console.log('Switched to team:', team.name)
70+
}
71+
}
72+
73+
// Handle team selection from sidebar
74+
const handleTeamSelectedFromSidebar = (data: { teamId: string; teamName: string }) => {
75+
// Find the team in our local teams list and update selectedTeam
76+
const team = teams.value.find(t => t.id === data.teamId)
77+
if (team) {
78+
selectedTeam.value = team
79+
console.log('Team selected from sidebar:', data.teamName)
80+
} else {
81+
// If team not found in current list, create a basic team object
82+
selectedTeam.value = { id: data.teamId, name: data.teamName } as Team
83+
console.log('Team selected from sidebar (not in current list):', data.teamName)
84+
}
85+
}
86+
5987
// Check user permissions
6088
const checkPermissions = async () => {
6189
try {
@@ -72,7 +100,24 @@ const checkPermissions = async () => {
72100
}
73101
74102
// Create columns with permissions
75-
const columns = computed(() => createColumns(handleManageTeam, userPermissions.value))
103+
const columns = computed(() => createColumns(
104+
handleManageTeam,
105+
handleSwitchTeam,
106+
selectedTeam.value?.id || null,
107+
userPermissions.value
108+
))
109+
110+
// Initialize selected team from sidebar teams
111+
const initializeSelectedTeam = async () => {
112+
try {
113+
const userTeams = await TeamService.getUserTeams()
114+
if (userTeams.length > 0) {
115+
selectedTeam.value = userTeams[0] // Default to first team
116+
}
117+
} catch (error) {
118+
console.error('Error initializing selected team:', error)
119+
}
120+
}
76121
77122
// Fetch teams from API
78123
const fetchTeams = async (): Promise<void> => {
@@ -118,8 +163,24 @@ onMounted(async () => {
118163
119164
await Promise.all([
120165
checkPermissions(),
121-
fetchTeams()
166+
fetchTeams(),
167+
initializeSelectedTeam()
122168
])
169+
170+
// Listen for team selection events from sidebar
171+
eventBus.on('team-selected', handleTeamSelectedFromSidebar)
172+
173+
// Listen for team updates from other components
174+
eventBus.on('teams-updated', () => {
175+
console.log('Teams updated event received, refreshing teams...')
176+
fetchTeams(true) // Force refresh to get latest data
177+
})
178+
})
179+
180+
onUnmounted(() => {
181+
// Clean up event listeners to prevent memory leaks
182+
eventBus.off('team-selected', handleTeamSelectedFromSidebar)
183+
eventBus.off('teams-updated')
123184
})
124185
125186
// Create table instance

services/frontend/src/views/teams/columns.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import type { ColumnDef } from '@tanstack/vue-table'
22
import { h } from 'vue'
33
import { Button } from '@/components/ui/button'
4-
import { Settings } from 'lucide-vue-next'
4+
import { Settings, ArrowRightLeft } from 'lucide-vue-next'
55
import type { TeamWithRole } from '@/services/teamService'
66

77
export const createColumns = (
88
onManageTeam: (teamId: string) => void,
9+
onSwitchTeam: (teamId: string) => void,
10+
selectedTeamId: string | null,
911
userPermissions: string[] = []
1012
): ColumnDef<TeamWithRole>[] => [
1113
{
@@ -52,6 +54,32 @@ export const createColumns = (
5254
)
5355
},
5456
},
57+
{
58+
id: 'switch',
59+
header: 'Switch Team',
60+
enableHiding: false,
61+
cell: ({ row }) => {
62+
const team = row.original
63+
const isSelected = selectedTeamId === team.id
64+
65+
return h('div', { class: 'flex justify-start' }, [
66+
h(Button, {
67+
variant: isSelected ? 'default' : 'outline',
68+
size: 'sm',
69+
class: 'h-8 px-3',
70+
disabled: isSelected,
71+
onClick: () => {
72+
if (!isSelected) {
73+
onSwitchTeam(team.id)
74+
}
75+
}
76+
}, () => [
77+
h(ArrowRightLeft, { class: 'h-4 w-4 mr-1' }),
78+
isSelected ? 'Selected' : 'Switch'
79+
])
80+
])
81+
},
82+
},
5583
{
5684
id: 'actions',
5785
header: 'Actions',

0 commit comments

Comments
 (0)