6080-a3-BigBrain / frontend / src / BackendRequests / playerRequest.jsx
playerRequest.jsx
Raw
const join = async (sessionId, name) => {
  const request = {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ name: name }),
  }
  console.log(request.body)
  const response = await fetch('http://localhost:5005/play/join/' + sessionId, request)
  const data = await response.json();
  return data
}

const getSessionStatus = async (playerId) => {
  const request = {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
    },
  }
  const response = await fetch('http://localhost:5005/play/' + playerId + '/status', request)
  const data = await response.json();
  return data
}

const getQuestionDetails = async (playerId) => {
  const request = {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
    },
  }
  const response = await fetch('http://localhost:5005/play/' + playerId + '/question', request)
  const data = await response.json();
  return data
}

const submitAnswer = async (playerId, answerIds) => {
  const request = {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ answerIds: answerIds }),
  }
  const response = await fetch('http://localhost:5005/play/' + playerId + '/answer', request)
  if (response.status === 400) {
    console.log(await response.text());
  }
  const data = await response.json();
  return data
}

const getQuestionResult = async (playerId) => {
  const request = {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
    },
  }
  const response = await fetch('http://localhost:5005/play/' + playerId + '/answer', request)
  if (response.status === 400) {
    console.log(await response.text());
  }
  const data = await response.json();
  return data
}

const getQuizResult = async (playerId) => {
  const request = {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
    },
  }
  const response = await fetch('http://localhost:5005/play/' + playerId + '/results', request)
  if (response.status === 400) {
    console.log(await response.text());
  }
  const data = await response.json();
  return data
}

export default { join, getSessionStatus, getQuestionDetails, submitAnswer, getQuestionResult, getQuizResult }