Skip to content

Commit

Permalink
Update VSCode settings and add SW component
Browse files Browse the repository at this point in the history
  • Loading branch information
m7medVision committed Jan 22, 2024
1 parent 27a5f42 commit 4c00e3a
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"editor.defaultFormatter": "denoland.vscode-deno"
},
"[javascript]": {
"editor.defaultFormatter": "denoland.vscode-deno"
"editor.defaultFormatter": "vscode.typescript-language-features"
},
"css.customData": [
".vscode/tailwind.json"
Expand Down
9 changes: 2 additions & 7 deletions components/Courses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Course, CourseGroup } from "@/utils/types.ts";
import Collapse from "./Collapse.tsx";
import CourseCard from "./CourseCard.tsx";
import ProgressSection from "@/islands/ProgressSection.tsx";
import SW from "@/islands/SW.tsx";

export default function Courses(
{ courses, total }: {
Expand All @@ -18,13 +19,7 @@ export default function Courses(
total={total}
/>
</div>
{
/* <div className="mt-5">
<h1 class="w-1/2 max-md:w-full text-5xl font-bold z-10 mb-2 my-auto">
الاساسيات
</h1>
</div> */
}
<SW />
<section class="flex flex-col gap-2 mb-4 mt-6">
{courses.map((course, index) => {
// Group of courses
Expand Down
4 changes: 4 additions & 0 deletions fresh.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import * as $_layout from "./routes/_layout.tsx";
import * as $about from "./routes/about.tsx";
import * as $group_slug_ from "./routes/group/[slug].tsx";
import * as $index from "./routes/index.tsx";
import * as $offline from "./routes/offline.tsx";
import * as $Editor from "./islands/Editor.tsx";
import * as $ProgressBar from "./islands/ProgressBar.tsx";
import * as $ProgressCheck from "./islands/ProgressCheck.tsx";
import * as $ProgressSection from "./islands/ProgressSection.tsx";
import * as $SW from "./islands/SW.tsx";
import * as $ThemeToggle from "./islands/ThemeToggle.tsx";
import * as $Toast from "./islands/Toast.tsx";
import * as $signals_store from "./islands/signals/store.ts";
Expand All @@ -26,12 +28,14 @@ const manifest = {
"./routes/about.tsx": $about,
"./routes/group/[slug].tsx": $group_slug_,
"./routes/index.tsx": $index,
"./routes/offline.tsx": $offline,
},
islands: {
"./islands/Editor.tsx": $Editor,
"./islands/ProgressBar.tsx": $ProgressBar,
"./islands/ProgressCheck.tsx": $ProgressCheck,
"./islands/ProgressSection.tsx": $ProgressSection,
"./islands/SW.tsx": $SW,
"./islands/ThemeToggle.tsx": $ThemeToggle,
"./islands/Toast.tsx": $Toast,
"./islands/signals/store.ts": $signals_store,
Expand Down
28 changes: 28 additions & 0 deletions islands/SW.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useState } from "preact/hooks";
import { useToast } from "./useToast.ts"
function SW() {
const [loading, setLoading] = useState(false)
const { showToast } = useToast()
async function handleInstall() {
const resp = await fetch("/sw-cache.json")
const data = await resp.json()
const keys = await caches.keys()
for (const key of keys) {
await caches.delete(key)
}
const cache = await caches.open("cache-v1")
await cache.addAll(data)
showToast({
msg: "تم تحميل الدروس بنجاح",
type: "info",
})
}
return <div class="w-full py-3">
<button onClick={handleInstall} disabled={loading} class="w-full btn btn-primary">
{
loading ? <span class="loading loading-spinner loading-xs"></span> : "تحميل الدروس"
}
</button>
</div>;
}
export default SW;
32 changes: 32 additions & 0 deletions routes/offline.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Head } from "$fresh/runtime.ts";
import Footer from "@/components/Footer.tsx";

export default function Offline() {
return <>
<Head>
<title>الصفحه غير موجوده</title>
</Head>
<main class="flex flex-col h-full-minus-bar">
<div class="px-4 py-8 mx-auto grow">
<div class="max-w-screen-md mx-auto flex flex-col md:mt-8 items-center justify-center">
<img
class="my-6"
src="/logo.webp"
width="128"
height="128"
title="لوغو نخلة جي اس"
alt="Website logo"
/>
<h1 class="text-4xl font-bold">نعتذر منكم لعدم توفر الصفحة</h1>
<p class="my-4">
أنت غير متصل بالانترنت، تأكد من اتصالك بالانترنت وحاول مرة اخرى.
</p>
<a title="العودى الى الصفحة الرئيسية" href="/" class="underline">
العودى الى الصفحة الرئيسية
</a>
</div>
</div>
<Footer />
</main>
</>
}
81 changes: 46 additions & 35 deletions static/sw.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,49 @@
const CACHE_NAME = 'cache-' + Math.floor(Math.random() * 1000000);
self.addEventListener('install', async function(event) {
const cacheNames = await caches.keys();
await Promise.all(
cacheNames.map(function(cacheName) {
if (cacheName !== CACHE_NAME) {
return caches.delete(cacheName);
}
})
);
const resp = await fetch('/sw-cache.json');
const FILES_TO_CACHE = await resp.json();
console.log('Installing Service Worker...');
console.log('Caching Files...');
console.log(FILES_TO_CACHE.join('\n'));
event.waitUntil(
caches.open(CACHE_NAME).then(function(cache) {
return cache.addAll(FILES_TO_CACHE);
})
);
const CACHE_NAME = 'cache-v1';
self.addEventListener('install', async function (event) {
const cacheNames = await caches.keys();
await Promise.all(
cacheNames.map(function (cacheName) {
if (cacheName !== CACHE_NAME) {
return caches.delete(cacheName);
}
})
);
const resp = await fetch('/sw-cache.json');
const FILES_TO_CACHE = await resp.json();
caches.open(CACHE_NAME).then(function (cache) {
return cache.addAll(FILES_TO_CACHE);
})

});
self.addEventListener('activate', function(event) {

});
self.addEventListener('fetch', function(event) {
// console.log('Fetching:', event.request.url);
event.respondWith(
caches.match(event.request).then(function(response) {
// I KNOW THIS IS NOT THE BEST WAY TO DO THIS
if (!response) {
caches.open(CACHE_NAME).then(function(cache) {
cache.add(event.request.url);
});
}
return response || fetch(event.request);
self.addEventListener('activate', function (event) {
event.waitUntil(
caches.keys().then(function (cacheNames) {
return Promise.all(
cacheNames.map(function (cacheName) {
if (cacheName !== CACHE_NAME) {
return caches.delete(cacheName);
}
})
);
);
})
);
event.waitUntil(
caches.open(CACHE_NAME).then(async function (cache) {
const resp = await fetch('/sw-cache.json');
const FILES_TO_CACHE = await resp.json();
return cache.addAll(FILES_TO_CACHE);
})
);
});
self.addEventListener('fetch', function (event) {
event.respondWith(
caches.match(event.request).then(function (response) {
if (!response) {
caches.open(CACHE_NAME).then(function (cache) {
cache.add(event.request.url);
});
}
return response || fetch(event.request);
})
);
});

0 comments on commit 4c00e3a

Please sign in to comment.