import { useState } from "react";
import "./VerseValidator.css";
import { StringDiff } from "react-string-diff";
import { containsKorean, jamoSubstringMatch } from './utils';
const STATE = {
INCORRECT: 0,
PARTIAL: 1,
CORRECT: 2,
};
// function to render and handle logic of each of the cells
const VerseValidator = ({ element: { pack, title, chapterTitle, reference, verse } , toHideReference, t, index}) => { // useful use of destructuring here
const [inputReference, setReference] = useState('')
const [referenceBool, setReferenceBool] = useState(STATE.INCORRECT)
const [inputChapterTitle, setChapterTitle] = useState('')
const [chapterTitleBool, setChapterTitleBool] = useState(STATE.INCORRECT)
const [inputTitle, setTitle] = useState('')
const [titleBool, setTitleBool] = useState(STATE.INCORRECT)
const [inputVerse, setVerse] = useState('')
const [verseBool, setVerseBool] = useState(STATE.INCORRECT)
const[hintBool, setHintBool] = useState(false)
const[diffBool, setDiffBool] = useState(false)
const [isComposing, setIsComposing] = useState(false);
// handle reset
const handleReset = () => {
setReference('');
setReferenceBool(STATE.INCORRECT);
setChapterTitle('');
setChapterTitleBool(STATE.INCORRECT);
setTitle('');
setTitleBool(STATE.INCORRECT);
setVerse('');
setVerseBool(STATE.INCORRECT);
setDiffBool(false); // optionally hide answer again
};
// Handle the start of composition
const handleCompositionStart = () => {
setIsComposing(true);
};
function resultChecker(string1, string2) {
var result = STATE.INCORRECT; // init
// contains korean
if (containsKorean(string1)) {
if (string1 === string2) {
result = STATE.CORRECT;
} else if (jamoSubstringMatch(string2, string1) & string1 !== "") {
result = STATE.PARTIAL;
} else {
result = STATE.INCORRECT;
}
} else { // does not contain korean
if (string1 === string2) {
result = STATE.CORRECT;
} else if (string2.startsWith(string1) & string1 !== "") {
result = STATE.PARTIAL;
} else {
result = STATE.INCORRECT;
}
}
return result;
}
// function to check correctness of reference input
const referenceChange = (e) => {
const value = e.target.value;
const string1 = String(value)
.replace(/\s+/g, "")
.toLowerCase()
.normalize("NFC");
const string2 = String(reference)
.replace(/\s+/g, "")
.toLowerCase()
.normalize("NFC");
const result = resultChecker(string1, string2);
setReference(value);
setReferenceBool(result);
};
const referenceClassName = `reference-box${
referenceBool === STATE.CORRECT ? " correct" :
referenceBool === STATE.PARTIAL ? " partial" :
" incorrect"
}`;
{/* function to check correctness of title input */}
const titleChange = (e) => {
const value = e.target.value;
let string1 = value;
let string2 = title;
string1 = String(string1)
.replace(/[\p{P}\p{S}]/gu, "") // Removes punctuation and symbols
.replace(/\s+/g, "") // Removes all whitespace
.toLowerCase()
.normalize("NFC"); // Normalizes to NFC form
string2 = String(string2)
.replace(/[\p{P}\p{S}]/gu, "")
.replace(/\s+/g, "")
.toLowerCase()
.normalize("NFC");
const result = resultChecker(string1, string2);
setTitle(value);
setTitleBool(result);
};
const titleClassName = `chapter-title-box${
titleBool=== STATE.CORRECT ? " correct" :
titleBool === STATE.PARTIAL ? " partial" :
" incorrect"
}`;
{/* function to check correctness of chapter title input */}
const chapterTitleChange = (e) => {
const value = e.target.value;
let string1 = value;
let string2 = chapterTitle;
string1 = String(string1)
.replace(/[\p{P}\p{S}]/gu, "")
.replace(/\s+/g, "")
.toLowerCase()
.normalize("NFC");
string2 = String(string2)
.replace(/[\p{P}\p{S}]/gu, "")
.replace(/\s+/g, "")
.toLowerCase()
.normalize("NFC");
const result = resultChecker(string1, string2);
setChapterTitle(value);
setChapterTitleBool(result);
};
const chapterTitleClassName = `title-box${
chapterTitleBool=== STATE.CORRECT ? " correct" :
chapterTitleBool === STATE.PARTIAL ? " partial" :
" incorrect"
}`;
// check verse input
const verseChange = (e) => {
const value = e.target.value;
let string1 = value;
let string2 = verse;
string1 = String(string1)
.replace(/[\p{P}\p{S}]/gu, "")
.replace(/\s+/g, "")
.toLowerCase()
.normalize("NFC");
string2 = String(string2)
.replace(/[\p{P}\p{S}]/gu, "")
.replace(/\s+/g, "")
.toLowerCase()
.normalize("NFC");
const result = resultChecker(string1, string2);
setVerse(value);
setVerseBool(result);
};
const verseClassName = `verse-box${
verseBool === STATE.CORRECT ? " correct" :
verseBool === STATE.PARTIAL ? " partial" :
" incorrect"
}`;
// const DiffViewer = ({oldValue, newValue}) => {
// const string1 = String(oldValue)
// .replace(/[\p{P}\p{S}]/gu, "")
// .toLowerCase()
// .normalize("NFC");
// const string2 = String(newValue)
// .replace(/[\p{P}\p{S}]/gu, "")
// .toLowerCase()
// .normalize("NFC");
// return (