-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6065 from owncloud/pagination-to-composable
[full-ci] refactor pagination into composable, cleanup store, views and tests
- Loading branch information
Showing
47 changed files
with
883 additions
and
555 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './useRouteQuery' | ||
export * from './useRouter' |
25 changes: 25 additions & 0 deletions
25
packages/web-app-files/src/composables/router/useRouteQuery.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { customRef, Ref } from '@vue/composition-api' | ||
import { useRouter } from './useRouter' | ||
|
||
type QueryValue = string | (string | null)[] | ||
|
||
export const useRouteQuery = (name: string, defaultValue?: QueryValue): Ref<QueryValue> => { | ||
const router = useRouter() | ||
|
||
return customRef<QueryValue>((track, trigger) => { | ||
router.afterEach((to, from) => to.query[name] !== from.query[name] && trigger()) | ||
|
||
return { | ||
get() { | ||
track() | ||
return router.currentRoute.query[name] || defaultValue | ||
}, | ||
async set(v) { | ||
await router.replace({ | ||
query: { [name]: v } | ||
}) | ||
trigger() | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { getCurrentInstance } from '@vue/composition-api' | ||
import VueRouter from 'vue-router' | ||
|
||
export const useRouter = (): VueRouter => { | ||
return getCurrentInstance().proxy.$router | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './mutationSubscription' | ||
export * from './store' | ||
export * from './useMutationSubscription' | ||
export * from './useStore' |
2 changes: 1 addition & 1 deletion
2
...composables/store/mutationSubscription.ts → ...posables/store/useMutationSubscription.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
38 changes: 38 additions & 0 deletions
38
packages/web-app-files/src/composables/useDefaults/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { customRef, Ref } from '@vue/composition-api' | ||
|
||
interface Defaults { | ||
pagination: { | ||
perPage: Ref | ||
} | ||
} | ||
|
||
let defaults: Defaults | ||
|
||
/** | ||
* interim solution till options // configuration api is in place | ||
*/ | ||
export const useDefaults = (): Defaults => { | ||
if (defaults) { | ||
return defaults | ||
} | ||
|
||
defaults = { | ||
pagination: { | ||
perPage: customRef<string>((track, trigger) => { | ||
const defaultValue = '100' | ||
return { | ||
get() { | ||
track() | ||
return window.localStorage.getItem('oc_filesPageLimit') || defaultValue | ||
}, | ||
set(v) { | ||
window.localStorage.setItem('oc_filesPageLimit', v) | ||
trigger() | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
return defaults | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/web-app-files/src/composables/usePagination/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { ref, computed, ComputedRef, unref } from '@vue/composition-api' | ||
import { MaybeRef } from '../utils' | ||
|
||
interface PaginationOptions<T> { | ||
page: MaybeRef<number> | ||
perPage: MaybeRef<number> | ||
items: MaybeRef<Array<T>> | ||
} | ||
|
||
interface PaginationResult<T> { | ||
items: ComputedRef<Array<T>> | ||
total: ComputedRef<number> | ||
} | ||
|
||
export function usePagination<T>(options: PaginationOptions<T>): PaginationResult<T> { | ||
const page = ref(options.page) | ||
const perPage = ref(options.perPage) | ||
const total = computed(() => Math.ceil(unref(options.items).length / perPage.value) || 1) | ||
const items = computed(() => { | ||
if (!perPage.value) { | ||
return unref(options.items) | ||
} | ||
|
||
const start = (page.value - 1) * perPage.value | ||
const end = start + perPage.value | ||
|
||
return unref(options.items).slice(start, end) | ||
}) | ||
|
||
return { | ||
items, | ||
total | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Ref } from '@vue/composition-api' | ||
|
||
export type MaybeRef<T> = T | Ref<T> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.