Improved update & add logic #410

This commit is contained in:
Sven Heidemann 2023-11-18 23:10:28 +01:00
parent 74dba4b981
commit 171aa63df9
10 changed files with 91 additions and 42 deletions

View File

@ -168,7 +168,7 @@ class ScheduledEvent(TableABC):
UPDATE `ScheduledEvents` UPDATE `ScheduledEvents`
SET `Interval` = '{self._interval.value}', SET `Interval` = '{self._interval.value}',
`Name` = '{self._name}', `Name` = '{self._name}',
`Description` = '{self._start_time}', `Description` = {"NULL" if self._description is None else f"'{self._description}'"},
`ChannelId` = {"NULL" if self._channel_id is None else f"'{self._channel_id}'"}, `ChannelId` = {"NULL" if self._channel_id is None else f"'{self._channel_id}'"},
`StartTime` = '{self._start_time}', `StartTime` = '{self._start_time}',
`EndTime` = {"NULL" if self._end_time is None else f"'{self._end_time}'"}, `EndTime` = {"NULL" if self._end_time is None else f"'{self._end_time}'"},

View File

@ -69,13 +69,22 @@ class ScheduledEventMutation(QueryABC):
scheduled_event = self._scheduled_events.get_scheduled_event_by_id(input["id"]) scheduled_event = self._scheduled_events.get_scheduled_event_by_id(input["id"])
self._can_user_mutate_data(scheduled_event.server, UserRoleEnum.moderator) self._can_user_mutate_data(scheduled_event.server, UserRoleEnum.moderator)
scheduled_event.short_name = input["shortName"] if "shortName" in input else scheduled_event.short_name scheduled_event.interval = (
scheduled_event.interval = input["interval"] if "interval" in input else scheduled_event.interval ScheduledEventIntervalEnum(input["interval"]) if "interval" in input else scheduled_event.interval
)
scheduled_event.name = input["name"] if "name" in input else scheduled_event.name scheduled_event.name = input["name"] if "name" in input else scheduled_event.name
scheduled_event.description = input["description"] if "description" in input else scheduled_event.description scheduled_event.description = input["description"] if "description" in input else scheduled_event.description
scheduled_event.channel_id = input["channelId"] if "channelId" in input else scheduled_event.channel_id scheduled_event.channel_id = input["channelId"] if "channelId" in input else scheduled_event.channel_id
scheduled_event.start_time = input["startTime"] if "startTime" in input else scheduled_event.start_time scheduled_event.start_time = (
scheduled_event.end_time = input["endTime"] if "endTime" in input else scheduled_event.end_time datetime.strptime(input["startTime"], "%Y-%m-%dT%H:%M:%S.%fZ")
if "startTime" in input
else scheduled_event.start_time
)
scheduled_event.end_time = (
datetime.strptime(input["endTime"], "%Y-%m-%dT%H:%M:%S.%fZ")
if "endTime" in input
else scheduled_event.end_time
)
scheduled_event.entity_type = input["entityType"] if "entityType" in input else scheduled_event.entity_type scheduled_event.entity_type = input["entityType"] if "entityType" in input else scheduled_event.entity_type
scheduled_event.location = input["location"] if "location" in input else scheduled_event.location scheduled_event.location = input["location"] if "location" in input else scheduled_event.location

View File

@ -13,7 +13,7 @@ class ScheduledEventHistoryQuery(DataQueryWithHistoryABC):
self.set_field("id", lambda x, *_: x.id) self.set_field("id", lambda x, *_: x.id)
self.set_field("id", lambda x, *_: x.id) self.set_field("id", lambda x, *_: x.id)
self.set_field("interval", lambda x, *_: x.interval) self.set_field("interval", lambda x, *_: x.interval.value)
self.set_field("name", lambda x, *_: x.name) self.set_field("name", lambda x, *_: x.name)
self.set_field("description", lambda x, *_: x.description) self.set_field("description", lambda x, *_: x.description)
self.set_field("channel_id", lambda x, *_: x.channel_id) self.set_field("channel_id", lambda x, *_: x.channel_id)

View File

