Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
9.1 kB
1
Indexable
import axios from "axios";
import {
       HIDE_SUCCESSFULLY_SETUP_SCREEN,
       HIDE_LOCKEDIN_SETUP_SCREEN,
       PASSWORD_MATCH_MODULE_STATE_FAIL,
       PASSWORD_MATCH_MODULE_STATE,
       PASSWORD_MATCH_FAILED,
       CHECK_LOCKEDIN_END,
       CHECK_LOCKEDIN_START,
       CHECK_LOCKEDIN_MODULE,
       HIDE_INITIAL_SCREEN,
       LOCK_MODULES_SCREEN,
       CREATE_LOCKED_FAIL,
       SHOW_INITIAL_SCREEN,
       CHECK_LOCKEDIN_CREATED_FAIL,
       EMOTIONAL_WILL_MODULE_STATE,
       ASSET_VAULT_MODULE_STATE,
       GET_MODULE_STATE_FAIL,
       EMOTIONAL_WILL_MODULE_STATE_CHANGE,
       ASSET_VAULT_MODULE_STATE_CHANGE,
       MODULE_STATE_CHANGE_FAIL,
       HIDE_LOCK_MODULES_SCREEN,
       SUCCESSFULLY_SETUP_SCREEN,
       HIDE_LOCKEDIN_SCREEN
} from "../constant/lockedInConstant"
import {
     createLockedInUserUrl,
     checkLockedInMatchUrl,
     checkLockedInCreatedUrl,
     checkLockForModuleUrl,
     updateLockedInModuleStateUrl,
     updateLockedInUrl,
      } from "../api/platform_api"
import { getDecryptedKeyValuePair } from "../../utilities/security";


// created locked in for user to enable and disable the emotional will and asset vault to access
  export const createLockedInUser = (lockedIn) => async (dispatch) => {
     try {
        const token = getDecryptedKeyValuePair("token");
        const response = await axios.post(`${createLockedInUserUrl}`,
        {lockedIn},
          {
            headers: {
              "Content-Type": "application/json",
               Authorization : `${token}`,

            },
          }
          )
          // if password validation correct then a pop up screen for success set up
       if(response.data.success === true) {
        dispatch({
          type:HIDE_LOCKEDIN_SETUP_SCREEN
        })
        
      
       }
       
     } catch (error) {
      dispatch({
        type : HIDE_SUCCESSFULLY_SETUP_SCREEN
      })
      dispatch({
        type : CREATE_LOCKED_FAIL,
        payload : error.response && error.response.data.message
      })
     }
  }
  // this fucntion is for whether user set up locked in feature or not, if he is already done then shoe enable and disable screen or show setup screen
  export const checkLockedInCreated = () => async (dispatch) => {
    try {
     dispatch({
       type : CHECK_LOCKEDIN_MODULE
     })
     const token = getDecryptedKeyValuePair("token")
     const response = await axios.get(
     `${checkLockedInCreatedUrl}`,
     {
       headers: {
         "Content-Type": "application/json",
         Authorization : `${token}`,
         
       },
     }
     )
     if (response.data.success === true && response.data.data.isPresent) {
       dispatch ( {
         type : HIDE_INITIAL_SCREEN
       })
       dispatch ( {
         type : HIDE_LOCKEDIN_SETUP_SCREEN
       })
       dispatch ( {
         type : LOCK_MODULES_SCREEN
       })
         
     }
     if (response.data.success=== true && !response.data.data.isPresent) {
     
        dispatch ( {
          type : SHOW_INITIAL_SCREEN
         })
         dispatch({
          type : HIDE_LOCK_MODULES_SCREEN
         })
       
     }

    } catch (error) {
      
      dispatch({
        type: CHECK_LOCKEDIN_CREATED_FAIL,
        payload: error.response && error.response.data.message,
      });
    
   }
 }


  //  when user disable the emotionl or asset valut toggle , he has to give password 
  // so this function is for checking whether password matching or not, if yes then disable if not then he has 3 attempts if fail then timer on
  export const checkLockedInMatch = (lockedIn) => async (dispatch) => {
    try {
      dispatch({
        type: CHECK_LOCKEDIN_START
      })
      const token = getDecryptedKeyValuePair("token")
   
       const response = await axios.post(`${checkLockedInMatchUrl}`,
       {lockedIn},
       {
        headers: {
          "Content-Type": "application/json",
          Authorization : `${token}`,

        },
      }
      )
      if (response.data.success === true) {
          if(response.data.data.attemptsRemain) {
            // password wrong and left attempts pop up
             dispatch({
              type : PASSWORD_MATCH_MODULE_STATE_FAIL,
              payload : response.data.data
            })
            dispatch({
              type:CHECK_LOCKEDIN_END
            })
          } else {
            // password right and can disable the toggle
            dispatch({
              type : PASSWORD_MATCH_MODULE_STATE,
              payload : response.data.data
            
            })
            dispatch({
              type:CHECK_LOCKEDIN_END
            })
          }
      }
    // if no more attempts left
       if (response.data.success === false) {
        if (response.data.data) {
          dispatch({
            type : PASSWORD_MATCH_MODULE_STATE_FAIL,
            payload : response.data.data
          })
          dispatch({
            type:CHECK_LOCKEDIN_END
          })
        }
      }
    } catch (error) {
      dispatch({
        type : CHECK_LOCKEDIN_END
      })
      dispatch({
        type: PASSWORD_MATCH_FAILED,
        payload: error.response && error.response.data.message,
      });
    }
  }
  // this function is to update the emotional will, asset vault toggle according to user actions
