Beispiel #1
0
// Reducer
export default function UserStateReducer(state = initialState, action = {}) {
  switch (action.type) {
    case USER_CLASS_CONFIRM:
      return state.set('classId', action.classId);
    case SELECT_SCHOOL:
      //console.log('SELECT_SCHOOL:',action.schoolId);
      return state.set('lastSchoolId', action.schoolId);
    case LOAD_SCHOOL_REQUEST:
      //console.log('LOAD_SCHOOL_REQUEST');
      return loop(
        state.set('loading',true),
        Effects.promise(requestLoadSchools)
      );
    case LOAD_SCHOOL_RESPONSE:
      //console.log('LOAD_SCHOOL_RESPONSE',action.payload);
      return state
        .set('loading', false)
        .set('schoolList',fromJS(action.payload.schoolList));
    case CLEAR_CLASS_LIST:
      //console.log('CLEAR_CLASS_LIST',action.classList);
      return state.set('classList', List())
                  .set('lastClassId',-1);
    case SELECT_CLASS:
      //console.log('SELECT_CLASS:', action.classId);
      return state.set('lastClassId', action.classId);
    case LOAD_SCHOOL_CLASS_REQUEST:
      return loop(
        state.set('loading', true),
        Effects.promise(requestLoadSchoolClass, action.schoolId)
      );
    case LOAD_SCHOOL_CLASS_RESPONSE:
      //console.log('class of the school:', fromJS(action.payload.classList));
      return state.set('loading',false)
        .set('classList', fromJS(action.payload.classList));
    case SAVE_USER_INFO:
      return state.set('userName',action.userName)
                  .set('userDob', moment(action.userDob).format('YYYY-MM-DD'))
                  .set('userLeftEye', action.userLeftEye)
                  .set('userRightEye', action.userRightEye)
                  .set('userCell', action.userCell)
                  .set('targetLength', action.targetLength)
                  .set('classId', action.classId)
                  .set('userWx', action.userWx);
    case INIT_USER_ID:
      return state.set('userId', DeviceInfo.getUniqueID());
    case INIT_USER_INFO:
      //console.log('type of moment:',typeof moment(), moment());
      //console.log('DeviceInfo:',DeviceInfo);
      return state.set('userId',DeviceInfo.getUniqueID())
                  .set('userDate',moment().format('YYYY-MM-DD'))
                  .set('userPhoto', 'noava')
                  .set('userName','摩粒用户')
                  .set('userDob', '2000-01-01')
                  .set('userLeftEye', '5.0')
                  .set('userRightEye', '5.0')
                  .set('userCell', '13800138999')
                  .set('targetLength',60)
                  .set('userWx', 'wx_1293993');
    case USER_CHANGE_PHOTO:
      return state.set('userPhoto',action.userUri);

    case LOAD_FAQ_LIST_REQUEST:
      //console.log('LOAD_POSTS_REQUEST');
      return loop(
        state.set('loading',true),
        Effects.promise(loadFaqsResponse)
      );
    case FAQ_SELECTED:
      //console.log('FAQ_SELECTED:',action);
      //var theFaq = state.get('faqList').find((value,index,array) => {
      //  return value.get('POST_ID') === action.faqId;
      //});
      return state.set('faqId', action.faqId);

    case LOAD_FAQ_LIST_RESPONSE:
      //console.log('load_faq_list_response:', action.payload);
      var lastFaqId = -1;
      action.payload.faqList.forEach((element) =>{
        if (element.POST_ID > lastFaqId) {
          lastFaqId = element.POST_ID;
        }
      }, this);
      //console.log('lastFaqId:',lastFaqId);
      if (lastFaqId < 0) //wrong data:不更新文章列表
        {return state.set('loading', false).set('lastFaqId',lastFaqId);}
      return state.set('loading', false).set('lastFaqId',lastFaqId).set('faqList',fromJS(action.payload.faqList));

    case LOAD_USER_REQUEST:
      return loop(
        state.set('loading', true),
        Effects.promise(requestLoadUser)
      );

    case LOAD_USER_RESPONSE:
      return state
        .set('loading', false)
        .set(...action.payload);

    default:
      return state;
  }
}
Beispiel #2
0
// Reducer
export default function ReportStateReducer(state = initialState, action = {}) {
  let theDay;
  let dayInfo;
  let curVal;
  let totalVal;
  let curRate;
  let totalRate;
  const today = new Date();

  let firstMonthDay = new Date(today.getFullYear(), today.getMonth(),1);
  let theData;
  let monthValues = new Array();
  let monthMeanValues = new Array();
  let j = 0;

  switch (action.type) {

    case LOAD_USER_DIFF_REQUEST:
      return loop(
        state.set('loading', true),
        Effects.promise(requestLoadUserDiffData, action.userId)
      );

    case LOAD_USER_DIFF_RESPONSE:
      //console.log('ReportStat:LOAD_USER_DIFF_RESPONSE:', action.payload);
      let theUserDiffData = eval('(' + action.payload.UserStatData + ')');
      //console.log('thedata:', theUserDiffData);
      let normalTotal = 0;
      let abTotal = 0;
      theUserDiffData.map((item) => {
        if (item.label === '正常') {
          normalTotal = item.value;
        }
        else {
          abTotal += item.value;
        }
        item.value /= 60;
      });
      let userPieData = {
        dataSets: [{
          values: theUserDiffData,
          label: ' ',
          config: {
            colors: [processColor(global.greenColor), processColor(global.yellowColor), processColor(global.redColor), processColor(global.blueColor), processColor(global.subColor)],
            valueTextSize: 12,
            valueTextColor: processColor(global.headerColor),
            sliceSpace: 5,
            selectionShift: 10
          }
        }]
      };

      return state.set('loading', false)
                  .set('pieData', userPieData)
                  .set('pieCaption', '合格率:' + parseInt(100 - abTotal * 100 / normalTotal) + '%');
    case LOAD_CLASS_CUR_MONTH_REQUEST:
      return loop(
        state.set('loading', true),
        Effects.promise(requestLoadClassCurMonthData, action.classId)
      );
    case LOAD_CLASS_CUR_MONTH_RESPONSE:
      //console.log('ReportStat:LOAD_CUR_MONTH_RESPONSE:', action.payload);
      theData = eval('(' + action.payload.ClassStatData + ')');
      totalVal = 0;
      for (j = 0;j < 32;j++) {
        theDay = new Date(firstMonthDay.setDate(firstMonthDay.getDate() + 1));
        if (theDay.getMonth() > today.getMonth()) {
          break;
        }
        dayInfo = theDay.getFullYear() + '-' +
                  ((theDay.getMonth() + 1) < 10 ? ('0' + (theDay.getMonth() + 1)) : (theDay.getMonth() + 1)) + '-' +
                  ((theDay.getDate() < 10) ? '0' + theDay.getDate() : theDay.getDate());
        curVal = 0;
        theData.map((item) => {
          if (item.studyDate === dayInfo) {
            curVal = item.totalSecs / 60; //convert to minutes
            //curRate = 100- parseInt(item.alertSecs* 100 / item.totalSecs);
            //console.log('bingo!', dayInfo)
          }
        });
        totalVal += curVal;
        monthValues.push({y: curVal});
      }
      //console.log('month days:',j);
      totalVal /= j;
      for (let jj = 0;jj < j;jj++) {
        monthMeanValues.push({y: totalVal});
      }
      //console.log('thedata:', monthValues, monthMeanValues);

      let theClassMonthData = {
        dataSets: [{
          values: monthValues,
          label: '班级平均学习时长统计',
          config: {
            lineWidth: 1.5,
            mode: 'HORIZONTAL_BEZIER',
            drawCircles: false,
            autoScaleMinMaxEnabled: true,
            drawCubicIntensity: 2.3,
            drawCubic: false,
            drawValues: true,
            drawHighlightIndicators: false,
            color: COLOR_ORANGE,
            drawFilled: true,
            fillColor: COLOR_ORANGE,
            fillAlpha: 60
          }
        }]
      };
      return state.set('loading', false)
                  .set('studyTimeDataMonthClass', theClassMonthData)
                  ;
    case LOAD_CUR_MONTH_REQUEST:
      return loop(
        state.set('loading', true),
        Effects.promise(requestLoadCurMonthData, action.userId)
      );
    case LOAD_CUR_MONTH_RESPONSE:
      //console.log('ReportStat:LOAD_CUR_MONTH_RESPONSE:', action.payload);

      theData = eval('(' + action.payload.StatData + ')');
      totalVal = 0;
      for (j = 0;j < 32;j++) {
        theDay = new Date(firstMonthDay.setDate(firstMonthDay.getDate() + 1));
        if (theDay.getMonth() > today.getMonth()) {
          break;
        }
        dayInfo = theDay.getFullYear() + '-' +
                  ((theDay.getMonth() + 1) < 10 ? ('0' + (theDay.getMonth() + 1)) : (theDay.getMonth() + 1)) + '-' +
                  ((theDay.getDate() < 10) ? '0' + theDay.getDate() : theDay.getDate());
        curVal = 0;
        theData.map((item) => {
          if (item.studyDate === dayInfo) {
            curVal = item.totalSecs / 60; //convert to minutes
            //curRate = 100- parseInt(item.alertSecs* 100 / item.totalSecs);
            //console.log('bingo!', dayInfo)
          }
        });
        totalVal += curVal;
        monthValues.push({y: curVal});
      }
      //console.log('month days:',j);
      totalVal /= j;
      for (let jj = 0;jj < j;jj++) {
        monthMeanValues.push({y: totalVal});
      }
      //console.log('thedata:', monthValues, monthMeanValues);

      var theMonthData = {
        dataSets: [{
          values: monthValues,
          label: '学习时长统计',
          config: {
            lineWidth: 1.5,
            mode: 'HORIZONTAL_BEZIER',
            drawCircles: false,
            autoScaleMinMaxEnabled: true,
            drawCubicIntensity: 2.3,
            drawCubic: false,
            drawValues: true,
            drawHighlightIndicators: false,
            color: COLOR_ORANGE,
            drawFilled: true,
            fillColor: COLOR_ORANGE,
            fillAlpha: 60
          }
        },{
          values: monthMeanValues,
          label: '平均学习时长',
          config: {
            lineWidth: 1.5,
            drawCircles: false,
            drawHighlightIndicators: false,
            drawValues: true,
            autoScaleMinMaxEnabled: true,
            color: COLOR_GREEN,
            drawFilled: true,
            fillColor: COLOR_GREEN,
            fillAlpha: 40
          }
        }]
      };
      return state.set('loading', false)
                  .set('studyTimeDataMonth', theMonthData)
                  ;

    case LOAD_LAST_REPORT_REQUEST:
      return loop(
        state.set('loading', true),
        Effects.promise(requestLoadLastReport, action.userId)
      );

    case LOAD_LAST_REPORT_RESPONSE:
      //console.log('ReportStat:LOAD_LAST_REPORT_RESPONSE:', action.payload)
      //GetLast Week day info

      let lastFirst = new Date(today.setDate(today.getDate() - today.getDay() - 7));
      let weekValues = new Array();
      let weekRates = new Array();
      let weekDatas = eval('(' + action.payload.WeekStatData + ')');
      //console.log('week Data:', weekDatas);
      totalVal = 0;
      totalRate = 0;
      weekRates.push({y: 0});
      for (var i = 1;i < 8;i++) {
        theDay = new Date(lastFirst.setDate(lastFirst.getDate() + 1));
        dayInfo = theDay.getFullYear() + '-' +
            ((theDay.getMonth() + 1) < 10 ? ('0' + (theDay.getMonth() + 1)) : (theDay.getMonth() + 1)) + '-' +
            ((theDay.getDate() < 10) ? '0' + theDay.getDate() : theDay.getDate());
        //console.log('week Days:',dayInfo);
        curVal = 0;
        curRate = 0;
        weekDatas.map((item) => {
          if (item.studyDate === dayInfo) {
            curVal = item.totalSecs / 60;
            curRate = 100 - parseInt(item.alertSecs * 100 / item.totalSecs);
            //console.log('bingo!', dayInfo)
          }
          //console.log('item:',item.studyDate, item.totalSecs,item.alertSecs)
        });
        totalVal += curVal;
        totalRate += curRate;
        weekValues.push({y: curVal});
        weekRates.push({y: curRate});
      }
      //console.log('week data:',weekValues,totalVal, totalRate);
      totalVal /= 7;
      totalRate /= 7;

      var theWeekRateData = {
        barData: {
          dataSets: [{
            values: weekRates,
            label: '坐姿达标率',
            config: {
              color: COLOR_ORANGE, //processColor('#ff7122'),//'teal'),
              barSpacePercent: 40,
              barShadowColor: processColor('lightgrey'),
              highlightAlpha: 90,
              highlightColor: processColor('red')
            }
          }]
        },
        lineData: {
          dataSets: [{
            values: [totalRate, totalRate, totalRate, totalRate, totalRate, totalRate, totalRate, totalRate, totalRate],
            label: '平均达标率',
            config: {
              drawValues: false,
              colors: [COLOR_GREEN],
              mode: 'CUBIC_BEZIER',
              autoScaleMinMaxEnabled: true,
              drawCircles: false,
              lineWidth: 2,
              drawFilled: true,
              fillColor: COLOR_GREEN,
              fillAlpha: 60
            }
          }]
        }
      };

      var theWeekData = {
        dataSets: [{
          values: weekValues,
          label: '学习时长统计',
          config: {
            lineWidth: 1.5,
            mode: 'HORIZONTAL_BEZIER',
            drawCircles: false,
            drawCubicIntensity: 2.3,
            drawCubic: true,
            drawHighlightIndicators: false,
            color: COLOR_ORANGE,
            drawFilled: true,
            fillColor: COLOR_ORANGE,
            fillAlpha: 60
          }
        },{
          values: [{y: totalVal},{y: totalVal},{y: totalVal},{y: totalVal},{y: totalVal},{y: totalVal},{y: totalVal}],
          label: '平均学习时长',
          config: {
            lineWidth: 1.5,
            drawCircles: true,
            drawHighlightIndicators: false,
            color: COLOR_GREEN,
            drawFilled: true,
            fillColor: COLOR_GREEN,
            fillAlpha: 60
          }
        }]
      };
      return state
        .set('loading', false)
        .set('studyTimeData', theWeekData)
        .set('posData',theWeekRateData);
    default:
      return state;
  }
}
 it('should trigger a requestRandomNumber side effect', () => {
   expect(effects).toEqual(
     Effects.promise(CounterStateActions.requestRandomNumber)
   );
 });