c3lf-system-3/web/src/utils.js
jedi 598f758332
All checks were successful
/ deploy (push) Successful in 4m33s
/ test (push) Successful in 2m56s
reduce shadow logouts, by actually logging out when token fails or is expired
2024-12-24 15:56:24 +01:00

138 lines
No EOL
4.5 KiB
JavaScript

import store from '@/store'
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';
}
const http = {
get: async (url, token) => {
if (!token) {
return null;
}
const response = await fetch('/api' + url, {
method: 'GET',
headers: {
"Content-Type": "application/json",
"Authorization": `Token ${token}`,
},
});
if (response.status === 401)
throw {http_status: response.status};
const success = response.status === 200 || response.status === 201;
return {data: await response.json() || {}, success};
},
post: async (url, data, token) => {
if (!token) {
return null;
}
const response = await fetch('/api' + url, {
method: 'POST',
headers: {
"Content-Type": "application/json",
"Authorization": `Token ${token}`,
},
body: JSON.stringify(data),
});
if (response.status === 401)
throw {http_status: response.status};
const success = response.status === 200 || response.status === 201;
return {data: await response.json() || {}, success};
},
put: async (url, data, token) => {
if (!token) {
return null;
}
const response = await fetch('/api' + url, {
method: 'PUT',
headers: {
"Content-Type": "application/json",
"Authorization": `Token ${token}`,
},
body: JSON.stringify(data),
});
if (response.status === 401)
throw {http_status: response.status};
const success = response.status === 200 || response.status === 201;
return {data: await response.json() || {}, success};
},
patch: async (url, data, token) => {
if (!token) {
return null;
}
const response = await fetch('/api' + url, {
method: 'PATCH',
headers: {
"Content-Type": "application/json",
"Authorization": `Token ${token}`,
},
body: JSON.stringify(data),
});
if (response.status === 401)
throw {http_status: response.status};
const success = response.status === 200 || response.status === 201;
return {data: await response.json() || {}, success};
},
delete: async (url, token) => {
if (!token) {
return null;
}
const response = await fetch('/api' + url, {
method: 'DELETE',
headers: {
"Content-Type": "application/json",
"Authorization": `Token ${token}`,
},
});
if (response.status === 401)
throw {http_status: response.status};
const success = response.status === 204;
return {data: await response.text() || {}, success};
}
}
const http_session = token => ({
get: async (url) => await http.get(url, token).catch((e) => {
if (e.http_status === 401) store.commit('logout');
return {data: {}, success: false};
}),
post: async (url, data) => await http.post(url, data, token).catch((e) => {
if (e.http_status === 401) store.commit('logout');
return {data: {}, success: false};
}),
put: async (url, data) => await http.put(url, data, token).catch((e) => {
if (e.http_status === 401) store.commit('logout');
return {data: {}, success: false};
}),
patch: async (url, data) => await http.patch(url, data, token).catch((e) => {
if (e.http_status === 401) store.commit('logout');
return {data: {}, success: false};
}),
delete: async (url) => await http.delete(url, token).catch((e) => {
if (e.http_status === 401) store.commit('logout');
return {data: {}, success: false};
}),
});
export {ticketStateColorLookup, ticketStateIconLookup, http, http_session};