Skip to content

Commit 64a5edf

Browse files
committed
device and software everything done
1 parent d5a2c6f commit 64a5edf

File tree

8 files changed

+394
-0
lines changed

8 files changed

+394
-0
lines changed

src/actions/devices.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @module "actions.devices"
3+
* @desc Actions for interactions with profile details API.
4+
*/
5+
import { createActions } from 'redux-actions';
6+
7+
import { getService as getDevicesService } from '../services/devices';
8+
9+
function getDevicesInit() {}
10+
/**
11+
* @static
12+
* @desc Creates an action that gets linked accounts.
13+
*
14+
* @param {Object} handle Topcoder member profile.basicInfo: null
15+
* @param {String} tokenV3 Topcoder auth token v3.
16+
* @return {Action}
17+
*/
18+
function getDevicesDone(handle, tokenV3) {
19+
const token= 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlcyI6WyJhZG1pbmlzdHJhdG9yIl0sImlzcyI6Imh0dHBzOi8vYXBpLnRvcGNvZGVyLWRldi5jb20iLCJoYW5kbGUiOiJoZWZmYW4iLCJleHAiOjE3NjYyODkyNDYsInVzZXJJZCI6IjEzMjQ1NiIsImlhdCI6MTQ1MDkyOTI0NiwiZW1haWwiOm51bGwsImp0aSI6IjEzNjljNjAwLWUwYTEtNDUyNS1hN2M3LTU2YmU3ZDgxM2Y1MSJ9.hp5peSoj-fh3KFkskvBpfUFIcJNtsv4zIMFV-D8F3JA';
20+
// console.log("education service returned", getEducationService(token).getEducation(handle, tokenV3));
21+
return getDevicesService(token).getDevices(handle, tokenV3);
22+
//.then(data => data.json());
23+
}
24+
25+
function updateDevicesInit() {}
26+
27+
function updateDevicesDone(device, handle) {
28+
// console.log("Updated basic info/basic info actions: ", basicInfo);
29+
const token= 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlcyI6WyJhZG1pbmlzdHJhdG9yIl0sImlzcyI6Imh0dHBzOi8vYXBpLnRvcGNvZGVyLWRldi5jb20iLCJoYW5kbGUiOiJoZWZmYW4iLCJleHAiOjE3NjYyODkyNDYsInVzZXJJZCI6IjEzMjQ1NiIsImlhdCI6MTQ1MDkyOTI0NiwiZW1haWwiOm51bGwsImp0aSI6IjEzNjljNjAwLWUwYTEtNDUyNS1hN2M3LTU2YmU3ZDgxM2Y1MSJ9.hp5peSoj-fh3KFkskvBpfUFIcJNtsv4zIMFV-D8F3JA';
30+
const service= getDevicesService(token);
31+
return service.updateDevices(device, handle);
32+
}
33+
export default createActions({
34+
DEVICES: {
35+
GET_DEVICES_INIT: getDevicesInit,
36+
GET_DEVICES_DONE: getDevicesDone,
37+
UPDATE_DEVICES_INIT: updateDevicesInit,
38+
UPDATE_DEVICES_DONE: updateDevicesDone
39+
}
40+
});

