Skip to content

Commit

Permalink
Merge pull request #7 from kamyabnazari/kn-upload-avatar-functionality
Browse files Browse the repository at this point in the history
Kn upload Profile Avatar File and display
  • Loading branch information
kamyabnazari authored May 28, 2023
2 parents 8cda284 + 3c2a176 commit b665b46
Show file tree
Hide file tree
Showing 24 changed files with 1,076 additions and 128 deletions.
530 changes: 520 additions & 10 deletions frontend/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"type": "module",
"dependencies": {
"daisyui": "^2.51.6",
"pdfjs-dist": "^3.6.172",
"pocketbase": "^0.15.0",
"zod": "^3.21.4"
}
Expand Down
12 changes: 6 additions & 6 deletions frontend/src/lib/components/ActionCards.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@
import IconCreate from '~icons/solar/clipboard-add-outline';
</script>

<div class="card bg-base-100 w-96 flex-1 shadow-md">
<div class="card bg-base-100 w-96 flex-1 shadow-lg">
<div class="card-body">
<h2 class="card-title">Read a new file</h2>
<p>Upload a new file and ask it your questions!</p>
<div class="card-actions justify-end py-4">
<a href="/dashboard/file-read">
<a href="/dashboard/file-upload">
<button class="btn btn-primary gap-4">
<IconFile style="font-size: x-large" />
Read
<IconFile style="font-size: x-large" class="text-primary-content" />
Upload
</button></a
>
</div>
</div>
</div>
<div class="card bg-base-100 w-96 flex-1 shadow-md">
<div class="card bg-base-100 w-96 flex-1 shadow-lg">
<div class="card-body">
<h2 class="card-title">Create a new file</h2>
<p>Tell us what you want and create your file!</p>
<div class="card-actions justify-end py-4">
<a href="/dashboard/file-create">
<button class="btn btn-primary gap-4">
<IconCreate style="font-size: x-large" />
<IconCreate style="font-size: x-large" class="text-primary-content" />
Create
</button>
</a>
Expand Down
96 changes: 69 additions & 27 deletions frontend/src/lib/components/FileTable.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
<script lang="ts">
import IconPDF from '~icons/bxs/file-pdf';
import IconDownload from '~icons/solar/download-square-outline';
import IconBin from '~icons/solar/trash-bin-trash-outline';
import IconRead from '~icons/solar/chat-unread-outline';
import { currentUser, pb } from '$lib/pocketbase';
import type { Record } from 'pocketbase';
import { onMount } from 'svelte';
const urlPDF =
'https://raw.githubusercontent.com/vinodnimbalkar/svelte-pdf/369db2f9edbf5ab8c87184193e1404340729bb3a/public/sample.pdf';
onMount(async () => {
await fetchDocuments();
});
let documentList: Record[] = [];
async function fetchDocuments() {
try {
const response = await pb.collection('documents').getFullList({
sort: '-created',
filter: `owner='${$currentUser?.id}'`
});
documentList = response || [];
console.log(documentList);
} catch (error) {
console.error('Fetch error:', error);
}
}
const downloadPdf = () => {
window.open(urlPDF);
};
</script>

<div class="w-full overflow-x-auto">
<div class="w-full overflow-x-auto rounded-lg shadow-lg">
<table class="table w-full">
<!-- head -->
<thead>
Expand All @@ -15,35 +46,46 @@
<th>Name</th>
<th>Type</th>
<th>Create Date</th>
<th />
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr>
<th>
<label>
<input type="checkbox" class="checkbox" />
</label>
</th>
<td>
<div class="flex items-center space-x-3">
<div class="mask mask-square flex h-10 w-10 items-center justify-center">
<IconPDF style="font-size: x-large" />
{#each documentList as document}
<tr>
<th>
<label>
<input type="checkbox" class="checkbox" />
</label>
</th>
<td>
<div class="flex items-center space-x-3">
<div>
<div class="font-bold">{document.name}</div>
</div>
</div>
<div>
<div class="font-bold">file-23.pdf</div>
</td>
<td>
<span class="text-sm">Uploaded</span><br />
<span class="badge badge-ghost badge-sm">PDF</span>
</td>
<td>13.05.2023</td>
<th
><div class="flex flex-row gap-4">
<a href="/dashboard/file-read"
><button class="btn btn-square btn-primary"
><IconRead style="font-size: x-large;" /></button
></a
>
<button class="btn btn-square btn-info" on:click={downloadPdf}
><IconDownload style="font-size: x-large;" />
</button>
<button class="btn btn-square btn-warning"
><IconBin style="font-size: x-large;" /></button
>
</div>
</div>
</td>
<td>
<span class="text-sm">Uploaded</span><br />
<span class="badge badge-ghost badge-sm">PDF</span>
</td>
<td>13.05.2023</td>
<th>
<button class="btn btn-sm btn-ghost">Actions</button>
</th>
</tr>
</th>
</tr>
{/each}
</tbody>
<!-- foot -->
<tfoot>
Expand All @@ -52,7 +94,7 @@
<th>Name</th>
<th>Type</th>
<th>Create Date</th>
<th />
<th>Actions</th>
</tr>
</tfoot>
</table>
Expand Down
19 changes: 13 additions & 6 deletions frontend/src/lib/components/Navbar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import IconMoon from '~icons/solar/moon-outline';
import IconSun from '~icons/solar/sun-2-outline';
import { onMount, onDestroy } from 'svelte';
import { get, writable } from 'svelte/store';
import { writable } from 'svelte/store';
import { getImageURL } from '$lib/utils';
const theme = writable('lofi');
Expand Down Expand Up @@ -37,10 +38,10 @@
}
</script>

