@@ -71,6 +71,11 @@ const websocketUrl = POSTGRAPHILE_CONFIG.graphqlUrl.match(/^https?:/)
71
71
l . port !== 80 && l . port !== 443 ? ':' + l . port : ''
72
72
} ${ POSTGRAPHILE_CONFIG . graphqlUrl } `;
73
73
74
+ const STORAGE_KEYS = {
75
+ SAVE_HEADERS_TEXT : 'PostGraphiQL:saveHeadersText' ,
76
+ HEADERS_TEXT : 'PostGraphiQL:headersText' ,
77
+ } ;
78
+
74
79
/**
75
80
* The standard GraphiQL interface wrapped with some PostGraphile extensions.
76
81
* Including a JWT setter and live schema udpate capabilities.
@@ -85,7 +90,8 @@ class PostGraphiQL extends React.PureComponent {
85
90
schema : null ,
86
91
query : '' ,
87
92
showHeaderEditor : false ,
88
- headersText : '{\n"Authorization": null\n}\n' ,
93
+ saveHeadersText : this . _storage . get ( STORAGE_KEYS . SAVE_HEADERS_TEXT ) === 'true' ? true : false ,
94
+ headersText : this . _storage . get ( STORAGE_KEYS . HEADERS_TEXT ) || '{\n"Authorization": null\n}\n' ,
89
95
headersTextValid : true ,
90
96
explorerIsOpen : this . _storage . get ( 'explorerIsOpen' ) === 'false' ? false : true ,
91
97
haveActiveSubscription : false ,
@@ -510,6 +516,22 @@ class PostGraphiQL extends React.PureComponent {
510
516
) ;
511
517
} ;
512
518
519
+ handleToggleSaveHeaders = ( ) => {
520
+ this . setState (
521
+ oldState => ( { saveHeadersText : ! oldState . saveHeadersText } ) ,
522
+ ( ) => {
523
+ this . _storage . set (
524
+ STORAGE_KEYS . SAVE_HEADERS_TEXT ,
525
+ JSON . stringify ( this . state . saveHeadersText ) ,
526
+ ) ;
527
+ this . _storage . set (
528
+ STORAGE_KEYS . HEADERS_TEXT ,
529
+ this . state . saveHeadersText ? this . state . headersText : '' ,
530
+ ) ;
531
+ } ,
532
+ ) ;
533
+ } ;
534
+
513
535
renderSocketStatus ( ) {
514
536
const { socketStatus, error } = this . state ;
515
537
if ( socketStatus === null ) {
@@ -681,13 +703,18 @@ class PostGraphiQL extends React.PureComponent {
681
703
open = { this . state . showHeaderEditor }
682
704
value = { this . state . headersText }
683
705
valid = { this . state . headersTextValid }
706
+ toggleSaveHeaders = { this . handleToggleSaveHeaders }
707
+ saveHeaders = { this . state . saveHeadersText }
684
708
onChange = { e =>
685
709
this . setState (
686
710
{
687
711
headersText : e . target . value ,
688
712
headersTextValid : isValidJSON ( e . target . value ) ,
689
713
} ,
690
714
( ) => {
715
+ if ( this . state . headersTextValid && this . state . saveHeadersText ) {
716
+ this . _storage . set ( STORAGE_KEYS . HEADERS_TEXT , this . state . headersText ) ;
717
+ }
691
718
if ( this . state . headersTextValid && this . subscriptionsClient ) {
692
719
// Reconnect to websocket with new headers
693
720
this . subscriptionsClient . close ( false , true ) ;
@@ -706,7 +733,7 @@ class PostGraphiQL extends React.PureComponent {
706
733
}
707
734
}
708
735
709
- function EditHeaders ( { children, open, value, onChange, valid } ) {
736
+ function EditHeaders ( { children, open, value, onChange, valid, saveHeaders , toggleSaveHeaders } ) {
710
737
return (
711
738
< div
712
739
className = "graphiql-container not-really"
@@ -723,6 +750,10 @@ function EditHeaders({ children, open, value, onChange, valid }) {
723
750
< div className = "doc-explorer-rhs" > { children } </ div >
724
751
</ div >
725
752
< div className = "doc-explorer-contents" >
753
+ < label >
754
+ < input type = "checkbox" checked = { saveHeaders } onChange = { toggleSaveHeaders } />
755
+ save headers to localStorage
756
+ </ label >
726
757
< textarea
727
758
value = { value }
728
759
onChange = { onChange }
0 commit comments