Skip to content
This repository has been archived by the owner on Oct 23, 2020. It is now read-only.

submit questions on paste #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
99 changes: 60 additions & 39 deletions apps/js/Question.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,56 @@ export default function Question({
}
}, [moveCursor.current]);

const submitValue = async (val, savedVal) => {
if (val === savedVal || saving) {
return;
}
setSaving(true);
try {
const ret = await post("submit_question", {
id: question.id,
value: val,
token: getToken(),
exam: examContext.exam,
});
setSaving(false);
if (!ret.ok) {
setFailText("Server failed to respond, please try again.");
examContext.onInternetError();
return;
}
try {
const data = await ret.json();
if (!data.success) {
setFailText("Server responded but failed to save, please refresh and try again.");
examContext.onInternetError();
} else {
setSavedValue(val);
setFailText("");
}
} catch {
setFailText("Server returned invalid JSON. Please try again.");
examContext.onInternetError();
}
} catch {
setSaving(false);
setFailText("Unable to reach server, your network may have issues.");
examContext.onInternetError();
}
};

const handlePaste = (e) => {
e.preventDefault();
const { target } = e;
const start = target.selectionStart;
const end = target.selectionEnd;
const paste = e.clipboardData.getData('text');
const newValue = `${value.substring(0, start)}${e.clipboardData.getData('text')}${value.substring(end)}`;
setValue(newValue);
submitValue(newValue, savedValue);
moveCursor.current = { target, pos: start + paste.length };
}

let contents;
if (question.type === "multiple_choice") {
contents = (
Expand Down Expand Up @@ -108,7 +158,13 @@ export default function Question({
} else if (question.type === "short_answer") {
contents = (
<InputGroup className="mb-3">
<FormControl value={value} onChange={(e) => { setValue(e.target.value); }} />
<FormControl
value={value}
onChange={(e) => {
setValue(e.target.value);
}}
onPaste={handlePaste}
/>
</InputGroup>
);
} else if (question.type === "short_code_answer") {
Expand All @@ -120,6 +176,7 @@ export default function Question({
onChange={(e) => {
setValue(e.target.value);
}}
onPaste={handlePaste}
/>
</InputGroup>
);
Expand All @@ -133,6 +190,7 @@ export default function Question({
onChange={(e) => {
setValue(e.target.value);
}}
onPaste={handlePaste}
/>
</InputGroup>
);
Expand All @@ -158,49 +216,12 @@ export default function Question({
onChange={(e) => {
setValue(e.target.value);
}}
onPaste={handlePaste}
/>
</InputGroup>
);
}

const submitValue = async (val, savedVal) => {
if (val === savedVal || saving) {
return;
}
setSaving(true);
try {
const ret = await post("submit_question", {
id: question.id,
value: val,
token: getToken(),
exam: examContext.exam,
});
setSaving(false);
if (!ret.ok) {
setFailText("Server failed to respond, please try again.");
examContext.onInternetError();
return;
}
try {
const data = await ret.json();
if (!data.success) {
setFailText("Server responded but failed to save, please refresh and try again.");
examContext.onInternetError();
} else {
setSavedValue(val);
setFailText("");
}
} catch {
setFailText("Server returned invalid JSON. Please try again.");
examContext.onInternetError();
}
} catch {
setSaving(false);
setFailText("Unable to reach server, your network may have issues.");
examContext.onInternetError();
}
};

const submit = () => submitValue(value, savedValue);

const debouncedSubmit = useCallback(debounce(submitValue, 3000), []);
Expand Down