@ -12,7 +12,7 @@ class ScheduledEventQuery(DataQueryWithHistoryABC):
DataQueryWithHistoryABC.__init__(self, "ScheduledEvent", "ScheduledEventsHistory", ScheduledEventHistory, db) DataQueryWithHistoryABC.__init__(self, "ScheduledEvent", "ScheduledEventsHistory", ScheduledEventHistory, db)
self.set_field("id", lambda x, *_: x.id) self.set_field("id", lambda x, *_: x.id)
self.set_field("interval", lambda x, *_: x.interval) self.set_field("interval", lambda x, *_: x.interval.value)
self.set_field("name", lambda x, *_: x.name) self.set_field("name", lambda x, *_: x.name)
self.set_field("description", lambda x, *_: x.description) self.set_field("description", lambda x, *_: x.description)
self.set_field("channelId", lambda x, *_: x.channel_id) self.set_field("channelId", lambda x, *_: x.channel_id)

View File

@ -33,9 +33,11 @@ export interface ScheduledEventFilter {
server?: ServerFilter; server?: ServerFilter;
} }
export enum ScheduledEventInterval { // export enum ScheduledEventInterval {
daily = "daily", // daily = "daily",
weekly = "weekly", // weekly = "weekly",
monthly = "monthly", // monthly = "monthly",
yearly = "yearly" // yearly = "yearly"
} // }
export type ScheduledEventInterval = "daily" | "weekly" | "monthly" | "month"

View File

