c3lf-system-3/web/src/utils.js

103 lines
3.1 KiB
JavaScript
Raw Normal View History

2024-01-02 16:46:34 +00:00
function ticketStateColorLookup(ticket) {
if (ticket.startsWith('closed_')) {
return 'secondary';
}
if (ticket.startsWith('pending_')) {
return 'warning';
}
if (ticket.startsWith('waiting_')) {
return 'primary';
}
return 'danger';
}
function ticketStateIconLookup(ticket) {
if (ticket.startsWith('closed_')) {
return 'check';
}
if (ticket.startsWith('pending_')) {
return 'exclamation';
}
if (ticket.startsWith('waiting_')) {
return 'hourglass';
}
return 'exclamation';
}
2024-01-26 00:03:00 +00:00
const http = {
2024-01-31 21:51:01 +00:00
get: async (url, token) => {
2024-01-26 00:03:00 +00:00
if (!token) {
return null;
}
const response = await fetch('/api' + url, {
2024-01-31 21:51:01 +00:00
method: 'GET',
2024-01-26 00:03:00 +00:00
headers: {
"Content-Type": "application/json",
2024-01-31 21:51:01 +00:00
"Authorization": `Token ${token}`,
2024-01-26 00:03:00 +00:00
},
});
2024-01-31 21:51:01 +00:00
const success = response.status === 200 || response.status === 201;
return {data: await response.json() || {}, success};
},
post: async (url, data, token) => {
2024-01-26 00:03:00 +00:00
if (!token) {
return null;
}
const response = await fetch('/api' + url, {
2024-01-31 21:51:01 +00:00
method: 'POST',
2024-01-26 00:03:00 +00:00
headers: {
"Content-Type": "application/json",
2024-02-02 23:49:15 +00:00
"Authorization": `Token ${token}`,
2024-01-26 00:03:00 +00:00
},
2024-01-31 21:51:01 +00:00
body: JSON.stringify(data),
2024-01-26 00:03:00 +00:00
});
2024-01-31 21:51:01 +00:00
const success = response.status === 200 || response.status === 201;
return {data: await response.json() || {}, success};
2024-01-26 00:03:00 +00:00
},
put: async (url, data, token) => {
if (!token) {
return null;
}
const response = await fetch('/api' + url, {
method: 'PUT',
headers: {
"Content-Type": "application/json",
2024-02-02 23:49:15 +00:00
"Authorization": `Token ${token}`,
2024-01-26 00:03:00 +00:00
},
body: JSON.stringify(data),
});
2024-01-31 21:51:01 +00:00
const success = response.status === 200 || response.status === 201;
return {data: await response.json() || {}, success};
2024-01-26 00:03:00 +00:00
},
patch: async (url, data, token) => {
if (!token) {
return null;
}
const response = await fetch('/api' + url, {
method: 'PATCH',
headers: {
"Content-Type": "application/json",
2024-02-02 23:49:15 +00:00
"Authorization": `Token ${token}`,
2024-01-26 00:03:00 +00:00
},
body: JSON.stringify(data),
});
2024-01-31 21:51:01 +00:00
const success = response.status === 200 || response.status === 201;
return {data: await response.json() || {}, success};
2024-01-26 00:03:00 +00:00
},
delete: async (url, token) => {
if (!token) {
return null;
}
const response = await fetch('/api' + url, {
method: 'DELETE',
headers: {
"Content-Type": "application/json",
"Authorization": `Token ${token}`,
},
});
2024-01-31 21:51:01 +00:00
const success = response.status === 200 || response.status === 201;
return {data: await response.json() || {}, success};
2024-01-26 00:03:00 +00:00
}
}
export {ticketStateColorLookup, ticketStateIconLookup, http};