Skip to content

Commit

Permalink
remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Oct 16, 2024
1 parent 27823a3 commit c7e39cc
Showing 1 changed file with 4 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,49 +1,16 @@
import { Dispatch } from "react";
import { DetailViewAction } from "../../../../contexts/detailview-context";
import { IDetailView } from "../../../../interfaces/IDetailView";
import { Orientation } from "../../../../interfaces/IFileIndexItem";
import { CastToInterface } from "../../../../shared/cast-to-interface";
import FetchGet from "../../../../shared/fetch/fetch-get";
import { UrlQuery } from "../../../../shared/url/url-query";

export async function RequestNewFileHash(
state: IDetailView,
setIsLoading: React.Dispatch<React.SetStateAction<boolean>>,
dispatch: Dispatch<DetailViewAction>
): Promise<boolean | null> {
const resultGet = await FetchGet(new UrlQuery().UrlIndexServerApi({ f: state.subPath }));
if (!resultGet) return null;
if (resultGet.statusCode !== 200) {
console.error(resultGet);
setIsLoading(false);
return null;
}
const media = new CastToInterface().MediaDetailView(resultGet.data).data;
const orientation = media?.fileIndexItem?.orientation
? media.fileIndexItem.orientation
: Orientation.Horizontal;

// the hash changes if you rotate an image
if (media.fileIndexItem.fileHash === state.fileIndexItem.fileHash) return false;

dispatch({
type: "update",
orientation,
fileHash: media.fileIndexItem.fileHash,
filePath: media.fileIndexItem.filePath
});
setIsLoading(false);
return true;
}
import { RequestNewFileHash } from "./request-new-filehash";

export function TriggerFileHashRequest(
state: IDetailView,
setIsLoading: React.Dispatch<React.SetStateAction<boolean>>,
dispatch: Dispatch<DetailViewAction>,
retry: number = 0
retry: number = 0,
delay: number = 3000
) {
const maxRetries = 3;
const delay = 3000;

const attemptRequest = (currentRetry: number) => {
setTimeout(() => {
Expand All @@ -57,5 +24,5 @@ export function TriggerFileHashRequest(
}, delay);
};

attemptRequest(retry);
return attemptRequest(retry);
}

0 comments on commit c7e39cc

Please sign in to comment.