Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/manage docs #43

Open
wants to merge 16 commits into
base: dev
Choose a base branch
from
24 changes: 24 additions & 0 deletions src/lib/api/getAll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export async function getAllUsers(token: string) {
try {
const response = await fetch(
'https://sucu-backend-2024-689509857491.asia-southeast1.run.app/api/v1/users',
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
}
}
);

if (!response.ok) {
const errorData = await response.json();
throw new Error(`Error: ${errorData.message}`);
}

const data = await response.json();
return data;
} catch (error) {
console.error('Failed to fetch users:', error);
}
}
22 changes: 13 additions & 9 deletions src/lib/components/Pagination/Pagination.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts">
import { createEventDispatcher } from 'svelte';
import { cn } from '../../utils/cn';
import Fa from 'svelte-fa';
import { faChevronLeft, faChevronRight } from '@fortawesome/free-solid-svg-icons';
Expand All @@ -12,9 +13,12 @@
let totalPages: number = Math.ceil(Arrayitem.length / parseInt(itemsPerPage));
let paginatedItems: string[] = [];

const dispatch = createEventDispatcher();

function changePage(page: number | string) {
currentPage = page;
paginateItems();
dispatch('pageChange', { paginatedItems, itemsPerPage });
}

function paginateItems() {
Expand Down Expand Up @@ -45,9 +49,17 @@
return pages;
}

function changeItemsPerPage(newItemsPerPage: string) {
itemsPerPage = newItemsPerPage;
totalPages = Math.ceil(Arrayitem.length / parseInt(itemsPerPage));
paginateItems();
dispatch('pageChange', { paginatedItems, itemsPerPage });
}

$: {
totalPages = Math.ceil(Arrayitem.length / parseInt(itemsPerPage));
paginateItems();
dispatch('pageChange', { paginatedItems, itemsPerPage });
}

$: if (!itemsPerPage || parseInt(itemsPerPage) < 1) {
Expand Down Expand Up @@ -100,18 +112,10 @@
items={pageChoice}
bind:currentChoice={itemsPerPage}
outerClass="w-20 bg-opacity-50 "
on:change={(e) => (itemsPerPage = e.detail)}
on:change={(e) => changeItemsPerPage(e.detail)}
/>
</div>
<div class="flex items-center">/ page</div>
</div>
</div>
</div>

<div class="items-list mt-5">
{#each paginatedItems as item}
<div class="item">
{item}
</div>
{/each}
</div>
69 changes: 63 additions & 6 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,68 @@
<script>
<script lang="ts">
import Footer from '$lib/components/Footer/Footer.svelte';
import Navbar from '$lib/components/Navbar.svelte';
import Sidebar from '$lib/components/Sidebar/Sidebar.svelte';
import { getMe } from '$lib/api/getme';
import { getDecryptedToken } from '$lib/api/getToken';
import '../styles/app.css';
import { onMount } from 'svelte';

let isAdminPage = false;
let isAuthLoginPage = false;
let menuItems = [
{ title: 'ประกาศ', href: '/admin' },
{ title: 'เอกสาร', href: '/admin/documents' },
{ title: 'สถิติ และ งบประมาณ', href: '/admin/stats' },
{ title: 'จัดการผู้ใช้งาน', href: '/admin/users' }
];

let user = {
name: 'Not found',
lastname: 'Not found',
role: 'Not found',
id: 'Not found'
};

onMount(async () => {
const pathname = window.location.pathname;
isAdminPage = pathname.startsWith('/admin');
isAuthLoginPage = pathname === '/auth/login';

if (isAdminPage) {
const token = getDecryptedToken();
if (token) {
try {
const userData = await getMe(token);
user = {
name: userData.result.first_name,
lastname: userData.result.last_name,
role: userData.result.role,
id: userData.result.id
};
} catch (error) {
console.error('Failed to fetch user data:', error);
}
} else {
console.log('No token found');
window.location.replace('/auth/login');
}
}
});
</script>

<div>
<Navbar />
<slot />
<Footer />
</div>
{#if isAdminPage}
<div class="min-h-screen bg-white z-50 w-full">
<Sidebar {menuItems} {user} />
<slot />
</div>
{:else if isAuthLoginPage}
<div>
<slot />
</div>
{:else}
<div>
<Navbar />
<slot />
<Footer />
</div>
{/if}
Loading
Loading