Reference Source

src/components/presenters/map-presenter.js

  1. import BasePresenter from './presenter';
  2. import { HomeM, MapM, ProfileM, AlertM } from '../models/export-models';
  3.  
  4. /**
  5. * Class for the Map Presenter.
  6. * @extends BasePresenter
  7. */
  8. class MapPresenter extends BasePresenter {
  9. /**
  10. * Creates an instance of the MapPresenter.
  11. * @constructor
  12. */
  13. constructor(view){
  14. super();
  15.  
  16. this.view = view;
  17. HomeM.subscribe(this);
  18. MapM.subscribe(this);
  19. }
  20. /**
  21. * @private
  22. * We're cheating here because the map presenter subscribes too late to receive data from the home model
  23. * so we just force a notifyAll and move on. This only happens when the component mounts
  24. */
  25. forceRequestData() {
  26. HomeM.forceNotifyAll();
  27. }
  28.  
  29. /**
  30. * Returns the data from the MapModel.
  31. *
  32. * @return {Object} The data from the map model.
  33. */
  34. getData = () => {
  35. return MapM.get();
  36. }
  37.  
  38. /**
  39. * If the view or presenter is destroyed, unsubscribe the presenter from the model.
  40. */
  41. onDestroy = () => {
  42. HomeM.unsubscribe(this);
  43. MapM.unsubscribe(this);
  44. };
  45.  
  46. /**
  47. * onUpdated only called from HomeModel because MapModel doesn't call it.
  48. * Since data in MapModel should be the same as the from the HomeModel, we don't have to read from the database in the MapModel
  49. * and we can just wait for data to be received.
  50. */
  51. onUpdated = (data) => {
  52. MapM.update(data);
  53. this.forceRefresh(); // We always want to force a refresh for the model because data only comes from the HomeModel
  54. }
  55.  
  56. /**
  57. * Force the view/presenter to refresh the view's data
  58. */
  59. forceRefresh = () => {
  60. this.view.setState({
  61. markers: this.getData()
  62. });
  63. };
  64.  
  65. /**
  66. * Get the profile picture from the profile model
  67. *
  68. * @param {Function} callback - A callback function that will be called with the resulting data
  69. */
  70. getProfileImage = (callback) => {
  71. ProfileM.getProfileData(callback);
  72. }
  73.  
  74. /**
  75. * Get the number of notifications from the Alerts model.
  76. *
  77. * @return {Number} The number of notifications
  78. */
  79. getNotificationCount = () => {
  80. return AlertM.getNotificationsCount();
  81. }
  82.  
  83. /**
  84. * Gets the user's current location from the model.
  85. *
  86. * @return {Object} The longitude and latitude of the user's current location
  87. */
  88. getUserLocation = () => {
  89. return MapM.getCurrentLocation();
  90. }
  91.  
  92. filterMarkers = (markers, selectedFilter) => {
  93. if (!selectedFilter || selectedFilter.length === 0 || selectedFilter.name === 'None') {
  94. return markers;
  95. }
  96. let newMarkers = markers;
  97. const filter = (selectedFilter.name).replace("< ", "");
  98. newMarkers = newMarkers.filter((el) => {
  99. const timeago = el.data.timeago;
  100. const timeAgoNums = parseInt((timeago).replace(/[^0-9]/g, ''));
  101. switch (filter) {
  102. case "1 min ago":
  103. return timeago.includes('s');
  104. break;
  105. case "1 hour ago":
  106. return timeago.includes('min') || timeago.includes('mins') || timeago.includes('s');
  107. break;
  108. case "12 hours ago":
  109. return timeAgoNums < 12 && (timeago.includes('h') || timeago.includes('min') || timeago.includes('mins') || timeago.includes('s'));
  110. break;
  111. case "1 day ago":
  112. return timeago.includes('h') || timeago.includes('min') || timeago.includes('mins') || timeago.includes('s');;
  113. break;
  114. case "7 days ago":
  115. return timeAgoNums < 7 && (timeago.includes('d') || timeago.includes('h') || timeago.includes('min') || timeago.includes('mins') || timeago.includes('s'));
  116. break;
  117. case "1 month ago":
  118. return (timeago.includes('d') || timeago.includes('h') || timeago.includes('min') || timeago.includes('mins') || timeago.includes('s'));
  119. break;
  120. case "1 year ago":
  121. return timeAgoNums < 365 && (timeago.includes('mo') || timeago.includes('d') || timeago.includes('h') || timeago.includes('min') || timeago.includes('mins') || timeago.includes('s'));
  122. break;
  123. }
  124. });
  125. return newMarkers;
  126. }
  127. }
  128. export default MapPresenter;