Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.18 KB | None | 0 0
  1. import { Component, OnInit, Query } from '@angular/core';
  2. import {
  3. GetAppointments,
  4. GetAppointmentsGQL,
  5. HealthSystemStatusesGQL,
  6. ApptChartDataGQL,
  7. ApptChartData,
  8. ApptCalendarMonth,
  9. ApptCalendarSidebar,
  10. ApptCalendarMonthGQL,
  11. ApptCalendarSidebarGQL,
  12. ApptQueue,
  13. ApptQueueGQL,
  14. ChangeApptStatusGQL,
  15. GetSinglePatientGQL,
  16. GetSinglePatient,
  17. } from '../../generated/graphql';
  18. import { Subscription } from 'rxjs';
  19. import gql from 'graphql-tag';
  20. import { QueryRef } from 'apollo-angular';
  21. import { isEqual, format, getMonth, getDay, getYear, getDate } from 'date-fns';
  22. import { PusherService } from 'src/app/services/pusher.service';
  23. import { NzMessageService } from 'ng-zorro-antd';
  24.  
  25. @Component({
  26. selector: 'app-appointment-requests',
  27. templateUrl: './appointment-requests.page.html',
  28. styleUrls: ['./appointment-requests.page.scss'],
  29. })
  30. export class AppointmentRequestsPage implements OnInit {
  31. loading: boolean = true;
  32. status_ids: Array<any>;
  33. // heatlh_system_statuses
  34. statuses: any;
  35. statuses_subscription: Subscription;
  36. statuses_loading: boolean = true;
  37. // appointments
  38. today_appointments: any;
  39. appointments_queue_query: QueryRef<ApptQueue.Query, ApptQueue.Variables>;
  40. appointments_queue_sub: Subscription;
  41. appointments_display_data: Array<any> = [];
  42. // filtering
  43. listOfFilterStatus = [
  44. { text: 'Joe', value: 'Joe' },
  45. { text: 'Jim', value: 'Jim' }
  46. ];
  47. sortValue = null;
  48. sortKey = null;
  49. // pagination
  50. pageIndex: number = 1;
  51. pageSize: number;
  52. total: number;
  53. searchStatusList: number[] = [];
  54. // editing
  55. editCache = {};
  56. // chart
  57. chart_query: QueryRef<ApptChartData.Query, ApptChartData.Variables>;
  58. chart_query_sub: Subscription;
  59. chart_data: any[];
  60. colorScheme = {
  61. domain: ['#1d68fb', '#33c0fc', '#4afffe', '#afffff', '#fffc63', '#fdbd2d']
  62. };
  63. chart_view: any[] = undefined;
  64. // calendar
  65. selectedDate = new Date();
  66. selectedMonth = getMonth(new Date()) + 1;
  67. selectedDay = getDate(new Date());
  68. selectedYear = getYear(new Date());
  69. appt_month_query: QueryRef<ApptCalendarMonth.Query, ApptCalendarMonth.Variables>;
  70. appt_month_data: any[] = [];
  71. appt_month_sub: Subscription;
  72. appt_sidebar_query: QueryRef<ApptCalendarSidebar.Query, ApptCalendarSidebar.Variables>;
  73. appt_sidebar_sub: Subscription;
  74. appt_sidebar_data: any[] = [];
  75. appt_sidebar_loading: boolean = true;
  76. // drawer
  77. detail_drawer_visible = false;
  78. detail_id: string = null;
  79. detail_data: GetSinglePatient.Patient = null;
  80.  
  81.  
  82. constructor(
  83. private getStatuses: HealthSystemStatusesGQL,
  84. private getAppointments: GetAppointmentsGQL,
  85. private getChartData: ApptChartDataGQL,
  86. private getApptMonth: ApptCalendarMonthGQL,
  87. private getApptSidebar: ApptCalendarSidebarGQL,
  88. private getQueue: ApptQueueGQL,
  89. private changeAppt: ChangeApptStatusGQL,
  90. private Pusher: PusherService,
  91. private message: NzMessageService,
  92. private getPatient: GetSinglePatientGQL
  93. ) { }
  94.  
  95. ngOnInit() {
  96. this.getStatuses.watch({}, {
  97. fetchPolicy: 'network-only'
  98. }).valueChanges.subscribe(result => {
  99. console.log('statuses', result);
  100. this.listOfFilterStatus = result.data.statuses.data.map(item => {
  101. return {
  102. text: item.status_name,
  103. value: item.id
  104. };
  105. });
  106. this.statuses_loading = result.loading;
  107. this.statuses = result.data.statuses.data;
  108. // this.status_ids = result.data.statuses.data.map(item => item.id);
  109. // should only fetch In-Queue appts
  110. this.status_ids = [1];
  111.  
  112. this.fetchChartData();
  113. // this.fetchAppointments();
  114. this.fetchQueue();
  115. this.fetchApptCalendarMonth();
  116. this.fetchApptCalendarDay();
  117. });
  118.  
  119. this.Pusher.channel.bind('App\\Events\\DataUpdated', data => {
  120. console.log('pusher data', data);
  121. this.message.loading('Refreshing data', { nzDuration: 1000 });
  122. this.appointments_queue_query.refetch({ page_index: this.pageIndex });
  123. this.fetchChartData();
  124. this.fetchApptCalendarMonth();
  125. });
  126. }
  127.  
  128.  
  129. selectDateChange(select: Date): void {
  130. console.log(`Select value: ${select}`);
  131. if (this.selectedMonth !== getMonth(select) + 1) {
  132. console.log('different month rerun query');
  133. this.selectedMonth = getMonth(select) + 1;
  134. this.selectedDay = getDate(select);
  135. this.fetchApptCalendarMonth();
  136. }
  137.  
  138. this.selectedMonth = getMonth(select) + 1;
  139. this.selectedDay = getDate(select);
  140. this.selectedYear = getYear(select);
  141. this.fetchApptCalendarDay();
  142. }
  143.  
  144.  
  145. isDateEqual(date_1: string, date_2: string) {
  146. const format_date_1 = format(
  147. date_1,
  148. 'MM/DD/YYYY'
  149. );
  150.  
  151. const format_date_2 = format(
  152. date_2,
  153. 'MM/DD/YYYY'
  154. );
  155.  
  156. return isEqual(
  157. format_date_1,
  158. format_date_2
  159. );
  160. }
  161.  
  162.  
  163. fetchAppointments() {
  164. this.getAppointments.watch({ status_ids: this.status_ids, page_index: this.pageIndex }, { fetchPolicy: 'network-only'}).valueChanges.subscribe(result => {
  165. console.log('appointments', result);
  166. this.loading = result.loading;
  167. this.appointments_display_data = result.data.appointments.data.appointments;
  168. this.pageIndex = result.data.appointments.data.current_page;
  169. this.pageSize = result.data.appointments.data.appointments.length;
  170. this.total = result.data.appointments.data.total;
  171. this.updateEditCache();
  172. });
  173. }
  174.  
  175. fetchQueue() {
  176. this.loading = true;
  177. this.appointments_queue_query = this.getQueue.watch({ page_index: this.pageIndex }, { fetchPolicy: 'network-only' });
  178.  
  179. this.appointments_queue_sub = this.appointments_queue_query.valueChanges.subscribe(result => {
  180. console.log('appointments queue', result);
  181.  
  182. if (!result.data.queue.data.appointments) {
  183. this.appointments_display_data = [];
  184. this.pageIndex = 1;
  185. this.pageSize = 0;
  186. this.total = 0;
  187. this.updateEditCache();
  188. this.loading = false;
  189. return;
  190. }
  191.  
  192. this.loading = result.loading;
  193. this.appointments_display_data = result.data.queue.data.appointments;
  194. this.pageIndex = result.data.queue.data.current_page;
  195. this.pageSize = result.data.queue.data.appointments.length;
  196. this.total = result.data.queue.data.total;
  197. this.updateEditCache();
  198. this.loading = false;
  199. });
  200. }
  201.  
  202. fetchApptCalendarMonth() {
  203. console.log(this.selectedMonth, this.selectedDay, this.selectedYear, new Date());
  204. this.appt_month_query = this.getApptMonth.watch({
  205. status_ids: this.status_ids,
  206. page_index: this.pageIndex,
  207. year: this.selectedYear,
  208. month: this.selectedMonth
  209. }, {
  210. fetchPolicy: 'network-only'
  211. });
  212.  
  213. this.appt_month_sub = this.appt_month_query.valueChanges.subscribe(result => {
  214. console.log('appt_month_query', result);
  215. this.appt_month_data = result.data.appointments_month_view.data.appointments;
  216. });
  217. }
  218.  
  219. fetchApptCalendarDay() {
  220. this.appt_sidebar_loading = true;
  221. this.appt_sidebar_query = this.getApptSidebar.watch({
  222. status_ids: this.status_ids,
  223. page_index: this.pageIndex,
  224. year: this.selectedYear,
  225. month: this.selectedMonth,
  226. day: this.selectedDay
  227. }, {
  228. fetchPolicy: 'network-only'
  229. });
  230.  
  231. this.appt_sidebar_sub = this.appt_sidebar_query.valueChanges.subscribe(result => {
  232. console.log('appt_sidebar_query', result);
  233. this.appt_sidebar_loading = result.loading;
  234. this.appt_sidebar_data = result.data.appointments_day_view.data.appointments;
  235. });
  236. }
  237.  
  238. fetchChartData() {
  239. this.chart_query = this.getChartData.watch({}, {
  240. fetchPolicy: 'network-only'
  241. });
  242. this.chart_query_sub = this.chart_query.valueChanges.subscribe(result => {
  243. console.log(result);
  244. this.chart_data = result.data.appointment_chart_data.data.statuses;
  245. });
  246. }
  247.  
  248. sort(sort: { key: string, value: string }): void {
  249. this.sortKey = sort.key;
  250. this.sortValue = sort.value;
  251. this.searchData();
  252. }
  253.  
  254. searchData(reset: boolean = false): void {
  255. if (reset) {
  256. this.pageIndex = 1;
  257. }
  258.  
  259. this.loading = true;
  260.  
  261. this.appointments_queue_query.fetchMore({
  262. variables: {
  263. page_index: this.pageIndex
  264. },
  265. updateQuery: (prev, { fetchMoreResult }) => {
  266. if (!fetchMoreResult) { return prev; }
  267.  
  268. console.log(fetchMoreResult.queue);
  269. this.loading = false;
  270. this.total = fetchMoreResult.queue.data.total;
  271. this.appointments_display_data = fetchMoreResult.queue.data.appointments;
  272. this.updateEditCache();
  273. },
  274. });
  275. }
  276.  
  277. updateFilter(value: number[]): void {
  278. this.searchStatusList = value;
  279. this.searchData(true);
  280. }
  281.  
  282. startEdit(id: string): void {
  283. this.editCache[ id ].edit = true;
  284. }
  285.  
  286. cancelEdit(id: string): void {
  287. const index = this.appointments_display_data.findIndex(item => item.id === id);
  288. this.editCache[ id ] = {
  289. data: { ...this.appointments_display_data[ index ] },
  290. edit: false
  291. };
  292. }
  293.  
  294. saveEdit(id: string): void {
  295. const index = this.appointments_display_data.findIndex(item => item.id === id);
  296. const status_id = this.statuses.filter(item => item.status_name === this.editCache[id].data.status)[0].id;
  297. const appt_id = this.editCache[id].data.id;
  298. Object.assign(this.appointments_display_data[ index ], this.editCache[ id ].data);
  299. this.changeApptStatus(appt_id, status_id);
  300. this.editCache[ id ].edit = false;
  301. }
  302.  
  303. updateEditCache(): void {
  304. this.appointments_display_data.forEach(item => {
  305. this.editCache[ item.id ] = {
  306. edit: false,
  307. data: { ...item }
  308. };
  309. });
  310. }
  311.  
  312. openDetail(id, patient_id) {
  313. console.log(id, patient_id);
  314. this.detail_drawer_visible = true;
  315. this.detail_data = patient_id;
  316. this.getPatient.fetch({ id: patient_id }).subscribe(result => {
  317. console.log(result);
  318. this.detail_data = result.data.patient;
  319. });
  320. }
  321.  
  322. closeDetail() {
  323. this.detail_drawer_visible = false;
  324. this.detail_data = null;
  325. // unsub from appt query
  326. }
  327.  
  328. changeApptStatus(appt_id, status_id) {
  329. this.loading = true;
  330. this.changeAppt.mutate({
  331. id: appt_id,
  332. status_id: status_id
  333. }).subscribe(result => {
  334. console.log(result);
  335. if (result.data.changeAppointmentStatus.success) {
  336. this.loading = false;
  337. this.appointments_queue_query.refetch();
  338. this.chart_query.refetch();
  339. }
  340. });
  341. }
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement