Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable and disable surveys #2179

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/app/system/manage-surveys/manage-surveys.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@
<ng-container matColumnDef="status">
<th mat-header-cell *matHeaderCellDef mat-sort-header> {{ 'labels.inputs.Status' | translate }} </th>
<td mat-cell *matCellDef="let survey">
<div [className]="!isActive(survey.validFrom, survey.validTo) === true ? ('labels.inputs.Disabled' | translate) : ('labels.inputs.Enabled' | translate)">
<fa-icon matTooltip="{{ !isActive(survey.validFrom, survey.validTo) === true ? ('labels.inputs.Disabled' | translate) : ('labels.inputs.Enabled' | translate) }}" matTooltipPosition="right" icon="circle" size="lg"></fa-icon>
<div [className]="isActive(survey.validFrom, survey.validTo) ? 'enabled' : 'disabled'">
<fa-icon matTooltip="{{ !isActive(survey.validFrom, survey.validTo) ? ('labels.inputs.Disabled' | translate) : ('labels.inputs.Enabled' | translate) }}" matTooltipPosition="right" icon="circle" size="lg"></fa-icon>
</div>
</td>
</ng-container>

<ng-container matColumnDef="action">
<th mat-header-cell *matHeaderCellDef mat-sort-header> {{'labels.inputs.Action' | translate}} </th>
<td mat-cell *matCellDef="let survey">
<button mat-button *ngIf="!isActive(survey.validFrom, survey.validTo)" color="accent">
<button mat-button *ngIf="!isActive(survey.validFrom, survey.validTo)" color="accent" (click)="activate(survey); $event.stopPropagation();">
<fa-icon icon="lock-open" class="m-r-10"></fa-icon>{{'labels.buttons.Activate' | translate }}
</button>
<button mat-button *ngIf="isActive(survey.validFrom, survey.validTo)" color="warn">
<button mat-button *ngIf="isActive(survey.validFrom, survey.validTo)" color="warn" (click)="deactivate(survey); $event.stopPropagation();">
<fa-icon icon="lock" class="m-r-10"></fa-icon>{{'labels.buttons.Deactivate' | translate}}
</button>
</td>
Expand Down
45 changes: 39 additions & 6 deletions src/app/system/manage-surveys/manage-surveys.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
import { ActivatedRoute } from '@angular/router';

/** Custom Services */
import { SystemService } from '../system.service';

/**
* Manage Surveys component.
*/
Expand All @@ -31,20 +34,23 @@ export class ManageSurveysComponent implements OnInit {
* Retrieves the surveys data from `resolve`.
* @param {ActivatedRoute} route Activated Route.
*/
constructor(private route: ActivatedRoute) {
constructor(
private route: ActivatedRoute,
private systemService: SystemService,
) {
this.route.data.subscribe(( data: { surveys: any }) => {
this.surveysData = data.surveys;
});
}

/**
* Returns whether an survey is active based on its duration
* @param {number} validFrom Date valid from
* @param {number} validTo Date valid to
* @param {string} validFrom Date valid from (yyyy-MM-dd)
* @param {string} validTo Date valid to (yyyy-MM-dd)
*/
isActive(validFrom: number, validTo: number) {
const curdate = new Date().getTime();
return (curdate > validFrom && curdate < validTo);
isActive(validFrom: string, validTo: string) {
const curdate = new Date().toISOString().split('T')[0];
return (curdate >= validFrom && curdate <= validTo);
}

/**
Expand Down Expand Up @@ -78,4 +84,31 @@ export class ManageSurveysComponent implements OnInit {
this.dataSource.filter = filterValue.trim().toLowerCase();
}

/**
* Activates a survey.
* @param {any} survey Survey to activate.
*/
activate(survey: any) {
this.systemService.activateSurvey(survey.id).subscribe(() => {
const today = new Date().toISOString().split('T')[0];
// This mimics the server-side logic
survey.validFrom = today;
survey.validTo = today;
});
}

/**
* Deactivates a survey.
* @param {any} survey Survey to deactivate.
*/
deactivate(survey: any) {
this.systemService.deactivateSurvey(survey.id).subscribe(() => {
const date = new Date();
date.setDate(date.getDate() - 1); // Set to yesterday
const yesterday = date.toISOString().split('T')[0];
// This mimics the server-side logic
survey.validTo = yesterday;
});
}

}
18 changes: 18 additions & 0 deletions src/app/system/system.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,24 @@ export class SystemService {
return this.http.put(`/surveys/${surveyId}`, survey);
}

/**
* Activates a survey.
* @param {number} surveyId Survey ID.
* @returns {Observable<any>}
*/
activateSurvey(surveyId: number): Observable<any> {
return this.http.post(`/surveys/${surveyId}?command=activate`, null);
}

/**
* Deactivates a survey.
* @param {number} surveyId Survey ID.
* @returns {Observable<any>}
*/
deactivateSurvey(surveyId: number): Observable<any> {
return this.http.post(`/surveys/${surveyId}?command=deactivate`, null);
}


/**
* @returns {Observable<any>} Fetches Jobs.
Expand Down
Loading