verber.ts 3.9 KB
Newer Older
M
Marcin Maciaszczyk 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright 2017 The Kubernetes Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15
import {HttpClient, HttpHeaders} from '@angular/common/http';
M
Marcin Maciaszczyk 已提交
16 17 18 19
import {EventEmitter, Injectable} from '@angular/core';
import {MatDialog, MatDialogConfig} from '@angular/material';
import {ObjectMeta, TypeMeta} from '@api/backendapi';

20
import {AlertDialog, AlertDialogConfig} from '../../dialogs/alert/dialog';
M
Marcin Maciaszczyk 已提交
21
import {DeleteResourceDialog} from '../../dialogs/deleteresource/dialog';
M
Marcin Maciaszczyk 已提交
22 23
import {EditResourceDialog} from '../../dialogs/editresource/dialog';
import {RawResource} from '../../resources/rawresource';
M
Marcin Maciaszczyk 已提交
24 25 26 27 28 29

import {ResourceMeta} from './actionbar';

@Injectable()
export class VerberService {
  onDelete = new EventEmitter<boolean>();
M
Marcin Maciaszczyk 已提交
30
  onEdit = new EventEmitter<boolean>();
M
Marcin Maciaszczyk 已提交
31

32
  constructor(private readonly dialog_: MatDialog, private readonly http_: HttpClient) {}
M
Marcin Maciaszczyk 已提交
33 34

  showDeleteDialog(displayName: string, typeMeta: TypeMeta, objectMeta: ObjectMeta): void {
M
Marcin Maciaszczyk 已提交
35
    const dialogConfig = this.getDialogConfig_(displayName, typeMeta, objectMeta);
M
Marcin Maciaszczyk 已提交
36 37
    this.dialog_.open(DeleteResourceDialog, dialogConfig).afterClosed().subscribe((doDelete) => {
      if (doDelete) {
M
Marcin Maciaszczyk 已提交
38
        const url = RawResource.getUrl(typeMeta, objectMeta);
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
        this.http_.delete(url).subscribe(
            () => {
              this.onDelete.emit(true);
            },
            (err) => {
              if (err) {
                const alertDialogConfig: MatDialogConfig<AlertDialogConfig> = {
                  width: '630px',
                  data: {
                    title: err.statusText || 'Internal server error',
                    // TODO Add || this.localizerService_.localize(err.data).
                    message: 'Could not delete the resource',
                    confirmLabel: 'OK',
                  }
                };
                this.dialog_.open(AlertDialog, alertDialogConfig);
              }
            });
M
Marcin Maciaszczyk 已提交
57 58 59 60 61
      }
    });
  }

  showEditDialog(displayName: string, typeMeta: TypeMeta, objectMeta: ObjectMeta): void {
M
Marcin Maciaszczyk 已提交
62
    const dialogConfig = this.getDialogConfig_(displayName, typeMeta, objectMeta);
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    this.dialog_.open(EditResourceDialog, dialogConfig).afterClosed().subscribe((result) => {
      if (result) {
        const url = RawResource.getUrl(typeMeta, objectMeta);
        const headers = new HttpHeaders();
        headers.set('Content-Type', 'application/json');
        headers.set('Accept', 'application/json');
        this.http_.put(url, JSON.parse(result), {headers})
            .subscribe(
                () => {
                  this.onEdit.emit(true);
                },
                (err) => {
                  if (err) {
                    const alertDialogConfig: MatDialogConfig<AlertDialogConfig> = {
                      width: '630px',
                      data: {
                        title: err.statusText || 'Internal server error',
                        // TODO Add || this.localizerService_.localize(err.data).
                        message: 'Could not edit the resource',
                        confirmLabel: 'OK',
                      }
                    };
                    this.dialog_.open(AlertDialog, alertDialogConfig);
                  }
                });
      }
    });
M
Marcin Maciaszczyk 已提交
90 91
  }

M
Marcin Maciaszczyk 已提交
92 93 94 95 96 97 98 99 100 101
  getDialogConfig_(displayName: string, typeMeta: TypeMeta, objectMeta: ObjectMeta):
      MatDialogConfig<ResourceMeta> {
    return {
      width: '630px',
      data: {
        displayName,
        typeMeta,
        objectMeta,
      }
    };
M
Marcin Maciaszczyk 已提交
102 103
  }
}