import { createLogic, Done } from 'redux-logic';
import { EmployeeActions } from '../actions';
import { Dispatch, UserBody } from '../models';
import { Firestore, LogicDeps } from './';

const fetchEmployerDetailsLogic = createLogic({
  type: EmployeeActions.ActionKey.EMPLOYER_DETAILS_REQUEST,
  process({ action }: LogicDeps & { action: EmployeeActions.EmployerDetailsRequest },
    dispatch: Dispatch, done: Done) {
    console.log(action);

    Firestore
      .collection('employers')
      .doc(action.employerId)
      .get()
      .then((doc) => dispatch(EmployeeActions.employerDetailsSuccess(doc.data() as UserBody)))
      .catch((error) => {
        return dispatch(EmployeeActions.employerDetailsFail('Failed to get employer profile details'));
      })
      .then(() => done());
  },
});

export const employeeLogic = [
  fetchEmployerDetailsLogic,
];
