users reducers

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.0 kB
1
Indexable
Never
import moment from 'moment';
import { Strings } from '../../constants/constants';
import dispatchTypes from '../constants';
let initialState = {
  user: null,
  selectedDate: moment().format('YYYY-MM-DD'),
  userTrackersData: null,
  dailyGoalData: null,
  isPremium: false,
  isLoading: false,
  userType: Strings.GUEST,
  getGymPartnerResponse: {},
};

const checkUserType = parsedUser => {
  if (parsedUser.guest_user) {
    return Strings.GUEST;
  }
  if (parsedUser.signup_flow && !parsedUser.is_purchased_plan) {
    return Strings.FREE;
  }
  // IF ENTERS THIS CONDITION THE USER BECOMES PAID

  if (parsedUser?.subscription?.length > 0
    && parsedUser.is_diet_assign) {
    return Strings.IS_DIET_ASSIGN;
  }
  return Strings.HOUR_POP_UP;
};

export default (state = initialState, action) => {
  switch (action.type) {
    case dispatchTypes.storeUserDetails:
      return { ...state, user: action.payload, userType: checkUserType(action.payload) };
    case dispatchTypes.planPurchased:
      return { ...state, isPremium: action.payload };
    case dispatchTypes.logoutUser:
      return { ...state, user: null, isPremium: false, userTrackersData: null };
    case dispatchTypes.storeTrackerData:
      return { ...state, userTrackersData: action.payload };
    case dispatchTypes.storeDailyGoalData:
      return { ...state, dailyGoalData: action.payload };
    case dispatchTypes.loadingStart:
      return { ...state, isLoading: true };
    case dispatchTypes.loadingEnd:
      return { ...state, isLoading: false };
    case dispatchTypes.planPurchased:
      return { ...state, isPremium: action.payload };
    case dispatchTypes.logoutUser:
      return { ...state, user: null, isPremium: false };
    case dispatchTypes.selectDate:
      return { ...state, selectedDate: action.payload };
    case dispatchTypes.setGymPartner:
      return { ...state, getGymPartnerResponse: action.payload };
    default:
      return state;
  }
};
Leave a Comment