diff --git a/src/lib/common/Pager.svelte b/src/lib/common/Pager.svelte index 94d6dffe..92343d38 100644 --- a/src/lib/common/Pager.svelte +++ b/src/lib/common/Pager.svelte @@ -2,15 +2,36 @@ import { onMount } from 'svelte'; import Link from 'svelte-link'; - /** @type {import('$lib/types').Pagination} */ + /** @type {import('$lib/helpers/types').Pagination} */ export let pagination; + /** @type {(pageNum: number) => void} */ + export let pageTo; + + const firstPage = 1; + const offSet = 2; /** @type {number} */ $: totalPages = Math.ceil(pagination.count / pagination.size); /** @type {number} */ - $: offset = pagination.page * pagination.size; + $: start = (pagination.page - 1) * pagination.size + 1; + /** @type {number} */ + $: end = Math.min(pagination.page * pagination.size, pagination.count); + /** @type {number} */ + $: minPage = Math.max(Math.min(pagination.page - offSet, totalPages - 2 * offSet), firstPage); + /** @type {number} */ + $: maxPage = Math.min(Math.max(pagination.page + offSet, firstPage + 2 * offSet), totalPages); /** @type {number[]} */ - $: pages = Array.from(String(totalPages), Number); + $: pages = Array.from({ length: maxPage - minPage + 1 }, (_, i) => minPage + i); + + /** + * @param {any} e + * @param {number} pageNum + */ + function handlePageTo(e, pageNum) { + e.preventDefault(); + if (pagination.page === pageNum || pageNum < firstPage || pageNum > totalPages) return; + pageTo(pageNum); + } onMount(async () => { @@ -19,7 +40,7 @@
-

Showing {offset} to {offset + pagination.size} of {pagination.count} entries

+

Showing {start} to {end} of {pagination.count} entries

@@ -27,24 +48,32 @@
diff --git a/src/lib/scss/custom/components/_pagination.scss b/src/lib/scss/custom/components/_pagination.scss index b7b5d9f5..e7bda803 100644 --- a/src/lib/scss/custom/components/_pagination.scss +++ b/src/lib/scss/custom/components/_pagination.scss @@ -14,4 +14,9 @@ text-align: center; line-height: 32px; } - } \ No newline at end of file +} + +.page-link:focus { + outline: none; + box-shadow: none; +} \ No newline at end of file diff --git a/src/lib/services/conversation-service.js b/src/lib/services/conversation-service.js index 6b572f8b..557ae766 100644 --- a/src/lib/services/conversation-service.js +++ b/src/lib/services/conversation-service.js @@ -31,8 +31,8 @@ export async function getConversation(id) { */ export async function getConversations(filter) { let url = endpoints.conversationsUrl; - const response = await axios.get(url, { - params: filter + const response = await axios.post(url, { + ...filter }); return response.data; } diff --git a/src/routes/page/conversation/+page.svelte b/src/routes/page/conversation/+page.svelte index 061783f5..7df1b18e 100644 --- a/src/routes/page/conversation/+page.svelte +++ b/src/routes/page/conversation/+page.svelte @@ -10,7 +10,8 @@ DropdownToggle, Input, Row, - Table + Table, + Alert } from '@sveltestrap/sveltestrap'; import Breadcrumb from '$lib/common/Breadcrumb.svelte'; import HeadTitle from '$lib/common/HeadTitle.svelte'; @@ -20,48 +21,108 @@ import { getConversations, deleteConversation } from '$lib/services/conversation-service.js'; import { format } from '$lib/helpers/datetime'; + let isLoading = false; + let isComplete = false; + let isError = false; + const duration = 3000; + const firstPage = 1; + const pageSize = 10; + /** @type {import('$types').PagedItems} */ - let conversations = {count: 0, items: []}; + let conversations = { count: 0, items: [] }; + + /** @type {import('$types').ConversationFilter} */ + let initFilter = { + pager: { page: 1, size: pageSize, count: 0 } + }; /** @type {import('$types').ConversationFilter} */ - let filter = { - pager: { page: 0, size: 20, count: 0 } - }; + let filter = { ... initFilter }; /** @type {import('$types').Pagination} */ - let pager = filter.pager; + let pager = filter.pager; onMount(async () => { await getConversationList(); }); - function refreshPager() { - pager = { - page: filter.pager.page, - size: filter.pager.size, - count: conversations.count - } - } + /** @param {number} totalItemsCount */ + function refreshPager(totalItemsCount, page = firstPage, size = pageSize) { + pager = { + page: page, + size: pageSize, + count: totalItemsCount + }; + } async function getConversationList() { conversations = await getConversations(filter); + refreshPager(conversations.count, filter.pager.page, filter.pager.size); + } - refreshPager(); + async function refreshConversationList() { + filter = { ...initFilter }; + conversations = await getConversations(filter); + refreshPager(conversations.count); } /** @param {string} conversationId */ - async function handleConversationDeletion(conversationId) { - await deleteConversation(conversationId); - conversations.count--; - conversations.items = conversations.items.filter(x => x.id != conversationId); - refreshPager(); + function handleConversationDeletion(conversationId) { + isLoading = true; + deleteConversation(conversationId).then(async () => { + isLoading = false; + isComplete = true; + setTimeout(() => { + isComplete = false; + }, duration); + + await refreshConversationList(); + }).catch(err => { + isLoading = false; + isComplete = false; + isError = true; + setTimeout(() => { + isError = false; + }, duration); + }); } + + /** @param {number} pageNum */ + function pageTo(pageNum) { + pager = { + ...pager, + page: pageNum + }; + + filter = { + pager: pager + }; + + getConversationList(); + } - +{#if isLoading} + +
In Progress...
+
+{/if} + +{#if isComplete} + +
Update completed!
+
+{/if} + +{#if isError} + +
Error!
+
+{/if} + @@ -169,7 +230,7 @@
- +