/* * Squidex Headless CMS * * @license * Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. */ import { Injectable } from '@angular/core'; import { DialogService, shareSubscribed, State } from '@app/framework'; import { Observable } from 'rxjs'; import { finalize, tap } from 'rxjs/operators'; import { BackupDto, BackupsService } from './../services/backups.service'; import { AppsState } from './apps.state'; interface Snapshot { // The current backups. backups: BackupsList; // Indicates if the backups are loaded. isLoaded?: boolean; // Indicates if the backups are loading. isLoading?: boolean; // Indicates if the user can create new backups. canCreate?: boolean; } type BackupsList = ReadonlyArray; @Injectable() export class BackupsState extends State { public backups = this.project(x => x.backups); public maxBackupsReached = this.projectFrom(this.backups, x => x.length >= 10); public isLoaded = this.project(x => x.isLoaded === true); public isLoading = this.project(x => x.isLoading === true); public canCreate = this.project(x => x.canCreate === true); constructor( private readonly appsState: AppsState, private readonly backupsService: BackupsService, private readonly dialogs: DialogService ) { super({ backups: [] }, 'Backups'); } public load(isReload = false, silent = false): Observable { if (isReload && !silent) { this.resetState('Loading Initial'); } return this.loadInternal(isReload, silent); } private loadInternal(isReload: boolean, silent: boolean): Observable { if (!silent) { this.next({ isLoading: true }, 'Loading Started'); } return this.backupsService.getBackups(this.appName).pipe( tap(({ items: backups, canCreate }) => { if (isReload && !silent) { this.dialogs.notifyInfo('i18n:backups.reloaded'); } this.next({ backups, canCreate, isLoaded: true, isLoading: false }, 'Loading Success'); }), finalize(() => { this.next({ isLoading: false }, 'Loading Done'); }), shareSubscribed(this.dialogs, { silent })); } public start(): Observable { return this.backupsService.postBackup(this.appsState.appName).pipe( tap(() => { this.dialogs.notifyInfo('i18n:backups.started'); }), shareSubscribed(this.dialogs)); } public delete(backup: BackupDto): Observable { return this.backupsService.deleteBackup(this.appsState.appName, backup).pipe( tap(() => { this.dialogs.notifyInfo('i18n:backups.deleted'); }), shareSubscribed(this.dialogs)); } private get appName() { return this.appsState.appName; } }