Skip to content

armgit / dashy

Public Imported from GitHub
0

πŸš€ A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!

πŸ› Fixes section/widget edits saving to wrong section, by using sec name as id

Alicia Sykes204ec6a9e0b8
diff --git a/src/assets/locales/en.json b/src/assets/locales/en.json
index 3a9a75c1..02bb86da 100644
--- a/src/assets/locales/en.json
+++ b/src/assets/locales/en.json
@@ -369,7 +369,9 @@
"edit-section-title": "Edit Section",
"add-section-title": "Add New Section",
"edit-tooltip": "Click to Edit, or right-click for more options",
- "remove-confirm": "Are you sure you want to remove this section? This action can be undone later."
+ "remove-confirm": "Are you sure you want to remove this section? This action can be undone later.",
+ "missing-name-err": "A section name is required",
+ "duplicate-name-err": "A section named '{name}' already exists"
},
"edit-widget": {
"edit-widget-title": "Edit Widget",
diff --git a/src/components/InteractiveEditor/EditSection.vue b/src/components/InteractiveEditor/EditSection.vue
index 7b145d97..b58155b1 100644
--- a/src/components/InteractiveEditor/EditSection.vue
+++ b/src/components/InteractiveEditor/EditSection.vue
@@ -23,6 +23,7 @@ import { modalNames } from '@/utils/config/defaults';
import ErrorHandler, { InfoHandler, InfoKeys } from '@/utils/logging/ErrorHandler';
import safeClone from '@/utils/safeClone';
import pruneSchemaDefaults from '@/utils/config/pruneSchemaDefaults';
+import { makePageName } from '@/utils/config/ConfigHelpers';
import SaveCancelButtons from '@/components/InteractiveEditor/SaveCancelButtons';
import AccessError from '@/components/Configuration/AccessError';
@@ -57,7 +58,7 @@ export default {
name: 'EditSection',
components: { SaveCancelButtons, AccessError, SchemaForm },
props: {
- sectionIndex: { type: Number, default: -1 },
+ sectionName: { type: String, default: '' },
isAddNew: Boolean,
},
emits: ['closeEditSection'],
@@ -72,7 +73,7 @@ export default {
allowViewConfig() { return this.$store.getters.permissions.allowViewConfig; },
},
mounted() {
- const live = this.isAddNew ? null : this.$store.getters.getSectionByIndex(this.sectionIndex);
+ const live = this.isAddNew ? null : this.$store.getters.getSectionByName(this.sectionName);
this.sectionData = safeClone(live, {});
this.$modal.show(this.modalName);
},
@@ -81,14 +82,27 @@ export default {
this.$store.commit(StoreKeys.SET_MODAL_OPEN, false);
this.$emit('closeEditSection');
},
+ /* Section names used as id, so need to be present and unique */
+ validateName(name) {
+ if (!name || !name.trim()) return this.$t('interactive-editor.edit-section.missing-name-err');
+ const slug = makePageName(name);
+ const others = (this.$store.state.config.sections || [])
+ .filter((section) => this.isAddNew || (section.name || '') !== this.sectionName);
+ if (others.some((section) => makePageName(section.name) === slug)) {
+ return this.$t('interactive-editor.edit-section.duplicate-name-err', { name });
+ }
+ return null;
+ },
saveSection() {
try {
/* Form only edits metadata, so preserve the live section's items array. */
const payload = pruneSchemaDefaults(this.sectionData, this.customSchema);
+ const nameError = this.validateName(payload.name);
+ if (nameError) { this.$toast.error(nameError); return; }
if (!this.isAddNew) {
- const live = this.$store.getters.getSectionByIndex(this.sectionIndex);
+ const live = this.$store.getters.getSectionByName(this.sectionName);
if (live?.items) payload.items = live.items;
- this.$store.commit(StoreKeys.UPDATE_SECTION, { sectionIndex: this.sectionIndex, sectionData: payload });
+ this.$store.commit(StoreKeys.UPDATE_SECTION, { sectionName: this.sectionName, sectionData: payload });
} else {
this.$store.commit(StoreKeys.INSERT_SECTION, payload);
}
diff --git a/src/components/InteractiveEditor/EditWidget.vue b/src/components/InteractiveEditor/EditWidget.vue
index e75f7962..2b9a62a7 100644
--- a/src/components/InteractiveEditor/EditWidget.vue
+++ b/src/components/InteractiveEditor/EditWidget.vue
@@ -109,7 +109,7 @@ export default {
Input, Radio, AddIcon, BinIcon, AccessError, SaveCancelButtons,
},
props: {
- sectionIndex: { type: Number, required: true },
+ sectionName: { type: String, required: true },
widgetIndex: { type: Number, default: -1 },
isAddNew: Boolean,
},
@@ -131,7 +131,7 @@ export default {
/* Populate form before children render so Radio's `initialOption` is set on its first creation. */
created() {
if (this.isAddNew) return;
- const live = this.$store.getters.getSectionByIndex(this.sectionIndex);
+ const live = this.$store.getters.getSectionByName(this.sectionName);
const widget = safeClone(live?.widgets?.[this.widgetIndex], {});
this.form = {
type: widget.type || '',
@@ -186,10 +186,10 @@ export default {
try {
const widget = this.buildWidget();
if (this.isAddNew) {
- this.$store.commit(StoreKeys.INSERT_WIDGET, { sectionIndex: this.sectionIndex, widget });
+ this.$store.commit(StoreKeys.INSERT_WIDGET, { sectionName: this.sectionName, widget });
} else {
this.$store.commit(StoreKeys.UPDATE_WIDGET, {
- sectionIndex: this.sectionIndex,
+ sectionName: this.sectionName,
widgetIndex: this.widgetIndex,
widget,
});
diff --git a/src/components/LinkItems/Section.vue b/src/components/LinkItems/Section.vue
index a1cef879..508c91c1 100644
--- a/src/components/LinkItems/Section.vue
+++ b/src/components/LinkItems/Section.vue
@@ -85,7 +85,7 @@
<EditSection
v-if="editMenuOpen"
@closeEditSection="closeEditSection"
- :sectionIndex="index"
+ :sectionName="title"
:isAddNew="false"
/>
<!-- Right-click item options context menu -->
@@ -109,7 +109,7 @@
/>
<!-- Edit widget menu -->
<EditWidget v-if="editWidgetMenuOpen"
- :sectionIndex="index"
+ :sectionName="title"
:widgetIndex="editingWidgetIndex"
:isAddNew="addingWidget"
@closeEditWidget="closeEditWidget"
@@ -275,8 +275,7 @@ export default {
this.showRemoveConfirm = true;
},
confirmRemoveSection() {
- const payload = { sectionIndex: this.index, sectionName: this.title };
- this.$store.commit(StoreKeys.REMOVE_SECTION, payload);
+ this.$store.commit(StoreKeys.REMOVE_SECTION, { sectionName: this.title });
},
/* Open custom context menu, and set position */
openContextMenu(e) {
@@ -319,7 +318,7 @@ export default {
},
doRemoveWidget() {
this.$store.commit(StoreKeys.REMOVE_WIDGET, {
- sectionIndex: this.index,
+ sectionName: this.title,
widgetIndex: this.pendingRemoveWidgetIndex,
});
this.pendingRemoveWidgetIndex = -1;
diff --git a/src/store.js b/src/store.js
index 8bc7d9a4..53e1b519 100644
--- a/src/store.js
+++ b/src/store.js
@@ -5,7 +5,7 @@ import Keys from '@/utils/StoreMutations';
import {
makePageName, formatConfigPath, componentVisibility, configScope, stripRootOwnedFields,
} from '@/utils/config/ConfigHelpers';
-import { applyItemId } from '@/utils/config/SectionHelpers';
+import { applyItemId, mapSectionByName } from '@/utils/config/SectionHelpers';
import filterUserSections from '@/utils/CheckSectionVisibility';
import ErrorHandler, { InfoHandler, InfoKeys } from '@/utils/logging/ErrorHandler';
import {
@@ -252,9 +252,9 @@ const store = createStore({
return getUserState();
},
- getSectionByIndex: (state, getters) => (index) => {
- return getters.sections[index];
- },
+ getSectionByName: (state, getters) => (name) => (
+ getters.sections.find((section) => (section.name || '') === name)
+ ),
getItemById: (state, getters) => (id) => {
if (!id) return undefined;
let item;
@@ -358,10 +358,9 @@ const store = createStore({
})));
InfoHandler('Item updated', InfoKeys.EDITOR);
},
- [UPDATE_SECTION](state, { sectionIndex, sectionData }) {
- commitConfigField(state, 'sections', state.config.sections.map((s, i) => (
- i === sectionIndex ? sectionData : s
- )));
+ [UPDATE_SECTION](state, { sectionName, sectionData }) {
+ commitConfigField(state, 'sections',
+ mapSectionByName(state.config.sections, sectionName, () => sectionData));
InfoHandler('Section updated', InfoKeys.EDITOR);
},
[INSERT_SECTION](state, newSection) {
@@ -371,64 +370,47 @@ const store = createStore({
]);
InfoHandler('New section added', InfoKeys.EDITOR);
},
- [REMOVE_SECTION](state, { sectionIndex, sectionName }) {
+ [REMOVE_SECTION](state, { sectionName }) {
const current = state.config.sections;
- if (current[sectionIndex]?.name !== sectionName) return;
- commitConfigField(state, 'sections', current.filter((_, i) => i !== sectionIndex));
+ if (!current.some((section) => (section.name || '') === sectionName)) return;
+ commitConfigField(state, 'sections', current.filter((section) => (section.name || '') !== sectionName));
InfoHandler('Section removed', InfoKeys.EDITOR);
},
[INSERT_ITEM](state, { newItem, targetSection }) {
- const patched = state.config.sections.map((section) => {
- if (section.name !== targetSection) return section;
- return { ...section, items: [...(section.items || []), newItem] };
- });
- commitConfigField(state, 'sections', patched);
+ commitConfigField(state, 'sections', mapSectionByName(state.config.sections, targetSection,
+ (s) => ({ ...s, items: [...(s.items || []), newItem] })));
InfoHandler('New item added', InfoKeys.EDITOR);
},
[COPY_ITEM](state, { item, toSection, appendTo }) {
const newItem = { ...item };
- const patched = state.config.sections.map((section) => {
- if (section.name !== toSection) return section;
- const items = appendTo === 'beginning'
- ? [newItem, ...(section.items || [])]
- : [...(section.items || []), newItem];
- return { ...section, items };
- });
- commitConfigField(state, 'sections', patched);
+ commitConfigField(state, 'sections', mapSectionByName(state.config.sections, toSection, (s) => ({
+ ...s,
+ items: appendTo === 'beginning'
+ ? [newItem, ...(s.items || [])]
+ : [...(s.items || []), newItem],
+ })));
InfoHandler('Item copied', InfoKeys.EDITOR);
},
[REMOVE_ITEM](state, { itemId, sectionName }) {
- const patched = state.config.sections.map((section) => {
- if (section.name !== sectionName) return section;
- return {
- ...section,
- items: (section.items || []).filter((item) => item.id !== itemId),
- };
- });
- commitConfigField(state, 'sections', patched);
+ commitConfigField(state, 'sections', mapSectionByName(state.config.sections, sectionName,
+ (s) => ({ ...s, items: (s.items || []).filter((item) => item.id !== itemId) })));
InfoHandler('Item removed', InfoKeys.EDITOR);
},
- [INSERT_WIDGET](state, { sectionIndex, widget }) {
- commitConfigField(state, 'sections', state.config.sections.map((s, i) => (
- i === sectionIndex ? { ...s, widgets: [...(s.widgets || []), widget] } : s
- )));
+ [INSERT_WIDGET](state, { sectionName, widget }) {
+ commitConfigField(state, 'sections', mapSectionByName(state.config.sections, sectionName,
+ (s) => ({ ...s, widgets: [...(s.widgets || []), widget] })));
InfoHandler('New widget added', InfoKeys.EDITOR);
},
- [UPDATE_WIDGET](state, { sectionIndex, widgetIndex, widget }) {
- commitConfigField(state, 'sections', state.config.sections.map((s, i) => {
- if (i !== sectionIndex) return s;
- return {
- ...s,
- widgets: (s.widgets || []).map((w, wi) => (wi === widgetIndex ? widget : w)),
- };
- }));
+ [UPDATE_WIDGET](state, { sectionName, widgetIndex, widget }) {
+ commitConfigField(state, 'sections', mapSectionByName(state.config.sections, sectionName, (s) => ({
+ ...s,
+ widgets: (s.widgets || []).map((w, wi) => (wi === widgetIndex ? widget : w)),
+ })));
InfoHandler('Widget updated', InfoKeys.EDITOR);
},
- [REMOVE_WIDGET](state, { sectionIndex, widgetIndex }) {
- commitConfigField(state, 'sections', state.config.sections.map((s, i) => {
- if (i !== sectionIndex) return s;
- return { ...s, widgets: (s.widgets || []).filter((_, wi) => wi !== widgetIndex) };
- }));
+ [REMOVE_WIDGET](state, { sectionName, widgetIndex }) {
+ commitConfigField(state, 'sections', mapSectionByName(state.config.sections, sectionName,
+ (s) => ({ ...s, widgets: (s.widgets || []).filter((_, wi) => wi !== widgetIndex) })));
InfoHandler('Widget removed', InfoKeys.EDITOR);
},
[SET_THEME](state, theme) {
diff --git a/src/utils/config/ConfigSchema.json b/src/utils/config/ConfigSchema.json
index c463be89..cf29cace 100644
--- a/src/utils/config/ConfigSchema.json
+++ b/src/utils/config/ConfigSchema.json
@@ -885,7 +885,7 @@
"name": {
"title": "Section Name",
"type": "string",
- "description": "Title/ heading for a section"
+ "description": "Title/ heading for a section. Required and must be unique"
},
"icon": {
"title": "Section Icon",
diff --git a/src/utils/config/SectionHelpers.js b/src/utils/config/SectionHelpers.js
index c31ab3b6..62afe0ea 100644
--- a/src/utils/config/SectionHelpers.js
+++ b/src/utils/config/SectionHelpers.js
@@ -39,3 +39,8 @@ export const applyItemId = (inputSections) => (inputSections || []).map((section
}
return next;
});
+
+/* Returns new sections array, with the `transform` function applied to section with given `name` */
+export const mapSectionByName = (sections, name, transform) => (
+ sections.map((section) => ((section.name || '') === name ? transform(section) : section))
+);