moving function into file
This commit is contained in:
@@ -10,57 +10,16 @@ import Themes from './themes'
|
|||||||
import Swal from 'sweetalert2';
|
import Swal from 'sweetalert2';
|
||||||
import { Inertia } from '@inertiajs/inertia';
|
import { Inertia } from '@inertiajs/inertia';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
import * as commons from './common.js'
|
||||||
|
|
||||||
const appName = window.document.getElementsByTagName('title')[0]?.innerText || 'Laravel';
|
const appName = window.document.getElementsByTagName('title')[0]?.innerText || 'Laravel';
|
||||||
|
|
||||||
const { $token } = JSON.parse(document.getElementById('app').dataset.page).props
|
const { $token } = JSON.parse(document.getElementById('app').dataset.page).props
|
||||||
axios.defaults.headers.common['Authorization'] = `Bearer ${$token}`
|
axios.defaults.headers.common['Authorization'] = `Bearer ${$token}`
|
||||||
|
|
||||||
const can = (abilities) => {
|
Object.keys(commons).forEach(key => Object.defineProperty(window, key, {
|
||||||
const { $permissions } = usePage().props.value
|
value: commons[key],
|
||||||
|
}))
|
||||||
if (Array.isArray(abilities)) {
|
|
||||||
for (const ability of abilities) {
|
|
||||||
if (can(ability)) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (typeof abilities === 'string') {
|
|
||||||
return $permissions.find(permission => permission.name === abilities) !== undefined
|
|
||||||
} else if (typeof abilities === 'number') {
|
|
||||||
return $permissions.find(permission => permission.id === abilities) !== undefined
|
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
window.translations = {}
|
|
||||||
window.locale = localStorage.getItem('locale') || 'id'
|
|
||||||
|
|
||||||
const translation = () => axios.get(route('api.translation.get', window.locale))
|
|
||||||
.then(response => response.data)
|
|
||||||
.then(response => window.translations = response)
|
|
||||||
|
|
||||||
translation()
|
|
||||||
|
|
||||||
window.can = can
|
|
||||||
window.__ = (text, replacements = {}) => {
|
|
||||||
if (typeof text === 'string') {
|
|
||||||
if (window.translations.hasOwnProperty(text.trim().toLowerCase())) {
|
|
||||||
text = window.translations[text.trim().toLocaleLowerCase()]
|
|
||||||
} else {
|
|
||||||
axios.post(route(
|
|
||||||
'api.translation.register', window.locale
|
|
||||||
), { text }).then(translation)
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const key in replacements) {
|
|
||||||
text = text.replace(new RegExp(`:${key}`, 'g'), replacements[key])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return text
|
|
||||||
}
|
|
||||||
|
|
||||||
createInertiaApp({
|
createInertiaApp({
|
||||||
title: (title) => `${title} - ${appName}`,
|
title: (title) => `${title} - ${appName}`,
|
||||||
@@ -71,8 +30,7 @@ createInertiaApp({
|
|||||||
.use(ZiggyVue, Ziggy)
|
.use(ZiggyVue, Ziggy)
|
||||||
.mixin({
|
.mixin({
|
||||||
methods: {
|
methods: {
|
||||||
can,
|
...commons,
|
||||||
__,
|
|
||||||
themes: () => Themes,
|
themes: () => Themes,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -83,7 +41,7 @@ createInertiaApp({
|
|||||||
InertiaProgress.init({ color: '#4B5563' });
|
InertiaProgress.init({ color: '#4B5563' });
|
||||||
|
|
||||||
window.Swal = Swal
|
window.Swal = Swal
|
||||||
const Toast = Swal.mixin({
|
const Toast = window.Toast = Swal.mixin({
|
||||||
toast: true,
|
toast: true,
|
||||||
position: 'top-end',
|
position: 'top-end',
|
||||||
showConfirmButton: false,
|
showConfirmButton: false,
|
||||||
@@ -95,19 +53,8 @@ const Toast = Swal.mixin({
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const authorization = () => {
|
Inertia.on('start', commons.authorization)
|
||||||
const { $token } = usePage().props.value
|
Inertia.on('finish', commons.authorization)
|
||||||
|
|
||||||
if ($token) {
|
|
||||||
axios.defaults.headers.common['Authorization'] = `Bearer ${$token}`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Inertia.on('start', authorization)
|
|
||||||
Inertia.on('finish', authorization)
|
|
||||||
|
|
||||||
window.Toast = Toast
|
|
||||||
|
|
||||||
Inertia.on('finish', () => {
|
Inertia.on('finish', () => {
|
||||||
const { $flash } = usePage().props.value
|
const { $flash } = usePage().props.value
|
||||||
const { success, error, info, warning } = $flash
|
const { success, error, info, warning } = $flash
|
||||||
|
|||||||
54
resources/js/common.js
Normal file
54
resources/js/common.js
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
import { usePage } from "@inertiajs/inertia-vue3"
|
||||||
|
|
||||||
|
window.translations = {}
|
||||||
|
window.locale = localStorage.getItem('locale') || 'id'
|
||||||
|
|
||||||
|
export const getAllDefinedTransaction = () => axios.get(route('api.translation.get', window.locale))
|
||||||
|
.then(response => response.data)
|
||||||
|
.then(response => window.translations = response)
|
||||||
|
|
||||||
|
export const __ = (text, replacements = {}) => {
|
||||||
|
if (typeof text === 'string') {
|
||||||
|
if (window.translations.hasOwnProperty(text.trim().toLowerCase())) {
|
||||||
|
text = window.translations[text.trim().toLocaleLowerCase()]
|
||||||
|
} else {
|
||||||
|
axios.post(route(
|
||||||
|
'api.translation.register', window.locale
|
||||||
|
), { text }).then(getAllDefinedTransaction)
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const key in replacements) {
|
||||||
|
text = text.replace(new RegExp(`:${key}`, 'g'), replacements[key])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return text
|
||||||
|
}
|
||||||
|
|
||||||
|
export const can = (abilities) => {
|
||||||
|
const { $permissions } = usePage().props.value
|
||||||
|
|
||||||
|
if (Array.isArray(abilities)) {
|
||||||
|
for (const ability of abilities) {
|
||||||
|
if (can(ability)) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (typeof abilities === 'string') {
|
||||||
|
return $permissions.find(permission => permission.name === abilities) !== undefined
|
||||||
|
} else if (typeof abilities === 'number') {
|
||||||
|
return $permissions.find(permission => permission.id === abilities) !== undefined
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const authorization = () => {
|
||||||
|
const { $token } = usePage().props.value
|
||||||
|
|
||||||
|
if ($token) {
|
||||||
|
axios.defaults.headers.common['Authorization'] = `Bearer ${$token}`
|
||||||
|
}
|
||||||
|
|
||||||
|
return $token
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user