Handle Language setting from Laravel side

This commit is contained in:
Bubka 2020-02-27 12:18:46 +01:00
parent 53d8cce838
commit b30219f407
8 changed files with 100 additions and 4 deletions

View File

@ -0,0 +1,39 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class SettingController extends Controller
{
/**
* Get options
* @return [type] [description]
*/
public function index()
{
// Fetch all setting values
$settings = DB::table('options')->get();
return response()->json(['settings' => $settings], 200);
}
/**
* Save options
* @return [type] [description]
*/
public function store(Request $request)
{
// Store all setting values
foreach($request->all() as $opt => $val) {
option([$opt => $val]);
$settings[$opt] = option($opt);
}
return response()->json(['message' => __('settings.forms.setting_saved'), 'settings' => $settings], 200);
}
}

View File

@ -19,6 +19,7 @@ class Kernel extends HttpKernel
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\TrustProxies::class,
\App\Http\Middleware\SetLanguage::class,
];
/**

View File

@ -0,0 +1,22 @@
<?php
namespace App\Http\Middleware;
use Closure;
class SetLanguage
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
\App::setLocale(option('lang', 'en'));
return $next($request);
}
}

View File

@ -76,7 +76,8 @@ export default {
"language": "Language",
"confirm": [],
"forms": {
"edit_settings": "Edit settings"
"edit_settings": "Edit settings",
"setting_saved": "Settings saved"
}
},
"twofaccounts": {
@ -323,7 +324,8 @@ export default {
"language": "Langue",
"confirm": [],
"forms": {
"edit_settings": "Modifier les réglages"
"edit_settings": "Modifier les réglages",
"setting_saved": "Réglages sauvegardés"
}
},
"twofaccounts": {

View File

@ -17,7 +17,7 @@
success: '',
fail: '',
form: new Form({
lang: 'fr'
lang: ''
}),
options: [
{ text: this.$t('languages.en'), value: 'en' },
@ -26,8 +26,35 @@
}
},
methods : {
async mounted() {
const { data } = await this.axios.get('/api/settings')
data.settings.forEach((setting) => {
this.form[setting.key] = setting.value
})
},
methods : {
handleSubmit(e) {
e.preventDefault()
this.fail = ''
this.success = ''
this.form.post('/api/settings', {returnError: true})
.then(response => {
this.$router.go()
})
.catch(error => {
if( error.response.status === 400 ) {
this.fail = error.response.data.message
}
else if( error.response.status !== 422 ) {
this.$router.push({ name: 'genericError', params: { err: error.response } });
}
});
}
},
}
</script>

View File

@ -22,6 +22,7 @@
],
'forms' => [
'edit_settings' => 'Edit settings',
'setting_saved' => 'Settings saved',
],

View File

@ -22,6 +22,7 @@
],
'forms' => [
'edit_settings' => 'Modifier les réglages',
'setting_saved' => 'Réglages sauvegardés',
],

View File

@ -31,6 +31,9 @@
Route::patch('user', 'UserController@update');
Route::get('user', 'UserController@getDetails');
Route::get('settings', 'SettingController@index');
Route::post('settings', 'SettingController@store');
Route::delete('twofaccounts/batch', 'TwoFAccountController@batchDestroy');
Route::apiResource('twofaccounts', 'TwoFAccountController');
Route::post('twofaccounts/otp', 'TwoFAccountController@generateOTP')->name('twofaccounts.generateOTP');