Files
milsim-site-v4/ui/src/api/roles.ts
2025-12-17 10:38:07 -05:00

101 lines
2.4 KiB
TypeScript

export type Role = {
id: number;
name: string;
color: string;
description: string | null;
members: any[];
};
// @ts-ignore
const addr = import.meta.env.VITE_APIHOST;
export async function getRoles(): Promise<Role[]> {
const res = await fetch(`${addr}/roles`, {
credentials: 'include',
})
if (res.ok) {
return res.json() as Promise<Role[]>;
} else {
console.error("Something went wrong")
return [];
}
}
export async function createRole(name: string, color: string, description: string | null): Promise<Role | null> {
const res = await fetch(`${addr}/roles`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
credentials: 'include',
body: JSON.stringify({
name,
color,
description
}),
});
if (res.ok) {
return res.json() as Promise<Role>;
} else {
console.error("Something went wrong creating the role");
return null;
}
}
export async function addMemberToRole(member_id: number, role_id: number): Promise<boolean> {
const res = await fetch(`${addr}/memberRoles`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
credentials: 'include',
body: JSON.stringify({
member_id,
role_id
})
});
if (res.ok) {
return true;
} else {
console.error("Something went wrong adding the member to the role");
return false;
}
}
export async function removeMemberFromRole(member_id: number, role_id: number): Promise<boolean> {
const res = await fetch(`${addr}/memberRoles`, {
method: "DELETE",
credentials: 'include',
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
member_id,
role_id
})
});
if (res.ok) {
return true;
} else {
console.error("Something went wrong removing the member from the role");
return false;
}
}
export async function deleteRole(role_id: number): Promise<boolean> {
const res = await fetch(`${addr}/roles/${role_id}`, {
method: "DELETE",
credentials: 'include',
});
if (res.ok) {
return true;
} else {
console.error("Something went wrong deleting the role");
return false;
}
}