Skip to content

Commit

Permalink
feat: call api to save values
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasHirt committed Dec 10, 2024
1 parent 8a5146c commit 40b37dd
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -1,64 +1,66 @@
import { computed, ref, Ref } from 'vue'
import { computed, Ref, unref } from 'vue'
import {
getSettingsDefaultValue,
getSettingsValue,
SETTINGS_EMAIL_NOTIFICATION_BUNDLE_IDS,
SETTINGS_NOTIFICATION_BUNDLE_IDS,
SettingsBundle,
SettingsNotificationBundle,
SettingsValue
} from '../../helpers/settings'

export const useNotificationsSettings = (bundle: Ref<SettingsBundle>) => {
const values = ref({
[SettingsNotificationBundle.ShareCreated]: { mail: false, inapp: false },
[SettingsNotificationBundle.ShareRemoved]: { mail: false, inapp: false },
[SettingsNotificationBundle.ShareExpired]: { mail: false, inapp: false },
[SettingsNotificationBundle.SpaceShared]: { mail: false, inapp: false },
[SettingsNotificationBundle.SpaceUnshared]: { mail: false, inapp: false },
[SettingsNotificationBundle.SpaceMembershipExpired]: { mail: false, inapp: false },
[SettingsNotificationBundle.SpaceDisabled]: { mail: false, inapp: false },
[SettingsNotificationBundle.SpaceDeleted]: { mail: false, inapp: false },
[SettingsNotificationBundle.PostprocessingStepFinished]: { mail: false, inapp: false },
[SettingsNotificationBundle.ScienceMeshInviteTokenGenerated]: { mail: false, inapp: false }
})

const defaultValues = computed(() => {
if (!bundle.value) {
export const useNotificationsSettings = (
valueList: Ref<SettingsValue[]>,
bundle: Ref<SettingsBundle>
) => {
const values = computed(() => {
if (!unref(bundle)) {
return {}
}

return bundle.value.settings.reduce((acc, curr) => {
return unref(bundle).settings.reduce((acc, curr) => {
if (!SETTINGS_NOTIFICATION_BUNDLE_IDS.includes(curr.id)) {
return acc
}

acc[curr.id] = getSettingsDefaultValue(curr)
acc[curr.id] = getSettingsValue(curr, unref(valueList)) || getSettingsDefaultValue(curr)

return acc
}, {})
})

const options = computed<SettingsBundle['settings']>(() => {
if (!bundle.value) {
if (!unref(bundle)) {
return []
}

return bundle.value.settings.filter(({ id }) => SETTINGS_NOTIFICATION_BUNDLE_IDS.includes(id))
return unref(bundle).settings.filter(({ id }) => SETTINGS_NOTIFICATION_BUNDLE_IDS.includes(id))
})

const emailOptions = computed<SettingsBundle['settings']>(() => {
if (!bundle.value) {
if (!unref(bundle)) {
return []
}

return bundle.value.settings.filter(({ id }) =>
return unref(bundle).settings.filter(({ id }) =>
SETTINGS_EMAIL_NOTIFICATION_BUNDLE_IDS.includes(id)
)
})

const updateDefaultValues = (values: SettingsValue[], bundle: SettingsBundle) => {
// TODO: process and return values
}
const emailValues = computed(() => {
if (!unref(bundle)) {
return {}
}

return unref(bundle).settings.reduce((acc, curr) => {
if (!SETTINGS_EMAIL_NOTIFICATION_BUNDLE_IDS.includes(curr.id)) {
return acc
}

acc[curr.id] = getSettingsValue(curr, unref(valueList)) || getSettingsDefaultValue(curr)

return acc
}, {})
})

return { values, options, emailOptions, updateDefaultValues }
return { values, options, emailOptions, emailValues }
}
105 changes: 79 additions & 26 deletions packages/web-runtime/src/helpers/settings.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { captureException } from '@sentry/vue'

export interface SettingsValue {
identifier: {
bundle: string
Expand All @@ -18,7 +20,39 @@ export interface SettingsValue {
stringValue: string
}[]
}
collectionValue?: {
values: {
key: string
boolValue: boolean
}[]
}
}
}

interface SettingsBundleSetting {
description: string
displayName: string
id: string
name: string
resource: {
type: string
}
singleChoiceValue?: {
options: Record<string, any>[]
}
multiChoiceCollectionValue?: {
options: {
value: {
boolValue: {
default?: boolean
}
}
key: string
displayValue: string
attribute?: 'disabled'
}[]
}
boolValue?: Record<string, any>
}

export interface SettingsBundle {
Expand All @@ -29,31 +63,7 @@ export interface SettingsBundle {
resource: {
type: string
}
settings: {
description: string
displayName: string
id: string
name: string
resource: {
type: string
}
singleChoiceValue?: {
options: Record<string, any>[]
}
multiChoiceCollectionValue?: {
options: {
value: {
boolValue: {
default?: boolean
}
}
key: string
displayValue: string
attribute?: 'disabled'
}[]
}
boolValue?: Record<string, any>
}[]
settings: SettingsBundleSetting[]
type: string
roleId?: string
}
Expand Down Expand Up @@ -100,10 +110,53 @@ export const SETTINGS_EMAIL_NOTIFICATION_BUNDLE_IDS: string[] = [
SettingsEmailNotificationBundle.EmailSendingInterval
]

export function getSettingsDefaultValue(setting: SettingsBundle['settings']) {
export function getSettingsDefaultValue(setting: SettingsBundleSetting) {
if (setting.singleChoiceValue) {
const [option] = setting.singleChoiceValue.options

return {
value: option.value.stringValue,
displayValue: option.displayValue
}
}

if (setting.multiChoiceCollectionValue) {
return setting.multiChoiceCollectionValue.options.reduce((acc, curr) => {
acc[curr.key] = curr.value.boolValue.default

return acc
}, {})
}

const error = new Error('Unsupported setting value')

console.error(error)
captureException(error)

return null
}

export function getSettingsValue(
setting: SettingsBundleSetting,
valueList: SettingsValue[]
): boolean | string | null | { [key: string]: boolean } {
const { value } = valueList.find((v) => v.identifier.setting === setting.name) || {}

if (!value) {
return null
}

if (value.collectionValue) {
return setting.multiChoiceCollectionValue.options.reduce((acc, curr) => {
const val = value.collectionValue.values.find((v) => v.key === curr.key)

if (val) {
acc[curr.key] = val.boolValue
return acc
}

acc[curr.key] = curr.value.boolValue.default
return acc
}, {})
}
}
Loading

0 comments on commit 40b37dd

Please sign in to comment.