Skip to content

Commit

Permalink
Merge pull request #747 from AI4Bharat/revert-745-revert-735-autosave2
Browse files Browse the repository at this point in the history
Revert "Revert "disable autosave when any other save call is in progress""
  • Loading branch information
aparna-aa authored Jun 28, 2024
2 parents 25081da + 117f879 commit 4c9eecf
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 6 deletions.
17 changes: 13 additions & 4 deletions src/hooks/useAutoSave.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect } from "react";
import { useEffect, useState } from "react";
import { useRef } from "react";
import { useDispatch, useSelector } from "react-redux";
import { useParams } from "react-router-dom";
Expand All @@ -13,6 +13,13 @@ export const useAutoSave = () => {
const currentPage = useSelector((state) => state.commonReducer.currentPage);
const subs = useSelector((state) => state.commonReducer.subtitles);
const taskDetails = useSelector((state) => state.getTaskDetails.data);
const apiStatus = useSelector((state) => state.apiStatus);
const [apiInProgress, setApiInProgress] = useState(false);

useEffect(() => {
const { progress, success, data, apiType } = apiStatus;
setApiInProgress(progress);
}, [apiStatus]);

useEffect(() => {
const handleAutosave = () => {
Expand All @@ -38,8 +45,10 @@ export const useAutoSave = () => {
},
};

const obj = new SaveTranscriptAPI(reqBody, taskDetails?.task_type);
dispatch(APITransport(obj));
if(!apiInProgress){
const obj = new SaveTranscriptAPI(reqBody, taskDetails?.task_type);
dispatch(APITransport(obj));
}
};

saveIntervalRef.current = setInterval(handleAutosave, 60 * 1000);
Expand All @@ -61,5 +70,5 @@ export const useAutoSave = () => {
};

// eslint-disable-next-line
}, [subs]);
}, [subs, apiInProgress]);
};
4 changes: 2 additions & 2 deletions src/hooks/useUpdateTimeSpent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEffect } from "react";
import { useRef } from "react";
import { useDispatch } from "react-redux";
import { useParams } from "react-router-dom";
import { APITransport, UpdateTimeSpentPerTask } from "redux/actions";
import { APITransportUTS, UpdateTimeSpentPerTask } from "redux/actions";

export const useUpdateTimeSpent = (ref) => {
const { taskId } = useParams();
Expand All @@ -12,7 +12,7 @@ export const useUpdateTimeSpent = (ref) => {
useEffect(() => {
const handleUpdateTimeSpent = (time = 60) => {
const apiObj = new UpdateTimeSpentPerTask(taskId, time);
dispatch(APITransport(apiObj));
dispatch(APITransportUTS(apiObj));
};

timeSpentIntervalRef.current = setInterval(
Expand Down
120 changes: 120 additions & 0 deletions src/redux/actions/apitransport/apitransportUts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import axios from "axios";
import strings from "../../string";
import { setSnackBar } from "../Common";

const dispatchAPIAsync = (api) => {
return {
type: api.type,
endpoint: api.apiEndPoint(),
payload: api.getPayload(),
};
};

const success = (res, api, dispatch) => {
const { data, status } = res;

api.processResponse(data);

if (api.type) {
dispatch(dispatchAPIAsync(api));
}

if (data.message) {
dispatch(
setSnackBar({
open: true,
message: data.message,
variant: "success",
})
);
}

if (
typeof api.processNextSuccessStep === "function" &&
(status === 200 || status === 201)
) {
api.processNextSuccessStep(data);
}
};

const error = (err, api, dispatch) => {
const {
response: {
data: { message },
status,
},
} = err;

const {
error: {
message: { http },
},
} = strings;

let errorMsg = message ?? http[status];

if (typeof api.processNextErrorStep === "function") {
api.processNextErrorStep();
}

dispatch(
setSnackBar({
open: true,
message: errorMsg,
variant: "error",
})
);

if (status === 401 && api.type !== "GET_USER_ACCESS_TOKEN") {
window.location.replace("/#/login");
}
};

export default function dispatchAPIUts(api) {
return (dispatch) => {
const { method } = api;

let request;
switch (method) {
case "MULTIPART":
request = axios.post(
api.apiEndPoint(),
api.getFormData(),
api.getHeaders()
);
break;

case "PATCH":
request = axios.patch(
api.apiEndPoint(),
api.getBody(),
api.getHeaders()
);
break;

case "POST":
request = axios.post(
api.apiEndPoint(),
api.getBody(),
api.getHeaders()
);
break;

case "PUT":
request = axios.put(api.apiEndPoint(), api.getBody(), api.getHeaders());
break;

case "DELETE":
request = axios.delete(api.apiEndPoint(), api.getHeaders());
break;

default:
request = axios.get(api.apiEndPoint(), api.getHeaders());
break;
}

request
.then((res) => success(res, api, dispatch))
.catch((err) => error(err, api, dispatch));
};
}
2 changes: 2 additions & 0 deletions src/redux/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ import FetchVoiceoverChartAPI from "./api/Intro/FetchVoiceoverChart";
import FetchTranslationChartAPI from "./api/Intro/FetchTranslationChart";

import APITransport from "./apitransport/apitransport";
import APITransportUTS from "./apitransport/apitransportUts";
import UnSubscribeNewletterFromEmailAPI from "./api/User/UnSubscribeNewletterFromEmail";
import RegenerateResponseAPI from "./api/Project/RegenerateResponse";
import OnBoardingAPI from "./api/User/OnBoarding";
Expand Down Expand Up @@ -266,6 +267,7 @@ export {
setRangeStart,
setRangeEnd,
APITransport,
APITransportUTS,
FetchVoiceoverExportTypesAPI,
FetchSupportedBulkTaskTypeAPI,
setSnackBar,
Expand Down

0 comments on commit 4c9eecf

Please sign in to comment.