Skip to content

Commit

Permalink
fix(playground): fix race condition in sql playground loading (#1725)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zitrone44 authored Jan 19, 2025
1 parent 1a45513 commit 6b66ec1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,45 @@ import {
activateDatabaseFailure,
} from "./databases.actions";
import { AuthService } from "../../../../service/auth.service";
import { JWTToken } from "../../../../model/JWTToken";
import {
changeActiveDbId,
updateScheme,
} from "../../state/sql-playground.actions";

@Injectable()
export class DatabasesEffects {
private token: JWTToken;
constructor(
private actions$: Actions,
private sqlPlaygroundService: SqlPlaygroundService,
private snackbar: MatSnackBar,
authService: AuthService
) {
this.token = authService.isAuthenticated() ? authService.getToken() : null;
}
private authService: AuthService
) {}

loadDatabases$ = createEffect(() =>
this.actions$.pipe(
ofType(loadDatabases),
switchMap(() => {
if (!this.token) return;
return this.sqlPlaygroundService.getDatabases(this.token.id).pipe(
switchMap((databases) => {
if (databases.length == 0) {
// create default database if none exists
return of(createDatabase({ name: "Standard Datenbank" }));
} else {
const activeId =
databases.find(({ active }) => active)?.id ?? databases[0]?.id;
return of(
loadDatabasesSuccess({ databases }),
changeActiveDbId({ dbId: activeId }),
updateScheme()
);
}
}),
catchError((error) => of(loadDatabasesFailure({ error })))
);
})
switchMap(() =>
this.sqlPlaygroundService
.getDatabases(this.authService.getToken().id)
.pipe(
switchMap((databases) => {
if (databases.length == 0) {
// create default database if none exists
return of(createDatabase({ name: "Standard Datenbank" }));
} else {
const activeId =
databases.find(({ active }) => active)?.id ??
databases[0]?.id;
return of(
loadDatabasesSuccess({ databases }),
changeActiveDbId({ dbId: activeId }),
updateScheme()
);
}
}),
catchError((error) => of(loadDatabasesFailure({ error })))
)
)
)
);

Expand All @@ -68,7 +66,7 @@ export class DatabasesEffects {
ofType(createDatabase),
mergeMap((action) =>
this.sqlPlaygroundService
.createDatabase(this.token.id, action.name)
.createDatabase(this.authService.getToken().id, action.name)
.pipe(
map((database) => {
this.snackbar.open("Datenbank erfolgreich erstellt", "Ok", {
Expand All @@ -86,15 +84,17 @@ export class DatabasesEffects {
this.actions$.pipe(
ofType(deleteDatabase),
mergeMap((action) =>
this.sqlPlaygroundService.deleteDatabase(this.token.id, action.id).pipe(
map(() => {
this.snackbar.open("Datenbank erfolgreich gelöscht", "Ok", {
duration: 3000,
});
return deleteDatabaseSuccess({ id: action.id });
}),
catchError((error) => of(deleteDatabaseFailure({ error })))
)
this.sqlPlaygroundService
.deleteDatabase(this.authService.getToken().id, action.id)
.pipe(
map(() => {
this.snackbar.open("Datenbank erfolgreich gelöscht", "Ok", {
duration: 3000,
});
return deleteDatabaseSuccess({ id: action.id });
}),
catchError((error) => of(deleteDatabaseFailure({ error })))
)
)
)
);
Expand All @@ -104,7 +104,7 @@ export class DatabasesEffects {
ofType(activateDatabase),
mergeMap((action) =>
this.sqlPlaygroundService
.activateDatabase(this.token.id, action.id)
.activateDatabase(this.authService.getToken().id, action.id)
.pipe(
switchMap(() => {
this.snackbar.open("Datenbank erfolgreich aktiviert", "Ok", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Actions, createEffect, ofType } from "@ngrx/effects";
import { of } from "rxjs";
import { catchError, switchMap } from "rxjs/operators";
import { AuthService } from "../../../../service/auth.service";
import { JWTToken } from "../../../../model/JWTToken";
import { GroupRegistrationService } from "../../../../service/group-registration.sevice";
import {
loadGroups,
Expand All @@ -13,25 +12,24 @@ import {

@Injectable()
export class GroupsEffects {
private token: JWTToken;
constructor(
private actions$: Actions,
private groupRegistrationService: GroupRegistrationService,
authService: AuthService
) {
this.token = authService.isAuthenticated() ? authService.getToken() : null;
}
private authService: AuthService
) {}

loadGroups = createEffect(() =>
this.actions$.pipe(
ofType(loadGroups),
switchMap(() =>
this.groupRegistrationService.getRegisteredGroups(this.token.id).pipe(
switchMap((groups) => {
return of(loadGroupsSuccess({ groups }));
}),
catchError((error) => of(loadGroupsFailure({ error })))
)
this.groupRegistrationService
.getRegisteredGroups(this.authService.getToken().id)
.pipe(
switchMap((groups) => {
return of(loadGroupsSuccess({ groups }));
}),
catchError((error) => of(loadGroupsFailure({ error })))
)
)
)
);
Expand Down

0 comments on commit 6b66ec1

Please sign in to comment.