Skip to content

Commit

Permalink
fix: remove redundant data from stored macros (#1356)
Browse files Browse the repository at this point in the history
Signed-off-by: Mathis Mensing <github@matmen.dev>
Co-authored-by: Pedro Lamas <pedrolamas@gmail.com>
  • Loading branch information
matmen and pedrolamas authored Feb 12, 2024
1 parent 74def24 commit c105dc7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
15 changes: 10 additions & 5 deletions src/store/macros/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import type { GetterTree } from 'vuex'
import type { Macro, MacrosState } from './types'
import type { RootState } from '../types'

export const MACRO_DEFAULTS = {
alias: '',
visible: true,
disabledWhilePrinting: false,
color: '',
categoryId: '0',
order: undefined
}

export const getters: GetterTree<MacrosState, RootState> = {

/**
Expand All @@ -19,12 +28,8 @@ export const getters: GetterTree<MacrosState, RootState> = {
const variables = rootState.printer.printer[key]

const macro: Macro = {
...MACRO_DEFAULTS,
name,
alias: '',
visible: true,
disabledWhilePrinting: false,
color: '',
categoryId: '0',
...stored,
variables,
...{ config }
Expand Down
23 changes: 15 additions & 8 deletions src/store/macros/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ import { v4 as uuidv4 } from 'uuid'
import type { MutationTree } from 'vuex'
import { defaultState } from './state'
import type { Macro, MacroCategory, MacrosState } from './types'
import { MACRO_DEFAULTS } from '@/store/macros/getters'

const sanitizeMacroForStorage = (macro: Macro) => {
for (const key in macro) {
if (!(key === 'name' || key in MACRO_DEFAULTS)) {
delete macro[key as keyof Macro]
}
}

return macro
}

export const mutations: MutationTree<MacrosState> = {
/**
Expand All @@ -22,9 +33,7 @@ export const mutations: MutationTree<MacrosState> = {

// Updates a singular macro
setUpdateMacro (state, macro: Macro) {
const m = { ...macro }
delete m.config // Saving a macro should never include its config.
delete m.category // we don't need the category prop (we use categoryid)
const m = sanitizeMacroForStorage({ ...macro })
const i = state.stored.findIndex(m => m.name === macro.name)
if (i < 0) {
state.stored.push(m)
Expand All @@ -36,12 +45,10 @@ export const mutations: MutationTree<MacrosState> = {
setUpdateAllVisible (state, payload: { macros: Macro[]; visible: boolean }) {
payload.macros.forEach((macro: Macro) => {
const i = state.stored.findIndex(m => m.name === macro.name)
const processed = {
const processed = sanitizeMacroForStorage({
...macro,
visible: payload.visible
}
delete processed.config // Saving a macro should never include its config.
delete processed.category // we don't need the category prop (we use categoryid)
})
if (i < 0) {
state.stored.push(processed)
} else {
Expand Down Expand Up @@ -74,7 +81,7 @@ export const mutations: MutationTree<MacrosState> = {
state.categories.splice(i, 1)
state.stored.forEach((macro, i) => {
if (macro.categoryId === payload.id) {
const m = { ...macro }
const m = sanitizeMacroForStorage({ ...macro })
delete m.categoryId
Vue.set(state.stored, i, m)
}
Expand Down

0 comments on commit c105dc7

Please sign in to comment.