@@ -7,53 +7,73 @@ export default class extends Component {
7
7
8
8
constructor ( props ) {
9
9
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
+ } ;
10
22
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
+ } ;
26
30
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 ) ;
41
37
} 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 ;
47
43
}
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
+ ) ;
48
58
}
49
- }
59
+ } ;
50
60
51
61
componentDidMount ( ) {
52
62
if ( this . props . onMessage ) {
53
63
window . addEventListener ( 'message' , this . onMessage , true ) ;
54
64
}
55
65
}
56
66
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
+
57
77
componentWillUnmount ( ) {
58
78
if ( this . props . onMessage ) {
59
79
window . removeEventListener ( 'message' , this . onMessage , true ) ;
0 commit comments