Skip to content

Commit b0c0a75

Browse files
committed
Bug Fix: addData.errors.has not a function: StatePersistence plugin removed from root state, I made module Settings and stored localStorage.setItem('locale')
1 parent 5ca4f69 commit b0c0a75

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+4281
-908
lines changed

README.md

+135-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
npm run watch
99
```
1010

11-
> ## <h2 style="color:green;background:#eee;padding-left:13px;"> How to Sorting , Paginating and Filtering </h2>
11+
> ### <h2 style="color:green;background:#eee;padding-left:13px;"> How to Sorting , Paginating and Filtering </h2>
1212
</br>
1313
1414
### Step-1: Pagination.vue
@@ -406,11 +406,142 @@ export default {
406406
> OUTPUT
407407
[![ GET FILTER RESULT](docs/images/filter_result.png)](docs/images/filter_result.png)
408408

409-
> <p style="color:yellow;font-size:20px;">Now Preparing project for Localization & Translating</p>
409+
> ## <p style="color:green;font-size:20px;">Now Preparing project for Localization & Translating</p>
410+
410411

411412
Run this npm command
412413

413414
npm install vue-i18n vuex-persistedstate
414415

415-
# Here is final output of categories.vue
416-
[![Categories OUTPUT](docs/images/categories.vue.png)](docs/images/categories.vue.png)
416+
### Create i18n.js ( It acts like plugin)
417+
418+
``` javascript
419+
import Vue from'vue'
420+
import VueI18n from 'vue-i18n'
421+
422+
Vue.use(VueI18n);
423+
function checkDefaultLanguage() {
424+
let matched = null
425+
let languages = Object.getOwnPropertyNames(loadLocaleMessages())
426+
languages.forEach(lang => {
427+
if (lang === navigator.language) {
428+
matched = lang
429+
}
430+
})
431+
if (!matched) {
432+
languages.forEach(lang => {
433+
let languagePartials = navigator.language.split('-')[0]
434+
if (lang === languagePartials) {
435+
matched = lang
436+
}
437+
})
438+
}
439+
return matched
440+
}
441+
function loadLocaleMessages() {
442+
const locales = require.context(
443+
"./locales",
444+
true,
445+
/[A-Za-z0-9-_,\s]+\.json$/i
446+
);
447+
const messages = {};
448+
locales.keys().forEach(key => {
449+
const matched = key.match(/([A-Za-z0-9-_]+)\./i);
450+
if (matched && matched.length > 1) {
451+
const locale = matched[1];
452+
messages[locale] = locales(key);
453+
}
454+
});
455+
return messages;
456+
}
457+
export const selectedLocale = checkDefaultLanguage() || process.env.VUE_APP_I18N_LOCALE || 'en'
458+
export const languages = Object.getOwnPropertyNames(loadLocaleMessages())
459+
460+
export default new VueI18n({
461+
locale: selectedLocale,
462+
fallbackLocale: process.env.VUE_APP_I18N_FALLBACK_LOCALE || 'en',
463+
messages: loadLocaleMessages()
464+
})
465+
466+
```
467+
468+
Create `vue.config.js`
469+
``` js
470+
module.exports = {
471+
pluginOptions: {
472+
i18n: {
473+
locale: "en",s
474+
fallbackLocale: "en",
475+
localeDir: "locales",
476+
enableInSFC: false
477+
}
478+
}
479+
};
480+
```
481+
Folder Structure must like this (@ above code )
482+
483+
[![](docs/images/localization_folder_structure.png)](docs/images/localization_folder_structure.png)
484+
485+
## STORE
486+
487+
``` js
488+
// store.admin.js
489+
import Vue from 'vue';
490+
import Vuex from 'vuex';
491+
// import axios from "axios";
492+
import createPersistedState from 'vuex-persistedstate'
493+
import i18n, { selectedLocale } from './i18n'
494+
Vue.use(Vuex);
495+
496+
export default new Vuex.Store({
497+
state: {
498+
locale: selectedLocale
499+
},
500+
actions: {
501+
changeLocale({ commit }, newLocale) {
502+
i18n.locale = newLocale // important!
503+
commit('UPDATE_LOCALE', newLocale)
504+
}
505+
},
506+
mutations: {
507+
UPDATE_LOCALE(state, newLocale) {
508+
state.locale = newLocale
509+
}
510+
},
511+
plugins: [createPersistedState()]
512+
});
513+
514+
515+
```
516+
517+
``` js
518+
//admin.js
519+
520+
import router from './router.admin'
521+
import i18n, { selectedLocale } from './i18n'
522+
// IMPORT THE STORE
523+
import store from './store.admin';
524+
525+
router.beforeEach((to, from, next) => {
526+
if (store.state.locale !== selectedLocale) {
527+
store.dispatch('changeLocale' , store.state.locale)
528+
}
529+
}
530+
```
531+
532+
># Here is final output of categories
533+
534+
[![Categories OUTPUT](docs/images/categories.vue.png)](docs/images/categories.vue.png)
535+
536+
> # PHP Artisan Clear Commands
537+
php artisan config:clear && php artisan route:clear && php artisan view:clear
538+
539+
540+
So, what's NeXT ?
541+
* Cloud Storage (AWS S3)
542+
* Caching , Queue with redis
543+
* Emailing , Event Listener , Observer ?? Notifications
544+
545+
<p> :smile: much much fun are coming.. so stay with me. </p>
546+
547+
<h4>Happy Coding :smile: </h4>

app/Events/AdminLoginAlert.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Broadcasting\PresenceChannel;
8+
use Illuminate\Broadcasting\PrivateChannel;
9+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
10+
use Illuminate\Foundation\Events\Dispatchable;
11+
use Illuminate\Queue\SerializesModels;
12+
13+
class AdminLoginAlert
14+
{
15+
use Dispatchable, InteractsWithSockets, SerializesModels;
16+
public $loggedInAdmin;
17+
18+
/**
19+
* Create a new event instance.
20+
*
21+
* @return void
22+
*/
23+
public function __construct($loggedInAdmin)
24+
{
25+
$this->loggedInAdmin = $loggedInAdmin;
26+
}
27+
28+
/**
29+
* Get the channels the event should broadcast on.
30+
*
31+
* @return \Illuminate\Broadcasting\Channel|array
32+
*/
33+
public function broadcastOn()
34+
{
35+
return new PrivateChannel('channel-name');
36+
}
37+
}

app/Http/Controllers/AdminController.php

+17-11
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22

33
namespace App\Http\Controllers;
44

5+
// use App\Events\AdminLoginAlert as EventsAdminLoginAlert;
6+
// use App\Jobs\SendEmailJobs;
7+
// use App\Mail\AdminLoginAlert;
58
use App\Models\Admin;
69
use Illuminate\Http\Request;
710
use Illuminate\Support\Facades\Validator;
811
use Illuminate\Support\Facades\Auth;
912
use App\Providers\RouteServiceProvider;
1013
use Illuminate\Foundation\Auth\AuthenticatesUsers;
1114

15+
16+
use Illuminate\Support\Facades\Mail;
17+
1218
class AdminController extends Controller
13-
{
19+
{
1420
use AuthenticatesUsers;
1521

1622
// protected $redirectTo = '/admin';
@@ -20,16 +26,13 @@ public function __construct()
2026
// $this->middleware('guest:admin')->except('logout');
2127

2228
}
23-
29+
2430
public function getContent()
2531
{
2632
return \view('admin_layout');
2733
}
28-
29-
public function getLoginForm()
30-
{
31-
return \view('admin_login');
32-
}
34+
35+
3336
protected function createAdmin(Request $request)
3437
{
3538
$this->validator($request->all())->validate();
@@ -60,7 +63,7 @@ public function logout()
6063
}
6164
protected function guard(){
6265
return Auth::guard('admin');
63-
}
66+
}
6467

6568
/**
6669
* Store a newly created resource in storage.
@@ -84,19 +87,22 @@ public function login(Request $request)
8487
}
8588

8689
if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)) {
87-
90+
// email me /
91+
$admin = Auth::guard('admin')->user();
92+
// event(new EventsAdminLoginAlert($admin));
93+
// dispatch(new SendEmailJobs($admin));
8894
return response()->json([
8995
'success' => true ,
9096
'message' => 'Admin Login Successfully',
91-
'user' => Auth::guard('admin')->user()
97+
'user' => $admin
9298
], 200);
9399
}
94100
return response()->json([
95101
'errors' => [
96102
'password' => ['Your Password is incorrect']
97103
]
98104
] , 422);
99-
105+
100106
}
101107

102108

app/Jobs/SendEmailJobs.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use App\Mail\AdminAlertMail;
6+
use App\Models\Admin;
7+
use Illuminate\Bus\Queueable;
8+
use Illuminate\Contracts\Queue\ShouldQueue;
9+
use Illuminate\Foundation\Bus\Dispatchable;
10+
use Illuminate\Queue\InteractsWithQueue;
11+
use Illuminate\Queue\SerializesModels;
12+
use Illuminate\Support\Facades\Mail;
13+
use Illuminate\Support\Facades\Storage;
14+
15+
class SendEmailJobs implements ShouldQueue
16+
{
17+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
18+
public $loggedInAdmin;
19+
/**
20+
* Create a new job instance.
21+
*
22+
* @return void
23+
*/
24+
public function __construct($loggedInAdmin)
25+
{
26+
$this->loggedInAdmin = $loggedInAdmin;
27+
}
28+
29+
/**
30+
* Execute the job.
31+
*
32+
* @return void
33+
*/
34+
public function handle()
35+
{
36+
if($this->loggedInAdmin->is_super != 1){
37+
$superAdmins = Admin::where('is_super',1)->get();
38+
foreach ($superAdmins as $to) {
39+
Mail::to($to->email)->send(new AdminAlertMail($this->loggedInAdmin));
40+
Storage::disk('public')->put('log.json',`Logged In Admin: `.$to->email);
41+
}
42+
}
43+
}
44+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace App\Listeners;
4+
5+
use App\Events\AdminLoginAlert;
6+
use App\Mail\AdminAlertMail;
7+
use App\Models\Admin;
8+
use Illuminate\Contracts\Queue\ShouldQueue;
9+
use Illuminate\Queue\InteractsWithQueue;
10+
use Illuminate\Support\Facades\Mail;
11+
use Illuminate\Support\Facades\Storage;
12+
use Log;
13+
14+
class AdminLoginAlertListener implements ShouldQueue
15+
{
16+
/**
17+
* Create the event listener.
18+
*
19+
* @return void
20+
*/
21+
public function __construct()
22+
{
23+
}
24+
25+
/**
26+
* Handle the event.
27+
*
28+
* @param AdminLoginAlert $event
29+
* @return void
30+
*/
31+
public function handle($event)
32+
{
33+
if($event->loggedInAdmin->is_super != 1){
34+
$superAdmins = Admin::where('is_super',1)->get();
35+
foreach ($superAdmins as $to) {
36+
Mail::to($to->email)->send(new AdminAlertMail($event->loggedInAdmin));
37+
Storage::disk('public')->put('log.json',`Logged In Admin: `.$to->email);
38+
}
39+
}
40+
41+
}
42+
}

app/Mail/AdminAlertMail.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace App\Mail;
4+
5+
use App\Listeners\AdminLoginAlertListener;
6+
use Illuminate\Bus\Queueable;
7+
use Illuminate\Contracts\Queue\ShouldQueue;
8+
use Illuminate\Mail\Mailable;
9+
use Illuminate\Queue\SerializesModels;
10+
use Illuminate\Support\Facades\Log;
11+
12+
class AdminAlertMail extends Mailable
13+
{
14+
use Queueable, SerializesModels;
15+
public $loggedUser;
16+
17+
/**
18+
* Create a new message instance.
19+
*
20+
* @return void
21+
*/
22+
public function __construct($loggedUser)
23+
{
24+
$this->loggedUser = $loggedUser;
25+
}
26+
27+
/**
28+
* Build the message.
29+
*
30+
* @return $this
31+
*/
32+
public function build()
33+
{
34+
return $this->markdown('emails.admin_log_mail')->with([
35+
'email' => $this->loggedUser->email,
36+
'name' => $this->loggedUser->name
37+
]);
38+
}
39+
}

0 commit comments

Comments
 (0)