initial commit
This commit is contained in:
9
src/app.d.ts
vendored
Normal file
9
src/app.d.ts
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
// See https://kit.svelte.dev/docs/types#app
|
||||
// for information about these interfaces
|
||||
// and what to do when importing types
|
||||
declare namespace App {
|
||||
// interface Locals {}
|
||||
// interface PageData {}
|
||||
// interface Error {}
|
||||
// interface Platform {}
|
||||
}
|
12
src/app.html
Normal file
12
src/app.html
Normal file
@ -0,0 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" class="dark">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
|
||||
<meta name="viewport" content="width=device-width" />
|
||||
%sveltekit.head%
|
||||
</head>
|
||||
<body data-sveltekit-preload-data="hover" data-theme="wintry">
|
||||
<div style="display: contents">%sveltekit.body%</div>
|
||||
</body>
|
||||
</html>
|
4
src/app.postcss
Normal file
4
src/app.postcss
Normal file
@ -0,0 +1,4 @@
|
||||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
@tailwind variants;
|
1
src/lib/index.ts
Normal file
1
src/lib/index.ts
Normal file
@ -0,0 +1 @@
|
||||
// place files you want to import through the `$lib` alias in this folder.
|
6
src/lib/notif.ts
Normal file
6
src/lib/notif.ts
Normal file
@ -0,0 +1,6 @@
|
||||
export interface Notification {
|
||||
id: number,
|
||||
text: string,
|
||||
date: Date,
|
||||
status: { username: string, status: string }[],
|
||||
}
|
5
src/routes/+layout.svelte
Normal file
5
src/routes/+layout.svelte
Normal file
@ -0,0 +1,5 @@
|
||||
<script lang="ts">
|
||||
import '../app.postcss';
|
||||
</script>
|
||||
|
||||
<slot />
|
29
src/routes/+page.svelte
Normal file
29
src/routes/+page.svelte
Normal file
@ -0,0 +1,29 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
|
||||
//OnMount
|
||||
onMount(() => {
|
||||
console.log('Hello World!');
|
||||
|
||||
//Request notification permission
|
||||
if ('Notification' in window) {
|
||||
Notification.requestPermission();
|
||||
setTimeout(() => {
|
||||
new Notification('Hello World!');
|
||||
}, 5000);
|
||||
// new Notification('Hello World!');
|
||||
//Service Worker
|
||||
if ('serviceWorker' in navigator) {
|
||||
navigator.serviceWorker.register('src/sw.js');
|
||||
}
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<!-- YOU CAN DELETE EVERYTHING IN THIS PAGE -->
|
||||
|
||||
<div class="container h-full mx-auto flex justify-center items-center">
|
||||
<div class="space-y-5">
|
||||
<h1 class="h1">notification thing!</h1>
|
||||
</div>
|
||||
</div>
|
57
src/routes/notif/+server.ts
Normal file
57
src/routes/notif/+server.ts
Normal file
@ -0,0 +1,57 @@
|
||||
import { error } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
import type { Notification } from '$lib/notif';
|
||||
|
||||
let notifications: Notification[] = [
|
||||
{ id: 1, text: "This is a notification", date: new Date(), status: [{ username: "user1", status: "new" }, { username: "user2", status: "new" }] },
|
||||
{ id: 2, text: "This is another notification", date: new Date(), status: [{ username: "user1", status: "new" }, { username: "user2", status: "new" }] },
|
||||
{ id: 3, text: "This is a third notification", date: new Date(), status: [{ username: "user1", status: "new" }, { username: "user2", status: "new" }] },
|
||||
]
|
||||
|
||||
// GET handler
|
||||
// Returns JSON array of all notifications with "new" in the status dictionary for the current user
|
||||
// Sets the status of all notifications to "old"
|
||||
|
||||
export const GET: RequestHandler = ({ url }) => {
|
||||
const { searchParams } = new URL(url);
|
||||
const username = searchParams.get('username');
|
||||
if (!username) {
|
||||
return error(400, 'username is required');
|
||||
}
|
||||
const user_notifications = notifications.filter(notification => {
|
||||
// return true;
|
||||
const user_status = notification.status.find(status => status.username === username);
|
||||
if (user_status?.status === "new") {
|
||||
console.log("found one!")
|
||||
user_status.status = "old";
|
||||
//@ts-ignore
|
||||
// notifications.find(n => notification.id === n.id).status.find(status => status.username === username).status = "old";
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
return new Response(JSON.stringify(user_notifications), { headers: { 'content-type': 'application/json' } });
|
||||
};
|
||||
|
||||
|
||||
|
||||
// POST handler
|
||||
// Creates a new notification
|
||||
export const POST: RequestHandler = async ({ request }) => {
|
||||
|
||||
const notification = await request.json();
|
||||
|
||||
notification.id = notifications.length + 1;
|
||||
notification.date = new Date();
|
||||
notification.status = [{ username: "user1", status: "new" }, { username: "user2", status: "new" }];
|
||||
|
||||
notifications = [...notifications, notification];
|
||||
|
||||
return new Response(JSON.stringify(notification), {
|
||||
status: 201,
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
};
|
53
src/sw.js
Normal file
53
src/sw.js
Normal file
@ -0,0 +1,53 @@
|
||||
// @ts-nocheck
|
||||
|
||||
|
||||
// sw.js
|
||||
// self.addEventListener('install', event => {
|
||||
// event.waitUntil(
|
||||
// caches.open('static-cache').then(cache => {
|
||||
// // cache files
|
||||
// })
|
||||
// );
|
||||
// });
|
||||
|
||||
self.addEventListener('activate', event => {
|
||||
console.log('Service worker activating...');
|
||||
event.waitUntil(self.clients.claim());
|
||||
});
|
||||
|
||||
// self.addEventListener('fetch', event => {
|
||||
// event.respondWith(
|
||||
// caches.match(event.request).then(response => {
|
||||
// return response || fetch(event.request);
|
||||
// })
|
||||
// );
|
||||
// });
|
||||
|
||||
// Send notification after 5 seconds
|
||||
function notif() {
|
||||
console.log('Checking notifications...');
|
||||
fetch("/notif?username=user1").then((res) => {
|
||||
res.json().then((data) => {
|
||||
if (data.length) {
|
||||
for (const d of data) {
|
||||
console.log(d);
|
||||
self.registration.showNotification('New Notification', {
|
||||
body: d.text,
|
||||
});
|
||||
console.log('Sent notification');
|
||||
}
|
||||
} else {
|
||||
console.log('No new notifications');
|
||||
}
|
||||
});
|
||||
});
|
||||
// self.registration.showNotification('Test Notification', {
|
||||
// body: 'This is a test notification sent by the service worker!',
|
||||
// });
|
||||
setTimeout(() => {
|
||||
notif();
|
||||
}, 5000);
|
||||
}
|
||||
setTimeout(() => {
|
||||
notif();
|
||||
}, 5000);
|
Reference in New Issue
Block a user