You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
2.9 KiB
95 lines
2.9 KiB
5 months ago
|
import { Component, Inject } from '@angular/core';
|
||
|
import { FormGroup, Validators, FormBuilder } from '@angular/forms';
|
||
|
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
|
||
|
import { HttpService } from '@app/core/services/http.service';
|
||
|
import { I18nService } from '@app/core/services/i18n.service';
|
||
|
import { ToastService } from '@app/core/services/toast.service';
|
||
|
|
||
|
@Component({
|
||
|
selector: 'crud-template-edit',
|
||
|
templateUrl: './edit.component.html',
|
||
|
styleUrls: ['./edit.component.scss']
|
||
|
})
|
||
|
export class CrudEditComponent {
|
||
|
|
||
|
constructor(
|
||
|
@Inject(MAT_DIALOG_DATA)
|
||
|
private _data: any,
|
||
|
private _formBuilder: FormBuilder,
|
||
|
private _httpService: HttpService,
|
||
|
private _i18nService: I18nService,
|
||
|
private _toastService: ToastService,
|
||
|
private _dialogRef: MatDialogRef<CrudEditComponent>
|
||
|
) {
|
||
|
const controls = {};
|
||
|
_data.fields.forEach((item: any) => {
|
||
|
const value = [
|
||
|
_data.data ?
|
||
|
item.convertor ?
|
||
|
item.convertor(_data.data[item.id]) :
|
||
|
_data.data[item.id] :
|
||
|
item.default
|
||
|
];
|
||
|
const validator = [];
|
||
|
if (item.validator) {
|
||
|
validator.push(...item.validator);
|
||
|
}
|
||
|
if (item.required) {
|
||
|
validator.push(Validators.required);
|
||
|
}
|
||
|
if (validator.length) {
|
||
|
value.push(validator);
|
||
|
}
|
||
|
controls[item.id] = value;
|
||
|
});
|
||
|
this._form = this._formBuilder.group(controls);
|
||
|
}
|
||
|
|
||
|
private _form: FormGroup;
|
||
|
|
||
|
public get data() {
|
||
|
return this._data.data;
|
||
|
}
|
||
|
|
||
|
public get id() {
|
||
|
return this._data.data ? this._data.data[this._data.pk] : null;
|
||
|
}
|
||
|
|
||
|
public get fields() {
|
||
|
return this._data.fields;
|
||
|
}
|
||
|
|
||
|
public get form() {
|
||
|
return this._form;
|
||
|
}
|
||
|
public func() { }
|
||
|
|
||
|
// 模板编辑框【保存】按钮
|
||
|
public async save(): Promise<void> {
|
||
|
const data = {};
|
||
|
this._data.fields.forEach((item: any) => {
|
||
|
data[item.id] = this._form.controls[item.id].value;
|
||
|
});
|
||
|
debugger
|
||
|
// 如果有 ID,执行 PUT 请求 如果没有 ID,执行 POST 请求
|
||
|
const res: boolean = (this.id ?
|
||
|
await this._httpService.put(`${this._data.api}/${this.id}`, data).catch(e => {
|
||
|
//debugger
|
||
|
if (!this._data.error || this._data.error(e) !== false) {
|
||
|
this._dialogRef.close({ success: false }); // put 函数执行不成功,关闭对话框,并标记为失败
|
||
|
}
|
||
|
}) :
|
||
|
await this._httpService.post(this._data.api, data).catch(e => {
|
||
|
//debugger
|
||
|
if (!this._data.error || this._data.error(e) !== false) {
|
||
|
this._dialogRef.close({ success: false }); // post 函数执行不成功,关闭对话框,并标记为失败
|
||
|
}
|
||
|
})
|
||
|
) !== undefined;
|
||
|
if (res) {
|
||
|
this._toastService.show(this._i18nService.translate('shared.notification.success'));// 操作成功提示
|
||
|
this._dialogRef.close({ success: true }); // 关闭对话框
|
||
|
}
|
||
|
}
|
||
|
}
|