2019-11-12 18:30:26 +01:00
|
|
|
import { ComponentType } from '@angular/cdk/portal';
|
|
|
|
import { Injectable } from '@angular/core';
|
2020-04-01 12:46:06 +02:00
|
|
|
import { MatDialog } from '@angular/material/dialog';
|
2019-11-12 18:30:26 +01:00
|
|
|
|
|
|
|
import { CollectionStringMapperService } from 'app/core/core-services/collection-string-mapper.service';
|
|
|
|
import { Collection } from 'app/shared/models/base/collection';
|
|
|
|
import { PollState, PollType } from 'app/shared/models/poll/base-poll';
|
|
|
|
import { mediumDialogSettings } from 'app/shared/utils/dialog-settings';
|
|
|
|
import { BasePollDialogComponent } from 'app/site/polls/components/base-poll-dialog.component';
|
|
|
|
import { ViewBasePoll } from 'app/site/polls/models/view-base-poll';
|
2020-03-17 18:21:13 +01:00
|
|
|
import { PollService } from 'app/site/polls/services/poll.service';
|
2019-11-12 18:30:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract class for showing a poll dialog. Has to be subclassed to provide the right `PollService`
|
|
|
|
*/
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
2020-03-17 18:21:13 +01:00
|
|
|
export abstract class BasePollDialogService<V extends ViewBasePoll, S extends PollService> {
|
|
|
|
protected dialogComponent: ComponentType<BasePollDialogComponent<V, S>>;
|
2019-11-12 18:30:26 +01:00
|
|
|
|
2020-02-24 16:55:07 +01:00
|
|
|
public constructor(private dialog: MatDialog, private mapper: CollectionStringMapperService) {}
|
2019-11-12 18:30:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens the dialog to enter votes and edit the meta-info for a poll.
|
|
|
|
*
|
|
|
|
* @param data Passing the (existing or new) data for the poll
|
|
|
|
*/
|
2020-02-24 16:55:07 +01:00
|
|
|
public async openDialog(viewPoll: Partial<V> & Collection): Promise<void> {
|
2019-11-12 18:30:26 +01:00
|
|
|
const dialogRef = this.dialog.open(this.dialogComponent, {
|
2020-02-24 16:55:07 +01:00
|
|
|
data: viewPoll,
|
2019-11-12 18:30:26 +01:00
|
|
|
...mediumDialogSettings
|
|
|
|
});
|
|
|
|
const result = await dialogRef.afterClosed().toPromise();
|
|
|
|
if (result) {
|
2020-02-24 16:55:07 +01:00
|
|
|
const repo = this.mapper.getRepository(viewPoll.collectionString);
|
|
|
|
if (!viewPoll.poll) {
|
2019-11-12 18:30:26 +01:00
|
|
|
await repo.create(result);
|
|
|
|
} else {
|
|
|
|
let update = result;
|
2020-02-24 16:55:07 +01:00
|
|
|
if (viewPoll.state !== PollState.Created) {
|
2019-11-12 18:30:26 +01:00
|
|
|
update = {
|
|
|
|
title: result.title,
|
|
|
|
onehundred_percent_base: result.onehundred_percent_base,
|
|
|
|
majority_method: result.majority_method,
|
|
|
|
description: result.description
|
|
|
|
};
|
2020-02-24 16:55:07 +01:00
|
|
|
if (viewPoll.type === PollType.Analog) {
|
2019-11-12 18:30:26 +01:00
|
|
|
update = {
|
|
|
|
...update,
|
|
|
|
votes: result.votes,
|
|
|
|
publish_immediately: result.publish_immediately
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2020-02-24 16:55:07 +01:00
|
|
|
await repo.patch(update, <V>viewPoll);
|
2019-11-12 18:30:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|