Skip to content

Commit

Permalink
fix: Infinite scrolling
Browse files Browse the repository at this point in the history
Remove scrolling

New scrolling

Temp. workaround for double toastrs

Clean up
  • Loading branch information
ChrisToxz committed Jul 17, 2023
1 parent 22a1c5d commit 78c86d6
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 51 deletions.
12 changes: 9 additions & 3 deletions app/Http/Controllers/SlipController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@

class SlipController extends Controller
{
public function index()
public function index(Request $request)
{

$slips = Slip::latest()->with('mediable')->paginate(6);
$slips = Slip::latest()->with('mediable')
->paginate(3)
->withQueryString();

if ($request->wantsJson()) {
return $slips;
}

return inertia('Dashboard', [
'slips' => $slips
Expand All @@ -43,7 +49,7 @@ public function store(Request $request)
*/

$type = $request->get('type');

$request->validate([
'title' => 'nullable|string|max:200',
'description' => 'nullable|string|max:200',
Expand Down
1 change: 1 addition & 0 deletions resources/js/Components/Dashboard/VideoCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,6 @@ const TypeIcon = computed(() => {
<!-- <video v-if="hoverEffect" ref="video" class="`transition-all duration-200 rounded-lg object-cover h-full w-full transition-all duration-500 ease-in-out -z-[1]" :src="slip.mediable.path" controls autoplay />-->
<DeleteSlipModal v-if="showDeleteModal" :slip="slip" @close="showDeleteModal = false" />
</span>
<slot />
</div>
</template>
1 change: 0 additions & 1 deletion resources/js/Components/Upload/UploadModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ const saveMedia = () => {
form.post('/slips', {
onSuccess: () => closeModal(),
})
closeModal()
}
const closeModal = () => {
Expand Down
4 changes: 3 additions & 1 deletion resources/js/Layouts/MainLayout.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<script setup>
import { ref } from 'vue'
import {ref} from 'vue'
import NavItem from '@/Components/Reusable/NavItem.vue'
import ApplicationLogo from '@/Components/Reusable/ApplicationLogo.vue'
import UploadModal from '@/Components/Upload/UploadModal.vue'
import SettingsModal from '@/Components/Settings/SettingsModal.vue'
import {Vue3Snackbar} from 'vue3-snackbar'
let showUploadModal = ref(false)
let showSettingsModal = ref(false)
Expand Down Expand Up @@ -39,6 +40,7 @@ let showSettingsModal = ref(false)
<transition name="modal">
<SettingsModal v-if="showSettingsModal" @close="showSettingsModal = false" />
</transition>
<vue3-snackbar top right />
</template>

<style scoped>
Expand Down
89 changes: 43 additions & 46 deletions resources/js/Pages/Dashboard.vue
Original file line number Diff line number Diff line change
@@ -1,46 +1,45 @@
<script setup>
import axios from 'axios'
import {Head, router, usePage} from '@inertiajs/vue3'
import {useSnackbar, Vue3Snackbar} from 'vue3-snackbar'
import {useSnackbar} from 'vue3-snackbar'
import MainLayout from '@/Layouts/MainLayout.vue'
import VideoCard from '@/Components/Dashboard/VideoCard.vue'
import {onMounted, reactive, ref} from 'vue'
import {computed, onMounted, ref} from 'vue'
const snackbar = useSnackbar()
const props = defineProps({
slips: Object,
})
const slips = ref(computed(() => usePage().props.slips))
let allSlips = reactive({data: props.slips.data})
const initUrl = usePage().url
const isFetching = ref(false)
const loadMoreIntersect = ref(null)
function loadMoreSlips() {
if (props.slips.next_page_url === null) {
return
}
router.get(props.slips.next_page_url, {}, {
preserveState: true,
preserveScroll: true,
only: ['slips'],
onSuccess: (page) => {
allSlips.data = [...allSlips.data, ...page.props.slips.data]
window.history.replaceState({}, '' , initUrl)
},
})
}
const fetchSlips = async () => {
const next_url = slips.value.next_page_url
if(!next_url) return
isFetching.value = true
await axios.get(next_url)
.then((response) => {
slips.value.data.push(...response.data.data)
slips.value.next_page_url = response.data.next_page_url
console.log(slips.value.meta)
isFetching.value = false
})
}
onMounted(() => {
const observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
loadMoreSlips()
fetchSlips()
}
})
}, {
rootMargin: '-100px 0px 0px 0px',
})
observer.observe(loadMoreIntersect.value)
})
/* Websockets
TODO: Move to composable
*/
Expand All @@ -49,28 +48,18 @@ window.Echo.channel('ss').listen('SlipProcessFinished', (e) => {
preserveState: true,
only:['slips'],
onSuccess: (page) => {
allSlips.data = page.props.slips.data
},
})
if(!e.failed){
snackbar.add({
type:'success',
text: 'Slip successfully processed',
})
}else{
snackbar.add({
type:'error',
text: 'Processing failed for ' + e.slip.title,
})
}
})
window.Echo.channel('ss').listen('SlipUploaded', (e) => {
router.reload({
preserveState: true,
only: ['slips'],
onSuccess: (page) => {
allSlips.data = page.props.slips.data
// I had to move this to success, so toastr doesn't appear double
if(!e.failed){
snackbar.add({
type:'success',
text: 'Slip successfully processed',
})
}else{
snackbar.add({
type:'error',
text: 'Processing failed for ' + e.slip.title,
})
}
},
})
})
Expand All @@ -81,12 +70,20 @@ window.Echo.channel('ss').listen('SlipUploaded', (e) => {
<MainLayout>
<div class="w-full flex justify-center">
<div class="w-[calc(100%-3rem)] grid lg:grid-cols-3 md:grid-cols-2 grid-cols-1 gap-7">
<VideoCard v-for="slip in allSlips.data" :key="slip.token" :slip="slip" />
<VideoCard v-for="slip in slips.data" :key="slip.token" :slip="slip" />
</div>
</div>
<span ref="loadMoreIntersect" />
<div class="flex items-center justify-center p-8">
<div v-if="isFetching">
<svg aria-hidden="true" class="w-8 h-8 mr-2 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor" />
<path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill" />
</svg>
<span class="sr-only">Loading more...</span>
</div>
</div>
</MainLayout>
<vue3-snackbar top right />
</template>
<style>
</style>

0 comments on commit 78c86d6

Please sign in to comment.