Nav apraksta

XHRExampleCookies.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * Copyright (c) 2018-present, Infinite Red, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. * @flow
  9. */
  10. 'use strict';
  11. var React = require('react');
  12. var ReactNative = require('react-native');
  13. var {StyleSheet, Text, TouchableHighlight, View, WebView} = ReactNative;
  14. var RCTNetworking = require('RCTNetworking');
  15. class XHRExampleCookies extends React.Component<any, any> {
  16. cancelled: boolean;
  17. constructor(props: any) {
  18. super(props);
  19. this.cancelled = false;
  20. this.state = {
  21. status: '',
  22. a: 1,
  23. b: 2,
  24. };
  25. }
  26. setCookie(domain: string) {
  27. var {a, b} = this.state;
  28. var url = `https://${domain}/cookies/set?a=${a}&b=${b}`;
  29. fetch(url).then(response => {
  30. this.setStatus(`Cookies a=${a}, b=${b} set`);
  31. this.refreshWebview();
  32. });
  33. this.setState({
  34. status: 'Setting cookies...',
  35. a: a + 1,
  36. b: b + 2,
  37. });
  38. }
  39. getCookies(domain: string) {
  40. fetch(`https://${domain}/cookies`)
  41. .then(response => {
  42. return response.json();
  43. })
  44. .then(data => {
  45. this.setStatus(
  46. `Got cookies ${JSON.stringify(data.cookies)} from server`,
  47. );
  48. this.refreshWebview();
  49. });
  50. this.setStatus('Getting cookies...');
  51. }
  52. clearCookies() {
  53. RCTNetworking.clearCookies(cleared => {
  54. this.setStatus('Cookies cleared, had cookies=' + cleared.toString());
  55. this.refreshWebview();
  56. });
  57. }
  58. refreshWebview() {
  59. this.refs.webview.reload();
  60. }
  61. setStatus(status: string) {
  62. this.setState({status});
  63. }
  64. render() {
  65. return (
  66. <View>
  67. <TouchableHighlight
  68. style={styles.wrapper}
  69. onPress={this.setCookie.bind(this, 'httpbin.org')}>
  70. <View style={styles.button}>
  71. <Text>Set cookie</Text>
  72. </View>
  73. </TouchableHighlight>
  74. <TouchableHighlight
  75. style={styles.wrapper}
  76. onPress={this.setCookie.bind(this, 'eu.httpbin.org')}>
  77. <View style={styles.button}>
  78. <Text>Set cookie (EU)</Text>
  79. </View>
  80. </TouchableHighlight>
  81. <TouchableHighlight
  82. style={styles.wrapper}
  83. onPress={this.getCookies.bind(this, 'httpbin.org')}>
  84. <View style={styles.button}>
  85. <Text>Get cookies</Text>
  86. </View>
  87. </TouchableHighlight>
  88. <TouchableHighlight
  89. style={styles.wrapper}
  90. onPress={this.getCookies.bind(this, 'eu.httpbin.org')}>
  91. <View style={styles.button}>
  92. <Text>Get cookies (EU)</Text>
  93. </View>
  94. </TouchableHighlight>
  95. <TouchableHighlight
  96. style={styles.wrapper}
  97. onPress={this.clearCookies.bind(this)}>
  98. <View style={styles.button}>
  99. <Text>Clear cookies</Text>
  100. </View>
  101. </TouchableHighlight>
  102. <Text>{this.state.status}</Text>
  103. <TouchableHighlight
  104. style={styles.wrapper}
  105. onPress={this.refreshWebview.bind(this)}>
  106. <View style={styles.button}>
  107. <Text>Refresh Webview</Text>
  108. </View>
  109. </TouchableHighlight>
  110. <WebView
  111. ref="webview"
  112. source={{uri: 'http://httpbin.org/cookies'}}
  113. style={{height: 100}}
  114. />
  115. </View>
  116. );
  117. }
  118. }
  119. var styles = StyleSheet.create({
  120. wrapper: {
  121. borderRadius: 5,
  122. marginBottom: 5,
  123. },
  124. button: {
  125. backgroundColor: '#eeeeee',
  126. padding: 8,
  127. },
  128. });
  129. module.exports = XHRExampleCookies;