Appointment.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import React from 'react';
  2. import {connect} from "react-redux";
  3. import {
  4. setAppointmentSpec,
  5. setAppointmentShedule,
  6. setAppointmentDoctor,
  7. clearAppointment,
  8. setAppointmentTime,
  9. setAppointmentComment,
  10. postOrders
  11. } from "../actions/actions";
  12. export class Appoint extends React.Component {
  13. componentDidMount() {
  14. console.log(this.props)
  15. this.props.setAppointmentDoctor(this.props.match.params.doctorId)
  16. }
  17. componentWillUnmount() {
  18. this.props.clearAppointment()
  19. }
  20. render() {
  21. const {doctors, appointment, timeArray, wrongDate,services} = this.props.app;
  22. const {match, setAppointmentSpec, setAppointmentShedule, setAppointmentTime, setAppointmentComment, postOrders} = this.props;
  23. const doctor = doctors.find(el => el._id === match.params.doctorId);
  24. let spec;
  25. if (appointment.spec){
  26. spec = services.find(el => el._id === appointment.spec)
  27. }
  28. let currentDate;
  29. let choosedData;
  30. if (doctor) {
  31. if (doctor.shedule && appointment.shedule) {
  32. currentDate = new Date().toISOString().split('T')[0];
  33. choosedData = doctor.shedule.find(el => el._id === appointment.shedule).data.split('T')[0];
  34. }
  35. }
  36. return (
  37. <>
  38. {doctor &&
  39. <div style={{display:'flex',flexDirection:'column',width:'400px',margin:'100px 0'}}>
  40. <img src={doctor.photo} alt=""/>
  41. <p>{doctor.name}</p>
  42. <p>{doctor.profession}</p>
  43. <p>Опыт работы {new Date().toISOString().split('T')[0].split('-')[0] - doctor.experience.split('T')[0].split('-')[0]} лет</p>
  44. <p>{doctor.skillsDescription}</p>
  45. {appointment.spec &&
  46. <div>
  47. <p>{spec.name}</p>
  48. <p>Duration:{spec.duration}h</p>
  49. <p>{spec.description}</p>
  50. <p>Price: {spec.price} грн.</p>
  51. </div>
  52. }
  53. <select onChange={(e)=>setAppointmentSpec(e.target.value)} defaultValue='choose spec'>
  54. <option disabled >choose spec</option>
  55. {
  56. doctor.speciality.map(el=> (
  57. <option key={el._id}>{el.name}</option>
  58. ))
  59. }
  60. </select>
  61. {appointment.spec &&
  62. <input
  63. type="date"
  64. onChange={(e) => setAppointmentShedule(doctor.shedule.find(el => new Date(el.data).toISOString().split('T')[0] === new Date(e.target.value).toISOString().split('T')[0])
  65. ? doctor.shedule.find(el => new Date(el.data).toISOString().split('T')[0] === new Date(e.target.value).toISOString().split('T')[0])._id
  66. : null
  67. )}
  68. />
  69. }
  70. {!appointment.shedule ?
  71. !wrongDate ?
  72. <p>There is no available time to choose on this date</p>
  73. : null
  74. : <div>
  75. { choosedData >= currentDate ?
  76. <div>
  77. <select onChange={(e)=>setAppointmentTime(e.target.value)} defaultValue='choose time'>
  78. <option disabled >choose time</option>
  79. {
  80. timeArray.map(el=> (
  81. Object.values(el)[0]
  82. ? <option key={Object.keys(el)[0]} style={{color:'green'}} > {Object.keys(el)[0]} </option>
  83. : <option key={Object.keys(el)[0]} style={{color:'red'}} disabled > {Object.keys(el)[0]} </option>
  84. ))
  85. }
  86. </select>
  87. </div>
  88. : <p>Choose current or future date</p>
  89. }
  90. </div>
  91. }
  92. {appointment.time &&
  93. <div style={{display:"flex",flexDirection:"column"}}>
  94. <input type='text' placeholder='Add comments here' onChange={(e)=>setAppointmentComment(e.target.value)}/>
  95. <button onClick={() => postOrders(appointment)}>Confirm Appointment</button>
  96. </div>
  97. }
  98. </div>
  99. }
  100. </>
  101. );
  102. }
  103. }
  104. const mapStateToProps = state => {
  105. return {
  106. app:state.app,
  107. }
  108. };
  109. const mapDispatchToProps = {
  110. setAppointmentSpec,
  111. setAppointmentShedule,
  112. setAppointmentDoctor,
  113. clearAppointment,
  114. setAppointmentTime,
  115. setAppointmentComment,
  116. postOrders
  117. };
  118. export default connect (mapStateToProps,mapDispatchToProps)(Appoint)