Skip to content

Commit

Permalink
change behavior of plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
SchroterQuentin committed Feb 29, 2024
1 parent a329c32 commit 29a9cbc
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 24 deletions.
2 changes: 2 additions & 0 deletions src/Bones.UI/composables/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./usePermissions"
export * from "./useTranslations"
39 changes: 39 additions & 0 deletions src/Bones.UI/composables/usePermissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { ref } from 'vue'

const _permissions = ref<string[]>([]);
const _setted = false;

export function usePermissions() {

const some = (...permissionCodes: string[]) => {
if (!_setted) {
console.warn("Permissions not setted yet");
}
return _permissions.value.some(p => permissionCodes.includes(p));
}

const every = (...permissionCodes: string[]) => {
if (!_setted) {
console.warn("Permissions not setted yet");
}
return permissionCodes.every(p => _permissions.value.includes(p));
}

const has = (permissionCode: string) => {
if (!_setted) {
console.warn("Permissions not setted yet");
}
return _permissions.value.includes(permissionCode);
}

const set = (permissions: string[]) => {
_permissions.value = permissions;
}

return {
some,
every,
has,
set
}
}
25 changes: 25 additions & 0 deletions src/Bones.UI/composables/useTranslations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ref } from 'vue'

const _translations = ref<{ code: string, value: string }[]>([]);

export function useTranslations() {

const $tr = (code: string, defaultValue: string, ...parameters: (string | number)[]): string => {
let translation = _translations.value.find(t => t.code === code)?.value ?? defaultValue;
if (translation && parameters.length) {
for (let p of parameters) {
translation = translation.replace(`{${parameters.indexOf(p)}}`, p.toString());
}
}
return translation;
};

const set = (translations: { code: string, value: string }[]) => {
_translations.value = translations;
}

return {
$tr,
set
}
}
1 change: 1 addition & 0 deletions src/Bones.UI/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import "./shims-plugin.d.ts"
import "./shims-axios.d.ts"

export * from "./abstractions"
export * from "./composables"
export * from "./core"
export * from "./tools"
export * from "./plugins"
1 change: 1 addition & 0 deletions src/Bones.UI/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "index.ts",
"author": "",
"license": "ISC",
"sideEffects": false,
"dependencies": {
"@types/lodash": "^4.14.197",
"ajv": "^8.11.0",
Expand Down
19 changes: 7 additions & 12 deletions src/Bones.UI/plugins/permissionPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { Plugin } from "vue";

export const PermissionPlugin: Plugin<PermissionOptions> = {
install: (app, options) => {
if (!options || !options.permissionsProvider) {
console.warn("Permission won't work without permissionsProvider")
return;
}
import { usePermissions } from '../composables/usePermissions';

export const PermissionPlugin: Plugin = {
install: (app) => {
const { some, every } = usePermissions();

app.config.globalProperties.$pm = {
some: options.permissionsProvider.some,
every: options.permissionsProvider.every
some,
every
}
}
}

export interface PermissionOptions {
permissionsProvider: { some: (...permissionCodes: string[]) => boolean, every: (...permissionCodes: string[]) => boolean };
}
17 changes: 6 additions & 11 deletions src/Bones.UI/plugins/translationPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { Plugin } from "vue";

export const TranslationPlugin: Plugin<TranslationOptions> = {
install: (app, options) => {
if (!options || !options.translationsProvider) {
console.warn("Translation won't work without translationsProvider")
return;
}
import { useTranslations } from '../composables';

app.config.globalProperties.$tr = options.translationsProvider.$tr;
}
}
export const TranslationPlugin: Plugin = {
install: (app) => {
const { $tr } = useTranslations();

export interface TranslationOptions {
translationsProvider: { $tr: (code: string, defaultLabel: string, ...parameters: string[]) => string }
app.config.globalProperties.$tr = $tr;
}
}
2 changes: 1 addition & 1 deletion src/Bones.UI/shims-plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Vue from "vue";

declare module "vue" {
interface ComponentCustomProperties {
$tr: (code: string, defaultLabel: string, ...parameters: string[]) => string;
$tr: (code: string, defaultLabel: string, ...parameters: (string | number)[]) => string;
$pm: {
some(...permissionCodes: string[]): boolean;
every(...permissionCodes: string[]): boolean;
Expand Down

0 comments on commit 29a9cbc

Please sign in to comment.