-
Notifications
You must be signed in to change notification settings - Fork 4
/
LibraryRegistrationForm.tsx
96 lines (88 loc) · 2.73 KB
/
LibraryRegistrationForm.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
import * as React from "react";
import { LibraryData, LibraryRegistrationData } from "../interfaces";
import EditableInput from "./EditableInput";
import { Form } from "library-simplified-reusable-components";
export interface LibraryRegistrationFormState {
checked: boolean;
}
export interface LibraryRegistrationFormProps {
library: LibraryData;
register: (library) => void;
checked: boolean;
buttonText: string;
disabled: boolean;
registrationData?: LibraryRegistrationData;
}
export default class LibraryRegistrationForm extends React.Component<
LibraryRegistrationFormProps,
LibraryRegistrationFormState
> {
constructor(props) {
super(props);
this.state = { checked: this.props.checked };
this.toggleChecked = this.toggleChecked.bind(this);
}
UNSAFE_componentWillReceiveProps(nextProps): void {
this.setState({ checked: nextProps.checked });
}
render(): JSX.Element {
const hasTerms =
this.props.registrationData &&
(this.props.registrationData.terms_of_service_html ||
this.props.registrationData.terms_of_service_link ||
this.props.registrationData.access_problem);
const disabled = this.props.disabled || (hasTerms && !this.state.checked);
return (
<Form
className="inline registration-status"
content={hasTerms && this.checkbox(this.props.library)}
disableButton={disabled}
onSubmit={() => this.props.register(this.props.library)}
buttonContent={this.props.buttonText}
withoutButton={
this.props.registrationData &&
this.props.registrationData.access_problem
}
/>
);
}
checkbox(library: LibraryData): JSX.Element {
const {
access_problem,
terms_of_service_html,
terms_of_service_link,
} = this.props.registrationData;
let element;
if (access_problem) {
element = <p className="bg-danger">{access_problem.detail}</p>;
} else if (terms_of_service_html) {
element = (
<p dangerouslySetInnerHTML={{ __html: terms_of_service_html }}></p>
);
} else {
element = (
<p>
You must read the <a href={terms_of_service_link}>terms of service</a>{" "}
before registering your library.
</p>
);
}
return (
<section className="registration-checkbox" key={library.uuid}>
{element}
{!access_problem && (
<EditableInput
elementType="input"
type="checkbox"
onChange={this.toggleChecked}
checked={this.state.checked}
label="I agree to the terms of service."
></EditableInput>
)}
</section>
);
}
toggleChecked(): void {
this.setState({ checked: !this.state.checked });
}
}