Skip to content

Commit

Permalink
Merge pull request #36 from indranilbhuin/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
indranilbhuin authored Jan 9, 2024
2 parents 62b9c44 + 42afa93 commit 06db0b9
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/redux/rootReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import debtReducer from './slice/debtDataSlice';
import allDebtReducer from './slice/allDebtDataSlice';
import everydayExpenseReducer from './slice/everydayExpenseDataSlice';
import allDataReducer from './slice/allDataSlice';
import individualDebtorReducer from './slice/IndividualDebtorSlice';

const rootReducer = combineReducers({
userOnboarding: userOnboardingReducer,
Expand All @@ -27,6 +28,7 @@ const rootReducer = combineReducers({
allDebt: allDebtReducer,
everydayExpense: everydayExpenseReducer,
allData: allDataReducer,
individualDebtor: individualDebtorReducer,
});

export type RootState = ReturnType<typeof rootReducer>;
Expand Down
22 changes: 21 additions & 1 deletion src/redux/saga/debtorSaga.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import {call, put, select, takeEvery} from 'redux-saga/effects';
import {selectUserId} from '../slice/userIdSlice';
import {FETCH_ALL_DEBTOR_DATA} from '../actionTypes';
import {getAllDebtorsByUserId} from '../../services/DebtorService';
import {
getAllDebtorsByUserId,
getDebtorByDebtorId,
} from '../../services/DebtorService';
import {setDebtorData} from '../slice/debtorDataSlice';
import {
getIndividualDebtorFaliure,
getIndividualDebtorRequest,
getIndividualDebtorSuccess,
} from '../slice/IndividualDebtorSlice';

function* fetchAllDebtors(): Generator<any, void, any> {
try {
Expand All @@ -16,6 +24,18 @@ function* fetchAllDebtors(): Generator<any, void, any> {
}
}

function* fetchDebtorbyId(action: any): Generator<any, void, any> {
try {
const debtorId = action.payload;
const debtor = yield call(getDebtorByDebtorId, debtorId);
yield put(getIndividualDebtorSuccess(debtor));
} catch (error) {
yield put(getIndividualDebtorFaliure(error));
console.error('Error fetching debtor:', error);
}
}

export function* watchFetchAllDebtors() {
yield takeEvery(FETCH_ALL_DEBTOR_DATA, fetchAllDebtors);
yield takeEvery(getIndividualDebtorRequest.type, fetchDebtorbyId);
}
11 changes: 9 additions & 2 deletions src/redux/saga/expenseSaga.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import {call, put, select, takeEvery} from 'redux-saga/effects';
import {selectUserId} from '../slice/userIdSlice';
import {getAllExpensesByDate, getAllExpensesByUserId} from '../../services/ExpenseService';
import {
getAllExpensesByDate,
getAllExpensesByUserId,
} from '../../services/ExpenseService';
import {
getExpenseFaliure,
getExpenseRequest,
getExpenseSuccess,
} from '../slice/expenseDataSlice';
import { getEverydayExpenseFaliure, getEverydayExpenseRequest, getEverydayExpenseSuccess } from '../slice/everydayExpenseDataSlice';
import {
getEverydayExpenseFaliure,
getEverydayExpenseRequest,
getEverydayExpenseSuccess,
} from '../slice/everydayExpenseDataSlice';

function* fetchAllExpenses(): Generator<any, void, any> {
try {
Expand Down
44 changes: 44 additions & 0 deletions src/redux/slice/IndividualDebtorSlice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {createSlice} from '@reduxjs/toolkit';
import {RootState} from '../rootReducer';

const initialState = {
individualDebtorData: [],
isLoading: false,
error: null,
};

const individualDebtorSlice = createSlice({
name: 'individualDebtor',
initialState,
reducers: {
getIndividualDebtorRequest: (state, _debtorId) => {
state.isLoading = true;
state.error = null;
},
getIndividualDebtorSuccess: (state, action) => {
console.log(action.payload);
state.isLoading = false;
state.error = null;
state.individualDebtorData = action.payload;
},
getIndividualDebtorFaliure: (state, action) => {
state.isLoading = false;
state.error = action.payload;
},
},
});

export const selectIndividualDebtorData = (state: RootState) =>
state.individualDebtor.individualDebtorData;
export const selectIndividualDebtorLoading = (state: RootState) =>
state.individualDebtor.isLoading;
export const selectIndividualDebtorError = (state: RootState) =>
state.individualDebtor.error;

export const {
getIndividualDebtorRequest,
getIndividualDebtorSuccess,
getIndividualDebtorFaliure,
} = individualDebtorSlice.actions;

export default individualDebtorSlice.reducer;
14 changes: 13 additions & 1 deletion src/screens/IndividualDebtsScreen/useIndividualDebts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import {FETCH_ALL_DEBTOR_DATA} from '../../redux/actionTypes';
import moment from 'moment';
import Debt from '../../schemas/DebtSchema';
import useAmountColor from '../../hooks/useAmountColor';
import {
getIndividualDebtorRequest,
selectIndividualDebtorData,
} from '../../redux/slice/IndividualDebtorSlice';

export type IndividualDebtsScreenRouteProp = RouteProp<
{
Expand Down Expand Up @@ -49,7 +53,9 @@ const useIndividualDebts = (route: IndividualDebtsScreenRouteProp) => {
const [paidToastVisible, setPaidToastVisible] = useState(false);
const [deleteDebtorVisible, setDeleteDebtorVisible] = useState(false);
const {debtorId, debtorType} = route.params;
const debtorName = individualDebtsCopy[0]?.debtor.title;
const individualDebtor = useSelector(selectIndividualDebtorData);
console.log('individuallll', individualDebtor);
const debtorName = individualDebtor?.title;

const borrowings = individualDebtsCopy.filter(
(debt: Debt) => debt.type === 'Borrow',
Expand Down Expand Up @@ -153,11 +159,17 @@ const useIndividualDebts = (route: IndividualDebtsScreenRouteProp) => {

useEffect(() => {
dispatch(getDebtRequest(debtorId));
dispatch(
getIndividualDebtorRequest(
Realm.BSON.ObjectID.createFromHexString(debtorId),
),
);
}, [debtorId]);

useEffect(() => {
if (refreshing) {
dispatch(getDebtRequest(debtorId));
dispatch(getIndividualDebtorRequest(debtorId));
setRefreshing(false);
}
}, [refreshing]);
Expand Down
18 changes: 18 additions & 0 deletions src/services/DebtorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,21 @@ export const getAllDebtorsByUserId = async (userId: Realm.BSON.ObjectId) => {

return debtorsByUserId;
};

export const getDebtorByDebtorId = async (debtorId: Realm.BSON.ObjectId) => {
const realm = await getRealm();

try {
const debtor = realm.objectForPrimaryKey('Debtor', debtorId);

if (debtor) {
return debtor;
} else {
console.error('Debtor not found.');
return null;
}
} catch (error) {
console.error('Error retrieving debtor name:', error);
return null;
}
};

0 comments on commit 06db0b9

Please sign in to comment.