insomnia/app/ui/components/cookie-list.js

111 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-08-10 01:56:27 +00:00
import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import autobind from 'autobind-decorator';
import {Cookie} from 'tough-cookie';
2017-08-19 22:34:16 +00:00
import {cookieToString} from '../../common/cookies';
import PromptButton from './base/prompt-button';
@autobind
2017-08-19 22:34:16 +00:00
class CookieList extends PureComponent {
shouldComponentUpdate (nextProps, nextState) {
return nextProps.cookies !== this.props.cookies;
}
_handleCookieAdd () {
const newCookie = new Cookie({
key: 'foo',
value: 'bar',
domain: this.props.newCookieDomainName,
2017-08-19 22:34:16 +00:00
path: '/',
secure: false,
httpOnly: false
});
this.props.onCookieAdd(newCookie);
}
_handleDeleteCookie (cookie) {
this.props.onCookieDelete(cookie);
}
render () {
2017-08-19 22:34:16 +00:00
const {
cookies,
handleShowModifyCookieModal
} = this.props;
return (
<div>
2017-08-19 22:34:16 +00:00
<table className="table--fancy cookie-table table--striped">
<thead>
<tr>
<th style={{minWidth: '10rem'}}>Domain</th>
<th style={{width: '90%'}}>Cookie</th>
<th style={{width: '2rem'}} className="text-right">
<button className="btn btn--super-compact"
onClick={this._handleCookieAdd}
title="Add cookie">
2017-07-25 22:28:53 +00:00
<i className="fa fa-plus-circle"/>
</button>
</th>
</tr>
</thead>
<tbody key={cookies.length}>
{cookies.map((cookie, i) => {
const cookieString = cookieToString(Cookie.fromJSON(JSON.stringify(cookie)));
return (
<tr className="selectable" key={i}>
2017-08-19 22:34:16 +00:00
<td
onClick={() => handleShowModifyCookieModal(cookie)}>
{cookie.domain}
</td>
<td
onClick={() => handleShowModifyCookieModal(cookie)}>
{cookieString}
</td>
2017-08-19 22:34:16 +00:00
<td
onClick={null}
className="text-right">
2017-01-23 22:41:31 +00:00
<PromptButton className="btn btn--super-compact"
addIcon
2017-01-23 22:41:31 +00:00
confirmMessage=" "
onClick={e => this._handleDeleteCookie(cookie)}
title="Delete cookie">
<i className="fa fa-trash-o"/>
2017-01-23 22:41:31 +00:00
</PromptButton>
</td>
</tr>
);
})}
</tbody>
</table>
{cookies.length === 0 && (
<div className="pad faint italic text-center">
<p>
I couldn't find any cookies for you.
</p>
<p>
<button className="btn btn--clicky"
onClick={e => this._handleCookieAdd()}>
2017-07-25 22:28:53 +00:00
Add Cookie <i className="fa fa-plus-circle"/>
</button>
</p>
</div>
)}
</div>
);
}
}
2017-08-19 22:34:16 +00:00
CookieList.propTypes = {
onCookieAdd: PropTypes.func.isRequired,
onCookieDelete: PropTypes.func.isRequired,
cookies: PropTypes.array.isRequired,
2017-08-19 22:34:16 +00:00
newCookieDomainName: PropTypes.string.isRequired,
handleShowModifyCookieModal: PropTypes.func.isRequired
};
2017-08-19 22:34:16 +00:00
export default CookieList;