Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove redundant data from stored macros #1356

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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