This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Call.tsx
113 lines (104 loc) · 3.73 KB
/
Call.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { } from './NoContact';
import * as React from 'react';
import i18n from '../../services/i18n';
import { TranslationFunction } from 'i18next';
import { translate } from 'react-i18next';
import { Issue, Contact } from '../../common/model';
import { CallHeaderTranslatable, ContactDetails, Outcomes, ScriptTranslatable, NoContactSplitDistrict } from './index';
import { CallState, OutcomeData } from '../../redux/callState';
import { LocationState } from '../../redux/location/reducer';
// This defines the props that we must pass into this component.
export interface Props {
readonly issue: Issue;
readonly callState: CallState;
readonly locationState: LocationState;
readonly t: TranslationFunction;
readonly clearLocation: () => void;
readonly onSubmitOutcome: (data: OutcomeData) => Function;
}
export interface State {
issue: Issue;
currentContact: Contact | undefined;
currentContactIndex: number;
numberContactsLeft: number;
}
export class Call extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
// set initial state
this.state = this.setStateFromProps(props);
}
/**
* Set state from props when props
* are initialized or refreshed
*
* @param {Props} props
* @returns {State}
*/
setStateFromProps(props: Props): State {
let currentContactIndex = 0;
if (props.issue && props.callState.contactIndexes && props.callState.contactIndexes[props.issue.id]) {
currentContactIndex = props.callState.contactIndexes[props.issue.id];
}
const currentContact = (props.issue && props.issue.contacts
? props.issue.contacts[currentContactIndex]
: undefined);
const numberContactsLeft = props.issue && props.issue.contacts
? props.issue.contacts.length - (currentContactIndex + 1)
: 0;
return {
currentContact: currentContact,
currentContactIndex: currentContactIndex,
numberContactsLeft: numberContactsLeft,
issue: props.issue
};
}
componentWillReceiveProps(newProps: Props) {
this.setState(this.setStateFromProps(newProps));
}
render() {
return (
<section className="call">
<CallHeaderTranslatable
currentIssue={this.state.issue}
t={i18n.t}
/>
{this.props.locationState.splitDistrict ?
<NoContactSplitDistrict
splitDistrict={this.props.locationState.splitDistrict}
clearLocation={this.props.clearLocation}
t={i18n.t}
/> :
<ContactDetails
currentIssue={this.state.issue}
contactIndex={this.state.currentContactIndex}
t={i18n.t}
/>}
<ScriptTranslatable
issue={this.state.issue}
contactIndex={this.state.currentContactIndex}
locationState={this.props.locationState}
t={i18n.t}
/>
{this.props.locationState.splitDistrict ||
this.props.issue &&
(this.props.issue.contacts && this.props.issue.contacts.length === 0) ? <span/> :
<Outcomes
currentIssue={this.state.issue}
numberContactsLeft={this.state.numberContactsLeft}
currentContactId={(this.state.currentContact ? this.state.currentContact.id : '')}
onSubmitOutcome={this.props.onSubmitOutcome}
t={i18n.t}
/>}
{/* TODO: Fix people/person text for 1 contact left. Move logic to a function */}
{this.props.locationState.splitDistrict ? <span/> :
this.state.numberContactsLeft > 0 ?
<h3 aria-live="polite" className="call__contacts__left" >
{this.props.t('outcomes.contactsLeft', { contactsRemaining: this.state.numberContactsLeft })}
</h3> : ''
}
</section>
);
}
}
export const CallTranslatable = translate()(Call);