Fixed raw cookie editor deleting cookies

This commit is contained in:
Gregory Schier 2017-11-03 19:32:22 +01:00
parent 6ee6422e94
commit e99a74201a
2 changed files with 28 additions and 9 deletions

View File

@ -12,6 +12,7 @@ export function cookiesFromJar (jar) {
console.warn('Failed to get cookies form jar', err); console.warn('Failed to get cookies form jar', err);
resolve([]); resolve([]);
} else { } else {
// NOTE: Perform toJSON so we have a plain JS object instead of Cookie instance
resolve(cookies.map(c => c.toJSON())); resolve(cookies.map(c => c.toJSON()));
} }
}); });

View File

@ -78,25 +78,39 @@ class CookieModifyModal extends React.PureComponent<Props, State> {
await models.cookieJar.update(cookieJar); await models.cookieJar.update(cookieJar);
} }
_handleChangeRawValue (e: Event) { _handleChangeRawValue (e: SyntheticEvent<HTMLInputElement>) {
if (!(e.target instanceof HTMLInputElement)) { const value = e.currentTarget.value;
return;
}
const value = e.target.value;
clearTimeout(this._rawTimeout); clearTimeout(this._rawTimeout);
this._rawTimeout = setTimeout(async () => { this._rawTimeout = setTimeout(async () => {
const cookie = toughCookie.Cookie.parse(value); const oldCookie = this.state.cookie;
if (!this.state.cookie) { let cookie;
try {
// NOTE: Perform toJSON so we have a plain JS object instead of Cookie instance
cookie = toughCookie.Cookie.parse(value).toJSON();
} catch (err) {
console.warn(`Failed to parse cookie string "${value}"`, err);
return; return;
} }
if (!this.state.cookie || !oldCookie) {
return;
}
// Make sure cookie has an id
cookie.id = oldCookie.id;
await this._handleCookieUpdate(cookie); await this._handleCookieUpdate(cookie);
}, DEBOUNCE_MILLIS * 2); }, DEBOUNCE_MILLIS * 2);
} }
async _handleCookieUpdate (newCookie: Cookie) { async _handleCookieUpdate (newCookie: Cookie) {
const oldCookie = this.state.cookie;
if (!oldCookie) {
// We don't have a cookie to edit
return;
}
const cookie = clone(newCookie); const cookie = clone(newCookie);
// Sanitize expires field // Sanitize expires field
@ -111,9 +125,13 @@ class CookieModifyModal extends React.PureComponent<Props, State> {
const cookieJar = clone(this.props.cookieJar); const cookieJar = clone(this.props.cookieJar);
const {cookies} = cookieJar; const {cookies} = cookieJar;
const index = cookies.findIndex(c => c.id === cookie.id); const index = cookies.findIndex(c => c.id === cookie.id);
if (index < 0) {
console.warn(`Could not find cookie with id=${cookie.id} to edit`);
return;
}
cookieJar.cookies = [ cookieJar.cookies = [
...cookies.slice(0, index), ...cookies.slice(0, index),
cookie, cookie,