From b597f77db38368f44642aef40b69a69ac8874aea Mon Sep 17 00:00:00 2001
From: David Dal Busco
Date: Thu, 16 May 2024 21:40:49 +0200
Subject: [PATCH] feat: add entry
---
templates/vanilla-js-example/index.html | 5 +-
templates/vanilla-js-example/package.json | 3 +-
.../src/components/content.js | 11 ++
.../src/components/logout.js | 2 +-
.../src/components/modal.js | 150 ++++++++++++++++++
templates/vanilla-js-example/src/main.js | 4 +-
templates/vanilla-js-example/src/style.css | 2 +-
.../src/utils/render.utils.js | 12 +-
8 files changed, 175 insertions(+), 14 deletions(-)
create mode 100644 templates/vanilla-js-example/src/components/content.js
create mode 100644 templates/vanilla-js-example/src/components/modal.js
diff --git a/templates/vanilla-js-example/index.html b/templates/vanilla-js-example/index.html
index a479916..2636f39 100644
--- a/templates/vanilla-js-example/index.html
+++ b/templates/vanilla-js-example/index.html
@@ -29,9 +29,8 @@
Explore this demo app built with vanilla JavaScript, Tailwind, and
- Juno
-
- , showcasing a practical application of these technologies.
+ Juno, showcasing a practical application of these technologies.
diff --git a/templates/vanilla-js-example/package.json b/templates/vanilla-js-example/package.json
index 1ca0817..657f88a 100644
--- a/templates/vanilla-js-example/package.json
+++ b/templates/vanilla-js-example/package.json
@@ -20,6 +20,7 @@
"vite": "^5.2.0"
},
"dependencies": {
- "@junobuild/core": "^0.0.49"
+ "@junobuild/core": "^0.0.49",
+ "nanoid": "^5.0.7"
}
}
diff --git a/templates/vanilla-js-example/src/components/content.js b/templates/vanilla-js-example/src/components/content.js
new file mode 100644
index 0000000..62ca59e
--- /dev/null
+++ b/templates/vanilla-js-example/src/components/content.js
@@ -0,0 +1,11 @@
+import {renderLogout} from './logout';
+import {renderModal} from './modal';
+
+export const renderContent = (app) => {
+ app.innerHTML = `
+
+ ${renderModal(app)}
+
+ ${renderLogout(app)}
+
`;
+};
diff --git a/templates/vanilla-js-example/src/components/logout.js b/templates/vanilla-js-example/src/components/logout.js
index ab9768c..2b63b54 100644
--- a/templates/vanilla-js-example/src/components/logout.js
+++ b/templates/vanilla-js-example/src/components/logout.js
@@ -8,7 +8,7 @@ export const renderLogout = (app) => {
fn: signOut
});
- app.innerHTML = `
diff --git a/templates/vanilla-js-example/src/components/modal.js b/templates/vanilla-js-example/src/components/modal.js
new file mode 100644
index 0000000..eeadea6
--- /dev/null
+++ b/templates/vanilla-js-example/src/components/modal.js
@@ -0,0 +1,150 @@
+import {authSubscribe, setDoc, uploadFile} from '@junobuild/core';
+import {nanoid} from 'nanoid';
+import {addEventClick} from '../utils/render.utils';
+
+let user;
+authSubscribe((u) => (user = u));
+
+const submitEntry = async (modal) => {
+ // Demo purpose therefore edge case not properly handled
+ if ([null, undefined].includes(user)) {
+ return;
+ }
+
+ const textarea = modal.querySelector('#entryText');
+ const input = modal.querySelector('#fileEntry');
+
+ const inputText = textarea.value;
+ const file = input.files[0];
+
+ try {
+ let url;
+
+ if (file !== undefined) {
+ const filename = `${user.key}-${file.name}`;
+
+ const {downloadUrl} = await uploadFile({
+ collection: 'images',
+ data: file,
+ filename
+ });
+
+ url = downloadUrl;
+ }
+
+ const key = nanoid();
+
+ await setDoc({
+ collection: 'notes',
+ doc: {
+ key,
+ data: {
+ text: inputText,
+ ...(url !== undefined && {url})
+ }
+ }
+ });
+
+ closeModal(modal);
+
+ const reload = () => {
+ const event = new Event('reload');
+ window.dispatchEvent(event);
+ };
+
+ reload();
+ } catch (err) {
+ console.error(err);
+ }
+};
+
+const closeModal = (modal) => (modal.innerHTML = '');
+
+const showModal = () => {
+ const modal = document.querySelector('#modal');
+
+ addEventClick({
+ target: modal,
+ selector: '#closeModal',
+ fn: () => closeModal(modal)
+ });
+
+ addEventClick({
+ target: modal,
+ selector: '#submitEntry',
+ fn: async () => await submitEntry(modal)
+ });
+
+ modal.innerHTML = `
+
+
+
+
+
+
+
+
+
+
+ Close
+
+
+
+ Submit
+
+
+
+
+
+
+
+
+
+`;
+};
+
+export const renderModal = (app) => {
+ addEventClick({
+ target: app,
+ selector: '#addEntry',
+ fn: showModal
+ });
+
+ return `
+ Add an entry
+
+
+
+
+
+
+`;
+};
diff --git a/templates/vanilla-js-example/src/main.js b/templates/vanilla-js-example/src/main.js
index 6557b0e..80cfa73 100644
--- a/templates/vanilla-js-example/src/main.js
+++ b/templates/vanilla-js-example/src/main.js
@@ -1,6 +1,6 @@
import {authSubscribe, initJuno} from '@junobuild/core';
+import {renderContent} from './components/content';
import {renderLogin} from './components/login';
-import {renderLogout} from './components/logout';
import './style.css';
/**
@@ -14,7 +14,7 @@ authSubscribe((user) => {
return;
}
- renderLogout(app);
+ renderContent(app);
});
/**
diff --git a/templates/vanilla-js-example/src/style.css b/templates/vanilla-js-example/src/style.css
index bd6213e..b5c61c9 100644
--- a/templates/vanilla-js-example/src/style.css
+++ b/templates/vanilla-js-example/src/style.css
@@ -1,3 +1,3 @@
@tailwind base;
@tailwind components;
-@tailwind utilities;
\ No newline at end of file
+@tailwind utilities;
diff --git a/templates/vanilla-js-example/src/utils/render.utils.js b/templates/vanilla-js-example/src/utils/render.utils.js
index 98e6231..f7ba23b 100644
--- a/templates/vanilla-js-example/src/utils/render.utils.js
+++ b/templates/vanilla-js-example/src/utils/render.utils.js
@@ -5,9 +5,9 @@
* @param fn The function to trigger on click.
*/
export const addEventClick = ({target, selector, fn}) => {
- const observer = new MutationObserver(() => {
- observer.disconnect();
- document.querySelector(selector)?.addEventListener('click', fn, {passive: true});
- });
- observer.observe(target, {childList: true, subtree: true});
-}
\ No newline at end of file
+ const observer = new MutationObserver(() => {
+ observer.disconnect();
+ document.querySelector(selector)?.addEventListener('click', fn, {passive: true});
+ });
+ observer.observe(target, {childList: true, subtree: true});
+};