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

[24.0] Fix mounting utility usage of pinia (fixes charts form components) #18430

Merged
merged 1 commit into from
Jun 24, 2024
Merged
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
28 changes: 21 additions & 7 deletions client/src/utils/mountVueComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

import BootstrapVue from "bootstrap-vue";
import { iconPlugin, localizationPlugin, vueRxShortcutPlugin } from "components/plugins";
import { createPinia, getActivePinia, PiniaVuePlugin } from "pinia";
import Vue from "vue";

// Load Pinia
Vue.use(PiniaVuePlugin);

// Bootstrap components
Vue.use(BootstrapVue);

Expand All @@ -18,15 +22,25 @@ Vue.use(vueRxShortcutPlugin);
// font-awesome svg icon registration/loading
Vue.use(iconPlugin);

export const mountVueComponent = (ComponentDefinition) => {
function getOrCreatePinia() {
// We sometimes use this utility mounting function in a context where there
// is no existing vue application or pinia store (e.g. individual charts
// displayed in an iframe).
// To support both use cases, we will create a new pinia store and attach it
// to the vue application that is created for the component if missing.
return getActivePinia() || createPinia();
}

export function mountVueComponent(ComponentDefinition) {
const component = Vue.extend(ComponentDefinition);
return (propsData, el) => new component({ propsData, el });
};
return function (propsData, el) {
return new component({ propsData, el, pinia: getOrCreatePinia() });
};
}

export const replaceChildrenWithComponent = (el, ComponentDefinition, propsData = {}) => {
export function replaceChildrenWithComponent(el, ComponentDefinition, propsData = {}) {
const container = document.createElement("div");
el.replaceChildren(container);
const component = Vue.extend(ComponentDefinition);
const mountFn = (propsData, el) => new component({ propsData, el });
const mountFn = mountVueComponent(ComponentDefinition);
return mountFn(propsData, container);
};
}
Loading