src/actions/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import profileActions from './profile';
1010
import basicInfoActions from './basicInfo';
1111
import languageActions from './language';
1212
import educationActions from './education';
13+
import devicesActions from './devices';
14+
import softwareActions from './software';
1315
import memberActions from './members';
1416
import memberTaskActions from './member-tasks';
1517
import reviewOpportunityActions from './reviewOpportunity';
@@ -26,8 +28,10 @@ export const actions = {
2628
errors: errorActions.errors,
2729
profile: profileActions.profile,
2830
basicInfo: basicInfoActions.basicInfo,
31+
software: softwareActions.software,
2932
language: languageActions.language,
3033
education: educationActions.education,
34+
devices: devicesActions.devices,
3135
members: memberActions.members,
3236
memberTasks: memberTaskActions.memberTasks,
3337
reviewOpportunity: reviewOpportunityActions.reviewOpportunity,

src/actions/software.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @module "actions.software"
3+
* @desc Actions for interactions with profile details API.
4+
*/
5+
import { createActions } from 'redux-actions';
6+
7+
import { getService as getSoftwareService } from '../services/software';
8+
9+
function getSoftwareInit() {}
10+
/**
11+
* @static
12+
* @desc Creates an action that gets linked accounts.
13+
*
14+
* @param {Object} handle Topcoder member profile.basicInfo: null
15+
* @param {String} tokenV3 Topcoder auth token v3.
16+
* @return {Action}
17+
*/
18+
function getSoftwareDone(handle, tokenV3) {
19+
const token= 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlcyI6WyJhZG1pbmlzdHJhdG9yIl0sImlzcyI6Imh0dHBzOi8vYXBpLnRvcGNvZGVyLWRldi5jb20iLCJoYW5kbGUiOiJoZWZmYW4iLCJleHAiOjE3NjYyODkyNDYsInVzZXJJZCI6IjEzMjQ1NiIsImlhdCI6MTQ1MDkyOTI0NiwiZW1haWwiOm51bGwsImp0aSI6IjEzNjljNjAwLWUwYTEtNDUyNS1hN2M3LTU2YmU3ZDgxM2Y1MSJ9.hp5peSoj-fh3KFkskvBpfUFIcJNtsv4zIMFV-D8F3JA';
20+
// console.log("education service returned", getEducationService(token).getEducation(handle, tokenV3));
21+
return getSoftwareService(token).getSoftware(handle, tokenV3);
22+
//.then(data => data.json());
23+
}
24+
25+
function updateSoftwareInit() {}
26+
27+
function updateSoftwareDone(software, handle) {
28+
// console.log("Updated basic info/basic info actions: ", basicInfo);
29+
const token= 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlcyI6WyJhZG1pbmlzdHJhdG9yIl0sImlzcyI6Imh0dHBzOi8vYXBpLnRvcGNvZGVyLWRldi5jb20iLCJoYW5kbGUiOiJoZWZmYW4iLCJleHAiOjE3NjYyODkyNDYsInVzZXJJZCI6IjEzMjQ1NiIsImlhdCI6MTQ1MDkyOTI0NiwiZW1haWwiOm51bGwsImp0aSI6IjEzNjljNjAwLWUwYTEtNDUyNS1hN2M3LTU2YmU3ZDgxM2Y1MSJ9.hp5peSoj-fh3KFkskvBpfUFIcJNtsv4zIMFV-D8F3JA';
30+
const service= getSoftwareService(token);
31+
return service.updateSoftware(software, handle);
32+
}
33+
export default createActions({
34+
SOFTWARE: {
35+
GET_SOFTWARE_INIT: getSoftwareInit,
36+
GET_SOFTWARE_DONE: getSoftwareDone,
37+
UPDATE_SOFTWARE_INIT: updateSoftwareInit,
38+
UPDATE_SOFTWARE_DONE: updateSoftwareDone
39+
}
40+
});

src/reducers/devices.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* @module "reducers.profile"
3+
* @desc Reducer for Profile API data
4+
* @todo Document reducer state structure.
5+
*/
6+
import _ from 'lodash';
7+
import { handleActions } from 'redux-actions';
8+
import actions from '../actions/devices';
9+
import logger from '../utils/logger';
10+
import { fireErrorMessage } from '../utils/errors';
11+
12+
13+
14+
/**
15+
* Handles PROFILE/GET_EXTERNAL_ACCOUNTS_DONE action.
16+
* @param {Object} state
17+
* @param {Object} action Payload will be JSON from api call
18+
* @return {Object} New state
19+
*/
20+
function onGetDevicesDone(state, { payload, error }) {
21+
console.log("payload", payload);
22+
if (error) {
23+
return { ...state };
24+
}
25+
console.log("devices", payload.result.content[0]);
26+
return ({
27+
...state, devices: payload.result.content[0]
28+
});
29+
}
30+
31+
function onUpdateDevicesDone(state, { payload, error }) {
32+
const newState = { ...state, updatingDevices: false };
33+
34+
if (error) {
35+
logger.error('Failed to update devices info', payload);
36+
fireErrorMessage('ERROR: Failed to update devices info!');
37+
return newState;
38+
}
39+
console.log("{Payload Update devices/ devices reducers: ", payload);
40+
// if (!newState.info || newState.info.handle !== payload.handle) {
41+
// return newState;
42+
// }
43+
console.log("{New State Update devices/ devices reducers: ", newState);
44+
return {
45+
...newState,
46+
devices: {
47+
...newState.devices,
48+
...payload,
49+
},
50+
};
51+
}
52+
53+
/**
54+
* Creates a new Profile reducer with the specified initial state.
55+
* @param {Object} initialState Optional. Initial state.
56+
* @return {Function} Profile reducer.
57+
*/
58+
function create(initialState) {
59+
const a = actions.devices;
60+
return handleActions({
61+
[a.getDevicesInit]: state => state,
62+
[a.getDevicesDone]: onGetDevicesDone,
63+
[a.updateDevicesInit]: state => ({ ...state, updatingDevices: true }),
64+
[a.updateDevicesDone]: onUpdateDevicesDone,
65+
}, _.defaults(initialState, {devices: null}));
66+
}
67+
68+
/**
69+
* Factory which creates a new reducer with its initial state tailored to the
70+
* given options object, if specified (for server-side rendering). If options
71+
* object is not specified, it creates just the default reducer. Accepted options are:
72+
* @returns {Promise}
73+
* @resolves {Function(state, action): state} New reducer.
74+
*/
75+
export function factory() {
76+
return Promise.resolve(create());
77+
}
78+
79+
/* Reducer with the default initial state. */
80+
export default create();

src/reducers/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import profile, { factory as profileFactory } from './profile';
1414
import basicInfo, { factory as basicInfoFactory } from './basicInfo';
1515
import education, { factory as educationFactory } from './education';
1616
import language, { factory as languageFactory } from './language';
17+
import software, { factory as softwareFactory } from './software';
18+
import devices, { factory as devicesFactory } from './devices';
1719
import members, { factory as membersFactory } from './members';
1820
import lookup, { factory as lookupFactory } from './lookup';
1921
import memberTasks, { factory as memberTasksFactory } from './member-tasks';
@@ -36,6 +38,8 @@ export function factory(options) {
3638
basicInfo: basicInfoFactory(options),
3739
language: languageFactory(options),
3840
education: educationFactory(options),
41+
devices: devicesFactory(options),
42+
software: softwareFactory(options),
3943
lookup: lookupFactory(options),
4044
members: membersFactory(options),
4145
memberTasks: memberTasksFactory(options),
@@ -56,6 +60,8 @@ export default ({
5660
basicInfo,
5761
language,
5862
education,
63+
devices,
64+
software,
5965
lookup,
6066
members,
6167
memberTasks,

src/reducers/software.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* @module "reducers.profile"
3+
* @desc Reducer for Profile API data
4+
* @todo Document reducer state structure.
5+
*/
6+
import _ from 'lodash';
7+
import { handleActions } from 'redux-actions';
8+
import actions from '../actions/software';
9+
import logger from '../utils/logger';
10+
import { fireErrorMessage } from '../utils/errors';
11+
12+
13+
14+
/**
15+
* Handles PROFILE/GET_EXTERNAL_ACCOUNTS_DONE action.
16+
* @param {Object} state
17+
* @param {Object} action Payload will be JSON from api call
18+
* @return {Object} New state
19+
*/
20+
function onGetSoftwareDone(state, { payload, error }) {
21+
console.log("payload", payload);
22+
if (error) {
23+
return { ...state };
24+
}
25+
console.log("software", payload.result.content[0]);
26+
return ({
27+
...state, software: payload.result.content[0]
28+
});
29+
}
30+
31+
function onUpdateSoftwareDone(state, { payload, error }) {
32+
const newState = { ...state, updatingSoftware: false };
33+
34+
if (error) {
35+
logger.error('Failed to update software info', payload);
36+
fireErrorMessage('ERROR: Failed to update software info!');
37+
return newState;
38+
}
39+
console.log("{Payload Update software/ software reducers: ", payload);
40+
// if (!newState.info || newState.info.handle !== payload.handle) {
41+
// return newState;
42+
// }
43+
console.log("{New State Update software/ software reducers: ", newState);
44+
return {
45+
...newState,
46+
software: {
47+
...newState.software,
48+
...payload,
49+
},
50+
};
51+
}
52+
53+
/**
54+
* Creates a new Profile reducer with the specified initial state.
55+
* @param {Object} initialState Optional. Initial state.
56+
* @return {Function} Profile reducer.
57+
*/
58+
function create(initialState) {
59+
const a = actions.software;
60+
return handleActions({
61+
[a.getSoftwareInit]: state => state,
62+
[a.getSoftwareDone]: onGetSoftwareDone,
63+
[a.updateSoftwareInit]: state => ({ ...state, updatingSoftware: true }),
64+
[a.updateSoftwareDone]: onUpdateSoftwareDone,
65+
}, _.defaults(initialState, {software: null}));
66+
}
67+
68+
/**
69+
* Factory which creates a new reducer with its initial state tailored to the
70+
* given options object, if specified (for server-side rendering). If options
71+
* object is not specified, it creates just the default reducer. Accepted options are:
72+
* @returns {Promise}
73+
* @resolves {Function(state, action): state} New reducer.
74+
*/
75+
export function factory() {
76+
return Promise.resolve(create());
77+
}
78+
79+
/* Reducer with the default initial state. */
80+
export default create();

src/services/devices.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* @module "services.members"
3+
* @desc This module provides a service for searching for Topcoder
4+
* members via API V3.
5+
*/
6+
7+
/* global XMLHttpRequest */
8+
import _ from 'lodash';
9+
import qs from 'qs';
10+
import logger from '../utils/logger';
11+
import { setErrorIcon, ERROR_ICON_TYPES } from 'utils/errors';
12+
import { getApiResponsePayloadV3 } from '../utils/tc';
13+
import { getApiV3 } from './api';
14+
import 'isomorphic-fetch';
15+
/**
16+
* Service class.
17+
*/
18+
class DevicesService {
19+
constructor(tokenV3) {
20+
this.private = {
21+
api: getApiV3(tokenV3),
22+
tokenV3,
23+
};
24+
}
25+
26+
27+
async getDevices(handle, token) {
28+
console.log("Entering devices service with handle ", handle, "and token ", token);
29+
let res = await this.fetch(
30+
`http://local.topcoder-dev.com/v3/members/${handle}/traits?traitIds=device`,{
31+
headers: {
32+
'Content-Type': 'application/json',
33+
'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlcyI6WyJhZG1pbmlzdHJhdG9yIl0sImlzcyI6Imh0dHBzOi8vYXBpLnRvcGNvZGVyLWRldi5jb20iLCJoYW5kbGUiOiJoZWZmYW4iLCJleHAiOjE3NjYyODkyNDYsInVzZXJJZCI6IjEzMjQ1NiIsImlhdCI6MTQ1MDkyOTI0NiwiZW1haWwiOm51bGwsImp0aSI6IjEzNjljNjAwLWUwYTEtNDUyNS1hN2M3LTU2YmU3ZDgxM2Y1MSJ9.hp5peSoj-fh3KFkskvBpfUFIcJNtsv4zIMFV-D8F3JA'
34+
}
35+
}
36+
);
37+
console.log("Response devices", res);
38+
if (!res.ok) throw new Error(res.statusText);
39+
if (res.status !== 200) throw new Error(res.content);
40+
return res.json();
41+
}
42+
async updateDevices(device, handle) {
43+
console.log("Updated devices/devices services: ", device);
44+
const res = await this.private.api.putJsonLocal(`http://local.topcoder-dev.com/v3/members/${handle}/traits`, { param: [device] });
45+
return getApiResponsePayloadV3(res);
46+
}
47+
fetch(endpoint, options = {}) {
48+
49+
console.log("endpoint ===>>>> " + endpoint);
50+
51+
const headers = options.headers ? _.clone(options.headers) : {};
52+
return fetch(`${endpoint}`, { ...options,
53+
headers,
54+
})
55+
.catch((e) => {
56+
setErrorIcon(ERROR_ICON_TYPES.NETWORK, `${endpoint}`, e.message);
57+
throw e;
58+
});
59+
}
60+
61+
62+
}
63+
let lastInstance = null;
64+
export function getService(tokenV3) {
65+
if (!lastInstance || tokenV3 !== lastInstance.private.tokenV3) {
66+
lastInstance = new DevicesService(tokenV3);
67+
}
68+
return lastInstance;
69+
}
70+
71+
/* Using default export would be confusing in this case. */
72+
export default undefined;

0 commit comments

Comments
 (0)