6080-a3-BigBrain / frontend / src / BackendRequests / questionRequests.jsx
questionRequests.jsx
Raw
// this file will be used for all question requests
// questionRequests.jsx

const createNewQuestion = async (token, question, gameId) => {
  const request = {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${token}`,
    },
  }
  const response = await fetch(`http://localhost:5005/admin/quiz/${gameId}`, request)
  const data = await response.json();
  console.log(data.questions)
  const newArray = data.questions
  newArray.push(question)

  const request2 = {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${token}`,
    },
    body: JSON.stringify({
      questions: newArray,
    })
  }
  const response2 = await fetch(`http://localhost:5005/admin/quiz/${gameId}`, request2)
  if (response2.status === 400) {
    console.log(await response2.text());
  }
}

const deleteQuestion = async (token, question, gameId) => {
  const request = {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${token}`,
    },
  }
  const response = await fetch(`http://localhost:5005/admin/quiz/${gameId}`, request)
  const data = await response.json();
  const oldArray = data.questions
  console.log(question)
  const newArray = oldArray.filter((item, itemIndex) => {
    return item !== question
  })
  console.log(newArray)

  const request2 = {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${token}`,
    },
    body: JSON.stringify({
      questions: newArray,
    })
  }
  const response2 = await fetch(`http://localhost:5005/admin/quiz/${gameId}`, request2)
  if (response2.status === 400) {
    console.log(await response2.text());
  }
}

export default { createNewQuestion, deleteQuestion }