Translation swap added

This commit is contained in:
David
2021-03-18 23:47:12 +01:00
parent 535f791455
commit 2bc931a4d3
7 changed files with 107 additions and 50 deletions

View File

@@ -3,7 +3,9 @@ import { replaceBoth } from "./language";
export const initialState = {
source: "auto",
target: "en",
query: ""
query: "",
delayedQuery: "",
translation: ""
}
type State = typeof initialState;
@@ -46,7 +48,10 @@ export default function reducer(state: State, action: Action) {
source: source !== target
? source
: initialState.source,
target
target,
query: state.translation,
delayedQuery: state.translation,
translation: ""
};
default:
return state;

View File

@@ -6,7 +6,7 @@ export async function googleScrape(
target: string,
query: string
): Promise<{
translation?: string,
translationRes?: string,
statusCode?: number,
errorMsg?: string
}> {
@@ -26,11 +26,11 @@ export async function googleScrape(
};
const html = await res.text();
const translation = cheerio.load(html)(".result-container").text().trim();
const translationRes = cheerio.load(html)(".result-container").text().trim();
return translation
return translationRes
? {
translation
translationRes
} : {
errorMsg: "An error occurred while parsing the translation"
};