feat: broadcasting item to refresh pages

This commit is contained in:
2022-11-13 21:04:45 -03:00
parent 567adce732
commit 7c09fc4896
21 changed files with 1354 additions and 135 deletions

2
resources/js/app.js vendored
View File

@@ -5,7 +5,7 @@
* building robust, powerful web applications using Vue and Laravel.
*/
require('./bootstrap');
import './bootstrap';
// window.Vue = require('vue');

View File

@@ -1,18 +1,5 @@
window._ = require('lodash');
window.Popper = require('popper.js').default;
/**
* We'll load jQuery and the Bootstrap jQuery plugin which provides support
* for JavaScript based Bootstrap features such as modals and tabs. This
* code may be modified to fit the specific needs of your application.
*/
try {
window.$ = window.jQuery = require('jquery');
require('bootstrap');
} catch (e) {}
import _ from 'lodash';
window._ = _;
/**
* We'll load the axios HTTP library which allows us to easily issue requests
@@ -20,37 +7,28 @@ try {
* CSRF token as a header based on the value of the "XSRF" token cookie.
*/
window.axios = require('axios');
import axios from 'axios';
window.axios = axios;
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
/**
* Next we will register the CSRF Token as a common header with Axios so that
* all outgoing HTTP requests automatically have it attached. This is just
* a simple convenience so we don't have to attach every token manually.
*/
let token = document.head.querySelector('meta[name="csrf-token"]');
if (token) {
window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}
/**
* Echo exposes an expressive API for subscribing to channels and listening
* for events that are broadcast by Laravel. Echo and event broadcasting
* allows your team to easily build robust real-time web applications.
*/
// import Echo from 'laravel-echo'
import Echo from 'laravel-echo';
// window.Pusher = require('pusher-js');
import Pusher from 'pusher-js';
window.Pusher = Pusher;
// window.Echo = new Echo({
// broadcaster: 'pusher',
// key: process.env.MIX_PUSHER_APP_KEY,
// cluster: process.env.MIX_PUSHER_APP_CLUSTER,
// encrypted: true
// });
window.Echo = new Echo({
broadcaster: 'pusher',
key: import.meta.env.VITE_PUSHER_APP_KEY,
wsHost: import.meta.env.VITE_PUSHER_HOST ? import.meta.env.VITE_PUSHER_HOST : `ws-${import.meta.env.VITE_PUSHER_APP_CLUSTER}.pusher.com`,
wsPort: import.meta.env.VITE_PUSHER_PORT ?? 80,
wssPort: import.meta.env.VITE_PUSHER_PORT ?? 443,
forceTLS: (import.meta.env.VITE_PUSHER_SCHEME ?? 'https') === 'https',
enabledTransports: ['ws', 'wss'],
});

10
resources/js/pusher.js vendored Normal file
View File

@@ -0,0 +1,10 @@
import 'https://js.pusher.com/7.2/pusher.min.js';
var pusher = new Pusher('93b3e504421787295454', {
cluster: 'us2'
});
var channel = pusher.subscribe('touchedItem');
channel.bind('RefreshPage', function(data) {
window.location.reload();
console.log(JSON.stringify(data));
});

View File

@@ -2,15 +2,7 @@
@section('content')
<script type="text/javascript">
setInterval(
function() {
if (!document.hasFocus() ) {
window.location.reload(true);
}
},
2*60000); //NOTE: period is passed in milliseconds
</script>
@vite('resources/js/pusher.js')
<div class="container">
<div class="row justify-content-center">
@@ -53,4 +45,4 @@
</div>
</div>
</div>
@endsection
@endsection

View File

@@ -11,7 +11,7 @@
<title>{{ config('app.name', 'Laravel') }} {{ isset($usedItems) && $usedItems > 0 ? "(${usedItems})" : '' }}</title>
<!-- Scripts -->
<script src="{{ asset('js/app.js') }}" defer></script>
@vite('resources/js/app.js')
<!-- Fonts -->
<link rel="dns-prefetch" href="https://fonts.gstatic.com">
@@ -88,4 +88,4 @@
</div>
</body>
</html>
</html>