Advertisement
Guest User

Untitled

a guest
Apr 17th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.54 KB | None | 0 0
  1. /* eslint no-param-reassign: "off" */
  2. import { types, flow } from 'mobx-state-tree';
  3. import Moment from 'moment';
  4. import 'moment/locale/ru';
  5. import Api from '../Service/Api';
  6. import ClinicStore from './ClinicStore';
  7. import DoctorStore from './DoctorStore';
  8. import ServiceStore from './ServiceStore';
  9. import ChatStore from './ChatStore';
  10. import ScheduleStore from './ScheduleStore';
  11. import BookingStore from './BookingStore';
  12. import UserStore from './UserStore';
  13.  
  14. const MainStore = types.model('MainStore', {
  15. doctor: types.maybe(types.reference(DoctorStore)),
  16. clinic: types.maybe(types.reference(ClinicStore)),
  17. service: types.maybe(types.reference(ServiceStore)),
  18. chat: types.maybe(types.reference(ChatStore)),
  19. schedule: types.maybe(types.reference(ScheduleStore)),
  20. appointment: types.maybe(types.reference(BookingStore)),
  21. booking: BookingStore,
  22. user: UserStore,
  23.  
  24. doctorList: types.array(DoctorStore),
  25. clinicList: types.array(ClinicStore),
  26. serviceList: types.array(ServiceStore),
  27. popularServiceList: types.optional(types.array(types.reference(ServiceStore)), []),
  28. chatList: types.array(ChatStore),
  29. scheduleList: types.array(ScheduleStore),
  30. appointmentList: types.array(BookingStore),
  31.  
  32. doctorListFiltrated: types.array(types.reference(DoctorStore)),
  33. clinicListFiltrated: types.array(types.reference(ClinicStore)),
  34. serviceListFiltrated: types.array(types.reference(ServiceStore)),
  35.  
  36. fetching: false,
  37. }).actions((self => ({
  38. afterCreate: () => {
  39. },
  40. loadData: flow(function* loadData() {
  41. self.setFetching(true);
  42. try {
  43. const serviceResponse = yield Api.getServiceList();
  44. if (
  45. serviceResponse &&
  46. serviceResponse.status &&
  47. serviceResponse.data.services &&
  48. Number(serviceResponse.data.code) === 200
  49. ) {
  50. self.setServiceList(serviceResponse.data.services);
  51. self.setServiceListFiltrated(self.serviceList);
  52. }
  53. const doctorResponse = yield Api.getDoctorList();
  54. if (
  55. doctorResponse &&
  56. doctorResponse.status &&
  57. doctorResponse.data.doctors &&
  58. Number(doctorResponse.data.code) === 200
  59. ) {
  60. self.setDoctorList(doctorResponse.data.doctors.map((doctor) => {
  61. const servicesIdList = serviceResponse.data.services
  62. .filter(service => service.doctors.some(d => d.id === doctor.id))
  63. .map(s => String(s.id));
  64.  
  65. return {
  66. ...doctor,
  67. serviceList: servicesIdList,
  68. };
  69. }));
  70. self.setDoctorListFiltrated(self.doctorList);
  71. }
  72. const clinicResponse = yield Api.getClinicList();
  73. if (
  74. clinicResponse &&
  75. clinicResponse.ok &&
  76. clinicResponse.data &&
  77. Number(clinicResponse.data.code) === 200
  78. ) {
  79. self.setClinicList(clinicResponse.data.clinics.map((clinic) => {
  80. const servicesIdList = serviceResponse.data.services
  81. .filter(service => service.clinics.some(c => c.id === clinic.id))
  82. .map(s => String(s.id));
  83. const doctorsIdList = doctorResponse.data.doctors
  84. .filter(doctor => doctor.clinics.some(c => c.id === clinic.id))
  85. .map(d => String(d.id));
  86.  
  87. return {
  88. ...clinic,
  89. serviceList: servicesIdList,
  90. doctorList: doctorsIdList,
  91. };
  92. }));
  93. self.setClinicListFiltrated(self.clinicList);
  94. }
  95. const appointmentResponse = yield Api.getAppointmentList(self.user.id);
  96. if (
  97. appointmentResponse &&
  98. appointmentResponse.ok &&
  99. appointmentResponse.data &&
  100. appointmentResponse.data.data &&
  101. appointmentResponse.data.data.listPacients &&
  102. Number(appointmentResponse.data.code) === 200
  103. ) {
  104. const protocolList = [];
  105. const pacientList = Array.isArray(appointmentResponse.data.data.listPacients)
  106. ? appointmentResponse.data.data.listPacients
  107. : [appointmentResponse.data.data.listPacients];
  108. pacientList.forEach((patient, patientIndex) => {
  109. patient.listProtocol.forEach((protocol, protocolIndex) => {
  110. const possibleDoctors = self.doctorList.filter(doc => doc.name === protocol.specialist);
  111. let doctor = null;
  112. if (possibleDoctors.length) {
  113. doctor = possibleDoctors[0].id;
  114. }
  115. const serviceList = self.serviceList.filter(ser => ser.title === protocol.header);
  116. protocolList.push({
  117. id: `${patientIndex}-${protocolIndex}`,
  118. user: self.user.id,
  119. date: new Date(protocol.dateReceipt),
  120. doctor,
  121. serviceList,
  122. status: BookingStore.STATUS.RECORDED,
  123. });
  124. });
  125. });
  126. self.setAppointmentList(protocolList);
  127. }
  128. self.setPopularServiceList(['002978', '000032']);
  129. } catch (ex) {
  130. console.error(ex, 'ERROR! the data upload failed');
  131. } finally {
  132. self.setFetching(false);
  133. }
  134. }),
  135. getScheduleList: flow(function* getScheduleList(clinicId, doctorId, offset = 0) {
  136. const result = [];
  137. try {
  138. const response = yield Api.getScheduleList(clinicId, doctorId, offset);
  139. if (
  140. response &&
  141. response.status &&
  142. Number(response.data.code) === 200 &&
  143. response.data.grid &&
  144. response.data.dates
  145. ) {
  146. response.data.dates.forEach((date, index) => {
  147. const schedule = {
  148. date: new Date(date),
  149. id: `${date}_${index}`,
  150. timeList: [],
  151. };
  152. if (response.data.grid.length > index &&
  153. response.data.grid[index].some(g => String(g.status) !== ScheduleStore.NOT_AVAILABLE)) {
  154. schedule.timeList = response.data.grid[index]
  155. .filter(g => Number(g.status) !== ScheduleStore.NOT_AVAILABLE)
  156. .map(g => ({
  157. ...g,
  158. status: String(g.status),
  159. id: `${schedule.id}_${g.time}`,
  160. }));
  161. }
  162. result.push(schedule);
  163. });
  164. }
  165. } catch (ex) {
  166. console.error(ex, 'ERROR! getScheduleList');
  167. }
  168.  
  169. return result;
  170. }),
  171. enroll: () => {
  172. const {
  173. user, doctor, clinic, serviceList, date, time,
  174. } = self.booking;
  175. let result = new Promise((resolve, reject) => reject());
  176. if (
  177. doctor &&
  178. clinic &&
  179. date &&
  180. time &&
  181. user &&
  182. user.phone && user.birthday && user.firstName && user.lastName &&
  183. serviceList && serviceList.length
  184. ) {
  185. Moment.locale('ru');
  186. result = Api.enroll(
  187. clinic.id, doctor.id, serviceList[0].id, Moment(date).format('YYYY-MM-DD'), time,
  188. user.lastName, user.firstName, user.patronymic, Moment(user.birthday).format('YYYY-MM-DD'), user.phone,
  189. ).then(res => res).catch(ex => console.error(ex, 'ERROR! enroll'));
  190. }
  191. return result;
  192. },
  193. login: flow(function* login(phone, password) {
  194. return yield Api.login(phone, password);
  195. }),
  196. orderValidation: (orderId, smsCode) => Api.orderValidation(orderId, smsCode),
  197. setFetching: (value) => {
  198. self.fetching = value;
  199. },
  200. setDoctorList: (value) => {
  201. self.doctorList = value;
  202. },
  203. setAppointmentList: (value) => {
  204. self.appointmentList = value;
  205. },
  206. setServiceList: (value) => {
  207. self.serviceList = value;
  208. },
  209. setPopularServiceList: (value) => {
  210. self.popularServiceList = value;
  211. },
  212. setClinicList: (value) => {
  213. self.clinicList = value;
  214. },
  215. setScheduleList: (value) => {
  216. self.scheduleList = value;
  217. },
  218. setDoctorListFiltrated: (value) => {
  219. self.doctorListFiltrated = value ? value.map(d => String(d.id)) : [];
  220. },
  221. setServiceListFiltrated: (value) => {
  222. self.serviceListFiltrated = value ? value.map(s => String(s.id)) : [];
  223. },
  224. setClinicListFiltrated: (value) => {
  225. const ids = value ? value.map(c => String(c.id)) : [];
  226. self.clinicListFiltrated = ids;
  227. },
  228. setDoctor: (value) => {
  229. self.doctor = value;
  230. },
  231. setClinic: (value) => {
  232. self.clinic = value;
  233. },
  234. setService: (value) => {
  235. self.service = value;
  236. },
  237. setSchedule: (value) => {
  238. self.schedule = value;
  239. },
  240. setUser: (value) => {
  241. self.user = value;
  242. if (value) {
  243. self.booking.setUser(self.user.id);
  244. }
  245. },
  246. setBooking: (value) => {
  247. self.booking = value;
  248. },
  249. clearBooking: () => {
  250. self.booking.setClinic(null);
  251. self.booking.setDoctor(null);
  252. self.booking.setServiceList([]);
  253. self.booking.setDate(null);
  254. self.booking.setTime(null);
  255. },
  256. setAppointment: (value) => {
  257. self.appointment = value;
  258. },
  259. })))
  260. .views((self => ({
  261. getClinicList({ serviceId, doctorId }) {
  262. let list = self.clinicList.slice(0);
  263. if (serviceId) {
  264. list = list.filter(clinic => clinic.serviceList.some(s => s.id === serviceId));
  265. }
  266. if (doctorId) {
  267. list = list.filter(clinic => clinic.doctorList.some(d => d.id === doctorId));
  268. }
  269. return list;
  270. },
  271. })));
  272.  
  273. export default MainStore.create({
  274. chatList: [],
  275. popularServiceList: [],
  276. serviceList: [],
  277. doctorList: [],
  278. clinicList: [],
  279. serviceListFiltrated: [],
  280. doctorListFiltrated: [],
  281. clinicListFiltrated: [],
  282. scheduleList: [],
  283. appointmentList: [],
  284. booking: {
  285. id: '-1',
  286. serviceList: [],
  287. status: BookingStore.STATUS.RECORDED,
  288. },
  289. user: {
  290. idPacient: '0',
  291. phone: '+7(905)052-12-43',
  292. password: 'p8rpc6',
  293. },
  294. clinic: '1',
  295. doctor: '00007',
  296. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement