2024-04-15 19:39:03 +02:00
|
|
|
import { defineStore } from "pinia"
|
|
|
|
|
import { useContentStore } from "~/composables/stores/useContentStore.js"
|
|
|
|
|
import integrationsList from "~/data/forms/integrations.json"
|
2024-03-28 18:14:30 +01:00
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
export const formIntegrationsEndpoint = "/open/forms/{formid}/integrations"
|
2024-03-28 18:14:30 +01:00
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
export const useFormIntegrationsStore = defineStore("form_integrations", () => {
|
2024-03-28 18:14:30 +01:00
|
|
|
const contentStore = useContentStore()
|
2024-04-15 19:39:03 +02:00
|
|
|
const integrations = ref(new Map())
|
2024-03-28 18:14:30 +01:00
|
|
|
|
|
|
|
|
const availableIntegrations = computed(() => {
|
|
|
|
|
const user = useAuthStore().user
|
2024-08-27 16:49:43 +02:00
|
|
|
const featureFlagsStore = useFeatureFlagsStore()
|
2024-03-28 18:14:30 +01:00
|
|
|
if (!user) return integrations.value
|
|
|
|
|
|
|
|
|
|
const enrichedIntegrations = new Map()
|
|
|
|
|
for (const [key, integration] of integrations.value.entries()) {
|
2024-08-27 16:49:43 +02:00
|
|
|
if (featureFlagsStore.getFlag(`integrations.${key}`, true)) {
|
|
|
|
|
enrichedIntegrations.set(key, {
|
|
|
|
|
...integration,
|
|
|
|
|
id: key,
|
|
|
|
|
requires_subscription: !user.is_subscribed && integration.is_pro,
|
|
|
|
|
})
|
|
|
|
|
}
|
2024-03-28 18:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return enrichedIntegrations
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const integrationsBySection = computed(() => {
|
2024-04-15 19:39:03 +02:00
|
|
|
const groupedObject = {}
|
2024-03-28 18:14:30 +01:00
|
|
|
for (const [key, integration] of availableIntegrations.value.entries()) {
|
2024-04-15 19:39:03 +02:00
|
|
|
const sectionName = integration.section_name
|
2024-03-28 18:14:30 +01:00
|
|
|
if (!groupedObject[sectionName]) {
|
2024-04-15 19:39:03 +02:00
|
|
|
groupedObject[sectionName] = {}
|
2024-03-28 18:14:30 +01:00
|
|
|
}
|
|
|
|
|
groupedObject[sectionName][key] = integration
|
|
|
|
|
}
|
2024-04-15 19:39:03 +02:00
|
|
|
return groupedObject
|
2024-03-28 18:14:30 +01:00
|
|
|
})
|
|
|
|
|
|
2024-07-26 14:50:31 +02:00
|
|
|
const fetchFormIntegrations = (formId, refresh=true) => {
|
|
|
|
|
if (refresh) {
|
|
|
|
|
contentStore.resetState()
|
|
|
|
|
contentStore.startLoading()
|
|
|
|
|
}
|
2024-04-15 19:39:03 +02:00
|
|
|
return useOpnApi(formIntegrationsEndpoint.replace("{formid}", formId)).then(
|
|
|
|
|
(response) => {
|
|
|
|
|
contentStore.save(response.data.value)
|
|
|
|
|
contentStore.stopLoading()
|
|
|
|
|
},
|
|
|
|
|
)
|
2024-03-28 18:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getAllByFormId = (formId) => {
|
|
|
|
|
return contentStore.getAll.value.filter((item) => {
|
2024-04-15 19:39:03 +02:00
|
|
|
return item.form_id ? item.form_id === formId : false
|
2024-03-28 18:14:30 +01:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const initIntegrations = () => {
|
|
|
|
|
if (integrations.value.size === 0) {
|
|
|
|
|
integrations.value = new Map(Object.entries(integrationsList))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
initIntegrations()
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...contentStore,
|
|
|
|
|
initIntegrations,
|
|
|
|
|
availableIntegrations,
|
|
|
|
|
integrationsBySection,
|
|
|
|
|
fetchFormIntegrations,
|
|
|
|
|
getAllByFormId,
|
|
|
|
|
}
|
|
|
|
|
})
|