@@ -7,53 +7,73 @@ export default class extends Component {
77
88 constructor ( props ) {
99 super ( props ) ;
10+ this . handleSource ( props . source , props . newWindow ) ;
11+ }
12+
13+ handleSource = ( source , newWindow ) => {
14+ if ( ! source . method ) return ;
15+
16+ if ( newWindow ) {
17+ this . handleSourceInNewWindow ( source ) ;
18+ } else {
19+ this . handleSourceInIFrame ( source ) ;
20+ }
21+ } ;
1022
11- const { source } = props ;
12- if ( source . method ) {
13- if ( props . newWindow ) {
14- if ( source . method === 'POST' ) {
15- const contentType = source . headers [ 'Content-Type' ] ;
16- let body = '' ;
17- if ( contentType && contentType . includes ( 'application/x-www-form-urlencoded' ) ) {
18- body = Qs . parse ( source . body ) ;
19- } else {
20- console . warn (
21- '[WebView] When opening a new window, this content-type is not supported yet, please make a PR!' ,
22- contentType
23- ) ;
24- return ;
25- }
23+ handleSourceInIFrame = source => {
24+ const { uri, ...options } = source ;
25+ const baseUrl = uri . substr ( 0 , uri . lastIndexOf ( '/' ) + 1 ) ;
26+ fetch ( uri , options )
27+ . then ( response => response . text ( ) )
28+ . then ( html => this . setState ( { html : `<base href="${ baseUrl } " />` + html } ) ) ;
29+ } ;
2630
27- window . open (
28- require ( './postMock.html' ) +
29- '?' +
30- Qs . stringify ( {
31- uri : source . uri ,
32- body : JSON . stringify ( body ) ,
33- } )
34- ) ;
35- } else {
36- console . warn (
37- '[WebView] When opening a new window, this method is not supported yet, please make a PR!' ,
38- source . method
39- ) ;
40- }
31+ handleSourceInNewWindow = source => {
32+ if ( source . method === 'POST' ) {
33+ const contentType = source . headers [ 'Content-Type' ] ;
34+ let body = '' ;
35+ if ( contentType && contentType . includes ( 'application/x-www-form-urlencoded' ) ) {
36+ body = Qs . parse ( source . body ) ;
4137 } else {
42- const { uri , ... options } = source ;
43- const baseUrl = uri . substr ( 0 , uri . lastIndexOf ( '/' ) + 1 ) ;
44- fetch ( uri , options )
45- . then ( response => response . text ( ) )
46- . then ( html => this . setState ( { html : `<base href=" ${ baseUrl } " />` + html } ) ) ;
38+ console . warn (
39+ '[WebView] When opening a new window, this content-type is not supported yet, please make a PR!' ,
40+ contentType
41+ ) ;
42+ return ;
4743 }
44+
45+ window . open (
46+ require ( './postMock.html' ) +
47+ '?' +
48+ Qs . stringify ( {
49+ uri : source . uri ,
50+ body : JSON . stringify ( body ) ,
51+ } )
52+ ) ;
53+ } else {
54+ console . warn (
55+ '[WebView] When opening a new window, this method is not supported yet, please make a PR!' ,
56+ source . method
57+ ) ;
4858 }
49- }
59+ } ;
5060
5161 componentDidMount ( ) {
5262 if ( this . props . onMessage ) {
5363 window . addEventListener ( 'message' , this . onMessage , true ) ;
5464 }
5565 }
5666
67+ componentWillReceiveProps ( nextProps ) {
68+ if (
69+ this . props . source . uri !== nextProps . source . uri ||
70+ this . props . source . method !== nextProps . source . method ||
71+ this . props . source . body !== nextProps . source . body
72+ ) {
73+ this . handleSource ( nextProps . source , nextProps . newWindow ) ;
74+ }
75+ }
76+
5777 componentWillUnmount ( ) {
5878 if ( this . props . onMessage ) {
5979 window . removeEventListener ( 'message' , this . onMessage , true ) ;
0 commit comments