export const updateLockedInModuleState =  (moduleType,state) => async (dispatch) => {
  const token = getDecryptedKeyValuePair("token")
   
  try {
    const response = await axios.patch(`${updateLockedInModuleStateUrl}`,
    {
      moduleType,
      state
    },
    {
      headers: {
        "Content-Type": "application/json",
        Authorization : `${token}`,
      },
    }
    )
     if (response.data.success === true) {
        if (moduleType === "EW") {
           dispatch(
            {
              type : EMOTIONAL_WILL_MODULE_STATE_CHANGE,
              payload : response.data.data.isEnabled
            }
           )
        }
        if (moduleType === "AV") {
          dispatch({
            type : ASSET_VAULT_MODULE_STATE_CHANGE,
            payload : response.data.data.isEnabled
          }
          )
        }
     } 
    
  } catch (error) {
    dispatch({
      type : MODULE_STATE_CHANGE_FAIL,
      payload : error.response && error.response.data.message
    })
  }
}

// this function is for checking the current state of emotional,asset vault toggle 
export const checkLockForModule = (moduleType) => async (dispatch) => {
  try {
      const token = getDecryptedKeyValuePair("token")
      const response = await axios.post(`${checkLockForModuleUrl}`,
      {moduleType},
      {
        headers: {
          "Content-Type": "application/json",
          Authorization :`${token}`
        },
      }
      )
      if(response.data.success === true) {
        if (moduleType === "EW") {
          dispatch(
           {
             type : EMOTIONAL_WILL_MODULE_STATE,
             payload : response.data.data.isEnabled
           }
          )
       }
       if (moduleType === "AV") {
         dispatch({
           type : ASSET_VAULT_MODULE_STATE,
           payload : response.data.data.isEnabled
         }
         )
       }
      }

  } catch (error) {
     dispatch({
      type:GET_MODULE_STATE_FAIL,
      payload : error.response && error.response.data.message
     })
  }
}

export const updateLockedIn = (lockedIn, newLockedIn) => async (dispatch) => {
  try {
    const token = getDecryptedKeyValuePair("token");
    const response = await axios.patch(`${updateLockedInUrl}`,
      {
        lockedIn: lockedIn,
        newLockedIn: newLockedIn
      },
      {
        headers: {
          "Authorization": `${token}`,
          "Content-Type": "application/json"
        }
      }
    );
    if (response.data.success === true) {
      if(Object.hasOwn(response.data, 'data')){  // Incorrect password entered
        dispatch({
          type: PASSWORD_MATCH_MODULE_STATE_FAIL,
          payload: { 
            isMatch: response.data.data.isMatch, 
            attemptsRemain: response.data.data.attemptsRemain 
          }
        });
      }
      else{                                     // Correct password entered
        dispatch({
          type: PASSWORD_MATCH_MODULE_STATE,
          payload: { 
            isMatch: true,
          }
        });
        dispatch({
          type: HIDE_LOCKEDIN_SCREEN,
        });
        dispatch({
          type: SUCCESSFULLY_SETUP_SCREEN,
        });
      }
    }

  } catch (error) {
    // dispatch({
    //      type : LOCK_MODULES_SCREEN,
    //    })
    // dispatch({
    //   type: HIDE_LOCKEDIN_SCREEN,
    // });
    dispatch({
      type:GET_MODULE_STATE_FAIL,
      payload : error.response && error.response.data.message
     })
  }
};
Leave a Comment