|
| 1 | +import React, { Component } from 'react' |
| 2 | +import {Link, Route} from 'react-router-dom' |
| 3 | +import {colour_contrast, group_by} from '../../utils' |
| 4 | +import {If, Bull} from '../shared/Tools' |
| 5 | +import AptModal from './AptModal' |
| 6 | + |
| 7 | +const LS_KEY = '_tcs_user_data_' |
| 8 | + |
| 9 | +const group_appointments = apts => { |
| 10 | + // group appointments by month then day |
| 11 | + return group_by(apts, a => a.start.substr(0, 7)) |
| 12 | + .map(apts_ => ({ |
| 13 | + date: apts_[0].start, |
| 14 | + appointments: group_by(apts_, a => a.start.substr(0, 10)) |
| 15 | + })) |
| 16 | +} |
| 17 | + |
| 18 | +const Apt = ({apt, props, appointment_attendees}) => { |
| 19 | + let status |
| 20 | + const spaces_ctx = {spaces: apt.attendees_max === null ? null : apt.attendees_max - apt.attendees_count} |
| 21 | + if (appointment_attendees && appointment_attendees[apt.id] !== undefined) { |
| 22 | + status = props.config.get_text('spaces_attending', spaces_ctx) |
| 23 | + } else { |
| 24 | + status = props.config.get_text('spaces', spaces_ctx) |
| 25 | + } |
| 26 | + return ( |
| 27 | + <Link to={props.root.url(`appointment/${apt.link}`)} className="tcs-item"> |
| 28 | + <div className={`tcs-apt ${colour_contrast(apt.service_colour)}`} style={{background: apt.service_colour}}> |
| 29 | + <div style={{fontWeight: 700, marginRight: 8, minWidth: 68}}> |
| 30 | + {props.config.format_dt(apt.start, 'time')} |
| 31 | + </div> |
| 32 | + <div className="tcs-truncate"> |
| 33 | + {apt.topic}<Bull/>{apt.service_name} |
| 34 | + </div> |
| 35 | + <div className="tcs-right" style={{fontWeight: 700}}> |
| 36 | + {props.config.format_money(apt.price)} |
| 37 | + </div> |
| 38 | + <div className="tcs-truncate" style={{gridColumn: 2}}> |
| 39 | + {status} |
| 40 | + </div> |
| 41 | + <div className="tcs-right"> |
| 42 | + {props.config.format_duration(apt.finish, apt.start)} |
| 43 | + </div> |
| 44 | + </div> |
| 45 | + </Link> |
| 46 | + ) |
| 47 | +} |
| 48 | + |
| 49 | +const AptDayGroup = ({appointments, props, appointment_attendees}) => { |
| 50 | + const first_apt = appointments[0] |
| 51 | + return ( |
| 52 | + <div className="tcs-apt-group-day"> |
| 53 | + <div className="tcs-day"> |
| 54 | + {props.config.format_dt(first_apt.start, 'weekday')} |
| 55 | + <div className="tcs-day-no">{props.config.format_dt(first_apt.start, 'day')}</div> |
| 56 | + </div> |
| 57 | + <div> |
| 58 | + {appointments.map(apt => ( |
| 59 | + <Apt key={apt.id} apt={apt} props={props} appointment_attendees={appointment_attendees}/> |
| 60 | + ))} |
| 61 | + </div> |
| 62 | + </div> |
| 63 | + ) |
| 64 | +} |
| 65 | + |
| 66 | +class Appointments extends Component { |
| 67 | + constructor (props) { |
| 68 | + super(props) |
| 69 | + this.state = { |
| 70 | + appointments: null, |
| 71 | + page: 1, |
| 72 | + more_pages: false, |
| 73 | + display_data: null, |
| 74 | + appointment_attendees: null, |
| 75 | + } |
| 76 | + this.sso_args = null |
| 77 | + this.update = this.update.bind(this) |
| 78 | + this.signin = this.signin.bind(this) |
| 79 | + this.signout = this.signout.bind(this) |
| 80 | + this.root_url = this.props.root.url('') |
| 81 | + } |
| 82 | + |
| 83 | + componentDidMount () { |
| 84 | + this.update_display_data() |
| 85 | + this.update() |
| 86 | + } |
| 87 | + |
| 88 | + page_url (page) { |
| 89 | + page = page || this.state.page |
| 90 | + let url = this.root_url |
| 91 | + if (page > 1) { |
| 92 | + url += `${url.substr(-1) === '/' ? '' : '/'}page/${page}` |
| 93 | + } |
| 94 | + return url |
| 95 | + } |
| 96 | + |
| 97 | + async update () { |
| 98 | + const mp = this.props.history.location.pathname.match(/page\/(\d+)/) |
| 99 | + const page = mp ? parseInt(mp[1], 10) : 1 |
| 100 | + const appointments = await this.props.root.requests.get('appointments', { |
| 101 | + page, pagination: this.props.config.pagination, |
| 102 | + }) |
| 103 | + this.props.config.event_callback('updated_appointments', appointments) |
| 104 | + const on_previous_pages = (page - 1) * this.props.config.pagination |
| 105 | + this.sso_args && await this.update_attendees() |
| 106 | + this.setState({ |
| 107 | + appointments, page, |
| 108 | + more_pages: appointments.count > appointments.results.length + on_previous_pages, |
| 109 | + }) |
| 110 | + } |
| 111 | + |
| 112 | + signin () { |
| 113 | + const process_message = event => { |
| 114 | + try { |
| 115 | + JSON.parse(event.data) |
| 116 | + } catch (e) { |
| 117 | + return |
| 118 | + } |
| 119 | + event.source.close() |
| 120 | + window.sessionStorage[LS_KEY] = event.data |
| 121 | + this.update_display_data() |
| 122 | + this.update_attendees() |
| 123 | + } |
| 124 | + window.addEventListener('message', process_message, false) |
| 125 | + window.open( |
| 126 | + `${this.props.config.auth_url}?site=${encodeURIComponent(window.location.href)}`, |
| 127 | + 'Auth', |
| 128 | + 'width=1000,height=700,left=100,top=100,scrollbars,toolbar=0,resizable' |
| 129 | + ) |
| 130 | + } |
| 131 | + |
| 132 | + signout () { |
| 133 | + this.setState({ |
| 134 | + display_data: null, |
| 135 | + appointment_attendees: null, |
| 136 | + }) |
| 137 | + this.sso_args = null |
| 138 | + window.sessionStorage.removeItem(LS_KEY) |
| 139 | + } |
| 140 | + |
| 141 | + update_display_data () { |
| 142 | + const raw_data = window.sessionStorage[LS_KEY] |
| 143 | + if (raw_data) { |
| 144 | + this.sso_args = JSON.parse(raw_data) |
| 145 | + this.setState({display_data: JSON.parse(this.sso_args.sso_data)}) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + async update_attendees () { |
| 150 | + try { |
| 151 | + const r = await this.props.root.requests.get('check-client', this.sso_args, {set_app_state: false}) |
| 152 | + this.setState({appointment_attendees: r.appointment_attendees}) |
| 153 | + } catch (e) { |
| 154 | + if (e.xhr.status === 401) { |
| 155 | + this.signout() |
| 156 | + } else { |
| 157 | + this.props.root.setState({error: e.msg}) |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + render () { |
| 163 | + const appointments = this.state.appointments ? this.state.appointments.results : [] |
| 164 | + const months = group_appointments(appointments) |
| 165 | + return ( |
| 166 | + <div className="tcs-app tcs-appointments"> |
| 167 | + {months.map(({date, appointments}, i) => ( |
| 168 | + <div className="tcs-apt-group-month" key={i}> |
| 169 | + <div className="tcs-title">{this.props.config.format_dt(date, 'month')}</div> |
| 170 | + {appointments.map((appointments, j) => ( |
| 171 | + <AptDayGroup key={j} appointments={appointments} props={this.props} |
| 172 | + appointment_attendees={this.state.appointment_attendees}/> |
| 173 | + ))} |
| 174 | + </div> |
| 175 | + ))} |
| 176 | + |
| 177 | + <If v={this.state.page > 1 || this.state.more_pages}> |
| 178 | + <div className="tcs-pagination"> |
| 179 | + <Link |
| 180 | + to={this.page_url(this.state.page - 1)} |
| 181 | + onClick={() => setTimeout(() => this.update(), 0)} |
| 182 | + className={'tcs-previous' + (this.state.page > 1 ? '' : ' tcs-disable')}> |
| 183 | + ‹‹ {this.props.config.get_text('previous')} |
| 184 | + </Link> |
| 185 | + <Link |
| 186 | + to={this.page_url(this.state.page + 1)} |
| 187 | + onClick={() => setTimeout(() => this.update(), 0)} |
| 188 | + className={'tcs-next' + (this.state.more_pages ? '' : ' tcs-disable')}> |
| 189 | + {this.props.config.get_text('next')} ›› |
| 190 | + </Link> |
| 191 | + </div> |
| 192 | + </If> |
| 193 | + <Route path={this.props.root.url('appointment/:id(\\d+):_extra')} render={props => ( |
| 194 | + <AptModal id={props.match.params.id} |
| 195 | + last_url={this.root_url} |
| 196 | + appointments={this.state.appointments && this.state.appointments.results} |
| 197 | + got_data={Boolean(this.state.appointments)} |
| 198 | + display_data={this.state.display_data} |
| 199 | + appointment_attendees={this.state.appointment_attendees} |
| 200 | + sso_args={this.sso_args} |
| 201 | + root={this.props.root} |
| 202 | + config={this.props.config} |
| 203 | + update={this.update} |
| 204 | + signin={this.signin} |
| 205 | + signout={this.signout} |
| 206 | + history={props.history}/> |
| 207 | + )}/> |
| 208 | + </div> |
| 209 | + ) |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +export default Appointments |
0 commit comments