2017-03-03 20:09:08 +00:00
|
|
|
import React, {PureComponent} from 'react';
|
2017-03-03 01:44:07 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
2017-03-08 05:52:17 +00:00
|
|
|
import Modal from '../base/modal';
|
|
|
|
import ModalBody from '../base/modal-body';
|
|
|
|
import ModalHeader from '../base/modal-header';
|
|
|
|
import ModalFooter from '../base/modal-footer';
|
|
|
|
import CookiesEditor from '../editors/cookies-editor';
|
2016-11-10 05:56:23 +00:00
|
|
|
import * as models from '../../../models';
|
2016-11-23 19:33:24 +00:00
|
|
|
import {trackEvent} from '../../../analytics/index';
|
2016-08-15 17:04:36 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
@autobind
|
2017-02-28 21:32:23 +00:00
|
|
|
class CookiesModal extends PureComponent {
|
2017-03-03 01:44:07 +00:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
cookieJar: null,
|
|
|
|
workspace: null,
|
|
|
|
filter: ''
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
_setModalRef (n) {
|
|
|
|
this.modal = n;
|
|
|
|
}
|
2016-08-15 17:04:36 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_setFilterInputRef (n) {
|
|
|
|
this.filterInput = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
_hide () {
|
|
|
|
this.modal.hide();
|
|
|
|
}
|
2017-02-28 21:32:23 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
async _saveChanges () {
|
2016-08-15 17:04:36 +00:00
|
|
|
const {cookieJar} = this.state;
|
2016-11-10 01:15:27 +00:00
|
|
|
await models.cookieJar.update(cookieJar);
|
2016-10-02 20:57:00 +00:00
|
|
|
this._load(this.state.workspace);
|
2016-08-15 17:04:36 +00:00
|
|
|
}
|
2017-03-03 01:44:07 +00:00
|
|
|
|
|
|
|
_handleCookieUpdate (oldCookie, cookie) {
|
2016-08-15 17:04:36 +00:00
|
|
|
const {cookieJar} = this.state;
|
|
|
|
const {cookies} = cookieJar;
|
|
|
|
const index = cookies.findIndex(c => c.domain === oldCookie.domain && c.key === oldCookie.key);
|
|
|
|
|
|
|
|
cookieJar.cookies = [
|
|
|
|
...cookies.slice(0, index),
|
|
|
|
cookie,
|
|
|
|
...cookies.slice(index + 1)
|
|
|
|
];
|
|
|
|
|
|
|
|
this._saveChanges(cookieJar);
|
2016-11-23 19:33:24 +00:00
|
|
|
trackEvent('Cookie', 'Update');
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-08-15 17:04:36 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleCookieAdd (cookie) {
|
2016-08-15 17:04:36 +00:00
|
|
|
const {cookieJar} = this.state;
|
|
|
|
const {cookies} = cookieJar;
|
|
|
|
cookieJar.cookies = [cookie, ...cookies];
|
|
|
|
this._saveChanges(cookieJar);
|
2016-11-23 19:33:24 +00:00
|
|
|
trackEvent('Cookie', 'Create');
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-08-15 17:04:36 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleCookieDelete (cookie) {
|
2016-08-15 17:04:36 +00:00
|
|
|
const {cookieJar} = this.state;
|
|
|
|
const {cookies} = cookieJar;
|
|
|
|
|
|
|
|
// NOTE: This is sketchy because it relies on the same reference
|
|
|
|
cookieJar.cookies = cookies.filter(c => c !== cookie);
|
|
|
|
|
|
|
|
this._saveChanges(cookieJar);
|
2016-11-23 19:33:24 +00:00
|
|
|
trackEvent('Cookie', 'Delete');
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-08-15 17:04:36 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleFilterChange (e) {
|
2017-02-28 21:32:23 +00:00
|
|
|
const filter = e.target.value;
|
2016-11-10 21:03:12 +00:00
|
|
|
this.setState({filter});
|
2016-11-23 19:33:24 +00:00
|
|
|
trackEvent('Cookie Editor', 'Filter Change');
|
2017-03-03 20:09:08 +00:00
|
|
|
}
|
2016-08-15 17:04:36 +00:00
|
|
|
|
|
|
|
_getFilteredSortedCookies () {
|
|
|
|
const {cookieJar, filter} = this.state;
|
|
|
|
|
|
|
|
if (!cookieJar) {
|
|
|
|
// Nothing to do yet.
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const {cookies} = cookieJar;
|
|
|
|
return cookies.filter(c => {
|
|
|
|
const toSearch = JSON.stringify(c).toLowerCase();
|
|
|
|
return toSearch.indexOf(filter.toLowerCase()) !== -1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
async _load (workspace) {
|
2016-11-10 01:15:27 +00:00
|
|
|
const cookieJar = await models.cookieJar.getOrCreateForWorkspace(workspace);
|
2016-10-02 20:57:00 +00:00
|
|
|
this.setState({cookieJar, workspace});
|
2016-08-15 17:04:36 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
async show (workspace) {
|
|
|
|
await this._load(workspace);
|
2016-08-15 17:04:36 +00:00
|
|
|
this.modal.show();
|
|
|
|
this.filterInput.focus();
|
2016-11-23 19:33:24 +00:00
|
|
|
trackEvent('Cookie Editor', 'Show');
|
2016-08-15 17:04:36 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 22:31:30 +00:00
|
|
|
toggle (workspace) {
|
2016-08-15 17:04:36 +00:00
|
|
|
this.modal.toggle();
|
|
|
|
this.filterInput.focus();
|
2016-10-02 20:57:00 +00:00
|
|
|
this._load(workspace);
|
2016-08-15 17:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const filteredCookies = this._getFilteredSortedCookies();
|
|
|
|
const {filter} = this.state;
|
|
|
|
|
|
|
|
return (
|
2017-03-01 21:15:56 +00:00
|
|
|
<Modal ref={this._setModalRef} wide top tall {...this.props}>
|
2016-12-21 23:37:48 +00:00
|
|
|
<ModalHeader>Manage Cookies</ModalHeader>
|
2017-03-01 21:15:56 +00:00
|
|
|
<ModalBody className="cookie-editor" noScroll>
|
2016-12-21 23:37:48 +00:00
|
|
|
<div className="pad">
|
|
|
|
<div className="form-control form-control--outlined">
|
|
|
|
<label>Filter Cookies
|
2017-02-28 21:32:23 +00:00
|
|
|
<input ref={this._setFilterInputRef}
|
|
|
|
onChange={this._handleFilterChange}
|
2016-12-21 23:37:48 +00:00
|
|
|
type="text"
|
|
|
|
placeholder="twitter.com"
|
|
|
|
defaultValue=""/>
|
|
|
|
</label>
|
2016-09-22 04:17:38 +00:00
|
|
|
</div>
|
2016-08-15 17:04:36 +00:00
|
|
|
</div>
|
|
|
|
<div className="cookie-editor__editor border-top">
|
|
|
|
<div className="pad-top">
|
|
|
|
<CookiesEditor
|
|
|
|
cookies={filteredCookies}
|
2017-02-28 21:32:23 +00:00
|
|
|
onCookieUpdate={this._handleCookieUpdate}
|
|
|
|
onCookieAdd={this._handleCookieAdd}
|
|
|
|
onCookieDelete={this._handleCookieDelete}
|
2016-08-15 17:04:36 +00:00
|
|
|
// Set the domain to the filter so that it shows up if we're filtering
|
|
|
|
newCookieDomainName={filter || 'domain.com'}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</ModalBody>
|
|
|
|
<ModalFooter>
|
2016-10-21 17:20:36 +00:00
|
|
|
<div className="margin-left faint italic txt-sm tall">
|
2016-08-15 17:04:36 +00:00
|
|
|
* cookies are automatically sent with relevant requests
|
|
|
|
</div>
|
2017-02-28 21:32:23 +00:00
|
|
|
<button className="btn" onClick={this._hide}>
|
2016-10-21 17:20:36 +00:00
|
|
|
Done
|
|
|
|
</button>
|
2016-08-15 17:04:36 +00:00
|
|
|
</ModalFooter>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-10 18:30:50 +00:00
|
|
|
CookiesModal.propTypes = {};
|
2016-08-15 17:04:36 +00:00
|
|
|
|
|
|
|
// export CookiesModal;
|
|
|
|
export default CookiesModal;
|