Basic user management
This commit is contained in:
80
resources/js/components/ajaxHandler.js
Normal file
80
resources/js/components/ajaxHandler.js
Normal file
@@ -0,0 +1,80 @@
|
||||
import { ref } from "vue"
|
||||
|
||||
export function useAjax() {
|
||||
const loading = ref(false)
|
||||
const error = ref(null)
|
||||
const data = ref(null)
|
||||
|
||||
async function request(url, options = {}) {
|
||||
loading.value = true
|
||||
error.value = null
|
||||
data.value = null
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
method: options.method || "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...(options.headers || {}),
|
||||
},
|
||||
body: options.body ? JSON.stringify(options.body) : null,
|
||||
})
|
||||
|
||||
if (!response.ok) throw new Error(`HTTP ${response.status}`)
|
||||
const result = await response.json()
|
||||
data.value = result
|
||||
return result
|
||||
} catch (err) {
|
||||
error.value = err
|
||||
console.error("AJAX Error:", err)
|
||||
return null
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function download(url, options = {}) {
|
||||
loading.value = true
|
||||
error.value = null
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
method: options.method || "GET",
|
||||
headers: {
|
||||
...(options.headers || {}),
|
||||
},
|
||||
body: options.body ? JSON.stringify(options.body) : null,
|
||||
})
|
||||
|
||||
if (!response.ok) throw new Error(`HTTP ${response.status}`)
|
||||
|
||||
const blob = await response.blob()
|
||||
const filename =
|
||||
options.filename ||
|
||||
response.headers
|
||||
.get("Content-Disposition")
|
||||
?.split("filename=")[1]
|
||||
?.replace(/["']/g, "") ||
|
||||
"download.bin"
|
||||
|
||||
const downloadUrl = window.URL.createObjectURL(blob)
|
||||
const a = document.createElement("a")
|
||||
a.href = downloadUrl
|
||||
a.download = filename
|
||||
document.body.appendChild(a)
|
||||
a.click()
|
||||
a.remove()
|
||||
window.URL.revokeObjectURL(downloadUrl)
|
||||
|
||||
return true
|
||||
} catch (err) {
|
||||
error.value = err
|
||||
console.error("Download Error:", err)
|
||||
return false
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
return {data, loading, error, request, download}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<link rel="stylesheet" href="css/app.css" />
|
||||
<link rel="stylesheet" href="css/elements.css" />
|
||||
<link rel="stylesheet" href="/css/app.css" />
|
||||
<link rel="stylesheet" href="/css/elements.css" />
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<meta name="csrf-token" content="{{ csrf_token() }}">
|
||||
|
||||
Reference in New Issue
Block a user