Advertisement
yuvarajupadhyaya

Learn Actions example ProfileAction

Oct 24th, 2022
860
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 17.84 KB | Source Code | 0 0
  1. import axios from "axios";
  2. import { BaseApiUrl } from "../constants/apiRoutes";
  3. import {
  4.   PROFILE_LOAD,
  5.   GET_STATE_LOAD,
  6.   GET_GRADE_LOAD,
  7.   GET_DISTRICT_LOAD,
  8.   GET_VDC_MUNICIPALITY_LOAD,
  9.   PROFILE_LOAD_SUBJECT,
  10.   LOAD_GRADE_AND_SUBJECT,
  11.   SET_MESSAGE,
  12.   PROFILE_UPDATED_RESPONSE,
  13.   TRANSACTION_FAILED,
  14.   LOAD_APPROVED_SCHOOL,
  15.   MAKE_PROFILE_PROS_EMPTY,
  16.   USER_GOALS_RETRIEVED,
  17.   USER_GOALS_SAVED,
  18.   AUTHOR_PROFILE_LOAD,
  19.   AUTHOR_PROFILE_COURSE_LOAD,
  20.   UPDATE_FOLLOW_STATE
  21. } from "../reducers/types";
  22. import { isEmpty } from "../helpers/validator";
  23. import { setMessageWithDispatch } from "./commonAction";
  24.  
  25. export const GetCurrentUser = () => dispatch => {
  26.   axios.get(BaseApiUrl + "Users/GetOverallProfileDetail").then(
  27.     resp => {
  28.       if (resp.data.Success === true) {
  29.         dispatch({
  30.           type: PROFILE_LOAD,
  31.           payload: {
  32.             profileData: resp.data.Result.UserInfo,
  33.             CourseProgress: resp.data.Result.ProgressInfo,
  34.             EarnedBadges: resp.data.Result.BadgeSection,
  35.             Certificates: resp.data.Result.Certificate,
  36.             QuizScore: resp.data.Result.QuizScore,
  37.             TotalBadgeCount: resp.data.Result.TotalBadgeCount
  38.           }
  39.         });
  40.       } else {
  41.         dispatch({
  42.           type: SET_MESSAGE,
  43.           payload: isEmpty(resp.data.Message)
  44.             ? "Failed to load Learner."
  45.             : resp.data.Message
  46.         });
  47.       }
  48.     },
  49.     err => {
  50.       dispatch({
  51.         type: SET_MESSAGE,
  52.         payload: "Failed to connect to the server.."
  53.       });
  54.     }
  55.   );
  56. };
  57.  
  58. // export const GetCurrentInstructor = () => dispatch => {
  59. //   axios.get(BaseApiUrl + "Users/GetCurrentRegisteredUser").then(
  60. //     resp => {
  61. //       if (resp.data.Success === true) {
  62. //         dispatch({
  63. //           type: PROFILE_LOAD,
  64. //           payload: resp.data
  65. //         });
  66. //       } else {
  67. //         dispatch({
  68. //           type: SET_MESSAGE,
  69. //           payload: isEmpty(resp.data.Message)
  70. //             ? "Failed to load Learner."
  71. //             : resp.data.Message
  72. //         });
  73. //       }
  74. //     },
  75. //     err => {
  76. //       dispatch({
  77. //         type: SET_MESSAGE,
  78. //         payload: "Failed to connect to the server.."
  79. //       });
  80. //     }
  81. //   );
  82. // };
  83. export const GetGradesWithSubjectUpdatedInstructor = TeachingSubjects => dispatch => {
  84.   axios
  85.     .get(
  86.       BaseApiUrl +
  87.         "Users/GetGradesWithSubjectUpdatedInstructor/?" +
  88.         "&TeachingSubjects=" +
  89.         TeachingSubjects
  90.     )
  91.     .then(
  92.       resp => {
  93.         if (resp.data.Success === true) {
  94.           dispatch({
  95.             type: LOAD_GRADE_AND_SUBJECT,
  96.             payload: resp.data
  97.           });
  98.         } else {
  99.           dispatch({
  100.             type: SET_MESSAGE,
  101.             payload: "Failed to load Grade with subjects."
  102.           });
  103.         }
  104.       },
  105.       err => {
  106.         dispatch({
  107.           type: SET_MESSAGE,
  108.           payload: "Failed to connect to the server.."
  109.         });
  110.       }
  111.     );
  112. };
  113.  
  114. export const GetGrades = () => dispatch => {
  115.   axios.get(BaseApiUrl + "Mobile/GetGrades").then(resp => {
  116.     dispatch(
  117.       {
  118.         type: GET_GRADE_LOAD,
  119.         payload: resp.data.Result
  120.       },
  121.       err => {
  122.         console.log("Failed to load grade ", err);
  123.       }
  124.     );
  125.   });
  126. };
  127.  
  128. export const GetApprovedSchoolList = searchText => dispatch => {
  129.   axios
  130.     .get(BaseApiUrl + "Institute/GetApprovedSchoolList/" + searchText)
  131.     .then(resp => {
  132.       dispatch(
  133.         {
  134.           type: LOAD_APPROVED_SCHOOL,
  135.           payload: resp.data.Result
  136.         },
  137.         err => {
  138.           console.log("Failed to load school ", err);
  139.         }
  140.       );
  141.     });
  142. };
  143.  
  144. export const GetStates = () => dispatch => {
  145.   axios.get(BaseApiUrl + "Distributors/GetStates").then(resp => {
  146.     dispatch(
  147.       {
  148.         type: GET_STATE_LOAD,
  149.         payload: resp.data.Result
  150.       },
  151.       err => {
  152.         console.log("Failed to load state ", err);
  153.       }
  154.     );
  155.   });
  156. };
  157.  
  158. export const GetDistrict = stateId => dispatch => {
  159.   axios.get(BaseApiUrl + "Distributors/GetDistrict/" + stateId).then(resp => {
  160.     dispatch(
  161.       {
  162.         type: GET_DISTRICT_LOAD,
  163.         payload: resp.data.Result
  164.       },
  165.       err => {
  166.         console.log("Failed to load district ", err);
  167.       }
  168.     );
  169.   });
  170. };
  171.  
  172. export const GetVdcMunicipality = districtId => dispatch => {
  173.   axios
  174.     .get(BaseApiUrl + "Distributors/GetVdcMunicipality/" + districtId)
  175.     .then(resp => {
  176.       dispatch(
  177.         {
  178.           type: GET_VDC_MUNICIPALITY_LOAD,
  179.           payload: resp.data.Result
  180.         },
  181.         err => {
  182.           console.log("Failed to load Vdc/Municipality ", err);
  183.         }
  184.       );
  185.     });
  186. };
  187.  
  188. export const GetSubject = gradeId => dispatch => {
  189.   axios
  190.     .get(BaseApiUrl + "Subjects/GetSubjectsByGradeId/?" + "&gradeId=" + gradeId)
  191.     .then(resp => {
  192.       dispatch(
  193.         {
  194.           type: PROFILE_LOAD_SUBJECT,
  195.           payload: resp.data.Result
  196.         },
  197.         err => {
  198.           console.log("Neema learning material data load error : ", err);
  199.         }
  200.       );
  201.     });
  202. };
  203.  
  204. export const ChangeEmailReactWeb = (email, password, callback) => dispatch => {
  205.   axios({
  206.     url:
  207.       BaseApiUrl +
  208.       "Users/ChangeUserEmail?newEmail=" +
  209.       email +
  210.       "&password=" +
  211.       password +
  212.       "&forMobile=false",
  213.     method: "POST",
  214.     headers: {
  215.       "Content-Type": "application/json; charset=utf-8"
  216.     }
  217.   }).then(
  218.     resp => {
  219.       if (resp.data.Success) {
  220.         dispatch({
  221.           type: SET_MESSAGE,
  222.           payload: resp.data.Message
  223.         });
  224.         // dispatch({
  225.         //   type: TRANSACTION_FAILED,
  226.         //   payload: true
  227.         // });
  228.         callback && callback(resp.data);
  229.       } else {
  230.         callback && callback(resp.data);
  231.         // dispatch({
  232.         //   type: SET_MESSAGE,
  233.         //   payload: resp.data.Message
  234.         // });
  235.         // dispatch({
  236.         //   type: TRANSACTION_FAILED,
  237.         //   payload: false
  238.         // });
  239.       }
  240.     },
  241.     err => {
  242.       dispatch({
  243.         type: SET_MESSAGE,
  244.         payload: "Failed to change Email."
  245.       });
  246.       // dispatch({
  247.       //   type: TRANSACTION_FAILED,
  248.       //   payload: false
  249.       // });
  250.     }
  251.   );
  252. };
  253.  
  254. export const ChangePhoneNumber = (phoneNo, password, callback) => dispatch => {
  255.   axios({
  256.     url:
  257.       BaseApiUrl +
  258.       "Users/ChangeUserPhoneNumber?newNumber=" +
  259.       phoneNo +
  260.       "&password=" +
  261.       password,
  262.     method: "POST",
  263.     headers: {
  264.       "Content-Type": "application/json; charset=utf-8"
  265.     }
  266.   }).then(
  267.     resp => {
  268.       if (resp.data.Success) {
  269.         dispatch({
  270.           type: SET_MESSAGE,
  271.           payload: resp.data.Message
  272.         });
  273.         // dispatch({
  274.         //   type: TRANSACTION_FAILED,
  275.         //   payload: true
  276.         // });
  277.         callback && callback(resp.data);
  278.       } else {
  279.         callback && callback(resp.data);
  280.         // dispatch({
  281.         //   type: SET_MESSAGE,
  282.         //   payload: resp.data.Message
  283.         // });
  284.         // dispatch({
  285.         //   type: TRANSACTION_FAILED,
  286.         //   payload: false
  287.         // });
  288.       }
  289.     },
  290.     err => {
  291.       dispatch({
  292.         type: SET_MESSAGE,
  293.         payload: "Failed to change Email."
  294.       });
  295.       // dispatch({
  296.       //   type: TRANSACTION_FAILED,
  297.       //   payload: false
  298.       // });
  299.     }
  300.   );
  301. };
  302.  
  303. export const UpdateUserDetails = (postData, callback) => dispatch => {
  304.   axios({
  305.     url: BaseApiUrl + "Users/UpdateUserDetails",
  306.     method: "POST",
  307.     data: postData,
  308.     headers: {
  309.       "Content-Type": "application/x-www-form-urlencoded"
  310.     }
  311.   }).then(
  312.     resp => {
  313.       if (resp.data.Success === true) {
  314.         dispatch({
  315.           type: SET_MESSAGE,
  316.           payload: "Profile updated sucessfully."
  317.         });
  318.         dispatch({
  319.           type: PROFILE_UPDATED_RESPONSE,
  320.           payload: resp.data.Result
  321.         });
  322.         if (
  323.           localStorage.IsProfileComplete === "False" &&
  324.           resp.data.Result.IsProfileCompleted
  325.         ) {
  326.           localStorage.IsProfileComplete === "True";
  327.         }
  328.         callback && callback();
  329.       } else {
  330.         dispatch({
  331.           type: SET_MESSAGE,
  332.           payload:
  333.             resp.data && !isEmpty(resp.data.Message)
  334.               ? resp.data.Message
  335.               : "Failed to update profile."
  336.         });
  337.       }
  338.     },
  339.     err => {
  340.       dispatch({
  341.         type: SET_MESSAGE,
  342.         payload: "Failed to connect to the server.."
  343.       });
  344.     }
  345.   );
  346. };
  347.  
  348. // export const UpdateInstructorDetails = postData => dispatch => {
  349. //   axios({
  350. //     url: BaseApiUrl + "Users/UpdateInstructorDetails",
  351. //     method: "POST",
  352. //     data: postData,
  353. //     headers: {
  354. //       "Content-Type": "application/x-www-form-urlencoded"
  355. //     }
  356. //   }).then(
  357. //     resp => {
  358. //       if (resp.data.Success === true) {
  359. //         dispatch({
  360. //           type: SET_MESSAGE,
  361. //           payload: "Profile updated sucessfully."
  362. //         });
  363. //         dispatch({
  364. //           type: PROFILE_UPDATED_RESPONSE,
  365. //           payload: resp.data.Result
  366. //         });
  367. //          if((localStorage.IsProfileComplete==="False")&&(resp.data.Result.IsProfileCompleted)){
  368. //           localStorage.IsProfileComplete==="True";
  369. //          }
  370.  
  371. //       } else {
  372. //         dispatch({
  373. //           type: SET_MESSAGE,
  374. //           payload:
  375. //             resp.data && !isEmpty(resp.data.Message)
  376. //               ? resp.data.Message
  377. //               : "Failed to update profile."
  378. //         });
  379. //       }
  380. //     },
  381. //     err => {
  382. //       dispatch({
  383. //         type: SET_MESSAGE,
  384. //         payload: "Failed to connect to the server.."
  385. //       });
  386. //     }
  387. //   );
  388. // };
  389.  
  390. export const MakeProfilePropsEmpty = contents => dispatch => {
  391.   dispatch({
  392.     type: MAKE_PROFILE_PROS_EMPTY,
  393.     payload: contents
  394.   });
  395. };
  396.  
  397. export const verifyPhoneNumber = (code, callback) => dispatch => {
  398.   axios({
  399.     url: BaseApiUrl + "Users/VerifyUserPhoneNumber?code=" + code,
  400.     method: "POST",
  401.     headers: {
  402.       "Content-Type": "application/json; charset=utf-8"
  403.     }
  404.   }).then(
  405.     resp => {
  406.       if (resp.data.Success) {
  407.         dispatch({
  408.           type: SET_MESSAGE,
  409.           payload: resp.data.Message
  410.         });
  411.  
  412.         callback && callback(resp.data.Result.PhoneNo);
  413.       } else {
  414.         dispatch({
  415.           type: SET_MESSAGE,
  416.           payload: resp.data.Message
  417.         });
  418.         //callback&&callback(resp.data)
  419.       }
  420.     },
  421.     err => {
  422.       dispatch({
  423.         type: SET_MESSAGE,
  424.         payload: "Failed to verify phone."
  425.       });
  426.     }
  427.   );
  428. };
  429.  
  430. export const getUserGoals = () => dispatch => {
  431.   axios.get(BaseApiUrl + "Users/GetUserGoalInfo").then(
  432.     resp => {
  433.       if (resp.data.Success === true) {
  434.         dispatch({
  435.           type: USER_GOALS_RETRIEVED,
  436.           payload: {
  437.             UserGoals: resp.data.Result.UserGoals,
  438.             StudyPeriod: resp.data.Result.StudyPeriod,
  439.             StudyUtilization: resp.data.Result.StudyUtilization,
  440.             StudyAchievement: resp.data.Result.StudyAchievement
  441.           }
  442.         });
  443.       } else {
  444.         dispatch({
  445.           type: SET_MESSAGE,
  446.           payload: isEmpty(resp.data.Message)
  447.             ? "Failed to load goals setting."
  448.             : resp.data.Message
  449.         });
  450.       }
  451.     },
  452.     err => {
  453.       dispatch({
  454.         type: SET_MESSAGE,
  455.         payload: "Failed to connect to the server.."
  456.       });
  457.     }
  458.   );
  459. };
  460.  
  461. export const saveGoals = (postData, callback) => dispatch => {
  462.   axios.post(BaseApiUrl + "Users/SaveUserGoals", postData).then(
  463.     resp => {
  464.       if (resp.data.Success === true) {
  465.         dispatch({
  466.           type: SET_MESSAGE,
  467.           payload: "Goals saved"
  468.         });
  469.         dispatch({
  470.           type: USER_GOALS_SAVED,
  471.           payload: postData
  472.         });
  473.  
  474.         callback && callback();
  475.       } else {
  476.         dispatch({
  477.           type: SET_MESSAGE,
  478.           payload:
  479.             resp.data && !isEmpty(resp.data.Message)
  480.               ? resp.data.Message
  481.               : "Failed to save goals."
  482.         });
  483.       }
  484.     },
  485.     err => {
  486.       dispatch({
  487.         type: SET_MESSAGE,
  488.         payload: "Failed to connect to the server.."
  489.       });
  490.     }
  491.   );
  492. };
  493.  
  494. export const GetAuthorProfile = (_userid, callback) => dispatch => {
  495.   axios
  496.     .get(
  497.       BaseApiUrl +
  498.         "Users/GetOverallAuthorProfileDetail/?" +
  499.         "&userid=" +
  500.         _userid
  501.     )
  502.     .then(
  503.       resp => {
  504.         if (resp.data.Success === true) {
  505.           dispatch({
  506.             type: AUTHOR_PROFILE_LOAD,
  507.             payload: {
  508.               authorprofileData: resp.data.Result[0]
  509.             }
  510.           });
  511.           callback && callback();
  512.         } else {
  513.           dispatch({
  514.             type: SET_MESSAGE,
  515.             payload: isEmpty(resp.data.Message)
  516.               ? "Failed to load profile."
  517.               : resp.data.Message
  518.           });
  519.         }
  520.       },
  521.       err => {
  522.         dispatch({
  523.           type: SET_MESSAGE,
  524.           payload: "Failed to connect to the server.."
  525.         });
  526.       }
  527.     );
  528. };
  529.  
  530. export const retrieveAuthorCourseList = (
  531.   pageNumber = 1,
  532.   pagesize = 5,
  533.   userid,
  534.   callBack
  535. ) => dispatch => {
  536.   axios
  537.     .get(BaseApiUrl + "Users/GetAuthorProfileCourses", {
  538.       params: {
  539.         CurrentPage: pageNumber,
  540.         Take: pagesize,
  541.         userID: userid
  542.       }
  543.     })
  544.     .then(
  545.       resp => {
  546.         if (resp.data && resp.data.Success === true) {
  547.           dispatch({
  548.             type: AUTHOR_PROFILE_COURSE_LOAD,
  549.             // payload: isEmpty(resp.data.Result.CourseMessages)
  550.             //   ? []
  551.             //   : resp.data.Result.CourseMessages
  552.             payload: {
  553.               Result: isEmpty(resp.data.Result) ? [] : resp.data.Result,
  554.               CurrentPage: pageNumber
  555.             }
  556.           });
  557.           callBack && callBack(resp.data.Result);
  558.         } else {
  559.           dispatch({
  560.             type: SET_MESSAGE,
  561.             payload:
  562.               resp.data && !isEmpty(resp.data.Message)
  563.                 ? resp.data.Message
  564.                 : "Failed to retrieve course list."
  565.           });
  566.         }
  567.       },
  568.       err => {
  569.         dispatch({
  570.           type: SET_MESSAGE,
  571.           payload: "Failed to retrieve feed list."
  572.         });
  573.       }
  574.     );
  575. };
  576.  
  577. export const getAuthorProfileGrade = callback => dispatch => {
  578.   axios.get(BaseApiUrl + "Users/GetAuthorProfileGrade").then(
  579.     resp => {
  580.       if (resp.data.Success === true) {
  581.         callback && callback(resp.data.Result);
  582.       } else {
  583.         callback && callback([]);
  584.       }
  585.     },
  586.     err => {
  587.       dispatch({
  588.         type: SET_MESSAGE,
  589.         payload: "Failed to connect to the server.."
  590.       });
  591.     }
  592.   );
  593. };
  594. export const getAuthorProfileCourse = (userid, callback) => dispatch => {
  595.   axios
  596.     .get(BaseApiUrl + "Users/GetAuthorProfileCourse", {
  597.       params: {
  598.         userID: userid
  599.       }
  600.     })
  601.     .then(
  602.       resp => {
  603.         if (resp.data.Success === true) {
  604.           callback && callback(resp.data.Result);
  605.         } else {
  606.           callback && callback([]);
  607.         }
  608.       },
  609.       err => {
  610.         dispatch({
  611.           type: SET_MESSAGE,
  612.           payload: "Failed to connect to the server.."
  613.         });
  614.       }
  615.     );
  616. };
  617.  
  618. export const updateFollowStatus = (
  619.   userID,
  620.   isfollow,
  621.   successCallBack,
  622.   failureCallBack
  623. ) => dispatch => {
  624.   var failureAction = message => {
  625.     failureCallBack && failureCallBack();
  626.     dispatch({
  627.       type: SET_MESSAGE,
  628.       payload: !isEmpty(message) ? message : "Failed to update follow user."
  629.     });
  630.   };
  631.  
  632.   axios({
  633.     url:
  634.       BaseApiUrl +
  635.       "Users/ToggleFollowUnfollow?userID=" +
  636.       userID +
  637.       "&&follow=" +
  638.       isfollow,
  639.     method: "POST",
  640.     headers: {
  641.       "Content-Type": "application/json; charset=utf-8"
  642.     }
  643.   }).then(
  644.     resp => {
  645.       if (resp.data.Success) {
  646.         dispatch({
  647.           type: UPDATE_FOLLOW_STATE,
  648.  
  649.           IsFollowed: isfollow
  650.         });
  651.         successCallBack && successCallBack();
  652.       } else {
  653.         failureAction(resp.data.Message);
  654.       }
  655.     },
  656.     err => {
  657.       failureAction();
  658.     }
  659.   );
  660. };
  661.  
  662. export const GetCourseCreatedGrades = callBack => dispatch => {
  663.   axios.get(BaseApiUrl + "Course/GetCourseCreatedGrades").then(resp => {
  664.     if (resp && resp.data && resp.data.Success) {
  665.       callBack && callBack(resp.data.Result);
  666.     }
  667.   });
  668. };
  669.  
  670. export const UpdateShowProfileIncompleteMsg = (
  671.   postData,
  672.   callback
  673. ) => dispatch => {
  674.   axios
  675.     .post(BaseApiUrl + "Users/UpdateShowProfileIncompleteMsg", postData)
  676.     .then(
  677.       resp => {
  678.         if (resp.data.Success === true) {
  679.           callback && callback({ error: false });
  680.         }
  681.       },
  682.       err => {
  683.         console.log("error", err);
  684.         callback && callback({ error: err });
  685.       }
  686.     );
  687. };
  688.  
  689. export const ChangeRole = (postData, callback) => dispatch => {
  690.   axios
  691.     .post(BaseApiUrl + "Users/ChangeRole", postData, {
  692.       headers: {
  693.         "Content-Type": "multipart/form-data"
  694.       }
  695.     })
  696.     .then(
  697.       resp => {
  698.         if (resp.data.Success === true) {
  699.           setMessageWithDispatch(resp.data.Message, dispatch);
  700.           callback && callback({ error: false });
  701.         }
  702.         else if(resp.data.Success === false)
  703.         {
  704.           setMessageWithDispatch(resp.data.Message, dispatch);
  705.           callback && callback({ error: true });
  706.         }
  707.       },
  708.       err => {
  709.         console.log("error", JSON.stringify(err.response));
  710.         setMessageWithDispatch(
  711.           isEmpty(err.response.data.Message)
  712.             ? "Error"
  713.             : err.response.data.Message,
  714.           dispatch
  715.         );
  716.         callback && callback({ error: err });
  717.       }
  718.     );
  719. };
  720.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement