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

Search page with POST and FormData examples #3

Merged
merged 27 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
62e6792
basic greeting API example
thescientist13 Aug 8, 2023
88c9950
basic form setup and response messaging
thescientist13 Aug 8, 2023
3a8d051
patch package
thescientist13 Aug 8, 2023
a27c479
patch package
thescientist13 Aug 8, 2023
ccd19cd
install htmx from npm
thescientist13 Aug 8, 2023
ce938e5
test htmx POST on vercel
thescientist13 Aug 9, 2023
20b672d
more vercel debugging
thescientist13 Aug 9, 2023
b39afa8
basic form data parsing
thescientist13 Aug 9, 2023
585bc45
continued iterating on POST support for Requests
thescientist13 Aug 10, 2023
fcb4822
patch to latest greenwood alpha
thescientist13 Aug 12, 2023
01216db
patch vercel adapter for POST support
thescientist13 Aug 12, 2023
b2a07b2
fix local dev API losing query params
thescientist13 Aug 16, 2023
2fbf35b
upgrade latest greenwood v0.29.0-rc.3 with FormData support
thescientist13 Aug 27, 2023
a7350d9
add Search page example
thescientist13 Aug 27, 2023
78f5232
patch package for shared API bundles missing
thescientist13 Aug 27, 2023
2f6faa0
add placeholder text to forms
thescientist13 Aug 27, 2023
5bf6046
add link to Vercel
thescientist13 Aug 27, 2023
e0e86ef
style no search results text
thescientist13 Aug 27, 2023
ffad240
move styles to standalone file
thescientist13 Aug 27, 2023
4fd6599
restore htmx
thescientist13 Aug 27, 2023
86dd90a
restore required attribute to input tags
thescientist13 Aug 27, 2023
ce0d2d8
close no results response span tag
thescientist13 Aug 27, 2023
20c8c41
add alt attribute to card images
thescientist13 Aug 27, 2023
de12557
fix footer link styles
thescientist13 Aug 27, 2023
fc3e70f
resize spinner image
thescientist13 Aug 27, 2023
ba9b0d7
upgrade latest greenwood v0.29.0-alpha.4
thescientist13 Sep 1, 2023
9f03416
README grammar
thescientist13 Sep 1, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,395 changes: 1,370 additions & 25 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,28 @@
},
"keywords": [
"Greenwood",
"Web Components",
"HTML",
"htmx",
"Vercel",
"Serverless",
"SSR"
],
"scripts": {
"clean": "rimraf .greenwood public",
"develop": "greenwood develop",
"clean": "rimraf .greenwood public .vercel",
"dev": "greenwood develop",
"build": "greenwood build",
"serve": "npm run clean && npm run build && greenwood serve",
"start": "npm run serve"
"start": "npm run serve",
"postinstall": "patch-package"
},
"dependencies": {
"htmx.org": "^1.9.4"
},
"devDependencies": {
"@greenwood/cli": "~0.29.0-alpha.2",
"@greenwood/plugin-adapter-vercel": "~0.29.0-alpha.2",
"@greenwood/cli": "~0.29.0-alpha.3",
"@greenwood/plugin-adapter-vercel": "~0.29.0-alpha.3",
"patch-package": "^8.0.0",
"rimraf": "^5.0.1"
}
}
17 changes: 8 additions & 9 deletions src/api/greeting.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { getMessage } from '../services/message.js';

export async function handler(request) {
const params = new URLSearchParams(request.url.slice(request.url.indexOf('?')));
const name = params.has('name') ? params.get('name') : 'Greenwood';
const body = getMessage(name);
const headers = new Headers({
'Content-Type': 'text/html'
});
const formData = await request.formData();
const name = formData.has('name') ? formData.get('name') : 'Greenwood';
const body = `Hello ${name}! 👋`;

return new Response(body, { headers });
return new Response(body, {
headers: new Headers({
'Content-Type': 'text/html'
})
});
}
40 changes: 40 additions & 0 deletions src/api/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { renderFromHTML } from 'wc-compiler';
import { getArtists } from '../services/artists.js';

export async function handler(request) {
const formData = await request.formData();
const term = formData.has('term') ? formData.get('term') : '';
const artists = (await getArtists())
.filter((artist => {
return term !== '' && artist.name.toLowerCase().includes(term.toLowerCase());
}));
let body = '';

if (artists.length === 0) {
body = 'No results found.';
} else {
const { html } = await renderFromHTML(`
${
artists.map((item, idx) => {
const { name, imageUrl } = item;
return `
<app-card
title="${idx + 1}) ${name}"
thumbnail="${imageUrl}"
></app-card>
`;
}).join('')
}
`, [
new URL('../components/card.js', import.meta.url)
]);

body = html;
}

return new Response(body, {
headers: new Headers({
'Content-Type': 'text/html'
})
});
}
1 change: 1 addition & 0 deletions src/components/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default class Card extends HTMLElement {
border: 0;
font-size: 1rem;
border-radius: 5px;
cursor: pointer;
}
img {
max-width: 500px;
Expand Down
18 changes: 1 addition & 17 deletions src/pages/index.html
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
<html>
<head>
<script src="/node_modules/htmx.org/dist/htmx.js"></script>
<style>
.htmx-indicator {
opacity: 0;
transition: opacity 500ms ease-in;
}
.htmx-request .htmx-indicator {
opacity: 1;
}
.htmx-request.htmx-indicator {
opacity: 1;
}
</style>
</head>

<body>

<article>
<h2>Greeting API</h2>
<p>This is an example of a Greenwood API Route returning HTML when fetched on-submit of the form to display a message on the page. You can see it fire in the network tab as <i>/api/greeting</i></p>
<form
hx-get="/api/greeting"
hx-post="/api/greeting"
hx-target="#greeting-output"
hx-indicator="#greeting-spinner"
>
Expand Down
27 changes: 27 additions & 0 deletions src/pages/search.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<html>
<body>
<article>
<h2>Search API (w/ WCC)</h2>
<p>This is an example of a Greenwood API leveraging the FormData API and returning an HTML response that is generated by server-rendering a Web Component (with Declarative Shadow DOM). This same component is loaded on the client-side too, so that when you click the <i>Artist Details</i> button, state and interactivity can still be resumed. You can see it fire in the network tab as <i>/api/search</i></p>
<form
hx-post="/api/search"
hx-target="#search-artists-output"
hx-indicator="#search-spinner"
>
<label>
Search for...
<input type="text" name="term" required/>
</label>
<button type="submit">Search</button>
</form>
<img
id="search-spinner"
class="htmx-indicator"
width="50"
height="50"
src="/assets/spinner.gif"
/>
<div id="search-artists-output" class="artists-cards-container" aria-live="polite"></div>
</article>
</body>
</html>
7 changes: 0 additions & 7 deletions src/services/message.js

This file was deleted.

15 changes: 14 additions & 1 deletion src/templates/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<link rel="icon" href="/favicon.ico"/>

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro">
<script src="/node_modules/htmx.org/dist/htmx.js"></script>
<style>
:root, :host {
--color-white: white;
Expand Down Expand Up @@ -108,6 +109,17 @@
margin: 10px;
}
}

.htmx-indicator {
opacity: 0;
transition: opacity 500ms ease-in;
}
.htmx-request .htmx-indicator {
opacity: 1;
}
.htmx-request.htmx-indicator {
opacity: 1;
}
</style>

<script type="module" src="./components/card.js"></script>
Expand All @@ -118,7 +130,8 @@
<nav>
<ul>
<li><a href="/">Home</a> | </li>
<li><a href="/artists/">Artists</a></li>
<li><a href="/artists/">Artists</a> | </li>
<li><a href="/search/">Search</a></li>
</ul>
</nav>
</header>
Expand Down