moved migration hook to its own function

This commit is contained in:
ryan 2019-03-22 10:22:25 -07:00
parent a6c2a8c3cc
commit d8595e9f4e
3 changed files with 28 additions and 14 deletions

View File

@ -21,13 +21,18 @@ export interface PanelEditorProps<T = any> {
onOptionsChange: (options: T) => void; onOptionsChange: (options: T) => void;
} }
/**
* Called when a panel is first loaded with existing options
*/
export type PanelMigrationHook<TOptions = any> = (options: Partial<TOptions>) => Partial<TOptions>;
/** /**
* Called before a panel is initalized * Called before a panel is initalized
*/ */
export type PanelTypeChangedHook<TOptions = any> = ( export type PanelTypeChangedHook<TOptions = any> = (
options: Partial<TOptions>, options: Partial<TOptions>,
prevPluginId?: string, prevPluginId: string,
prevOptions?: any prevOptions: any
) => Partial<TOptions>; ) => Partial<TOptions>;
export class ReactPanelPlugin<TOptions = any> { export class ReactPanelPlugin<TOptions = any> {
@ -35,6 +40,7 @@ export class ReactPanelPlugin<TOptions = any> {
editor?: ComponentClass<PanelEditorProps<TOptions>>; editor?: ComponentClass<PanelEditorProps<TOptions>>;
defaults?: TOptions; defaults?: TOptions;
panelMigrationHook?: PanelMigrationHook<TOptions>;
panelTypeChangedHook?: PanelTypeChangedHook<TOptions>; panelTypeChangedHook?: PanelTypeChangedHook<TOptions>;
constructor(panel: ComponentClass<PanelProps<TOptions>>) { constructor(panel: ComponentClass<PanelProps<TOptions>>) {
@ -49,6 +55,13 @@ export class ReactPanelPlugin<TOptions = any> {
this.defaults = defaults; this.defaults = defaults;
} }
/**
* Called when the panel first loaded with
*/
setPanelMigrationHook(v: PanelMigrationHook<TOptions>) {
this.panelMigrationHook = v;
}
/** /**
* Called when the visualization changes. * Called when the visualization changes.
* Lets you keep whatever settings made sense in the previous panel * Lets you keep whatever settings made sense in the previous panel

View File

@ -98,10 +98,10 @@ export class DashboardPanel extends PureComponent<Props, State> {
} }
panel.changeType(pluginId, hook); panel.changeType(pluginId, hook);
} }
} else if (plugin.exports && plugin.exports.reactPanel) { } else if (plugin.exports && plugin.exports.reactPanel && panel.options) {
const hook = plugin.exports.reactPanel.panelTypeChangedHook; const hook = plugin.exports.reactPanel.panelMigrationHook;
if (hook) { if (hook) {
panel.options = hook(panel.options || {}, null, null); panel.options = hook(panel.options);
} }
} }

View File

@ -10,23 +10,23 @@ const optionsToKeep = ['valueOptions', 'stat', 'maxValue', 'maxValue', 'threshol
export const singleStatBaseOptionsCheck = ( export const singleStatBaseOptionsCheck = (
options: Partial<SingleStatBaseOptions>, options: Partial<SingleStatBaseOptions>,
prevPluginId?: string, prevPluginId: string,
prevOptions?: any prevOptions: any
) => { ) => {
if (prevOptions) { optionsToKeep.forEach(v => {
for (const otk of optionsToKeep) { if (prevOptions.hasOwnProperty(v)) {
if (prevOptions.hasOwnProperty(otk)) { options[v] = cloneDeep(prevOptions.display);
options[otk] = cloneDeep(prevOptions[otk]);
}
} }
} });
return options;
};
export const singleStatMigrationCheck = (options: Partial<SingleStatBaseOptions>) => {
// 6.1 renamed some stats, This makes sure they are up to date // 6.1 renamed some stats, This makes sure they are up to date
// avg -> mean, current -> last, total -> sum // avg -> mean, current -> last, total -> sum
const { valueOptions } = options; const { valueOptions } = options;
if (valueOptions && valueOptions.stat) { if (valueOptions && valueOptions.stat) {
valueOptions.stat = getStatsCalculators([valueOptions.stat]).map(s => s.id)[0]; valueOptions.stat = getStatsCalculators([valueOptions.stat]).map(s => s.id)[0];
console.log('CHANGED', valueOptions);
} }
return options; return options;
}; };
@ -34,3 +34,4 @@ export const singleStatBaseOptionsCheck = (
reactPanel.setEditor(SingleStatEditor); reactPanel.setEditor(SingleStatEditor);
reactPanel.setDefaults(defaults); reactPanel.setDefaults(defaults);
reactPanel.setPanelTypeChangedHook(singleStatBaseOptionsCheck); reactPanel.setPanelTypeChangedHook(singleStatBaseOptionsCheck);
reactPanel.setPanelMigrationHook(singleStatMigrationCheck);