Skip to content

Commit

Permalink
Refactored update code
Browse files Browse the repository at this point in the history
Used filter method instead of foreach loop
extracted hardcoded repetition type to constant.
  • Loading branch information
vijayjangid authored Oct 18, 2019
1 parent 062d836 commit e3a6d46
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/app/services/notes_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ let localDB = {
binNotes : new PouchDB('bin_notes_table')
}

const REPEATITION_TYPE = {
doesNotRepeat : 'doesnotrepeat',
daily : 'daily',
weekly : 'weekly',
monthly : 'monthly',
yearly : 'yearly'
}

@Injectable()
export class NotesTableService {
notes_tables_source = new BehaviorSubject<NotesTable[]>([]);
Expand Down Expand Up @@ -63,13 +71,7 @@ export class NotesTableService {
updateReminderTable(schema: string) {
this.getNotes(schema).then(
alldoc => {
let rows = alldoc.rows;
this.reminderTable[schema] = [];
rows.forEach(row => {
if (row.doc.reminder) {
this.reminderTable[schema].push(row);
}
});
this.reminderTable[schema] = alldoc.rows.filter((row) => row.doc.reminder);
});
}

Expand All @@ -92,7 +94,7 @@ export class NotesTableService {
checkForReminderRepeatation(row, todayDate, schema) {
let reminderDate = new Date(row.doc.reminder.date);
let repeatText = row.doc.reminder.repeat;
if (repeatText === 'doesnotrepeat') {
if (repeatText === REPEATITION_TYPE.doesNotRepeat) {
if (todayDate.getFullYear() === reminderDate.getFullYear() &&
todayDate.getMonth() === reminderDate.getMonth() &&
todayDate.getDate() === reminderDate.getDate() &&
Expand All @@ -101,27 +103,27 @@ export class NotesTableService {
{
this.pushNotification(row);
}
} else if (repeatText === 'daily') {
} else if (repeatText === REPEATITION_TYPE.daily) {
if (todayDate.getHours() === reminderDate.getHours() &&
todayDate.getMinutes() === reminderDate.getMinutes())
{
this.pushNotification(row);
}
} else if (repeatText === 'weekly') {
} else if (repeatText === REPEATITION_TYPE.weekly) {
if (todayDate.getDay() === reminderDate.getDay() &&
todayDate.getHours() === reminderDate.getHours() &&
todayDate.getMinutes() === reminderDate.getMinutes())
{
this.pushNotification(row);
}
} else if (repeatText === 'monthly') {
} else if (repeatText === REPEATITION_TYPE.monthly) {
if (todayDate.getDate() === reminderDate.getDate() &&
todayDate.getHours() === reminderDate.getHours() &&
todayDate.getMinutes() === reminderDate.getMinutes())
{
this.pushNotification(row);
}
} else if (repeatText === 'yearly') {
} else if (repeatText === REPEATITION_TYPE.yearly) {
if (todayDate.getMonth() === reminderDate.getMonth() &&
todayDate.getDate() === reminderDate.getDate() &&
todayDate.getHours() === reminderDate.getHours() &&
Expand Down

0 comments on commit e3a6d46

Please sign in to comment.