Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: clear ocrs after user logs out or switches company #2295

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iSunFA",
"version": "0.8.0+68",
"version": "0.8.0+69",
"private": false,
"scripts": {
"dev": "next dev",
Expand Down
66 changes: 42 additions & 24 deletions src/contexts/accounting_context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ export const AccountingProvider = ({ children }: IAccountingProvider) => {
const { trigger: getAIStatus } = APIHandler<ProgressStatus>(APIName.AI_ASK_STATUS);
const {
trigger: listUnprocessedOCR,
data: unprocessOCRs,
error: listError,
success: listSuccess,
code: listCode,
Expand Down Expand Up @@ -245,6 +244,7 @@ export const AccountingProvider = ({ children }: IAccountingProvider) => {
const [pendingOCRList, setPendingOCRList] = useState<IOCRItem[]>([]);
const [isDBReady, setIsDBReady] = useState(false);
const [pendingOCRListFromBrowser, setPendingOCRListFromBrowser] = useState<IOCRItem[]>([]);
const [unprocessedOCRs, setUnprocessedOCRs] = useState<IOCR[]>([]);

const getAccountListHandler = (
companyId: number,
Expand Down Expand Up @@ -457,9 +457,11 @@ export const AccountingProvider = ({ children }: IAccountingProvider) => {
}
};

const clearState = () => {
const clearOCRs = () => {
setPendingOCRList([]);
setPendingOCRListFromBrowser([]);
setUnprocessedOCRs([]);
setOCRList([]);
};

useEffect(() => {
Expand All @@ -477,7 +479,7 @@ export const AccountingProvider = ({ children }: IAccountingProvider) => {
}, [isDBReady, userAuth, selectedCompany]);

useEffect(() => {
clearState();
clearOCRs();
}, [signedIn, selectedCompany]);

useEffect(() => {
Expand All @@ -488,30 +490,46 @@ export const AccountingProvider = ({ children }: IAccountingProvider) => {

useEffect(() => {
let interval: NodeJS.Timeout | undefined;

if (OCRListParams && OCRListParams.update) {
interval = setInterval(() => {
listUnprocessedOCR({
params: {
companyId: OCRListParams.companyId,
},
});
interval = setInterval(async () => {
try {
const response = await listUnprocessedOCR({
params: {
companyId: OCRListParams.companyId,
},
});
if (response?.data) {
setUnprocessedOCRs(response.data);
}
} catch (error) {
clearInterval(interval);
}
}, 2000);
if (OCRListStatus.listSuccess !== listSuccess || OCRListStatus.listCode !== listCode) {
setOCRLisStatus({
listSuccess,
listCode,
});
}
if (listSuccess && unprocessOCRs) {
setOCRList((prevList) => mergeOCRLists(unprocessOCRs, prevList));
setPendingOCRList((prevList) => excludeUploadIdentifier(unprocessOCRs, prevList));
}
if (listSuccess === false) {
setOCRListParams((prev) => (prev ? { ...prev, update: false } : prev));
}
}
return () => clearInterval(interval);
}, [OCRListParams, listError, listSuccess, listCode, unprocessOCRs]);

return () => {
clearInterval(interval);
};
}, [OCRListParams]);

useEffect(() => {
if (OCRListStatus.listSuccess !== listSuccess || OCRListStatus.listCode !== listCode) {
setOCRLisStatus({
listSuccess,
listCode,
});
}

if (listSuccess) {
setOCRList((prevList) => mergeOCRLists(unprocessedOCRs, prevList));
setPendingOCRList((prevList) => excludeUploadIdentifier(unprocessedOCRs, prevList));
}

if (listSuccess === false) {
setOCRListParams((prev) => (prev ? { ...prev, update: false } : prev));
}
}, [listSuccess, listError, listCode, unprocessedOCRs]);

const generateAccountTitle = (account: IAccount | null) => {
if (account) return account.code + ' - ' + account.name;
Expand Down