Newer
Older
import {
APIListUnpaginated,
APIRetrieve,
createExtendableAPI,
useObjectFromStore,
} from '@rokoli/bnb/drf'
import { defineStore } from 'pinia'
import { computed, MaybeRefOrGetter, toRef } from 'vue'
import { createSteeringURL } from '@/api'
import { steeringAuthInit, useOnAuthBehaviour } from '@/stores/auth'
import { License } from '@/types'
type LicensedWork = {
credits: string
isUseExplicitlyGrantedByAuthor: boolean
}
export function useSelectedLicense(licenseId: MaybeRefOrGetter<License['id'] | null>) {
const { obj } = useObjectFromStore(licenseId, store)
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
return obj
}
export function useLicenseValidator(
license: MaybeRefOrGetter<License | null>,
licensedWork: MaybeRefOrGetter<LicensedWork>,
) {
const _license = toRef(license)
const _licensedWork = toRef(licensedWork)
const { t } = useI18n()
const isUseExplicitlyGrantedByAuthorError = computed(() => {
if (
_license?.value?.requiresExpressPermissionForPublication &&
!_licensedWork.value.isUseExplicitlyGrantedByAuthor
) {
return { message: t('license.validation.requiresExpressPermissionForPublication') }
}
return undefined
})
const needsAuthorError = computed(() => {
if (_license?.value?.needsAuthor && !_licensedWork.value.credits.trim()) {
return { message: t('license.validation.requiresCredits') }
}
return undefined
})
const hasError = computed(
() => isUseExplicitlyGrantedByAuthorError.value || needsAuthorError.value,
)
return { isUseExplicitlyGrantedByAuthorError, needsAuthorError, hasError }
}
export const useLicenseStore = defineStore('licenses', () => {
const endpoint = createSteeringURL.prefix('licenses')
const { api, base } = createExtendableAPI<License>(endpoint, steeringAuthInit)
const listOperations = APIListUnpaginated(api)
useOnAuthBehaviour(() => void listOperations.list())
...listOperations,