Skip to content

Commit d5a2c6f

Browse files
committed
education services
1 parent 226ede9 commit d5a2c6f

File tree

6 files changed

+220
-22
lines changed

6 files changed

+220
-22
lines changed

src/actions/education.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @module "actions.education"
3+
* @desc Actions for interactions with profile details API.
4+
*/
5+
import { createActions } from 'redux-actions';
6+
7+
import { getService as getEducationService } from '../services/education';
8+
9+
function getEducationInit() {}
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 getEducationDone(handle, tokenV3) {
19+
const token= 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlcyI6WyJhZG1pbmlzdHJhdG9yIl0sImlzcyI6Imh0dHBzOi8vYXBpLnRvcGNvZGVyLWRldi5jb20iLCJoYW5kbGUiOiJoZWZmYW4iLCJleHAiOjE3NjYyODkyNDYsInVzZXJJZCI6IjEzMjQ1NiIsImlhdCI6MTQ1MDkyOTI0NiwiZW1haWwiOm51bGwsImp0aSI6IjEzNjljNjAwLWUwYTEtNDUyNS1hN2M3LTU2YmU3ZDgxM2Y1MSJ9.hp5peSoj-fh3KFkskvBpfUFIcJNtsv4zIMFV-D8F3JA';
20+
// console.log("education service returned", getEducationService(token).getEducation(handle, tokenV3));
21+
return getEducationService(token).getEducation(handle, tokenV3);
22+
//.then(data => data.json());
23+
}
24+
25+
function updateEducationInit() {}
26+
27+
function updateEducationDone(education, handle) {
28+
// console.log("Updated basic info/basic info actions: ", basicInfo);
29+
const token= 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlcyI6WyJhZG1pbmlzdHJhdG9yIl0sImlzcyI6Imh0dHBzOi8vYXBpLnRvcGNvZGVyLWRldi5jb20iLCJoYW5kbGUiOiJoZWZmYW4iLCJleHAiOjE3NjYyODkyNDYsInVzZXJJZCI6IjEzMjQ1NiIsImlhdCI6MTQ1MDkyOTI0NiwiZW1haWwiOm51bGwsImp0aSI6IjEzNjljNjAwLWUwYTEtNDUyNS1hN2M3LTU2YmU3ZDgxM2Y1MSJ9.hp5peSoj-fh3KFkskvBpfUFIcJNtsv4zIMFV-D8F3JA';
30+
const service= getEducationService(token);
31+
return service.updateEducation(education, handle);
32+
}
33+
export default createActions({
34+
EDUCATION: {
35+
GET_EDUCATION_INIT: getEducationInit,
36+
GET_EDUCATION_DONE: getEducationDone,
37+
UPDATE_EDUCATION_INIT: updateEducationInit,
38+
UPDATE_EDUCATION_DONE: updateEducationDone
39+
}
40+
});

