Guest User

Untitled

a guest
Jun 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. componentWillMount() {
  2. //this.getTeam('國高', 'test-01', 10, 'free_point');
  3. this.getGMdata();
  4. }
  5.  
  6. //GET這個GM寫了幾筆資料
  7. async getGMdata() {
  8. const sessionToken = await AsyncStorage.getItem('sessionToken');
  9. const userID = await AsyncStorage.getItem('userID');
  10.  
  11. const params = {
  12. include: 'to_team',
  13. limit: 1000,
  14. where: {
  15. from_gm: {
  16. __type: 'Pointer',
  17. className: '_User',
  18. objectId: userID
  19. },
  20. }
  21. };
  22. const esc = encodeURIComponent;
  23. const query = Object.keys(params)
  24. .map(k => `${esc(k)}=${esc(JSON.stringify(params[k]))}`)
  25. .join('&');
  26. fetch(`${data.parseServerURL}/classes/Point?${query}`, {
  27. method: 'GET',
  28. headers: {
  29. 'X-Parse-Application-Id': data.parseAppId,
  30. 'X-Parse-REST-API-Key': data.paresApiKey,
  31. 'X-Parse-Session-Token': sessionToken
  32. }
  33. })
  34. .then((response) => response.json())
  35. .then(async (responseData) => {
  36. console.log(responseData);
  37. })
  38. .catch((error) => {
  39. console.log(error);
  40. });
  41. }
  42.  
  43. //value: 要給幾點, kinds: 哪種類別
  44. getTeam(batch, teamName, value, kinds) {
  45. const params = {
  46. where: {
  47. batch,
  48. name: teamName
  49. }
  50. };
  51. const esc = encodeURIComponent;
  52. const query = Object.keys(params)
  53. .map(k => `${esc(k)}=${esc(JSON.stringify(params[k]))}`)
  54. .join('&');
  55. fetch(`${data.parseServerURL}/classes/Team?${query}`, {
  56. method: 'GET',
  57. headers: {
  58. 'X-Parse-Application-Id': data.parseAppId,
  59. 'X-Parse-REST-API-Key': data.paresApiKey,
  60. }
  61. })
  62. .then((response) => response.json())
  63. .then(async (responseData) => {
  64. console.log(responseData);
  65. console.log(responseData.results[0][kinds]);
  66. this.putTeam(
  67. responseData.results[0].objectId,
  68. batch,
  69. value,
  70. kinds,
  71. responseData.results[0][kinds]
  72. );
  73. })
  74. .catch((error) => {
  75. console.log(error);
  76. });
  77. }
  78.  
  79. //value: 要給幾點, kinds: 哪種類別, originalValue: 原來類別的分數
  80. putTeam(teamID, batch, value, kinds, originalValue) {
  81. const params = {
  82.  
  83. };
  84.  
  85. params[kinds] = originalValue + value;
  86.  
  87. fetch(`${data.parseServerURL}/classes/Team/${teamID}`, {
  88. method: 'PUT',
  89. headers: {
  90. 'X-Parse-Application-Id': data.parseAppId,
  91. 'X-Parse-REST-API-Key': data.paresApiKey,
  92. },
  93. body: JSON.stringify(params)
  94. })
  95. .then((success) => {
  96. console.log(success);
  97. this.postPoint(teamID, batch, value, kinds);
  98. })
  99. .catch((err) => {
  100. console.log(err);// error handling ..
  101. });
  102. }
  103.  
  104. //value: 要給幾點, kinds: 哪種類別
  105. async postPoint(teamID, batch, value, kinds) {
  106. const sessionToken = await AsyncStorage.getItem('sessionToken');
  107. const userID = await AsyncStorage.getItem('userID');
  108.  
  109. const params = {
  110. to_team: {
  111. __type: 'Pointer',
  112. className: 'Team',
  113. objectId: teamID
  114. },
  115. from_gm: {
  116. __type: 'Pointer',
  117. className: '_User',
  118. objectId: userID
  119. },
  120.  
  121. ACL: {},
  122. kinds,
  123. batch,
  124. value
  125. };
  126.  
  127. params.ACL[userID] = { read: true, write: true };
  128. params.ACL['*'] = { read: true };
  129.  
  130. fetch(`${data.parseServerURL}/classes/Point/`, {
  131. method: 'POST',
  132. headers: {
  133. 'X-Parse-Application-Id': data.parseAppId,
  134. 'X-Parse-REST-API-Key': data.paresApiKey,
  135. 'X-Parse-Session-Token': sessionToken
  136. },
  137. body: JSON.stringify(params)
  138. })
  139. .then((success) => {
  140. console.log(success);
  141. })
  142. .catch((err) => {
  143. console.log(err);// error handling ..
  144. });
  145. }
Add Comment
Please, Sign In to add comment