From 35573417c411d95a63a67588883a6d0319c95b5d Mon Sep 17 00:00:00 2001 From: jfrye122 Date: Thu, 13 Apr 2023 13:33:12 -0400 Subject: [PATCH] added employee folder redux --- redux/employee/employee.actions.js | 29 +++++++++++++++++++ redux/employee/employee.reducer.js | 36 ++++++++++++++++++++++++ redux/employee/employee.sagas.js | 42 ++++++++++++++++++++++++++++ redux/employee/employee.selectors.js | 12 ++++++++ redux/employee/employee.types.js | 17 +++++++++++ 5 files changed, 136 insertions(+) create mode 100644 redux/employee/employee.actions.js create mode 100644 redux/employee/employee.reducer.js create mode 100644 redux/employee/employee.sagas.js create mode 100644 redux/employee/employee.selectors.js create mode 100644 redux/employee/employee.types.js diff --git a/redux/employee/employee.actions.js b/redux/employee/employee.actions.js new file mode 100644 index 0000000..c140d23 --- /dev/null +++ b/redux/employee/employee.actions.js @@ -0,0 +1,29 @@ +import EmployeeActionTypes from "./employee.types"; + +export const signInEmployeeStart = (employeeIdAndPin) => ({ + type: EmployeeActionTypes.SIGN_IN_EMPLOYEE_START, + payload: employeeIdAndPin, +}); + +export const signInEmployeeSuccess = (technician) => ({ + type: EmployeeActionTypes.SIGN_IN_EMPLOYEE_SUCCESS, + payload: technician, +}); + +export const signInEmployeeFailure = (error) => ({ + type: EmployeeActionTypes.SIGN_IN_EMPLOYEE_FAILURE, + payload: error, +}); + +export function loginEmployee() { + return { + type: EmployeeActionTypes.AUTHORIZING_EMPLOYEE_REQUEST, + payload:"text" + } +} +export function loginEmployeeSuccess(technician) { + return { + type: EmployeeActionTypes.AUTHORIZING_EMPLOYEE_SUCCESS, + payload: technician + } +} diff --git a/redux/employee/employee.reducer.js b/redux/employee/employee.reducer.js new file mode 100644 index 0000000..7a327f8 --- /dev/null +++ b/redux/employee/employee.reducer.js @@ -0,0 +1,36 @@ +import EmployeeActionTypes from "./employee.types"; + +const INITIAL_STATE = { + currentEmployee: { + authorized: null, + }, + employeeSigningIn: false, + error: null, +}; + +const employeeReducer = (state = INITIAL_STATE, action) => { + switch (action.type) { + case EmployeeActionTypes.SIGN_IN_EMPLOYEE_START: + return { + ...state, + employeeSigningIn: true, + error: null, + }; + case EmployeeActionTypes.SIGN_IN_EMPLOYEE_SUCCESS: + return { + ...state, + currentEmployee: action.payload, + error: null, + }; + case EmployeeActionTypes.SIGN_OUT_EMPLOYEE_SUCCESS: + return { + ...state, + currentEmployee: { authorized: false }, + error: null, + }; + default: + return state; + } +}; + +export default employeeReducer; diff --git a/redux/employee/employee.sagas.js b/redux/employee/employee.sagas.js new file mode 100644 index 0000000..fc04e7d --- /dev/null +++ b/redux/employee/employee.sagas.js @@ -0,0 +1,42 @@ +import EmployeeActionTypes from "./employee.types"; +import { + signInEmployeeStart, + signInEmployeeSuccess, + signInEmployeeFailure, +} from "./employee.actions"; +import { all, call, put, takeLatest } from "redux-saga/effects"; +import { logImEXEvent } from "../../firebase/firebase.analytics"; +import { selectBodyshop } from "../user/user.selectors"; +import axios from "axios"; + +export function* onSignInEmployeeStart() { + yield takeLatest( + EmployeeActionTypes.SIGN_IN_EMPLOYEE_START, + signInWithEmployeeId + ); +} +export function* signInWithEmployeeId({ payload: { employeeid, pin } }) { + try { + logImEXEvent("redux_sign_in_employee_attempt"); + //console.loging + const bodyshop = yield select(selectBodyshop); + const response = yield call(axios.post, "/tech/login", { + shopid: bodyshop.id, + employeeid: employeeid, + pin: pin, + }); + const { valid, technician, error } = response.data; + + if (valid) { + yield put(signInEmployeeSuccess(technician)); + } else { + yield put(signInEmployeeFailure(error)); + } + } catch (error) { + yield put(signInEmployeeFailure(error)); + } +} + +export function* employeeSagas() { + yield all([call(onSignInEmployeeStart)]); +} diff --git a/redux/employee/employee.selectors.js b/redux/employee/employee.selectors.js new file mode 100644 index 0000000..84790c8 --- /dev/null +++ b/redux/employee/employee.selectors.js @@ -0,0 +1,12 @@ +import { createSelector } from "reselect"; + +const selectEmployee = (state) => state.employee; + +export const selectCurrentEmployee = createSelector( + [selectEmployee], + (employee) => employee.currentEmployee + ); +export const selectSigningIn = createSelector( + [selectEmployee], + (employee) => employee.signingIn + ); \ No newline at end of file diff --git a/redux/employee/employee.types.js b/redux/employee/employee.types.js new file mode 100644 index 0000000..eb03657 --- /dev/null +++ b/redux/employee/employee.types.js @@ -0,0 +1,17 @@ +const EmployeeActionTypes = { + AUTHORIZING_EMPLOYEE_REQUEST:"AUTHORIZING_EMPLOYEE_REQUEST", + AUTHORIZING_EMPLOYEE_SUCCESS:"AUTHORIZING_EMPLOYEE_SUCCESS", + AUTHORIZING_EMPLOYEE_SUCCESS:"AUTHORIZING_EMPLOYEE_FAILURE", + SET_CURRENT_EMPLOYEE: "SET_CURRENT_EMPLOYEE", + SIGN_IN_EMPLOYEE_SUCCESS: "SIGN_IN_EMPLOYEE_SUCCESS", + SIGN_IN_EMPLOYEE_FAILURE: "SIGN_IN_EMPLOYEE_FAILURE", + SIGN_IN_EMPLOYEE_START: "SIGN_IN_EMPLOYEE_START", + CHECK_EMPLOYEE_SESSION: "CHECK_EMPLOYEE_SESSION", + SIGN_OUT_EMPLOYEE_START: "SIGN_OUT_EMPLOYEE_START", + SIGN_OUT_EMPLOYEE_SUCCESS: "SIGN_OUT_EMPLOYEE_SUCCESS", + SIGN_OUT_EMPLOYEE_FAILURE: "SIGN_OUT_EMPLOYEE_FAILURE", + UNAUTHORIZED_EMPLOYEE: "UNAUTHORIZED_EMPLOYEE", + UPDATE_EMPLOYEE_DETAILS: "UPDATE_EMPLOYEE_DETAILS", + UPDATE_EMPLOYEE_DETAILS_SUCCESS: "UPDATE_EMPLOYEE_DETAILS_SUCCESS", + }; + export default EmployeeActionTypes; \ No newline at end of file