forked from stac-labs/Spoke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCampaignTexterUIForm.jsx
132 lines (126 loc) · 4.04 KB
/
CampaignTexterUIForm.jsx
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import type from "prop-types";
import React from "react";
import orderBy from "lodash/orderBy";
import Slider from "./Slider";
import Divider from "material-ui/Divider";
import AutoComplete from "material-ui/AutoComplete";
import IconButton from "material-ui/IconButton";
import RaisedButton from "material-ui/RaisedButton";
import GSForm from "../components/forms/GSForm";
import yup from "yup";
import Form from "react-formal";
import OrganizationJoinLink from "./OrganizationJoinLink";
import CampaignFormSectionHeading from "./CampaignFormSectionHeading";
import { StyleSheet, css } from "aphrodite";
import theme from "../styles/theme";
import Toggle from "material-ui/Toggle";
import DeleteIcon from "material-ui/svg-icons/action/delete";
import { dataTest } from "../lib/attributes";
import { dataSourceItem } from "./utils";
import sideboxes from "../extensions/texter-sideboxes/components";
export default class CampaignTexterUIForm extends React.Component {
constructor(props) {
super(props);
const { formValues } = this.props;
const settingsData =
(formValues.texterUIConfig.options &&
JSON.parse(formValues.texterUIConfig.options)) ||
{};
// default sideboxes should be enabled until they're explicitly turned off
formValues.texterUIConfig.sideboxChoices.forEach(sb => {
if (sb.startsWith("default") && !settingsData.hasOwnProperty(sb)) {
settingsData[sb] = true;
}
});
this.state = settingsData;
}
onChange = formValues => {
console.log("onChange", formValues);
this.setState(formValues, () => {
this.props.onChange({
texterUIConfig: {
options: JSON.stringify(this.state),
sideboxChoices: this.props.formValues.texterUIConfig.sideboxChoices
}
});
});
};
toggleChange = (key, value) => {
console.log("toggleChange", key, value);
this.setState({ [key]: value }, newData => {
this.props.onChange({
texterUIConfig: {
options: JSON.stringify(this.state),
sideboxChoices: this.props.formValues.texterUIConfig.sideboxChoices
}
});
});
};
render() {
const keys = Object.keys(sideboxes);
console.log("CampaignTexterUIForm", this.state, this.props.formValues);
const adminItems = [];
const schemaObject = {};
keys.forEach(sb => {
const sidebox = sideboxes[sb];
const displayName = sidebox.displayName();
const AdminConfig = (this.state[sb] && sidebox.AdminConfig) || null;
if (sidebox.adminSchema) {
Object.assign(schemaObject, sidebox.adminSchema());
}
schemaObject[sb] = yup.boolean();
adminItems.push(
<div key={sb}>
<Toggle
{...dataTest(`toggle_${sb}`)}
label={
<div
style={{ ...theme.text.secondaryHeader, marginBottom: "10px" }}
>
{displayName}
</div>
}
toggled={this.state[sb]}
onToggle={(toggler, val) => this.toggleChange(sb, val)}
/>
{AdminConfig ? (
<div style={{ paddingLeft: "15px" }}>
<AdminConfig
settingsData={this.state}
onToggle={this.toggleChange}
organization={this.props.organization}
/>
</div>
) : null}
<div style={{ height: "20px" }}></div>
</div>
);
});
return (
<div>
<GSForm
schema={yup.object(schemaObject)}
value={this.state}
onChange={this.onChange}
>
{adminItems}
<Form.Button
type="submit"
onClick={this.props.onSubmit}
label={this.props.saveLabel}
disabled={this.props.saveDisabled}
{...dataTest("submitCampaignTexterUIForm")}
/>
</GSForm>
</div>
);
}
}
CampaignTexterUIForm.propTypes = {
formValues: type.object,
organization: type.object,
onChange: type.func,
onSubmit: type.func,
saveLabel: type.string,
saveDisabled: type.bool
};