state-storage.service.ts 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { Injectable } from '@angular/core';
  2. import { SessionStorageService } from 'ngx-webstorage';
  3. @Injectable({ providedIn: 'root' })
  4. export class StateStorageService {
  5. constructor(private $sessionStorage: SessionStorageService) {}
  6. getPreviousState() {
  7. return this.$sessionStorage.retrieve('previousState');
  8. }
  9. resetPreviousState() {
  10. this.$sessionStorage.clear('previousState');
  11. }
  12. storePreviousState(previousStateName, previousStateParams) {
  13. const previousState = { name: previousStateName, params: previousStateParams };
  14. this.$sessionStorage.store('previousState', previousState);
  15. }
  16. getDestinationState() {
  17. return this.$sessionStorage.retrieve('destinationState');
  18. }
  19. storeUrl(url: string) {
  20. this.$sessionStorage.store('previousUrl', url);
  21. }
  22. getUrl() {
  23. return this.$sessionStorage.retrieve('previousUrl');
  24. }
  25. storeDestinationState(destinationState, destinationStateParams, fromState) {
  26. const destinationInfo = {
  27. destination: {
  28. name: destinationState.name,
  29. data: destinationState.data
  30. },
  31. params: destinationStateParams,
  32. from: {
  33. name: fromState.name
  34. }
  35. };
  36. this.$sessionStorage.store('destinationState', destinationInfo);
  37. }
  38. }