Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the functionality of posting quiz and Logging out of user #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
104 changes: 104 additions & 0 deletions public/js/addQuiz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// import axios from "axios";

// const { default: Axios } = require("axios");

// adding quiz
const quizForm = document.getElementById("quiz");
const addQueBtn = document.getElementById("addQue");
const addQuizBtn = document.getElementById("addQuiz");

// questions array
let questions = [];

const addQuestion = () => {
console.log("question is added");
const answers = [];

const question = document.querySelector(".question").value;

let text1 = document.querySelector(".answer1").value;
let correct1 = document.getElementById("answer1").checked ? true : false;

answers.push({ text1, correct1 });

let text2 = document.querySelector(".answer2").value;
let correct2 = document.getElementById("answer2").checked ? true : false;
answers.push({ text2, correct2 });

let text3 = document.querySelector(".answer3").value;
let correct3 = document.getElementById("answer3").checked ? true : false;
answers.push({ text3, correct3 });

let text4 = document.querySelector(".answer4").value;
let correct4 = document.getElementById("answer4").checked ? true : false;
answers.push({ text4, correct4 });

// console.log(answers);
console.log({ question, answers });
questions.push({ question, answers });

document.querySelector(".answer1").value = "";
document.querySelector(".answer2").value = "";
document.querySelector(".answer3").value = "";
document.querySelector(".answer4").value = "";
document.querySelector(".question").value = "";
};

const submitQuiz = async () => {
const quizName = document.querySelector(".name").value;
if (quizName === "") {
alert("Quiz must have a name.");
return;
}

if (questions.length === 0) {
alert("Quiz must contain questions.");
return;
}

let cookie = document.cookie;
cookie = cookie.split("").slice(12, 36).join("");

console.log({ quizName, questions, cookie });

try {
const result = await axios({
method: "POST",
url: "http://127.0.0.1:3000/api/quiz",
data: {
quizName,
question: questions,
user: cookie,
},
});

console.log(result);
if (result.data.status === "Success") {
window.setTimeout(() => {
location.assign("/");
}, 1000);
}
} catch (error) {
console.log(error);
}
};

//adding the questions
addQueBtn.addEventListener("click", addQuestion);

document.addEventListener("keypress", function (event) {
if (event.keyCode === 13 || event.which === 13) {
addQuestion();
}
});

addQuizBtn.addEventListener("click", submitQuiz);

// const questions = [
// {
// question: 'What is 2 + 2?',
// answers: [
// { text: '4', correct: true },
// { text: '22', correct: false }
// ]
// }
22 changes: 22 additions & 0 deletions public/js/logout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// logging out the users
// import axios from "axios";

const logoutBtn = document.getElementById("logout");

if (logoutBtn) {
logoutBtn.addEventListener("click", async () => {
// console.log("user is logged out");
try {
const result = await axios({
method: "GET",
url: "/api/user/logout",
});

// console.log(result);
if (result.data.status === "success") location.reload(true);
} catch (error) {
console.log(error);
alert("Error Logging out!! Try again later");
}
});
}