@ -204,9 +204,10 @@ export class Mutations {
`; `;
static updateScheduledEvent = ` static updateScheduledEvent = `
mutation updateScheduledEvent($interval: String,$name: String,$description: String,$channelId: String,$startTime: String, $endTime: String,$entityType: Int,$location: String, $serverId: ID) { mutation updateScheduledEvent($id: ID, $interval: String,$name: String,$description: String,$channelId: String,$startTime: String, $endTime: String,$entityType: Int,$location: String, $serverId: ID) {
scheduledEvent { scheduledEvent {
updateScheduledEvent(input: { updateScheduledEvent(input: {
id: $id,
interval: $interval, interval: $interval,
name: $name, name: $name,
description: $description, description: $description,

View File

@ -1,5 +1,5 @@
<div class="edit-dialog"> <div class="edit-dialog">
<p-dialog [contentStyle]="{'overflow':'visible'}" [header]="header" [(visible)]="visible" [modal]="true" <p-dialog [contentStyle]="{'overflow':'visible'}" [(visible)]="visible" [header]="header" [modal]="true"
[draggable]="false" [resizable]="false" [draggable]="false" [resizable]="false"
[style]="{ width: '500px', height: '575px' }"> [style]="{ width: '500px', height: '575px' }">
<form [formGroup]="inputForm"> <form [formGroup]="inputForm">
@ -80,7 +80,7 @@
</div> </div>
<div class="btn-wrapper"> <div class="btn-wrapper">
<button pButton label="{{'common.abort' | translate}}" class="btn danger-btn" <button pButton label="{{'common.abort' | translate}}" class="btn danger-btn"
(click)="visible = false"></button> (click)="event = undefined"></button>
<button *ngIf="activeIndex < 1" pButton label="{{'common.continue' | translate}}" class="btn" <button *ngIf="activeIndex < 1" pButton label="{{'common.continue' | translate}}" class="btn"
(click)="next()" [disabled]="!inputForm"></button> (click)="next()" [disabled]="!inputForm"></button>
<button *ngIf="activeIndex == 1" pButton label="{{'common.save' | translate}}" class="btn" <button *ngIf="activeIndex == 1" pButton label="{{'common.save' | translate}}" class="btn"

View File

@ -1,4 +1,4 @@
import { Component, EventEmitter, Input, OnInit, Output } from "@angular/core"; import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from "@angular/core";
import { import {
EventType, EventType,
ScheduledEvent, ScheduledEvent,
@ -20,9 +20,28 @@ import { DatePipe } from "@angular/common";
templateUrl: "./edit-scheduled-event-dialog.component.html", templateUrl: "./edit-scheduled-event-dialog.component.html",
styleUrls: ["./edit-scheduled-event-dialog.component.scss"] styleUrls: ["./edit-scheduled-event-dialog.component.scss"]
}) })
export class EditScheduledEventDialogComponent implements OnInit { export class EditScheduledEventDialogComponent implements OnInit, OnChanges {
@Input() event?: ScheduledEvent; public _event?: ScheduledEvent;
@Output() save = new EventEmitter<ScheduledEvent>();
@Input()
set event(event: ScheduledEvent | undefined) {
this._event = event;
this.eventChange.emit(event);
}
get event(): ScheduledEvent | undefined {
return this._event;
}
@Output() eventChange: EventEmitter<ScheduledEvent> = new EventEmitter<ScheduledEvent>();
get header() {
if (this.event && this.event.createdAt === "" && this.event.modifiedAt === "") {
return this.translate.instant("view.server.scheduled_events.edit_dialog.add_header");
} else {
return this.translate.instant("view.server.scheduled_events.edit_dialog.edit_header");
}
}
get visible() { get visible() {
return this.event != undefined; return this.event != undefined;
@ -36,14 +55,6 @@ export class EditScheduledEventDialogComponent implements OnInit {
} }
} }
get header() {
if (this.event && this.event.createdAt === "" && this.event.modifiedAt === "") {
return this.translate.instant("view.server.scheduled_events.edit_dialog.add_header");
} else {
return this.translate.instant("view.server.scheduled_events.edit_dialog.edit_header");
}
}
public activeIndex: number = 0; public activeIndex: number = 0;
public inputForm!: FormGroup<{ public inputForm!: FormGroup<{
entityType: FormControl<number | undefined | null>; entityType: FormControl<number | undefined | null>;
@ -62,12 +73,24 @@ export class EditScheduledEventDialogComponent implements OnInit {
public guild: Guild = { channels: [], emojis: [], roles: [] }; public guild: Guild = { channels: [], emojis: [], roles: [] };
public times: string[] = []; public times: string[] = [];
public now = new Date(); public now = new Date();
public interval = Object.keys(ScheduledEventInterval).map(k => ( public interval = [
{ {
label: this.translate.instant(`view.server.scheduled_events.edit_dialog.location.intervals.${k}`), label: this.translate.instant("view.server.scheduled_events.edit_dialog.location.intervals.daily"),
value: k value: "daily"
},
{
label: this.translate.instant("view.server.scheduled_events.edit_dialog.location.intervals.weekly"),
value: "weekly"
},
{
label: this.translate.instant("view.server.scheduled_events.edit_dialog.location.intervals.monthly"),
value: "monthly"
},
{
label: this.translate.instant("view.server.scheduled_events.edit_dialog.location.intervals.yearly"),
value: "yearly"
} }
)); ];
constructor( constructor(
private translate: TranslateService, private translate: TranslateService,
@ -92,6 +115,10 @@ export class EditScheduledEventDialogComponent implements OnInit {
this.setInputForm(); this.setInputForm();
} }
public ngOnChanges() {
this.setInputForm();
}
public ngOnInit() { public ngOnInit() {
this.data.getServerFromRoute(this.route).then(server => { this.data.getServerFromRoute(this.route).then(server => {
this.server = server; this.server = server;
@ -154,9 +181,7 @@ export class EditScheduledEventDialogComponent implements OnInit {
location: this.inputForm.controls.location.value ?? undefined, location: this.inputForm.controls.location.value ?? undefined,
server: this.server server: this.server
}; };
this.visible = false;
this.save.emit(this.event);
this.event = undefined;
} }
public next() { public next() {

View File

@ -1,5 +1,6 @@
<app-edit-scheduled-event-dialog [event]="editableScheduledEvent" <div *ngIf="editableScheduledEvent">
(save)="onRowEditSave($event)"></app-edit-scheduled-event-dialog> <app-edit-scheduled-event-dialog [(event)]="editableScheduledEvent"></app-edit-scheduled-event-dialog>
</div>
<h1> <h1>
{{'view.server.scheduled_events.header' | translate}} {{'view.server.scheduled_events.header' | translate}}
@ -27,7 +28,7 @@
<div class="table-caption-btn-wrapper btn-wrapper"> <div class="table-caption-btn-wrapper btn-wrapper">
<button pButton label="{{'common.add' | translate}}" class="icon-btn btn" <button pButton label="{{'common.add' | translate}}" class="icon-btn btn"
icon="pi pi-plus" (click)="addScheduledEvent(dt)" icon="pi pi-plus" (click)="addScheduledEvent()"
[disabled]="isEditingNew || !user?.isModerator && !user?.isAdmin"> [disabled]="isEditingNew || !user?.isModerator && !user?.isAdmin">
</button> </button>
<button pButton label="{{'common.reset_filters' | translate}}" icon="pi pi-undo" <button pButton label="{{'common.reset_filters' | translate}}" icon="pi pi-undo"

View File

@ -60,7 +60,20 @@ export class ScheduledEventsComponent extends ComponentWithTable implements OnIn
private server: Server = {}; private server: Server = {};
public user: UserDTO | null = null; public user: UserDTO | null = null;
public query: string = Queries.scheduledEventWithHistoryQuery; public query: string = Queries.scheduledEventWithHistoryQuery;
public editableScheduledEvent?: ScheduledEvent = undefined; public _editableScheduledEvent?: ScheduledEvent = undefined;
set editableScheduledEvent(event: ScheduledEvent | undefined) {
if (!event) {
this.isEditingNew = false;
} else if (this._editableScheduledEvent && event && this._editableScheduledEvent !== event) {
this.onRowEditSave(event);
}
this._editableScheduledEvent = event;
}
get editableScheduledEvent(): ScheduledEvent | undefined {
return this._editableScheduledEvent;
}
public constructor( public constructor(
private authService: AuthService, private authService: AuthService,
@ -221,7 +234,6 @@ export class ScheduledEventsComponent extends ComponentWithTable implements OnIn
if (this.isEditingNew) { if (this.isEditingNew) {
this.spinner.showSpinner(); this.spinner.showSpinner();
this.data.mutation<ScheduledEventMutationResult>(Mutations.createScheduledEvent, { this.data.mutation<ScheduledEventMutationResult>(Mutations.createScheduledEvent, {
id: newScheduledEvent.id,
interval: newScheduledEvent.interval, interval: newScheduledEvent.interval,
name: newScheduledEvent.name, name: newScheduledEvent.name,
description: newScheduledEvent.description, description: newScheduledEvent.description,
@ -266,7 +278,6 @@ export class ScheduledEventsComponent extends ComponentWithTable implements OnIn
this.toastService.success(this.translate.instant("view.server.ScheduledEvents.message.scheduled_event_update"), this.translate.instant("view.server.ScheduledEvents.message.scheduled_event_update_d", { name: newScheduledEvent.name })); this.toastService.success(this.translate.instant("view.server.ScheduledEvents.message.scheduled_event_update"), this.translate.instant("view.server.ScheduledEvents.message.scheduled_event_update_d", { name: newScheduledEvent.name }));
this.loadNextPage(); this.loadNextPage();
}); });
this.editableScheduledEvent = undefined;
} }
public deleteScheduledEvent(ScheduledEvent: ScheduledEvent): void { public deleteScheduledEvent(ScheduledEvent: ScheduledEvent): void {
@ -288,7 +299,7 @@ export class ScheduledEventsComponent extends ComponentWithTable implements OnIn
}); });
} }
public addScheduledEvent(table: Table): void { public addScheduledEvent(): void {
this.isEditingNew = true; this.isEditingNew = true;
this.editableScheduledEvent = JSON.parse(JSON.stringify(this.newScheduledEventTemplate)); this.editableScheduledEvent = JSON.parse(JSON.stringify(this.newScheduledEventTemplate));
// const newScheduledEvent = JSON.parse(JSON.stringify(this.newScheduledEventTemplate)); // const newScheduledEvent = JSON.parse(JSON.stringify(this.newScheduledEventTemplate));