129 lines
4.8 KiB
TypeScript
129 lines
4.8 KiB
TypeScript
import React, {useEffect} from 'react';
|
|
import {FirebaseFirestoreTypes} from '@react-native-firebase/firestore';
|
|
import {RootState} from '../store/store';
|
|
import firestore from '@react-native-firebase/firestore';
|
|
import {useAppDispatch, useAppSelector} from '.';
|
|
import {updateCaseDetailsFirestore} from '../reducer/allCasesSlice';
|
|
import {CaseDetail} from '../screens/caseDetails/interface';
|
|
import {FirestoreUpdateTypes} from '../common/Constants';
|
|
import {toast} from '../../RN-UI-LIB/src/components/toast';
|
|
|
|
export interface CaseUpdates {
|
|
updateType: string;
|
|
updatedCaseDetail: CaseDetail;
|
|
}
|
|
|
|
const useFirestoreUpdates = () => {
|
|
const reduxStoreData = useAppSelector((state: RootState) => state);
|
|
const {
|
|
user: {user, isLoggedIn},
|
|
} = reduxStoreData;
|
|
|
|
const dispatch = useAppDispatch();
|
|
|
|
const handleCasesUpdate = (
|
|
querySnapshot: FirebaseFirestoreTypes.QuerySnapshot,
|
|
) => {
|
|
let newlyAddedCases = 0;
|
|
const caseUpdates: CaseUpdates[] = [];
|
|
querySnapshot
|
|
.docChanges()
|
|
.forEach(
|
|
(documentSnapshot: FirebaseFirestoreTypes.DocumentChange) => {
|
|
console.log('type....', documentSnapshot.type);
|
|
console.log(
|
|
'Updated Chbages .........',
|
|
documentSnapshot.doc.data(),
|
|
);
|
|
const updateType = documentSnapshot.type;
|
|
const updatedCaseDetail =
|
|
documentSnapshot.doc.data() as CaseDetail;
|
|
if (updateType === FirestoreUpdateTypes.ADDED) {
|
|
newlyAddedCases++;
|
|
}
|
|
caseUpdates.push({updateType, updatedCaseDetail});
|
|
},
|
|
);
|
|
// dispatch(updateCaseDetailsFirestore({}));
|
|
if (newlyAddedCases > 0) {
|
|
toast({
|
|
type: 'success',
|
|
text1: `${newlyAddedCases} new case${
|
|
newlyAddedCases > 1 ? 's' : ''
|
|
} added`,
|
|
});
|
|
}
|
|
};
|
|
|
|
const handleError = (err: any) => {
|
|
console.log('Error while fetching firestore snapshot', err);
|
|
};
|
|
|
|
const fakeAddCase = () => {
|
|
setTimeout(() => {
|
|
firestore()
|
|
// .doc(`users/${user?.referenceId}/cases/caseId2`)
|
|
.collection('case')
|
|
.add({
|
|
allocatedAt: 1670806248220,
|
|
caseStatus: 'IN_PROGRESS',
|
|
context: {
|
|
currentTask: null,
|
|
taskContext: [Object],
|
|
taskSequence: null,
|
|
},
|
|
createdAt: 1671086418938,
|
|
currentAllocationReferenceId:
|
|
'c495d1db-ee11-4235-84bd-6ab7e9b70e27',
|
|
currentTask: 'CALLING_TASK',
|
|
customerInfo: {
|
|
customerName: 'Tyrion Lannister',
|
|
customerReferenceId:
|
|
'e6d122cb-ca9c-4a16-85c9-7c354efe8c8e',
|
|
geoLocation:
|
|
'https://maps.google.com/?q=26.4273034,74.642422',
|
|
imageURL:
|
|
'https://ca.slack-edge.com/TEV8SHPQB-U03RK8J6N7Q-7035c43b0621-512',
|
|
primaryPhoneNumber: '8436942857',
|
|
},
|
|
id: '1d57089f-f367-4cd4-8b6c-124ef2e042e5',
|
|
isSynced: false,
|
|
loanDetails: {
|
|
disbursalDate: '2022-12-12',
|
|
disbursementAmount: 100,
|
|
firstDueDate: '2022-12-12',
|
|
loanAccountNumber: 'LAN3003',
|
|
loanAccountStatus: 'ACTIVE',
|
|
loanType: 'PERSONAL_LOAN',
|
|
productCode: 'PL-003',
|
|
tenureMonths: 10,
|
|
},
|
|
taskSequence: [
|
|
'COMMUNICATION_ADDRESS_VERIFICATION_TASK',
|
|
'PERMANENT_ADDRESS_VERIFICATION_TASK',
|
|
'GEO_LOCATION_VERIFICATION_TASK',
|
|
'CALLING_TASK',
|
|
'NEW_ADDRESS_VERIFICATION_TASK',
|
|
],
|
|
tasks: null,
|
|
updatedAt: 1672736996727,
|
|
});
|
|
}, 10000);
|
|
};
|
|
|
|
useEffect(() => {
|
|
if (!isLoggedIn) {
|
|
return;
|
|
}
|
|
// fakeAddCase();
|
|
const casesSubscriber = firestore()
|
|
.collection('case')
|
|
// .collection(`users/${user?.referenceId}/case`)
|
|
.onSnapshot(handleCasesUpdate, handleError);
|
|
// Stop listening for updates when no longer required
|
|
return () => casesSubscriber();
|
|
}, [isLoggedIn]);
|
|
};
|
|
|
|
export default useFirestoreUpdates;
|