import { store } from './store'

export const get_request = (url: string) => {
  // TODO: Progress bar
  return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest()
    xhr.open('GET', url)
    xhr.setRequestHeader('Content-Type', 'application/json')

    xhr.onload = () => {
      if (xhr.status === 200) {
        const data = JSON.parse(xhr.responseText)
        resolve(data)
      } else {
        console.error('Failed to get request data', xhr.status)
        reject(xhr.status)
      }
    }

    xhr.onerror = () => {
      console.error('Request error')
      reject('Network error')
    }

    xhr.send()
  })
}
