Skip to content

Commit

Permalink
#34: stores and applies reminder filter to local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirk Peter committed Aug 27, 2024
1 parent 6ed0988 commit 4955676
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ <h2 translate>Reminder</h2>

<footer>
<!-- type -->
<button *ngIf="reminder.reminderType" type="button" class="btn btn-clean" (click)="setTypeFilter(reminder.reminderType)">
<button *ngIf="types.length && reminder.reminderType" type="button" class="btn btn-clean" (click)="setTypeFilter(reminder.reminderType)">
<i class="m-r-4" [c8yIcon]="'tag'"></i>
<c8y-reminder-type [id]="reminder.reminderType"></c8y-reminder-type>
</button>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Component, OnDestroy } from '@angular/core';
import { AlertService, HeaderService } from '@c8y/ngx-components';
import { has } from 'lodash';
import { BsModalService } from 'ngx-bootstrap/modal';
import { BehaviorSubject, Subscription } from 'rxjs';
import {
Expand All @@ -11,6 +12,7 @@ import {
ReminderType,
REMINDER_DRAWER_OPEN_CLASS,
REMINDER_MAIN_HEADER_CLASS,
REMINDER_TYPE_FRAGMENT,
} from '../../reminder.model';
import { ReminderService } from '../../services/reminder.service';
import { ReminderModalComponent } from '../reminder-modal/reminder-modal.component';
Expand Down Expand Up @@ -52,7 +54,9 @@ export class ReminderDrawerComponent implements OnDestroy {
private reminderService: ReminderService,
private alertService: AlertService,
private modalService: BsModalService
) {
) {
this.initFilter();

// check if the actual drawer was opened
this.subscriptions.add(
this.headerService.rightDrawerOpen$.subscribe((open) => {
Expand All @@ -71,9 +75,6 @@ export class ReminderDrawerComponent implements OnDestroy {
this.digestReminders(reminders)
)
);

// get reminder types from service
this.types = this.reminderService.types;
}

ngOnDestroy(): void {
Expand Down Expand Up @@ -118,11 +119,12 @@ export class ReminderDrawerComponent implements OnDestroy {
this.filterReminders();
}

filterReminders() {
filterReminders(): void {
this.reminderGroups = this.reminderService.groupReminders(
this.reminders,
this.buildFilter()
);
this.reminderService.storeFilterConfig();
}

private toggleRightDrawer(open: boolean): void {
Expand Down Expand Up @@ -156,8 +158,23 @@ export class ReminderDrawerComponent implements OnDestroy {
const filters: ReminderGroupFilter = {};

// populate filters
if (this.typeFilter !== '') filters['reminderType'] = this.typeFilter;
if (this.typeFilter !== '')
filters[REMINDER_TYPE_FRAGMENT] = this.typeFilter;

return Object.keys(filters).length > 0 ? filters : null;
}

private initFilter(): void {
this.types = this.reminderService.types;

if (!this.types.length) {
this.reminderService.resetFilterConfig();
return;
}

const filters = this.reminderService.filters;

if (has(filters, REMINDER_TYPE_FRAGMENT))
this.typeFilter = filters[REMINDER_TYPE_FRAGMENT];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ReminderType,
REMINDER_TEXT_LENGTH,
REMINDER_TYPE,
REMINDER_TYPE_FRAGMENT,
} from '../../reminder.model';
import { ReminderService } from '../../services';

Expand Down Expand Up @@ -182,7 +183,7 @@ export class ReminderModalComponent implements OnInit {
if (!this.typeOptions.length) return;

this.fields.push({
key: 'reminderType',
key: REMINDER_TYPE_FRAGMENT,
type: 'select',
props: {
label: this.translateService.instant('Reminder type (optional)'),
Expand Down
8 changes: 5 additions & 3 deletions src/app/reminder-plugin/reminder.model.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { IEvent } from '@c8y/client';
import { IEvent, ITenantOption } from '@c8y/client';

export const REMINDER_TYPE = 'c8y_Reminder';
export const REMINDER_TYPE_FRAGMENT = 'reminderType';
export const REMINDER_INITIAL_QUERY_SIZE = 100;
export const REMINDER_DRAWER_OPEN_CLASS = 'drawerOpen';
export const REMINDER_MAIN_HEADER_CLASS = 'app-main-header';
export const REMINDER_MAX_COUNTER = 10;
export const REMINDER_TEXT_LENGTH = 100;
export const REMINDER_TENENAT_OPTION_CATEGORY = 'c8y.reminder';
export const REMINDER_TENENAT_OPTION_TYPE_KEY = 'types';
export const REMINDER_TENENAT_OPTION_CATEGORY: ITenantOption['category'] = 'c8y.reminder';
export const REMINDER_TENENAT_OPTION_TYPE_KEY: ITenantOption['key'] = 'types';
export const REMINDER_LOCAL_STORAGE_FILTER = 'c8y_rpFilter';

export const ReminderGroupStatus = {
due: 'DUE',
Expand Down
32 changes: 31 additions & 1 deletion src/app/reminder-plugin/services/reminder.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
ReminderStatus,
ReminderType,
REMINDER_INITIAL_QUERY_SIZE,
REMINDER_LOCAL_STORAGE_FILTER,
REMINDER_TENENAT_OPTION_CATEGORY,
REMINDER_TENENAT_OPTION_TYPE_KEY,
REMINDER_TYPE,
Expand All @@ -36,13 +37,18 @@ export class ReminderService {
return this._types;
}

get filters(): ReminderGroupFilter {
return this._filters;
}

private subscription = new Subscription();
private drawerRef?: ComponentRef<unknown>;
private drawer?: ReminderDrawerComponent;
private updateTimer?: NodeJS.Timeout;
private _reminderCounter = 0;
private _reminders: Reminder[] = [];
private _types: ReminderType[] = [];
private _filters: ReminderGroupFilter;

private get reminders(): Reminder[] {
return this._reminders;
Expand Down Expand Up @@ -73,6 +79,7 @@ export class ReminderService {
async init(): Promise<void> {
if (this.drawer) return;

this.loadFilterConfig();
this._types = await this.fetchReminderTypes();
void this.fetchActiveReminderCounter();
this.createDrawer();
Expand All @@ -99,7 +106,7 @@ export class ReminderService {

groupReminders(
reminders: Reminder[],
filters?: ReminderGroupFilter
filters = this._filters
): ReminderGroup[] {
let dueDate: number;
const now = new Date().getTime();
Expand Down Expand Up @@ -140,13 +147,28 @@ export class ReminderService {
upcoming.reminders = sortBy(upcoming.reminders, ['time']);
cleared.reminders = sortBy(cleared.reminders, ['lastUpdated']).reverse();

this._filters = filters;

return this.applyFilters([due, upcoming, cleared], filters);
}

clear(): void {
this.reminders = [];
}

storeFilterConfig(): void {
if (!this._filters) this.resetFilterConfig();

localStorage.setItem(
REMINDER_LOCAL_STORAGE_FILTER,
JSON.stringify(this._filters)
);
}

resetFilterConfig(): void {
localStorage.removeItem(REMINDER_LOCAL_STORAGE_FILTER);
}

async update(reminder: Reminder): Promise<IResult<Reminder>> {
const event: Partial<IEvent> = {
id: reminder.id,
Expand Down Expand Up @@ -393,4 +415,12 @@ export class ReminderService {
if (!check) return;
return reminder;
}

private loadFilterConfig(): void {
const stored = JSON.parse(
localStorage.getItem(REMINDER_LOCAL_STORAGE_FILTER)
);

if (stored) this._filters = stored;
}
}

0 comments on commit 4955676

Please sign in to comment.