/
index.tsx
403 lines (361 loc) · 11.8 KB
/
index.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import React, { Component } from 'react';
import Tooltip from 'rc-tooltip';
import FormDialog from '../FormDialog';
import DataTable from '../Table';
import SegmentItemButton from './itemButton';
import { fromJS } from 'immutable';
import Dropdown from '../Dropdown';
import Button, { ButtonIndigo } from '../Button';
import { PredicateType } from './types';
import I18n from '../../../../../src/shared/FakeI18n';
import { escapeHTML } from '@chaskiq/components/src/utils/htmlSanitize';
// import ClickAwayListener from '@material-ui/core/ClickAwayListener'
import defaultFields from '../../utils/defaultFields';
import styled from '@emotion/styled';
import { withRouter } from 'react-router-dom';
import { connect } from 'react-redux';
import { SaveIcon, DeleteIcon, PlusIcon } from '../icons';
const ButtonGroup = styled.div`
//display: inline-flex;
//display: -webkit-box;
margin-bottom: 2em;
display: inline-flex;
flex-wrap: wrap;
align-items: center;
button {
margin-right: 5px !important;
margin: 2px;
}
`;
function Spinner() {
return <p>run run run...</p>;
}
type SaveSegmentModalType = {
title?: string;
savePredicates: (value: any, callback: any) => void;
predicateCallback: any;
deleteSegment: any;
segment: {
id: string;
name: string;
predicates: Array<PredicateType>;
initialPredicates: Array<PredicateType>;
};
};
type SegmentManagerType = {
app: any;
store: any;
history: any;
predicates: Array<PredicateType>;
updatePredicate: any;
deletePredicate: any;
addPredicate: any;
meta: any;
collection: any;
data: any;
search: any;
columns: any;
toggleMapView: any;
enableMapView: any;
};
export class SaveSegmentModal extends Component<SaveSegmentModalType> {
state = {
isOpen: false,
action: 'update',
loading: false,
input: null,
};
open = () => this.setState({ isOpen: true });
close = () => this.setState({ isOpen: false });
input_ref = null;
secondaryAction = ({ _target }) => {
this.props.savePredicates(
{
action: this.state.action,
input: this.input_ref ? this.input_ref.value : null,
},
() => {
this.close();
if (this.props.predicateCallback) this.props.predicateCallback();
}
);
};
deleteAction = ({ _target }) => {
this.props.deleteSegment(this.props.segment.id, this.close);
};
handleChange = ({ target }) => {
this.setState({
action: target.value,
});
};
equalPredicates = () => {
return fromJS(this.props.segment.predicates).equals(
fromJS(this.props.segment.initialPredicates)
);
};
incompletePredicates = () => {
return this.props.segment.predicates.find((o) => !o.comparison || !o.value);
};
render() {
const { isOpen, loading } = this.state;
return (
<React.Fragment>
<div className="flex items-center">
<Tooltip
placement="bottom"
overlay={I18n.t('segment_manager.save_segment')}
>
<ButtonIndigo
isLoading={false}
arial-label={I18n.t('segment_manager.save_segment')}
variant={'icon'}
onClick={this.open}
size={'small'}
disabled={this.equalPredicates() || this.incompletePredicates()}
>
<SaveIcon variant="small" />{' '}
</ButtonIndigo>
</Tooltip>
<Tooltip
placement="bottom"
overlay={I18n.t('segment_manager.delete_segment')}
>
<ButtonIndigo
isLoading={false}
variant={'icon'}
arial-label={I18n.t('segment_manager.delete_segment')}
appearance={'link danger'}
onClick={this.deleteAction.bind(this)}
>
<DeleteIcon />
</ButtonIndigo>
</Tooltip>
</div>
{isOpen && (
<FormDialog
open={isOpen}
handleClose={this.close}
// contentText={"lipsum"}
titleContent={I18n.t('segment_manager.save_segment')}
formComponent={
!loading ? (
<div>
<p className="text-sm leading-5 text-gray-500">
{I18n.t('segment_manager.changes_notice')}
</p>
<label className="inline-flex items-center">
<input
type="radio"
className="form-radio h-4 w-4 text-indigo-600 transition duration-150 ease-in-out"
name="options"
value={'update'}
onChange={this.handleChange.bind(this)}
/>
<span
className="ml-2"
dangerouslySetInnerHTML={{
__html: I18n.t('segment_manager.save_changes_to', {
name: escapeHTML(this.props.segment.name),
}),
}}
></span>
</label>
<label className="inline-flex items-center">
<input
type="radio"
className="form-radio h-4 w-4 text-indigo-600 transition duration-150 ease-in-out"
name="options"
value={'new'}
onChange={this.handleChange.bind(this)}
/>
<span className="ml-2">
{I18n.t('segment_manager.create_segment')}
</span>
</label>
{this.state.action === 'new' && (
<input
className="mt-1 form-input block w-full py-2 px-3 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:shadow-outline-blue focus:border-blue-300 transition duration-150 ease-in-out sm:text-sm sm:leading-5"
autoFocus
id="name"
name="name"
type="email"
ref={(input) => (this.input_ref = input)}
/>
)}
</div>
) : (
<Spinner />
)
}
dialogButtons={
<React.Fragment>
<Button
size="xs"
// className="inline-flex justify-center w-full rounded-md border border-transparent px-4 py-2 bg-red-600 text-base leading-6 font-medium text-white shadow-sm hover:bg-red-500 focus:outline-none focus:border-red-700 focus:shadow-outline-red transition ease-in-out duration-150 sm:text-sm sm:leading-5"
onClick={this.secondaryAction.bind(this)}
variant={'success'}
>
{this.state.action === 'update' ? 'Save' : 'Save New'}
</Button>
<Button
size="xs"
onClick={this.close}
// className="inline-flex justify-center w-full rounded-md border border-gray-300 px-4 py-2 bg-white text-base leading-6 font-medium text-gray-700 shadow-sm hover:text-gray-500 focus:outline-none focus:border-blue-300 focus:shadow-outline transition ease-in-out duration-150 sm:text-sm sm:leading-5"
variant={'outlined'}
>
{I18n.t('common.cancel')}
</Button>
</React.Fragment>
}
// actions={actions}
// onClose={this.close}
// heading={this.props.title}
></FormDialog>
)}
</React.Fragment>
);
}
}
type InlineFilterDialogType = {
addPredicate: any;
app: any;
fields?: any;
};
export function InlineFilterDialog({
addPredicate,
app,
fields,
}: InlineFilterDialogType) {
const [dialogOpen, setDialogOpen] = React.useState(false);
const handleClick = (e, o) => {
addPredicate(o, (_token) => {
// this.props.handleClick(token)
setDialogOpen(false);
});
};
const availableFields = () => {
if (fields) return fields;
if (!app.customFields) return defaultFields;
return app.customFields.concat(defaultFields);
};
const f = availableFields();
const content = (
<div className="p-2--">
<div className="p-2">
<h2 className="text-sm leading-5 text-gray-900 dark:text-gray-100 font-bold">
{I18n.t('segment_manager.select_fields')}
</h2>
</div>
<div className="overflow-scroll h-48">
<ul className="divide-y divide-gray-200">
{f.map((o, i) => (
<li key={`select-fields-${i}`}>
<a
key={o.name}
onClick={(e) => handleClick(e, o)}
className="cursor-pointer block hover:bg-gray-100 dark:hover:bg-gray-800 focus:outline-none focus:bg-gray-100 dark:focus:text-gray-800 transition duration-150 ease-in-out"
>
<div className="flex items-center px-4 py-4 sm:px-6">
<span className="flex items-center text-sm leading-5 text-gray-700 dark:text-gray-100">
{o.name}
</span>
</div>
</a>
</li>
))}
</ul>
</div>
</div>
);
return (
<div>
<Dropdown
isOpen={dialogOpen}
onOpen={(v) => setDialogOpen(v)}
triggerButton={(cb) => (
<Button
variant="success"
className="flex flex-wrap"
color="primary"
size="sm"
onClick={cb}
>
<PlusIcon variant="small" /> {I18n.t('segment_manager.add_filters')}
</Button>
)}
>
{content}
</Dropdown>
</div>
);
}
class SegmentManager extends Component<SegmentManagerType> {
constructor(props) {
super(props);
}
handleClickOnSelectedFilter = (jwtToken) => {
const url = `/apps/${this.props.app.key}/segments/${this.props.store.segment.id}/${jwtToken}`;
this.props.history.push(url);
};
getTextForPredicate = (o) => {
if (o.type === 'match') {
return `${I18n.t('segment_manager.match')} ${
o.value === 'and'
? I18n.t('segment_manager.all')
: I18n.t('segment_manager.any')
} ${I18n.t('segment_manager.criteria')}`;
} else {
return `Match: ${o.attribute} ${o.comparison ? o.comparison : ''} ${
o.value ? o.value : ''
}`;
}
};
render() {
// this.props.actions.getPredicates()
return (
<div>
<ButtonGroup>
{this.props.predicates.map((o, i) => {
return (
<SegmentItemButton
key={i}
index={i}
predicates={this.props.predicates}
predicate={o}
open={!o.comparison}
appearance={o.comparison ? 'primary' : 'default'}
text={this.getTextForPredicate(o)}
updatePredicate={this.props.updatePredicate}
deletePredicate={this.props.deletePredicate}
/>
);
})}
<InlineFilterDialog
{...this.props}
app={this.props.app}
addPredicate={this.props.addPredicate}
// handleClick={this.handleClickOnSelectedFilter.bind(this)}
/>
{this.props.children}
</ButtonGroup>
{
<DataTable
meta={this.props.meta}
data={this.props.collection}
search={this.props.search}
columns={this.props.columns}
toggleMapView={this.props.toggleMapView}
enableMapView={this.props.enableMapView}
/>
}
</div>
);
}
}
function mapStateToProps(state) {
const { app } = state;
return {
app,
};
}
export default withRouter(connect(mapStateToProps)(SegmentManager));