notes.service.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { Injectable } from '@angular/core';
  2. import { JournalEntry } from 'src/interfaces/interfaces';
  3. @Injectable({
  4. providedIn: 'root',
  5. })
  6. export class NotesService {
  7. public isUnsaved: boolean = false;
  8. private isNewEntry: boolean = false;
  9. private unsavedEntry: JournalEntry | null = null;
  10. private unsavedIndex: number = -1;
  11. /**
  12. * Stores the unsaved entry and the index where it can be found.
  13. * @param entry The entry that was edited but not yet saved.
  14. * @param index The index of the entry in the entries list.
  15. */
  16. public setEntry(
  17. entry: JournalEntry,
  18. index: number,
  19. isNewEntry: boolean,
  20. ): void {
  21. this.unsavedEntry = entry;
  22. this.unsavedIndex = index;
  23. this.isNewEntry = isNewEntry;
  24. this.isUnsaved = true;
  25. }
  26. /**
  27. * Returns the entry that was edited but not yet saved when leaving the notes view.
  28. * @returns The unsaved entry and its index.
  29. */
  30. public getEntry(): { [key: string]: any } {
  31. return {
  32. entry: this.unsavedEntry,
  33. index: this.unsavedIndex,
  34. isNewEntry: this.isNewEntry,
  35. };
  36. }
  37. /**
  38. * Marks the entry as saved and resets the unsaved entry and index.
  39. */
  40. public markAsSaved(): void {
  41. this.isUnsaved = false;
  42. this.unsavedEntry = null;
  43. this.isNewEntry = false;
  44. this.unsavedIndex = -1;
  45. }
  46. }