Skip to content

Commit

Permalink
Merge pull request #73 from AnoshMalik/bugfix/history_and_database_in…
Browse files Browse the repository at this point in the history
…jection

Corrected history page not loading bug
  • Loading branch information
AnoshMalik authored May 18, 2023
2 parents be74ced + 9560fa6 commit 7c991dd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
24 changes: 13 additions & 11 deletions client/src/Components/MainContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ const MainContent = ({ user }) => {

// SPEECH OUTPUT FEATURE
useEffect(() => {
// console.log(user);
const synth = new SpeechSynthesisUtterance();
const voices = window.speechSynthesis.getVoices();
synth.voice = voices[0];
synth.lang = "en-GB";
setSynth(synth);
}, []);
}, [user]);

// useEffect(() => {
// // const synth = new SpeechSynthesisUtterance();
Expand All @@ -53,24 +54,24 @@ const MainContent = ({ user }) => {
synth.text = "Here are your suggestions! " + response;
}
if (speechToggle % 2 == 0) {
console.log("Stopped");
// console.log("Stopped");
SetSpeechToggle(speechToggle + 1);
setIsIconPaused(false);

window.speechSynthesis.cancel();
clearTimeout(timeOutId);
console.log("CLEAR TIMEOUT CALLED");
// console.log("CLEAR TIMEOUT CALLED");
} else {
console.log("Playing");
// console.log("Playing");
SetSpeechToggle(speechToggle + 1);
setIsIconPaused(true);

window.speechSynthesis.speak(synth);
// FOLLOWING LINES ARE NEEDED TO STOP CHROME CUTTING AUDIO OFF AFTER 15 SECONDS
window.speechSynthesis.pause();
console.log("PAUSED");
// console.log("PAUSED");
window.speechSynthesis.resume();
console.log("RESUMED");
// console.log("RESUMED");

// STOPS TEXT FROM LOOPING
// synth.onend = () => {
Expand All @@ -79,14 +80,14 @@ const MainContent = ({ user }) => {
// clearTimeout(timeOutId);
// };
if (window.speechSynthesis.speaking == false) {
console.log("Not speaking");
// console.log("Not speaking");
window.speechSynthesis.cancel();
clearTimeout(timeOutId);
} else {
console.log("Speaking");
// console.log("Speaking");
let timeId = setTimeout(handleSpeak, 10000);
SetTimeOutId(timeId);
console.log("SET TIMEOUT CALLED");
// console.log("SET TIMEOUT CALLED");
}
}
}
Expand Down Expand Up @@ -169,7 +170,7 @@ const MainContent = ({ user }) => {
} else if (value === content && saveCounter > 1) {
alert("Please update text on the left");
} else {
console.log(user.id);
// console.log(user.id);
// const github_id = user.id;
await fetch("/api/history", {
method: "POST",
Expand All @@ -179,7 +180,7 @@ const MainContent = ({ user }) => {
body: JSON.stringify({
input: content,
output: response,
user_id: 3,
user_id: user.id,
}),
})
.then((res) => res.json())
Expand Down Expand Up @@ -218,6 +219,7 @@ const MainContent = ({ user }) => {
onChange={(e) => {
setContent(e.target.value);
setResponse("");
SetSaveCounter(1);
}}
style={{ boxShadow: "0px 5px 10px grey" }}
/>
Expand Down
6 changes: 5 additions & 1 deletion client/src/pages/History.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ const History = ({ user }) => {
`http://localhost:3100/api/history?githubId=${user.id}&search=${search}&sort=${sort}`
)
.then((response) => response.json())
.then((data) => setHistory(data.data))
.then((data) => {
setHistory(data.data);
// console.log(data.data);
})
.catch((error) => {
console.log(error);
});
};

useEffect(() => {
if (user) {
// console.log(user);
sendRequest();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
13 changes: 9 additions & 4 deletions server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ router.get("/auth/login/success", (req, res) => {
res.json();
throw new Error("no user");
} else {
// eslint-disable-next-line no-console
// console.log(req.session.user);
res.json(req.session.user);
}
} catch (err) {
Expand Down Expand Up @@ -205,7 +207,7 @@ router.post("/corrections", async (req, res) => {
messages: [
{
role: "user",
content: `Can you correct this sentence for grammatical issues and give it three options: ${text}`,
content: `Correct the sentence for grammar and spelling errors and suggest three alternatives in a list form: ${text}`,
},
],
});
Expand All @@ -218,7 +220,7 @@ router.post("/corrections", async (req, res) => {
}); //console.log(completion.data.choices[0].text);*/

// eslint-disable-next-line no-console
console.log(completion.data.choices[0].message);
// console.log(completion.data.choices[0].message);

res.json({ msg: completion.data });
});
Expand All @@ -227,9 +229,12 @@ router.post("/corrections", async (req, res) => {
// Get all histories from db
router.get("/history", (req, res) => {
const { githubId, search, sort } = req.query;
// eslint-disable-next-line no-console
// console.log("API > GITHUBID > " + githubId);

if (githubId) {
let query = `SELECT history.* FROM users INNER JOIN history ON users.id = history.user_id AND users.github_id = ${githubId} `;
// let query = `SELECT history.* FROM users INNER JOIN history ON users.id = history.user_id AND users.github_id = ${githubId} `;
let query = `SELECT * FROM history WHERE user_id = ${githubId} `;
if (search && search.length < 50) {
query += ` AND ( history.input ~* '${search}' OR history.output ~* '${search}')`;
}
Expand All @@ -256,7 +261,7 @@ router.get("/history", (req, res) => {
router.post("/history", (req, res) => {
const { input, output, user_id } = req.body;
// eslint-disable-next-line no-console
console.log("github id - " + user_id);
// console.log("github id - " + user_id);
if (input && output && user_id) {
db.query(
// `INSERT INTO history(input ,output, user_id, timestamp ) VALUES ('${input}' ,'${output}','${user_id}', current_timestamp ) RETURNING id`
Expand Down

0 comments on commit 7c991dd

Please sign in to comment.