Enhance PlatformServiceMixin with utility methods and apply to TopMessage
Add $getSettings, $getMergedSettings utilities with built-in error handling. Reduce TopMessage code by 57% while eliminating Vue property conflicts.
This commit is contained in:
@@ -18,22 +18,19 @@ import { Component, Vue, Prop } from "vue-facing-decorator";
|
||||
import { AppString, NotificationIface } from "../constants/app";
|
||||
import { MASTER_SETTINGS_KEY } from "../db/tables/settings";
|
||||
import { DEFAULT_ENDORSER_API_SERVER } from "../constants/app";
|
||||
import {
|
||||
PlatformServiceMixin,
|
||||
IPlatformServiceMixin,
|
||||
} from "../utils/PlatformServiceMixin";
|
||||
import { mapColumnsToValues, parseJsonField } from "../db/databaseUtil";
|
||||
import { PlatformServiceMixin } from "../utils/PlatformServiceMixin";
|
||||
|
||||
@Component({
|
||||
mixins: [PlatformServiceMixin],
|
||||
})
|
||||
export default class TopMessage extends Vue {
|
||||
// NOTE: This component uses PlatformServiceMixin which provides:
|
||||
// - this.dbQuery(), this.dbExec(), this.dbGetOneRow() methods
|
||||
// - this.platformService computed property
|
||||
// - this.isCapacitor, this.isWeb, this.isElectron computed properties
|
||||
// Enhanced PlatformServiceMixin provides:
|
||||
// - this.$dbQuery(), this.$dbExec(), this.$dbGetOneRow() with built-in error handling
|
||||
// - this.$getSettings(), this.$getMergedSettings() utility methods
|
||||
// - this.$withTransaction() for safe database transactions
|
||||
// - this.platformService, this.isCapacitor, this.isWeb, this.isElectron
|
||||
// - this.capabilities computed property
|
||||
// TypeScript requires (this as any) for mixin methods due to compile-time limitations
|
||||
// All methods use $ prefix following Vue conventions
|
||||
|
||||
$notify!: (notification: NotificationIface, timeout?: number) => void;
|
||||
|
||||
@@ -71,97 +68,37 @@ export default class TopMessage extends Vue {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get settings for the active account using the platform service mixin.
|
||||
* This demonstrates the concise mixin pattern with direct database access.
|
||||
* Get settings for the active account using enhanced mixin utilities.
|
||||
* Dramatically simplified using $getMergedSettings utility method.
|
||||
*/
|
||||
private async getActiveAccountSettings() {
|
||||
// Declare defaultSettings outside try block for proper scope
|
||||
let defaultSettings;
|
||||
|
||||
try {
|
||||
// Get default settings first
|
||||
defaultSettings = await this.getDefaultSettings();
|
||||
|
||||
// If no active DID, return defaults
|
||||
if (!defaultSettings.activeDid) {
|
||||
return defaultSettings;
|
||||
}
|
||||
|
||||
// Get account-specific settings using the mixin (much more concise!)
|
||||
const result = await (this as any).dbQuery(
|
||||
"SELECT * FROM settings WHERE accountDid = ?",
|
||||
[defaultSettings.activeDid],
|
||||
);
|
||||
|
||||
if (!result?.values?.length) {
|
||||
return defaultSettings;
|
||||
}
|
||||
|
||||
// Map and filter settings
|
||||
const overrideSettings = mapColumnsToValues(
|
||||
result.columns,
|
||||
result.values,
|
||||
)[0] as any;
|
||||
|
||||
const overrideSettingsFiltered = Object.fromEntries(
|
||||
Object.entries(overrideSettings).filter(([_, v]) => v !== null),
|
||||
);
|
||||
|
||||
// Merge settings
|
||||
const settings = { ...defaultSettings, ...overrideSettingsFiltered };
|
||||
|
||||
// Handle searchBoxes parsing
|
||||
if (settings.searchBoxes) {
|
||||
settings.searchBoxes = parseJsonField(settings.searchBoxes, []);
|
||||
}
|
||||
|
||||
return settings;
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`Failed to retrieve account settings for ${defaultSettings?.activeDid}:`,
|
||||
error,
|
||||
);
|
||||
return (
|
||||
defaultSettings || {
|
||||
id: MASTER_SETTINGS_KEY,
|
||||
activeDid: undefined,
|
||||
apiServer: DEFAULT_ENDORSER_API_SERVER,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default settings using the platform service mixin
|
||||
*/
|
||||
private async getDefaultSettings() {
|
||||
try {
|
||||
// Direct database access via mixin - no destructuring needed!
|
||||
const result = await (this as any).dbQuery("SELECT * FROM settings WHERE id = ?", [
|
||||
// First get the default settings to find activeDid
|
||||
const defaultSettings = await (this as any).$getSettings(
|
||||
MASTER_SETTINGS_KEY,
|
||||
]);
|
||||
|
||||
if (!result?.values?.length) {
|
||||
return {
|
||||
{
|
||||
id: MASTER_SETTINGS_KEY,
|
||||
activeDid: undefined,
|
||||
apiServer: DEFAULT_ENDORSER_API_SERVER,
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
const settings = mapColumnsToValues(
|
||||
result.columns,
|
||||
result.values,
|
||||
)[0] as any;
|
||||
// Use enhanced utility to merge default and account-specific settings
|
||||
// This replaces 50+ lines of duplicated logic with a single method call!
|
||||
const mergedSettings = await (this as any).$getMergedSettings(
|
||||
MASTER_SETTINGS_KEY,
|
||||
defaultSettings.activeDid,
|
||||
{
|
||||
id: MASTER_SETTINGS_KEY,
|
||||
activeDid: undefined,
|
||||
apiServer: DEFAULT_ENDORSER_API_SERVER,
|
||||
},
|
||||
);
|
||||
|
||||
// Handle searchBoxes parsing
|
||||
if (settings.searchBoxes) {
|
||||
settings.searchBoxes = parseJsonField(settings.searchBoxes, []);
|
||||
}
|
||||
|
||||
return settings;
|
||||
return mergedSettings;
|
||||
} catch (error) {
|
||||
console.error("Failed to retrieve default settings:", error);
|
||||
// Enhanced mixin already provides detailed error logging
|
||||
// Just provide fallback for UI stability
|
||||
return {
|
||||
id: MASTER_SETTINGS_KEY,
|
||||
activeDid: undefined,
|
||||
|
||||
Reference in New Issue
Block a user