Skip to content

Commit

Permalink
Rework with Tanstack Query
Browse files Browse the repository at this point in the history
  • Loading branch information
GODrums committed Nov 22, 2024
1 parent 885a37c commit 65070a8
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions webapp/src/app/admin/teams/table/teams-table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,6 @@ export class AdminTeamsTableComponent {
onSettled: () => this.invalidateTeams()
}));

protected copyName(element: TeamInfo) {
console.log('Copying name', element);
navigator.clipboard.writeText(element.name!);
}

addLabelToTeam = injectMutation(() => ({
mutationFn: (team: TeamInfo) => lastValueFrom(this.adminService.addLabelToTeam(team.id, this._newLabelName.value ?? '')),
queryKey: ['admin', 'team', 'label', 'add'],
Expand All @@ -218,6 +213,25 @@ export class AdminTeamsTableComponent {
onSettled: () => this.invalidateTeams()
}));

removeRepositoryFromTeam = injectMutation(() => ({
mutationFn: ({ teamId, owner, repo }: { teamId: number; owner: string; repo: string }) =>
lastValueFrom(this.adminService.removeRepositoryFromTeam(teamId, owner, repo)),
queryKey: ['admin', 'team', 'repository', 'remove'],
onSettled: () => this.invalidateTeams()
}));

addRepositoryToTeam = injectMutation(() => ({
mutationFn: ({ teamId, owner, repo }: { teamId: number; owner: string; repo: string }) =>
lastValueFrom(this.adminService.addRepositoryToTeam(teamId, owner, repo)),
queryKey: ['admin', 'team', 'repository', 'add'],
onSettled: () => this.invalidateTeams()
}));

protected copyName(element: TeamInfo) {
console.log('Copying name', element);
navigator.clipboard.writeText(element.name!);
}

protected invalidateTeams() {
this.queryClient.invalidateQueries({ queryKey: ['admin', 'teams'] });
}
Expand All @@ -227,19 +241,11 @@ export class AdminTeamsTableComponent {
}

protected toggleRepository(team: TeamInfo, repository: string, checked: boolean) {
const separatedName = repository.split('/');
const [owner, repo] = repository.split('/');
if (checked) {
this.adminService.removeRepositoryFromTeam(team.id, separatedName[0], separatedName[1]).subscribe({
next: () => {
team.repositories = team.repositories.filter((r) => r.nameWithOwner !== repository);
},
error: (err) => console.error('Error removing repository', err)
});
this.removeRepositoryFromTeam.mutate({ teamId: team.id, owner, repo });
} else {
this.adminService.addRepositoryToTeam(team.id, separatedName[0], separatedName[1]).subscribe({
next: (newTeam) => team.repositories.push(newTeam.repositories[newTeam.repositories.length - 1]),
error: (err) => console.error('Error adding repository', err)
});
this.addRepositoryToTeam.mutate({ teamId: team.id, owner, repo });
}
}
}

0 comments on commit 65070a8

Please sign in to comment.