<div class="navbar text-base-content bg-base-100 sticky left-0 top-0 px-6 py-4 shadow-sm">
<div class="navbar text-base-content bg-base-100 sticky left-0 top-0 z-50 px-6 py-4 shadow-sm">
<div class="me-4 flex-none lg:hidden">
<label for="application-drawer" class="btn btn-square btn-ghost drawer-button">
<IconMenu style="font-size: x-large" />
<IconMenu style="font-size: x-large" class="text-primary" />
</label>
</div>
<div class="flex-1">
Expand All @@ -54,7 +55,13 @@
<!-- svelte-ignore a11y-label-has-associated-control -->
<label tabindex="0" class="btn btn-ghost btn-square avatar">
<div class="ring-primary ring-offset-base-100 w-8 rounded-md ring-2 ring-offset-2">
<img src="/example-avatar-image.jpeg" alt="Example Avatar" />
<img
src={$currentUser?.avatar
? getImageURL($currentUser?.collectionId, $currentUser?.id, $currentUser?.avatar)
: `https://ui-avatars.com/api/?name=${$currentUser?.name}`}
alt="user avatar"
id="avatar-preview-navbar"
/>
</div>
</label>
<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
Expand Down Expand Up @@ -90,7 +97,7 @@
</div>
<label class="swap swap-rotate px-4">
<input type="checkbox" bind:checked={isChecked} on:change={handleCheckboxChange} />
<IconSun class="swap-off fill-current" style="font-size: x-large" />
<IconMoon class="swap-on fill-current" style="font-size: x-large" />
<IconSun class="swap-off text-primary fill-current" style="font-size: x-large" />
<IconMoon class="swap-on text-warning fill-current" style="font-size: x-large" />
</label>
</div>
87 changes: 87 additions & 0 deletions frontend/src/lib/components/PDFViewer.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<script lang="ts">
import { onMount } from 'svelte';
import { GlobalWorkerOptions, getDocument, type PDFDocumentProxy } from 'pdfjs-dist';
import IconDownload from '~icons/solar/download-square-outline';
import IconPrint from '~icons/solar/printer-outline';
import IconLeftArrow from '~icons/solar/square-arrow-left-outline';
import IconRightArrow from '~icons/solar/square-arrow-right-outline';
let pdf: PDFDocumentProxy | null = null;
let currentPageNumber = 1;
let canvas: HTMLCanvasElement;
const urlPDF =
'https://raw.githubusercontent.com/vinodnimbalkar/svelte-pdf/369db2f9edbf5ab8c87184193e1404340729bb3a/public/sample.pdf';
const loadPage = async (pageNumber: number) => {
const page = await pdf.getPage(pageNumber);
const scale = 1;
const viewport = page.getViewport({ scale });
// Prepare canvas using PDF page dimensions
const context = canvas.getContext('2d') as CanvasRenderingContext2D;
canvas.height = viewport.height;
canvas.width = viewport.width;
// Render PDF page into canvas context
const renderContext = {
canvasContext: context,
viewport: viewport
};
await page.render(renderContext);
};
const prevPage = () => {
if (currentPageNumber > 1) {
currentPageNumber--;
loadPage(currentPageNumber);
}
};
const nextPage = () => {
if (currentPageNumber < pdf.numPages) {
currentPageNumber++;
loadPage(currentPageNumber);
}
};
const downloadPdf = () => {
window.open(urlPDF);
};
const printPdf = () => {
window.print();
};
onMount(async () => {
GlobalWorkerOptions.workerSrc =
'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.6.172/pdf.worker.js';
const loadingTask = getDocument(urlPDF);
pdf = await loadingTask.promise;
await loadPage(currentPageNumber);
});
</script>

