trc-client-core
Version:
The core of the TRC Client
243 lines (194 loc) • 5.68 kB
JavaScript
import {
LEARNING_PLAN_GET_RECEIVE,
LEARNING_PLAN_LIST_RECEIVE,
LEARNING_PLAN_DELETE_RECEIVE
} from 'trc-client-core/src/constants/ActionTypes';
import {
CERTIFICATION,
LEARNING_PLAN
} from 'trc-client-core/src/constants/PathwayType';
import { Map, List, fromJS, OrderedMap } from 'immutable';
const initialState = Map({
collection: OrderedMap(),
listReceived: false
});
/*
TODO: this really needs to be revisited for several reasons
A bunch of the data from the server doesn't match what is sent back to the server to update information
so for now data form the srver must be modified to match what the server SHOULD be sending us
please see temporarilyModifyLearningPlan(), modifyPathwayType(), createSegmentFromCourseMap() and createSegmentFromCertificationMap()
*/
/*
TODO: remove this once the server is sorted out
temporarilyModifyLearningPlan()
accepts a learnin plan (immutable map) and modifies the data
*/
const temporarilyModifyLearningPlan = function(learningPlanMap) {
return learningPlanMap
.set('pathwayType', modifyPathwayType(learningPlanMap.get('pathwayType')))
.set('segments', createSegmentFromCourseMap(learningPlanMap.get('courseLevelMap')))
.delete('courseLevelMap')
.set('certifications', createSegmentFromCertificationMap(learningPlanMap.get('certificationMap')))
.delete('certificationMap');
}
/*
TODO: remove this once the server is sorted out
modifyPathwayType()
the server give us uppercase pathwayTypes, but expects to receive title case pathwaysTypes when creating or updating a record
the server should give us title case pathwaysTypes OR expect uppercase pathwayTypes
until the, this makes them all title case
*/
const modifyPathwayType = function(pathwayType) {
switch(pathwayType) {
case "CERTIFICATION":
return CERTIFICATION;
case "LEARNING_PLAN":
return LEARNING_PLAN;
}
return pathwayType;
}
/*
TODO: remove this once the server is sorted out
createSegmentFromCoursemap()
temporary function to create segments from courseLevelMap
Server gives us:
{
courseLevelMap: {
INTRODUCTION : [
{
courseCode: "ABC123"
}
],
ANOTHER: []
}
}
but we want (and have to provide back to the server to update):
{
segments: [
{
name: "INTRODUCTION",
courses: [
{
courseCode: "ABC123"
}
]
},
{
name: "ANOTHER",
courses: []
}
]
}
*/
//
const createSegmentFromCourseMap = function(map) {
if(!map) {
return List();
}
return map.map((courses, segmentName) => Map()
.set('name', segmentName)
.set('courses', courses)
).toList();
}
/*
TODO: remove this once the server is sorted out
createSegmentFromCertificationMap()
temporary function to create segments from certificationMap
Server gives us:
{
certificationMap: {
INTRODUCTION : [
{
itemCode: "ABC123"
}
],
ANOTHER: []
}
}
but we want (and have to provide back to the server to update):
{
certifications: [
{
name: "INTRODUCTION",
courses: [
{
itemCode: "ABC123"
}
]
},
{
name: "ANOTHER",
courses: []
}
]
}
*/
const createSegmentFromCertificationMap = function(map) {
if(!map) {
return List();
}
return map.map((cert, segmentName) => {
if(!cert) {
return Map()
.set('name',segmentName)
.set('courses',fromJS([]));
}
return Map()
.set('name',segmentName)
.set('courses',fromJS([{
itemName: cert.get('itemName'),
itemCode: cert.get('itemCode')
}]));
}).toList();
}
/*
</dodgyhacks>
*/
//
// Learning plan reducer
//
export default function LearningPlanReducer(state = initialState, action) {
var plan;
switch (action.type) {
//
// GET
//
case LEARNING_PLAN_GET_RECEIVE:
plan = action.payload;
if(plan == null) {
return state;
}
const { careerPlanId } = plan;
const planMap = temporarilyModifyLearningPlan(fromJS(plan));
return state.setIn(['collection', careerPlanId], planMap);
//
// LIST
//
case LEARNING_PLAN_LIST_RECEIVE:
const plans = action.payload;
if(plans == null) {
return state;
}
// sort results by display name - occasionally these don't exist so substitute in some Zs to make these appear after z
plans.sort((a, b) => {
var aa = a.displayName || "zzz";
var bb = b.displayName || "zzz";
return aa.localeCompare(bb);
});
const orderedMapParams = fromJS(plans).map(ii => [ii.get('careerPlanId'), temporarilyModifyLearningPlan(ii)]);
return state
.set('listReceived', true)
.set('collection', OrderedMap(orderedMapParams));
//
// DELETE
//
case LEARNING_PLAN_DELETE_RECEIVE:
plan = action.payload;
if(plan == null) {
return state;
}
return state.deleteIn(['collection', plan.careerPlanId]);
default:
return state;
}
}