mirror of
https://github.com/ClusterCockpit/cc-backend
synced 2025-04-03 18:55:55 +02:00
- Adapt to new metricConfig logic - Footprint-Metrics generalized for bar card - Footprint-Metrics in stats filter and sorting - Frontend always uses GQL, except adminOptions - Job View will load scopes for all metrics on request
130 lines
3.5 KiB
Svelte
130 lines
3.5 KiB
Svelte
<script>
|
|
import { Card, CardTitle, CardBody } from "@sveltestrap/sveltestrap";
|
|
import { createEventDispatcher } from "svelte";
|
|
import { fade } from "svelte/transition";
|
|
|
|
const dispatch = createEventDispatcher();
|
|
|
|
let message = { msg: "", color: "#d63384" };
|
|
let displayMessage = false;
|
|
|
|
export let roles = [];
|
|
|
|
async function handleAddRole() {
|
|
const username = document.querySelector("#role-username").value;
|
|
const role = document.querySelector("#role-select").value;
|
|
|
|
if (username == "" || role == "") {
|
|
alert("Please fill in a username and select a role.");
|
|
return;
|
|
}
|
|
|
|
let formData = new FormData();
|
|
formData.append("username", username);
|
|
formData.append("add-role", role);
|
|
|
|
try {
|
|
const res = await fetch(`/config/user/${username}`, {
|
|
method: "POST",
|
|
body: formData,
|
|
});
|
|
if (res.ok) {
|
|
let text = await res.text();
|
|
popMessage(text, "#048109");
|
|
reloadUserList();
|
|
} else {
|
|
let text = await res.text();
|
|
throw new Error("Response Code " + res.status + "-> " + text);
|
|
}
|
|
} catch (err) {
|
|
popMessage(err, "#d63384");
|
|
}
|
|
}
|
|
|
|
async function handleRemoveRole() {
|
|
const username = document.querySelector("#role-username").value;
|
|
const role = document.querySelector("#role-select").value;
|
|
|
|
if (username == "" || role == "") {
|
|
alert("Please fill in a username and select a role.");
|
|
return;
|
|
}
|
|
|
|
let formData = new FormData();
|
|
formData.append("username", username);
|
|
formData.append("remove-role", role);
|
|
|
|
try {
|
|
const res = await fetch(`/config/user/${username}`, {
|
|
method: "POST",
|
|
body: formData,
|
|
});
|
|
if (res.ok) {
|
|
let text = await res.text();
|
|
popMessage(text, "#048109");
|
|
reloadUserList();
|
|
} else {
|
|
let text = await res.text();
|
|
throw new Error("Response Code " + res.status + "-> " + text);
|
|
}
|
|
} catch (err) {
|
|
popMessage(err, "#d63384");
|
|
}
|
|
}
|
|
|
|
function popMessage(response, rescolor) {
|
|
message = { msg: response, color: rescolor };
|
|
displayMessage = true;
|
|
setTimeout(function () {
|
|
displayMessage = false;
|
|
}, 3500);
|
|
}
|
|
|
|
function reloadUserList() {
|
|
dispatch("reload");
|
|
}
|
|
</script>
|
|
|
|
<Card>
|
|
<CardBody>
|
|
<CardTitle class="mb-3">Edit User Roles</CardTitle>
|
|
<div class="input-group mb-3">
|
|
<input
|
|
type="text"
|
|
class="form-control"
|
|
placeholder="username"
|
|
id="role-username"
|
|
/>
|
|
<select class="form-select" id="role-select">
|
|
<option selected value="">Role...</option>
|
|
{#each roles as role}
|
|
<option value={role}
|
|
>{role.charAt(0).toUpperCase() + role.slice(1)}</option
|
|
>
|
|
{/each}
|
|
</select>
|
|
<!-- PreventDefault on Sveltestrap-Button more complex to achieve than just use good ol' html button -->
|
|
<!-- see: https://stackoverflow.com/questions/69630422/svelte-how-to-use-event-modifiers-in-my-own-components -->
|
|
<button
|
|
class="btn btn-primary"
|
|
type="button"
|
|
id="add-role-button"
|
|
on:click|preventDefault={handleAddRole}>Add</button
|
|
>
|
|
<button
|
|
class="btn btn-danger"
|
|
type="button"
|
|
id="remove-role-button"
|
|
on:click|preventDefault={handleRemoveRole}>Remove</button
|
|
>
|
|
</div>
|
|
<p>
|
|
{#if displayMessage}<b
|
|
><code style="color: {message.color};" out:fade
|
|
>Update: {message.msg}</code
|
|
></b
|
|
>{/if}
|
|
</p>
|
|
</CardBody>
|
|
</Card>
|