<div class="ring-primary relative z-0">
<div class="flex flex-row justify-center gap-4 p-8">
<button class="btn btn-square" on:click={prevPage}
><IconLeftArrow style="font-size: x-large;" /></button
>
<button class="btn btn-square" on:click={nextPage}>
<IconRightArrow style="font-size: x-large;" /></button
>
<button class="btn btn-square btn-success" on:click={printPdf}
><IconPrint style="font-size: x-large;" /></button
>
<button class="btn btn-square btn-info" on:click={downloadPdf}
><IconDownload style="font-size: x-large;" /></button
>
</div>
<div class="ring-primary relative z-0 ring-2 ring-offset-4">
<canvas bind:this={canvas} class="w-full" />
</div>
</div>
24 changes: 16 additions & 8 deletions frontend/src/lib/components/Stats.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,45 @@
import IconPage from '~icons/solar/documents-outline';
</script>

<div class="stats flex-auto flex-nowrap shadow-md">
<div class="stats flex-auto flex-nowrap shadow-lg">
<div class="stat">
<div class="stat-figure text-secondary"><IconFile style="font-size: x-large;" /></div>
<div class="stat-figure text-secondary">
<IconFile style="font-size: x-large;" class="text-primary" />
</div>
<div class="stat-title">Uploaded files</div>
<div class="stat-value">118</div>
<div class="stat-desc">Jan 1st - Dec 1st</div>
</div>
<div class="stat">
<div class="stat-figure text-secondary"><IconNewFile style="font-size: x-large;" /></div>
<div class="stat-figure text-secondary">
<IconNewFile style="font-size: x-large;" class="text-primary" />
</div>
<div class="stat-title">Created files</div>
<div class="stat-value">24</div>
<div class="stat-desc flex flex-row gap-2">
<IconArrowUp style="font-size: small;" />
<IconArrowUp style="font-size: small;" class="text-success" />
<span>12 (22%)</span>
</div>
</div>
<div class="stat">
<div class="stat-figure text-secondary"><IconPage style="font-size: x-large;" /></div>
<div class="stat-figure text-secondary">
<IconPage style="font-size: x-large;" class="text-primary" />
</div>
<div class="stat-title">Total pages</div>
<div class="stat-value">459</div>
<div class="stat-desc flex flex-row gap-2">
<IconArrowDown style="font-size: small;" />
<IconArrowDown style="font-size: small;" class="text-warning" />
<span>52 (5%)</span>
</div>
</div>
<div class="stat">
<div class="stat-figure text-secondary"><IconWord style="font-size: x-large;" /></div>
<div class="stat-figure text-secondary">
<IconWord style="font-size: x-large;" class="text-primary" />
</div>
<div class="stat-title">Total words</div>
<div class="stat-value">24K</div>
<div class="stat-desc flex flex-row gap-2">
<IconArrowUp style="font-size: small;" />
<IconArrowUp style="font-size: small;" class="text-success" />
<span>2K (10%)</span>
</div>
</div>
Expand Down
34 changes: 17 additions & 17 deletions frontend/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/*
export const validateData = async (formData, schema) => {
const body = Object.fromEntries(formData);
export function showPreview(event: Event) {
const inputElement = event.target as HTMLInputElement;
const files = inputElement.files;
if (files?.length) {
const src = URL.createObjectURL(files[0]);
const previewProfile = document.getElementById('avatar-preview-profile') as HTMLImageElement;
const previewNavbar = document.getElementById('avatar-preview-navbar') as HTMLImageElement;

try {
const data = schema.parse(body);
return {
formData: data,
errors: null
};
} catch (err) {
console.log(err);
const errors = err.flatten();
return {
formData: body,
errors
};
previewProfile.src = src;
previewNavbar.src = src;
}
}

export const getImageURL = (collectionId, recordId, fileName, size = '0x0') => {
return `http://localhost:8090/api/files/${collectionId}/${recordId}/${fileName}?thumb=${size}`;
};

export const getDocumentURL = (collectionId, recordId, fileName, size = '0x0') => {
return `http://localhost:8090/api/files/${collectionId}/${recordId}/${fileName}?thumb=${size}`;
};
*/
Loading

0 comments on commit b665b46

Please sign in to comment.