From c69c8d2dd0ff6dc02090acfa43c563e3fa3adba1 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Tue, 22 Aug 2017 18:16:21 -0700 Subject: [PATCH] Better cookie handling/editing --- .editorconfig | 3 +- .../components/modals/cookie-modify-modal.js | 152 +++++++++--------- app/ui/components/modals/cookies-modal.js | 125 +++++++++----- flow-typed/deep-equal.js | 3 + 4 files changed, 167 insertions(+), 116 deletions(-) create mode 100644 flow-typed/deep-equal.js diff --git a/.editorconfig b/.editorconfig index 6c5f7a893..e4be6c0ab 100644 --- a/.editorconfig +++ b/.editorconfig @@ -3,4 +3,5 @@ charset = utf-8 indent_style = space indent_size = 2 insert_final_newline = true -trim_trailing_whitespace = true \ No newline at end of file +trim_trailing_whitespace = true +end_of_line = lf \ No newline at end of file diff --git a/app/ui/components/modals/cookie-modify-modal.js b/app/ui/components/modals/cookie-modify-modal.js index 5b38e1e1d..107648346 100644 --- a/app/ui/components/modals/cookie-modify-modal.js +++ b/app/ui/components/modals/cookie-modify-modal.js @@ -2,8 +2,10 @@ import React, {PureComponent} from 'react'; import {Tabs, TabList, Tab, TabPanel} from 'react-tabs'; import autobind from 'autobind-decorator'; +import deepEqual from 'deep-equal'; import * as toughCookie from 'tough-cookie'; import * as models from '../../../models'; +import clone from 'clone'; import {DEBOUNCE_MILLIS} from '../../../common/constants'; import {trackEvent} from '../../../analytics/index'; import Modal from '../base/modal'; @@ -96,9 +98,11 @@ class CookieModifyModal extends PureComponent { } async _handleCookieUpdate (oldCookie: Cookie, cookie: Cookie) { - const {cookieJar} = this.props; + // Clone so we don't modify the original + const cookieJar = clone(this.props.cookieJar); + const {cookies} = cookieJar; - const index = cookies.findIndex(c => c.domain === oldCookie.domain && c.key === oldCookie.key); + const index = cookies.findIndex(c => deepEqual(c, oldCookie)); cookieJar.cookies = [ ...cookies.slice(0, index), @@ -144,6 +148,21 @@ class CookieModifyModal extends PureComponent { return str.charAt(0).toUpperCase() + str.slice(1); } + _getRawCookieString () { + const {cookie} = this.state; + + if (!cookie) { + return ''; + } + + try { + return cookieToString(toughCookie.Cookie.fromJSON(JSON.stringify(cookie))); + } catch (err) { + console.warn('Failed to parse cookie string', err); + return ''; + } + } + render () { const { cookieJar, @@ -151,91 +170,78 @@ class CookieModifyModal extends PureComponent { handleGetRenderContext } = this.props; - if (!cookieJar) { - return null; - } - const { isValid, cookie } = this.state; - if (!cookie || !cookieJar) { - return null; - } - const textFields = ['key', 'value', 'domain', 'path', 'expires']; const checkFields = ['secure', 'httpOnly']; - let rawCookieString = ''; - try { - rawCookieString = cookieToString(toughCookie.Cookie.fromJSON(JSON.stringify(cookie))); - } catch (err) { - console.warn('Failed to parse cookie', err); - } - return ( Edit Cookie - - - - - - - - - - -
- {textFields.map((field, i) => { - const val = (cookie[field] || '').toString(); + {cookieJar && cookie && ( + + + + + + + + + + +
+ {textFields.map((field, i) => { + const val = (cookie[field] || '').toString(); - return ( -
-
+
+ {checkFields.map((field, i) => { + const checked = !!cookie[field]; + + return ( + -
- ); - })} -
-
- {checkFields.map((field, i) => { - const checked = !!cookie[field]; - - return ( - - ); - })} -
-
- -
- -
-
-
+ ); + })} +
+
+ +
+ +
+
+
+ )}