Ingen beskrivning

XHRExampleCookies.js 3.3KB

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