src/actions/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import challengeActions from './challenge';
99
import profileActions from './profile';
1010
import basicInfoActions from './basicInfo';
1111
import languageActions from './language';
12+
import educationActions from './education';
1213
import memberActions from './members';
1314
import memberTaskActions from './member-tasks';
1415
import reviewOpportunityActions from './reviewOpportunity';
@@ -26,6 +27,7 @@ export const actions = {
2627
profile: profileActions.profile,
2728
basicInfo: basicInfoActions.basicInfo,
2829
language: languageActions.language,
30+
education: educationActions.education,
2931
members: memberActions.members,
3032
memberTasks: memberTaskActions.memberTasks,
3133
reviewOpportunity: reviewOpportunityActions.reviewOpportunity,

src/reducers/education.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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/education';
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 onGetEducationDone(state, { payload, error }) {
21+
console.log("payload", payload);
22+
if (error) {
23+
return { ...state };
24+
}
25+
console.log("Education", payload.result.content[0]);
26+
return ({
27+
...state, education: payload.result.content[0]
28+
});
29+
}
30+
31+
function onUpdateEducationDone(state, { payload, error }) {
32+
const newState = { ...state, updatingEducation: false };
33+
34+
if (error) {
35+
logger.error('Failed to update user education', payload);
36+
fireErrorMessage('ERROR: Failed to update user education!');
37+
return newState;
38+
}
39+
// console.log("{Payload Update basic info/ basic info reducers: ", payload);
40+
// if (!newState.info || newState.info.handle !== payload.handle) {
41+
// return newState;
42+
// }
43+
// console.log("{New State Update basic info/ basic info reducers: ", newState);
44+
return {
45+
...newState,
46+
education: {
47+
...newState.education,
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+
console.log("Entered create of edu reducer");
60+
const a = actions.education;
61+
return handleActions({
62+
[a.getEducationInit]: state => state,
63+
[a.getEducationDone]: onGetEducationDone,
64+
[a.updateEducationInit]: state => ({ ...state, updatingEducation: true }),
65+
[a.updateEducationDone]: onUpdateEducationDone,
66+
}, _.defaults(initialState, {education: null}));
67+
}
68+
69+
/**
70+
* Factory which creates a new reducer with its initial state tailored to the
71+
* given options object, if specified (for server-side rendering). If options
72+
* object is not specified, it creates just the default reducer. Accepted options are:
73+
* @returns {Promise}
74+
* @resolves {Function(state, action): state} New reducer.
75+
*/
76+
export function factory() {
77+
return Promise.resolve(create());
78+
}
79+
80+
/* Reducer with the default initial state. */
81+
export default create();

src/reducers/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import errors, { factory as errorsFactory } from './errors';
1212
import challenge, { factory as challengeFactory } from './challenge';
1313
import profile, { factory as profileFactory } from './profile';
1414
import basicInfo, { factory as basicInfoFactory } from './basicInfo';
15+
import education, { factory as educationFactory } from './education';
1516
import language, { factory as languageFactory } from './language';
1617
import members, { factory as membersFactory } from './members';
1718
import lookup, { factory as lookupFactory } from './lookup';
@@ -34,6 +35,7 @@ export function factory(options) {
3435
profile: profileFactory(options),
3536
basicInfo: basicInfoFactory(options),
3637
language: languageFactory(options),
38+
education: educationFactory(options),
3739
lookup: lookupFactory(options),
3840
members: membersFactory(options),
3941
memberTasks: memberTasksFactory(options),
@@ -53,6 +55,7 @@ export default ({
5355
profile,
5456
basicInfo,
5557
language,
58+
education,
5659
lookup,
5760
members,
5861
memberTasks,

src/reducers/language.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,27 @@ function onGetLanguageDone(state, { payload, error }) {
2828
});
2929
}
3030

31-
// function onUpdateLanguageDone(state, { payload, error }) {
32-
// const newState = { ...state, updatingLanguage: false };
31+
function onUpdateLanguageDone(state, { payload, error }) {
32+
const newState = { ...state, updatingLanguage: false };
3333

34-
// if (error) {
35-
// logger.error('Failed to update user language', payload);
36-
// fireErrorMessage('ERROR: Failed to update user language!');
37-
// return newState;
38-
// }
39-
// // console.log("{Payload Update basic info/ basic info reducers: ", payload);
40-
// // if (!newState.info || newState.info.handle !== payload.handle) {
41-
// // return newState;
42-
// // }
43-
// // console.log("{New State Update basic info/ basic info reducers: ", newState);
44-
// return {
45-
// ...newState,
46-
// language: {
47-
// ...newState.language,
48-
// ...payload,
49-
// },
50-
// };
51-
// }
34+
if (error) {
35+
logger.error('Failed to update user language', payload);
36+
fireErrorMessage('ERROR: Failed to update user language!');
37+
return newState;
38+
}
39+
// console.log("{Payload Update basic info/ basic info reducers: ", payload);
40+
// if (!newState.info || newState.info.handle !== payload.handle) {
41+
// return newState;
42+
// }
43+
// console.log("{New State Update basic info/ basic info reducers: ", newState);
44+
return {
45+
...newState,
46+
language: {
47+
...newState.language,
48+
...payload,
49+
},
50+
};
51+
}
5252

5353
/**
5454
* Creates a new Profile reducer with the specified initial state.
@@ -60,8 +60,8 @@ function create(initialState) {
6060
return handleActions({
6161
[a.getLanguageInit]: state => state,
6262
[a.getLanguageDone]: onGetLanguageDone,
63-
// [a.updateLanguageInit]: state => ({ ...state, updatingBasicInfo: true }),
64-
// [a.updateLanguageDone]: onUpdateBasicInfoDone,
63+
[a.updateLanguageInit]: state => ({ ...state, updatingLanguage: true }),
64+
[a.updateLanguageDone]: onUpdateLanguageDone,
6565
}, _.defaults(initialState, {language: null}));
6666
}
6767

src/services/education.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 EducationService {
19+
constructor(tokenV3) {
20+
this.private = {
21+
api: getApiV3(tokenV3),
22+
tokenV3,
23+
};
24+
}
25+
26+
27+
async getEducation(handle, token) {
28+
console.log("Entering education service with handle ", handle, "and token ", token);
29+
let res = await this.fetch(
30+
`http://local.topcoder-dev.com/v3/members/${handle}/traits?traitIds=education`,{
31+
headers: {
32+
'Content-Type': 'application/json',
33+
'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlcyI6WyJhZG1pbmlzdHJhdG9yIl0sImlzcyI6Imh0dHBzOi8vYXBpLnRvcGNvZGVyLWRldi5jb20iLCJoYW5kbGUiOiJoZWZmYW4iLCJleHAiOjE3NjYyODkyNDYsInVzZXJJZCI6IjEzMjQ1NiIsImlhdCI6MTQ1MDkyOTI0NiwiZW1haWwiOm51bGwsImp0aSI6IjEzNjljNjAwLWUwYTEtNDUyNS1hN2M3LTU2YmU3ZDgxM2Y1MSJ9.hp5peSoj-fh3KFkskvBpfUFIcJNtsv4zIMFV-D8F3JA'
34+
}
35+
}
36+
);
37+
console.log("Response education", 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 updateEducation(education, handle) {
43+
console.log("Updated education services: ", education, " with handle ", handle );
44+
const res = await this.private.api.putJsonLocal(`http://local.topcoder-dev.com/v3/members/${handle}/traits`, { param: [education] });
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 EducationService(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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy