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

620 lines
26 KiB
JavaScript
Raw Normal View History

2024-06-18 18:10:10 +00:00
import {createStore} from 'vuex';
import router from './router';
2019-11-13 21:40:14 +00:00
2019-12-28 03:05:58 +00:00
import * as base64 from 'base-64';
import * as utf8 from 'utf8';
2024-11-16 23:16:54 +00:00
import {ticketStateColorLookup, ticketStateIconLookup, http, http_session} from "@/utils";
2024-06-22 23:04:32 +00:00
import sharedStatePlugin from "@/shared-state-plugin";
2024-06-22 18:56:51 +00:00
import persistentStatePlugin from "@/persistent-state-plugin";
2024-06-18 18:10:10 +00:00
const store = createStore({
2023-11-27 00:14:52 +00:00
state: {
keyIncrement: 0,
events: [],
2024-11-16 23:16:54 +00:00
items: [],
2023-11-27 00:14:52 +00:00
loadedBoxes: [],
toasts: [],
2024-01-07 20:38:25 +00:00
users: [],
groups: [],
2024-06-18 18:10:10 +00:00
state_options: [],
2024-06-26 16:42:56 +00:00
messageTemplates: [],
messageTemplateVariables: [],
shippingVouchers: [],
2024-11-16 23:16:54 +00:00
loadedItems: {},
loadedTickets: {},
2024-11-13 22:51:54 +00:00
lastEvent: 'all',
2024-06-22 23:04:32 +00:00
lastUsed: {},
searchQuery: '',
2024-01-07 20:38:25 +00:00
remember: false,
2024-06-18 18:10:10 +00:00
user: {
username: null,
password: null,
permissions: [],
token: null,
expiry: null,
},
thumbnailCache: {},
2024-06-22 23:04:32 +00:00
fetchedData: {
events: 0,
items: 0,
boxes: 0,
tickets: 0,
users: 0,
groups: 0,
states: 0,
2024-06-26 16:42:56 +00:00
messageTemplates: 0,
shippingVouchers: 0,
2024-06-22 23:04:32 +00:00
},
2024-06-22 18:56:51 +00:00
persistent_loaded: false,
2024-06-22 23:04:32 +00:00
shared_loaded: false,
afterInitHandlers: [],
showAddBoxModal: false,
2024-11-02 21:12:00 +00:00
showAddEventModal: false,
2024-06-26 16:42:56 +00:00
test: ['foo', 'bar', 'baz'],
shippingVoucherTypes: {
'2kg-de': '2kg Paket (DE)',
'5kg-de': '5kg Paket (DE)',
'10kg-de': '10kg Paket (DE)',
'2kg-eu': '2kg Paket (EU)',
'5kg-eu': '5kg Paket (EU)',
'10kg-eu': '10kg Paket (EU)',
}
2023-11-27 00:14:52 +00:00
},
getters: {
2024-06-18 18:10:10 +00:00
route: state => router.currentRoute.value,
2024-11-16 23:16:54 +00:00
session: state => http_session(state.user.token),
2024-06-18 18:10:10 +00:00
getEventSlug: state => router.currentRoute.value.params.event ? router.currentRoute.value.params.event : state.lastEvent,
2024-11-16 23:16:54 +00:00
getAllItems: state => Object.values(state.loadedItems).flat(),
getAllTickets: state => Object.values(state.loadedTickets).flat(),
getEventItems: (state, getters) => getters.getEventSlug === 'all' ? getters.getAllItems : getters.getAllItems.filter(t => t.event === getters.getEventSlug || (t.event == null && getters.getEventSlug === 'none')),
getEventTickets: (state, getters) => getters.getEventSlug === 'all' ? getters.getAllTickets : getters.getAllTickets.filter(t => t.event === getters.getEventSlug || (t.event == null && getters.getEventSlug === 'none')),
isItemsLoaded: (state, getters) => (getters.getEventSlug === 'all' || getters.getEventSlug === 'none') ? !!state.loadedItems : Object.keys(state.loadedItems).includes(getters.getEventSlug),
isTicketsLoaded: (state, getters) => (getters.getEventSlug === 'all' || getters.getEventSlug === 'none') ? !!state.loadedTickets : Object.keys(state.loadedTickets).includes(getters.getEventSlug),
2024-06-18 18:10:10 +00:00
getActiveView: state => router.currentRoute.value.name || 'items',
getFilters: state => router.currentRoute.value.query,
2024-01-07 20:38:25 +00:00
getBoxes: state => state.loadedBoxes,
2024-06-22 18:56:51 +00:00
checkPermission: state => (event, perm) => state.user.permissions &&
(state.user.permissions.includes(`${event}:${perm}`) || state.user.permissions.includes(`*:${perm}`)),
hasPermissions: state => state.user.permissions && state.user.permissions.length > 0,
activeUser: state => state.user.username || 'anonymous',
2024-01-02 16:46:34 +00:00
stateInfo: state => (slug) => {
const obj = state.state_options.filter((s) => s.value === slug)[0];
if (obj) {
return {
color: ticketStateColorLookup(obj.value),
icon: ticketStateIconLookup(obj.value),
slug: obj.value,
text: obj.text,
}
} else {
return {
color: 'danger',
icon: 'exclamation',
slug: slug,
text: 'Unknown'
}
}
},
availableShippingVoucherTypes: state => {
return Object.keys(state.shippingVoucherTypes).map(key => {
var count = state.shippingVouchers.filter(voucher => voucher.type === key && voucher.issue_thread === null).length;
return {id: key, count: count, name: state.shippingVoucherTypes[key]};
});
},
layout: (state, getters) => {
2024-06-18 18:10:10 +00:00
if (router.currentRoute.value.query.layout)
return router.currentRoute.value.query.layout;
if (getters.getActiveView === 'items')
return 'cards';
if (getters.getActiveView === 'tickets')
return 'tasks';
},
2024-01-07 20:38:25 +00:00
isLoggedIn(state) {
2024-06-18 18:10:10 +00:00
return state.user && state.user.username !== null && state.user.token !== null;
2024-01-07 20:38:25 +00:00
},
getThumbnail: (state) => (url) => {
if (!url) return null;
if (!(url in state.thumbnailCache))
return null;
return state.thumbnailCache[url];
},
2023-11-27 00:14:52 +00:00
},
mutations: {
2024-06-22 23:04:32 +00:00
updateLastUsed(state, diff) {
state.lastUsed = {...state.lastUsed, ...diff};
},
2024-01-07 20:38:25 +00:00
updateLastEvent(state, slug) {
state.lastEvent = slug;
2023-11-27 00:14:52 +00:00
},
replaceEvents(state, events) {
state.events = events;
2024-06-22 23:04:32 +00:00
state.fetchedData = {...state.fetchedData, events: Date.now()};
2023-11-27 00:14:52 +00:00
},
replaceTicketStates(state, states) {
state.state_options = states;
2024-06-22 23:04:32 +00:00
state.fetchedData = {...state.fetchedData, states: Date.now()};
},
2023-11-27 00:14:52 +00:00
changeView(state, {view, slug}) {
router.push({path: `/${slug}/${view}`});
},
replaceBoxes(state, loadedBoxes) {
state.loadedBoxes = loadedBoxes;
2024-06-22 23:04:32 +00:00
state.fetchedData = {...state.fetchedData, boxes: Date.now()};
2023-11-27 00:14:52 +00:00
},
2024-11-16 23:16:54 +00:00
setItems(state, {slug, items}) {
state.loadedItems[slug] = items;
state.loadedItems = {...state.loadedItems};
console.log(state.loadedItems)
},
replaceItems(state, items) {
const groups = Object.groupBy(items, i => i.event ? i.event : 'none')
for (const [key, value] of Object.entries(groups)) state.loadedItems[key] = value;
state.loadedItems = {...state.loadedItems};
console.log(state.loadedItems)
},
2023-11-27 00:14:52 +00:00
updateItem(state, updatedItem) {
2024-06-26 16:42:56 +00:00
const item = state.loadedItems[updatedItem.event ? updatedItem.event : 'none'].filter(
2024-11-16 23:16:54 +00:00
({uid}) => uid === updatedItem.uid)[0];
2023-11-27 00:14:52 +00:00
Object.assign(item, updatedItem);
},
removeItem(state, item) {
2024-06-26 16:42:56 +00:00
state.loadedItems[item.event ? item.event : 'none'] = state.loadedItems[item.event].filter(it => it !== item);
2023-11-27 00:14:52 +00:00
},
appendItem(state, item) {
2024-06-26 16:42:56 +00:00
state.loadedItems[item.event ? item.event : 'none'].push(item);
2024-11-16 23:16:54 +00:00
},
setTickets(state, {slug, tickets}) {
state.loadedTickets[slug] = tickets;
state.loadedTickets = {...state.loadedTickets};
console.log(state.loadedTickets)
2023-11-27 00:14:52 +00:00
},
2024-01-07 20:38:25 +00:00
replaceTickets(state, tickets) {
2024-11-16 23:16:54 +00:00
const groups = Object.groupBy(tickets, t => t.event ? t.event : 'none')
for (const [key, value] of Object.entries(groups)) state.loadedTickets[key] = value;
state.loadedTickets = {...state.loadedTickets};
console.log(state.loadedTickets)
2024-06-22 23:04:32 +00:00
},
updateTicket(state, updatedTicket) {
2024-06-26 16:42:56 +00:00
const ticket = state.loadedTickets[updatedTicket.event ? updatedTicket.event : 'none'].filter(
2024-11-16 23:16:54 +00:00
({id}) => id === updatedTicket.id)[0];
2024-06-22 23:04:32 +00:00
Object.assign(ticket, updatedTicket);
2024-11-16 23:16:54 +00:00
state.loadedTickets = {...state.loadedTickets};
2024-01-07 20:38:25 +00:00
},
replaceUsers(state, users) {
state.users = users;
2024-06-22 23:04:32 +00:00
state.fetchedData = {...state.fetchedData, users: Date.now()};
2024-01-07 20:38:25 +00:00
},
replaceGroups(state, groups) {
state.groups = groups;
2024-06-22 23:04:32 +00:00
state.fetchedData = {...state.fetchedData, groups: Date.now()};
2024-01-07 20:38:25 +00:00
},
2023-12-28 21:49:55 +00:00
openAddBoxModal(state) {
state.showAddBoxModal = true;
},
closeAddBoxModal(state) {
state.showAddBoxModal = false;
},
2024-11-02 21:12:00 +00:00
openAddEventModal(state) {
state.showAddEventModal = true;
},
closeAddEventModal(state) {
state.showAddEventModal = false;
},
2023-11-27 00:14:52 +00:00
createToast(state, {title, message, color}) {
2023-11-27 00:38:43 +00:00
var toast = {title, message, color, key: state.keyIncrement}
state.toasts.push(toast);
2023-11-27 00:14:52 +00:00
state.keyIncrement += 1;
2023-11-27 00:38:43 +00:00
return toast;
2023-11-27 00:14:52 +00:00
},
removeToast(state, key) {
state.toasts = state.toasts.filter(toast => toast.key !== key);
2024-01-07 20:38:25 +00:00
},
setRemember(state, remember) {
state.remember = remember;
},
setUser(state, user) {
2024-06-18 18:10:10 +00:00
state.user.username = user;
2024-01-07 20:38:25 +00:00
},
setPassword(state, password) {
2024-06-18 18:10:10 +00:00
state.user.password = password;
2024-01-07 20:38:25 +00:00
},
setPermissions(state, permissions) {
2024-06-18 18:10:10 +00:00
state.user.permissions = permissions;
2024-01-07 20:38:25 +00:00
},
setToken(state, {token, expiry}) {
2024-06-18 18:10:10 +00:00
const user = {...state.user};
user.token = token;
user.expiry = expiry;
state.user = user;
2024-01-07 20:38:25 +00:00
},
2024-06-18 18:10:10 +00:00
setUserInfo(state, user) {
state.user = user;
},
2024-01-07 20:38:25 +00:00
logout(state) {
2024-06-18 18:10:10 +00:00
const user = {...state.user};
user.user = null;
user.password = null;
user.token = null;
user.expiry = null;
user.permissions = null;
state.user = user;
},
2024-06-26 16:42:56 +00:00
setTest(state, test) {
state.test = test;
},
setThumbnail(state, {url, data}) {
state.thumbnailCache[url] = data;
},
2024-06-26 16:42:56 +00:00
setMessageTemplates(state, templates) {
state.messageTemplates = templates;
state.fetchedData = {...state.fetchedData, messageTemplates: Date.now()};
},
setMessageTemplateVariables(state, variables) {
state.messageTemplateVariables = variables;
},
setShippingVouchers(state, codes) {
state.shippingVouchers = codes;
state.fetchedData = {...state.fetchedData, shippingVouchers: Date.now()};
},
2023-11-27 00:14:52 +00:00
},
actions: {
2024-06-22 18:56:51 +00:00
async login({commit}, {username, password, remember}) {
2024-01-07 20:38:25 +00:00
commit('setRemember', remember);
try {
const data = await fetch('/api/2/login/', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({username: username, password: password}),
credentials: 'omit'
}).then(r => r.json())
2024-01-07 22:03:22 +00:00
if (data && data.token) {
2024-06-22 18:56:51 +00:00
const {data: {permissions}} = await http.get('/2/self/', data.token);
commit('setUserInfo', {...data, permissions, username, password});
2024-01-07 20:38:25 +00:00
return true;
} else {
return false;
}
} catch (e) {
console.error(e);
return false;
}
},
2024-11-16 23:16:54 +00:00
async reloadToken({commit, state}) {
2024-01-07 20:38:25 +00:00
try {
2024-06-18 18:10:10 +00:00
if (state.user.username && state.user.password) {
2024-01-07 20:38:25 +00:00
const data = await fetch('/api/2/login/', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
2024-06-18 18:10:10 +00:00
body: JSON.stringify({username: state.user.username, password: state.user.password}),
2024-01-07 20:38:25 +00:00
credentials: 'omit'
2024-01-07 22:03:22 +00:00
}).then(r => r.json()).catch(e => console.error(e))
if (data && data.token) {
2024-01-07 20:38:25 +00:00
commit('setToken', data);
return true;
}
}
} catch (e) {
console.error(e);
}
//credentials failed, logout
store.commit('logout');
},
//async verifyToken({commit, state}) {
2024-06-18 18:10:10 +00:00
async afterLogin({dispatch, state}) {
let promises = [];
promises.push(dispatch('loadBoxes'));
promises.push(dispatch('fetchTicketStates'));
promises.push(dispatch('loadEventItems'));
promises.push(dispatch('loadTickets'));
if (!state.user.permissions) {
promises.push(dispatch('loadUserInfo'));
}
await Promise.all(promises);
2024-01-07 20:38:25 +00:00
},
2024-06-22 23:04:32 +00:00
async afterSharedInit({dispatch, state}) {
const handlers = state.afterInitHandlers;
state.afterInitHandlers = [];
await Promise.all(handlers.map(h => h()).flat());
},
scheduleAfterInit({dispatch, state}, handler) {
if (state.shared_loaded) {
Promise.all(handler()).then(() => {
});
} else {
state.afterInitHandlers.push(handler);
}
},
2024-01-07 20:38:25 +00:00
async fetchImage({state}, url) {
2024-06-18 18:10:10 +00:00
return await fetch(url, {headers: {'Authorization': `Token ${state.user.token}`}});
2024-01-07 20:38:25 +00:00
},
2024-11-16 23:16:54 +00:00
async loadUserInfo({commit, getters}) {
const {data, success} = await getters.session.get('/2/self/');
2024-01-07 20:38:25 +00:00
commit('setPermissions', data.permissions);
},
2024-11-16 23:16:54 +00:00
async loadEvents({commit, state, getters}) {
2024-06-22 23:04:32 +00:00
if (!state.user.token) return;
if (state.fetchedData.events > Date.now() - 1000 * 60 * 60 * 24) return;
2024-11-16 23:16:54 +00:00
const {data, success} = await getters.session.get('/2/events/');
if (data && success) commit('replaceEvents', data);
2023-11-27 00:14:52 +00:00
},
2024-11-16 23:16:54 +00:00
async createEvent({commit, dispatch, state, getters}, event) {
const {data, success} = await getters.session.post('/2/events/', event);
if (data && success) commit('replaceEvents', [...state.events, data]);
2024-11-02 21:12:00 +00:00
},
2024-11-16 23:16:54 +00:00
async deleteEvent({commit, dispatch, state, getters}, event_id) {
const {data, success} = await getters.session.delete(`/2/events/${event_id}/`);
2024-11-02 21:12:00 +00:00
if (success) {
await dispatch('loadEvents')
commit('replaceEvents', [...state.events.filter(e => e.eid !== event_id)])
}
},
2024-11-06 20:11:18 +00:00
async updateEvent({commit, dispatch, state}, {id, partial_event}){
console.log(id, partial_event);
const {data, success} = await http.patch(`/2/events/${id}/`, partial_event, state.user.token);
if (success) {
commit('replaceEvents', [...state.events.filter(e => e.eid !== id), data])
}
},
2024-11-16 23:16:54 +00:00
async fetchTicketStates({commit, state, getters}) {
2024-06-22 23:04:32 +00:00
if (!state.user.token) return;
if (state.fetchedData.states > Date.now() - 1000 * 60 * 60 * 24) return;
2024-11-16 23:16:54 +00:00
const {data, success} = await getters.session.get('/2/tickets/states/');
if (data && success) commit('replaceTicketStates', data);
},
2024-11-16 23:16:54 +00:00
async changeEvent({dispatch, getters, commit}, eventName) {
await router.push({path: `/${eventName.slug}/${getters.getActiveView}/`});
//dispatch('loadEventItems');
2023-11-27 00:14:52 +00:00
},
2024-11-16 23:16:54 +00:00
async changeView({getters}, link) {
await router.push({path: `/${getters.getEventSlug}/${link.path}/`});
2023-11-27 00:14:52 +00:00
},
2024-11-16 23:16:54 +00:00
async showBoxContent({getters}, box) {
await router.push({path: `/${getters.getEventSlug}/items/`, query: {box}});
2023-11-27 00:14:52 +00:00
},
2024-01-07 20:38:25 +00:00
async loadEventItems({commit, getters, state}) {
2024-06-22 23:04:32 +00:00
if (!state.user.token) return;
if (state.fetchedData.items > Date.now() - 1000 * 60 * 60 * 24) return;
2024-01-07 20:38:25 +00:00
try {
const slug = getters.getEventSlug;
2024-11-16 23:16:54 +00:00
const {data, success} = await getters.session.get(`/2/${slug}/items/`);
2024-06-18 18:10:10 +00:00
if (data && success) {
2024-11-16 23:16:54 +00:00
commit('setItems', {slug, items: data});
2024-06-18 18:10:10 +00:00
}
2024-01-07 20:38:25 +00:00
} catch (e) {
console.error("Error loading items");
}
2023-11-27 00:14:52 +00:00
},
2024-06-18 18:10:10 +00:00
async searchEventItems({commit, getters, state}, query) {
const encoded_query = base64.encode(utf8.encode(query));
2024-11-16 23:16:54 +00:00
const slug = getters.getEventSlug;
const {
data, success
} = await getters.session.get(`/2/${slug}/items/${encoded_query}/`);
if (data && success) {
commit('setItems', {slug, items: data});
}
2023-11-27 00:14:52 +00:00
},
2024-11-16 23:16:54 +00:00
async loadBoxes({commit, state, getters}) {
2024-06-22 23:04:32 +00:00
if (!state.user.token) return;
if (state.fetchedData.boxes > Date.now() - 1000 * 60 * 60 * 24) return;
2024-11-16 23:16:54 +00:00
const {data, success} = await getters.session.get('/2/boxes/');
if (data && success) commit('replaceBoxes', data);
2023-11-27 00:14:52 +00:00
},
2024-11-16 23:16:54 +00:00
async createBox({commit, dispatch, state, getters}, box) {
const {data, success} = await getters.session.post('/2/boxes/', box);
2023-12-28 21:49:55 +00:00
commit('replaceBoxes', data);
dispatch('loadBoxes').then(() => {
commit('closeAddBoxModal');
});
},
2024-11-16 23:16:54 +00:00
async deleteBox({commit, dispatch, state, getters}, box_id) {
await getters.session.delete(`/2/boxes/${box_id}/`);
dispatch('loadBoxes');
},
2024-06-18 18:10:10 +00:00
async updateItem({commit, getters, state}, item) {
const {
2024-11-16 23:16:54 +00:00
data, success
} = await getters.session.put(`/2/${getters.getEventSlug}/item/${item.uid}/`, item);
2023-11-27 00:14:52 +00:00
commit('updateItem', data);
},
2024-06-18 18:10:10 +00:00
async markItemReturned({commit, getters, state}, item) {
2024-11-16 23:16:54 +00:00
await getters.session.patch(`/2/${getters.getEventSlug}/item/${item.uid}/`, {returned: true},
state.user.token);
2023-11-27 00:14:52 +00:00
commit('removeItem', item);
},
2024-06-18 18:10:10 +00:00
async deleteItem({commit, getters, state}, item) {
2024-11-16 23:16:54 +00:00
await getters.session.delete(`/2/${getters.getEventSlug}/item/${item.uid}/`, item);
2023-11-27 00:14:52 +00:00
commit('removeItem', item);
},
2024-06-18 18:10:10 +00:00
async postItem({commit, getters, state}, item) {
2023-11-27 00:14:52 +00:00
commit('updateLastUsed', {box: item.box, cid: item.cid});
2024-11-16 23:16:54 +00:00
const {data, success} = await getters.session.post(`/2/${getters.getEventSlug}/item/`, item);
2023-11-27 00:14:52 +00:00
commit('appendItem', data);
2024-01-07 20:38:25 +00:00
},
2024-11-16 23:16:54 +00:00
async loadTickets({commit, state, getters}) {
2024-06-22 23:04:32 +00:00
if (!state.user.token) return;
2024-11-16 23:16:54 +00:00
//if (state.fetchedData.tickets > Date.now() - 1000 * 60 * 60 * 24) return;
const {data, success} = await getters.session.get('/2/tickets/');
if (data && success) commit('replaceTickets', data);
2024-01-07 20:38:25 +00:00
},
async searchEventTickets({commit, getters, state}, query) {
const encoded_query = base64.encode(utf8.encode(query));
2024-11-16 23:16:54 +00:00
const {
data, success
} = await getters.session.get(`/2/${getters.getEventSlug}/tickets/${encoded_query}/`);
if (data && success) commit('replaceTickets', data);
},
2024-11-16 23:16:54 +00:00
async sendMail({commit, dispatch, state, getters}, {id, message}) {
const {data, success} = await getters.session.post(`/2/tickets/${id}/reply/`, {message},
state.user.token);
2024-06-18 18:10:10 +00:00
if (data && success) {
2024-06-22 23:04:32 +00:00
state.fetchedData.tickets = 0;
2024-06-18 18:10:10 +00:00
await dispatch('loadTickets');
}
2024-01-07 20:38:25 +00:00
},
2024-11-16 23:16:54 +00:00
async postManualTicket({commit, dispatch, state, getters}, {sender, message, title,}) {
const {data, success} = await getters.session.post(`/2/tickets/manual/`, {
name: title, sender, body: message, recipient: 'mail@c3lf.de'
});
2024-01-07 20:38:25 +00:00
await dispatch('loadTickets');
},
2024-11-16 23:16:54 +00:00
async postComment({commit, dispatch, state, getters}, {id, message}) {
const {data, success} = await getters.session.post(`/2/tickets/${id}/comment/`, {comment: message});
2024-06-18 18:10:10 +00:00
if (data && success) {
2024-06-22 23:04:32 +00:00
state.fetchedData.tickets = 0;
2024-06-18 18:10:10 +00:00
await dispatch('loadTickets');
}
},
2024-11-16 23:16:54 +00:00
async loadUsers({commit, state, getters}) {
2024-06-22 23:04:32 +00:00
if (!state.user.token) return;
if (state.fetchedData.users > Date.now() - 1000 * 60 * 60 * 24) return;
2024-11-16 23:16:54 +00:00
const {data, success} = await getters.session.get('/2/users/');
if (data && success) commit('replaceUsers', data);
2024-01-07 20:38:25 +00:00
},
2024-11-16 23:16:54 +00:00
async loadGroups({commit, state, getters}) {
2024-06-22 23:04:32 +00:00
if (!state.user.token) return;
if (state.fetchedData.groups > Date.now() - 1000 * 60 * 60 * 24) return;
2024-11-16 23:16:54 +00:00
const {data, success} = await getters.session.get('/2/groups/');
if (data && success) commit('replaceGroups', data);
2024-01-07 20:38:25 +00:00
},
2024-11-16 23:16:54 +00:00
async updateTicket({commit, state, getters}, ticket) {
const {data, success} = await getters.session.put(`/2/tickets/${ticket.id}/`, ticket);
commit('updateTicket', data);
},
2024-11-16 23:16:54 +00:00
async updateTicketPartial({commit, state, getters}, {id, ...ticket}) {
const {data, success} = await getters.session.patch(`/2/tickets/${id}/`, ticket);
commit('updateTicket', data);
},
2024-06-26 16:42:56 +00:00
async fetchMessageTemplates({commit, state}) {
if (!state.user.token) return;
if (state.messageTemplates.length > 0) return;
const {data, success} = await http.get('/2/message_templates/', state.user.token);
if (data && success) {
commit('setMessageTemplates', data);
}
},
async updateMessageTemplate({dispatch, state}, template) {
const {data, success} = await http.patch(`/2/message_templates/${template.id}/`,
{'message': template.message}, state.user.token);
if (data && success) {
state.fetchedData.messageTemplates = 0;
dispatch('fetchMessageTemplates');
}
},
async fetchMessageTemplateVariables({commit, state}) {
if (!state.user.token) return;
if (state.messageTemplateVariables.length > 0) return;
const {data, success} = await http.get('/2/message_template_variables/', state.user.token);
if (data && success) {
commit('setMessageTemplateVariables', data);
}
},
async createMessageTemplate({dispatch, state}, template_name) {
const {data, success} = await http.post('/2/message_templates/', {
name: template_name,
message: '-'
}, state.user.token);
if (data && success) {
dispatch('fetchMessageTemplates');
}
},
2024-11-16 23:16:54 +00:00
async fetchShippingVouchers({commit, state, getters}) {
if (!state.user.token) return;
if (state.fetchedData.shippingVouchers > Date.now() - 1000 * 60 * 60 * 24) return;
2024-11-16 23:16:54 +00:00
const {data, success} = await getters.session.get('/2/shipping_vouchers/');
if (data && success) {
commit('setShippingVouchers', data);
}
},
2024-11-16 23:16:54 +00:00
async createShippingVoucher({dispatch, state, getters}, code) {
const {data, success} = await getters.session.post('/2/shipping_vouchers/', code);
if (data && success) {
state.fetchedData.shippingVouchers = 0;
dispatch('fetchShippingVouchers');
}
},
2024-11-16 23:16:54 +00:00
async claimShippingVoucher({dispatch, state, getters}, {ticket, shipping_voucher_type}) {
const id = state.shippingVouchers.filter(voucher => voucher.type === shipping_voucher_type && voucher.issue_thread === null)[0].id;
2024-11-16 23:16:54 +00:00
const {data, success} = await getters.session.patch(`/2/shipping_vouchers/${id}/`, {issue_thread: ticket});
if (data && success) {
state.fetchedData.shippingVouchers = 0;
state.fetchedData.tickets = 0;
await Promise.all([dispatch('loadTickets'), dispatch('fetchShippingVouchers')]);
}
}
2024-01-14 15:01:32 +00:00
},
2024-06-26 16:42:56 +00:00
plugins: [
persistentStatePlugin({ // TODO change remember to some kind of enable field
prefix: "lf_",
debug: false,
isLoadedKey: "persistent_loaded",
state: [
"remember",
"user",
"events",
"lastUsed",
]
}),
sharedStatePlugin({
debug: false,
isLoadedKey: "shared_loaded",
clearingMutation: "logout",
afterInit: "afterSharedInit",
state: [
"test",
"state_options",
"fetchedData",
"loadedItems",
"users",
"groups",
"loadedBoxes",
"loadedTickets",
"messageTemplates",
"messageTemplatesVariables",
"shippingVouchers",
],
watch: [
"test",
"state_options",
"fetchedData",
"loadedItems",
"users",
"groups",
"loadedBoxes",
"loadedTickets",
"messageTemplates",
"messageTemplatesVariables",
"shippingVouchers",
],
mutations: [
//"replaceTickets",
],
}),
],
2019-11-13 21:40:14 +00:00
});
2024-06-18 18:10:10 +00:00
store.watch((state) => state.user, (user) => {
2024-01-07 20:38:25 +00:00
if (store.getters.isLoggedIn) {
2024-11-16 23:16:54 +00:00
if (router.currentRoute.value.name === 'login' && router.currentRoute.value.query.redirect) {
2024-06-18 18:10:10 +00:00
router.push(router.currentRoute.value.query.redirect);
2024-11-16 23:16:54 +00:00
} else if (router.currentRoute.value.name === 'login') {
2024-06-18 18:10:10 +00:00
router.push('/');
2024-11-16 23:16:54 +00:00
}
2024-06-18 18:10:10 +00:00
} else {
if (router.currentRoute.value.name !== 'login') {
router.push({
name: 'login',
query: {redirect: router.currentRoute.value.fullPath},
});
}
2024-01-07 20:38:25 +00:00
}
2019-12-27 01:06:04 +00:00
});
2024-06-18 18:10:10 +00:00
export default store;