This commit is contained in:
j3d1 2024-06-18 18:01:55 +02:00
parent a2633bff7a
commit f355639d88
2 changed files with 17 additions and 2 deletions

View file

@ -43,6 +43,8 @@ const store = createStore({
users: 0, users: 0,
groups: 0, groups: 0,
states: 0, states: 0,
messageTemplates: 0,
shippingVouchers: 0,
}, },
persistent_loaded: false, persistent_loaded: false,
shared_loaded: false, shared_loaded: false,
@ -221,12 +223,14 @@ const store = createStore({
}, },
setMessageTemplates(state, templates) { setMessageTemplates(state, templates) {
state.messageTemplates = templates; state.messageTemplates = templates;
state.fetchedData = {...state.fetchedData, messageTemplates: Date.now()};
}, },
setMessageTemplateVariables(state, variables) { setMessageTemplateVariables(state, variables) {
state.messageTemplateVariables = variables; state.messageTemplateVariables = variables;
}, },
setShippingVouchers(state, codes) { setShippingVouchers(state, codes) {
state.shippingVouchers = codes; state.shippingVouchers = codes;
state.fetchedData = {...state.fetchedData, shippingVouchers: Date.now()};
}, },
}, },
actions: { actions: {
@ -444,6 +448,8 @@ const store = createStore({
commit('updateTicket', data); commit('updateTicket', data);
}, },
async fetchMessageTemplates({commit, state}) { 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); const {data, success} = await http.get('/2/message_templates/', state.user.token);
if (data && success) { if (data && success) {
commit('setMessageTemplates', data); commit('setMessageTemplates', data);
@ -453,10 +459,13 @@ const store = createStore({
const {data, success} = await http.patch(`/2/message_templates/${template.id}/`, const {data, success} = await http.patch(`/2/message_templates/${template.id}/`,
{'message': template.message}, state.user.token); {'message': template.message}, state.user.token);
if (data && success) { if (data && success) {
state.fetchedData.messageTemplates = 0;
dispatch('fetchMessageTemplates'); dispatch('fetchMessageTemplates');
} }
}, },
async fetchMessageTemplateVariables({commit, state}) { 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); const {data, success} = await http.get('/2/message_template_variables/', state.user.token);
if (data && success) { if (data && success) {
commit('setMessageTemplateVariables', data); commit('setMessageTemplateVariables', data);
@ -472,6 +481,8 @@ const store = createStore({
} }
}, },
async fetchShippingVouchers({commit, state}) { async fetchShippingVouchers({commit, state}) {
if (!state.user.token) return;
if (state.fetchedData.shippingVouchers > Date.now() - 1000 * 60 * 60 * 24) return;
const {data, success} = await http.get('/2/shipping_vouchers/', state.user.token); const {data, success} = await http.get('/2/shipping_vouchers/', state.user.token);
if (data && success) { if (data && success) {
commit('setShippingVouchers', data); commit('setShippingVouchers', data);
@ -480,6 +491,7 @@ const store = createStore({
async createShippingVoucher({dispatch, state}, code) { async createShippingVoucher({dispatch, state}, code) {
const {data, success} = await http.post('/2/shipping_vouchers/', code, state.user.token); const {data, success} = await http.post('/2/shipping_vouchers/', code, state.user.token);
if (data && success) { if (data && success) {
state.fetchedData.shippingVouchers = 0;
dispatch('fetchShippingVouchers'); dispatch('fetchShippingVouchers');
} }
}, },
@ -490,7 +502,9 @@ const store = createStore({
success success
} = await http.patch(`/2/shipping_vouchers/${id}/`, {issue_thread: ticket}, state.user.token); } = await http.patch(`/2/shipping_vouchers/${id}/`, {issue_thread: ticket}, state.user.token);
if (data && success) { if (data && success) {
dispatch('fetchShippingVouchers'); state.fetchedData.shippingVouchers = 0;
state.fetchedData.tickets = 0;
await Promise.all([dispatch('loadTickets'), dispatch('fetchShippingVouchers')]);
} }
} }
}, },

View file

@ -42,7 +42,8 @@
</option> </option>
</select> </select>
<button class="form-control btn btn-success" <button class="form-control btn btn-success"
@click="claimShippingVoucher({ticket: ticket.id, shipping_voucher_type})"> @click="claimShippingVoucher({ticket: ticket.id, shipping_voucher_type}).then(()=>shipping_voucher_type=null)"
:disabled="!shipping_voucher_type">
Claim&nbsp;Shipping&nbsp;Voucher Claim&nbsp;Shipping&nbsp;Voucher
</button> </button>
</div> </div>