Skip to content

Commit

Permalink
2.4.42
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilan-riviere committed Jun 6, 2024
1 parent ab0f745 commit 743939a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@kiwilan/typescriptable-laravel",
"type": "module",
"version": "2.4.41",
"version": "2.4.42",
"description": "Add some helpers for your Inertia app with TypeScript.",
"author": "Ewilan Rivière <ewilan.riviere@gmail.com>",
"license": "MIT",
Expand Down
16 changes: 12 additions & 4 deletions plugin/src/composables/usePagination.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { computed, ref } from 'vue'
import { usePage } from '@inertiajs/vue3'
import { computed, onMounted, ref } from 'vue'

interface PaginateLink extends App.PaginateLink {
isLink?: boolean
Expand Down Expand Up @@ -109,10 +110,13 @@ export function usePagination(models: App.Paginate) {
nextPage.value = models.next_page_url ? `${models.next_page_url}&${getQuery()}` : undefined
}

paginate()

function convertUrl(queryName: string, queryValue: number | string) {
let currentUrl = window?.location.href
const { url, props } = usePage()
const baseURL = (props.ziggy as any)?.url
if (!baseURL)
return ''

let currentUrl = `${baseURL}${url}`
if (currentUrl.includes(`${queryName}=`))
currentUrl = currentUrl.replace(/page=\d+/, `${queryName}=${queryValue}`)
else if (currentUrl.includes('?'))
Expand All @@ -131,6 +135,10 @@ export function usePagination(models: App.Paginate) {
return convertUrl('page', models.current_page - 1)
})

onMounted(() => {
paginate()
})

return {
pages,
previousPage,
Expand Down
31 changes: 19 additions & 12 deletions plugin/src/composables/useQuery.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useForm } from '@inertiajs/vue3'
import { ref } from 'vue'
import { useForm, usePage } from '@inertiajs/vue3'
import { onMounted, ref } from 'vue'

export interface Query<T = any> extends App.Paginate<T> {
sort?: string
Expand All @@ -11,14 +11,14 @@ export interface SortItem {
value: string
}

const current = ref<Query>()
const total = ref<number>()
const isCleared = ref<boolean>(false)
const sort = ref<string>()
const limit = ref<number>(10)
const isReversed = ref(false)

export function useQuery<T>(propQuery: App.Paginate<T>, prop: string = 'query') {
const current = ref<Query<T>>()
const total = ref<number>()
const isCleared = ref<boolean>(false)
const sort = ref<string>()
const limit = ref<number>(10)
const isReversed = ref(false)

current.value = propQuery
total.value = propQuery.total
sort.value = current.value.sort
Expand All @@ -28,7 +28,12 @@ export function useQuery<T>(propQuery: App.Paginate<T>, prop: string = 'query')
* Set the sort value to the query.
*/
function initializeSort() {
const query = new URLSearchParams(window?.location.search)
const { url } = usePage()
let search: string | undefined
if (url.includes('?'))
search = `?${url.split('?')[1]}`

const query = new URLSearchParams(search)
const querySort = query.get('sort')
if (querySort)
sort.value = querySort
Expand Down Expand Up @@ -170,10 +175,12 @@ export function useQuery<T>(propQuery: App.Paginate<T>, prop: string = 'query')
}
}

initializeSort()
onMounted(() => {
initializeSort()
})

return {
request: current,
query: current,
total,
clear,
sortBy,
Expand Down

0 comments on commit 743939a

Please sign in to comment.