Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- componentWillMount() {
- //this.getTeam('國高', 'test-01', 10, 'free_point');
- this.getGMdata();
- }
- //GET這個GM寫了幾筆資料
- async getGMdata() {
- const sessionToken = await AsyncStorage.getItem('sessionToken');
- const userID = await AsyncStorage.getItem('userID');
- const params = {
- include: 'to_team',
- limit: 1000,
- where: {
- from_gm: {
- __type: 'Pointer',
- className: '_User',
- objectId: userID
- },
- }
- };
- const esc = encodeURIComponent;
- const query = Object.keys(params)
- .map(k => `${esc(k)}=${esc(JSON.stringify(params[k]))}`)
- .join('&');
- fetch(`${data.parseServerURL}/classes/Point?${query}`, {
- method: 'GET',
- headers: {
- 'X-Parse-Application-Id': data.parseAppId,
- 'X-Parse-REST-API-Key': data.paresApiKey,
- 'X-Parse-Session-Token': sessionToken
- }
- })
- .then((response) => response.json())
- .then(async (responseData) => {
- console.log(responseData);
- })
- .catch((error) => {
- console.log(error);
- });
- }
- //value: 要給幾點, kinds: 哪種類別
- getTeam(batch, teamName, value, kinds) {
- const params = {
- where: {
- batch,
- name: teamName
- }
- };
- const esc = encodeURIComponent;
- const query = Object.keys(params)
- .map(k => `${esc(k)}=${esc(JSON.stringify(params[k]))}`)
- .join('&');
- fetch(`${data.parseServerURL}/classes/Team?${query}`, {
- method: 'GET',
- headers: {
- 'X-Parse-Application-Id': data.parseAppId,
- 'X-Parse-REST-API-Key': data.paresApiKey,
- }
- })
- .then((response) => response.json())
- .then(async (responseData) => {
- console.log(responseData);
- console.log(responseData.results[0][kinds]);
- this.putTeam(
- responseData.results[0].objectId,
- batch,
- value,
- kinds,
- responseData.results[0][kinds]
- );
- })
- .catch((error) => {
- console.log(error);
- });
- }
- //value: 要給幾點, kinds: 哪種類別, originalValue: 原來類別的分數
- putTeam(teamID, batch, value, kinds, originalValue) {
- const params = {
- };
- params[kinds] = originalValue + value;
- fetch(`${data.parseServerURL}/classes/Team/${teamID}`, {
- method: 'PUT',
- headers: {
- 'X-Parse-Application-Id': data.parseAppId,
- 'X-Parse-REST-API-Key': data.paresApiKey,
- },
- body: JSON.stringify(params)
- })
- .then((success) => {
- console.log(success);
- this.postPoint(teamID, batch, value, kinds);
- })
- .catch((err) => {
- console.log(err);// error handling ..
- });
- }
- //value: 要給幾點, kinds: 哪種類別
- async postPoint(teamID, batch, value, kinds) {
- const sessionToken = await AsyncStorage.getItem('sessionToken');
- const userID = await AsyncStorage.getItem('userID');
- const params = {
- to_team: {
- __type: 'Pointer',
- className: 'Team',
- objectId: teamID
- },
- from_gm: {
- __type: 'Pointer',
- className: '_User',
- objectId: userID
- },
- ACL: {},
- kinds,
- batch,
- value
- };
- params.ACL[userID] = { read: true, write: true };
- params.ACL['*'] = { read: true };
- fetch(`${data.parseServerURL}/classes/Point/`, {
- method: 'POST',
- headers: {
- 'X-Parse-Application-Id': data.parseAppId,
- 'X-Parse-REST-API-Key': data.paresApiKey,
- 'X-Parse-Session-Token': sessionToken
- },
- body: JSON.stringify(params)
- })
- .then((success) => {
- console.log(success);
- })
- .catch((err) => {
- console.log(err);// error handling ..
- });
- }
Add Comment
Please, Sign In to add comment