-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecipes.html
150 lines (132 loc) · 3.52 KB
/
recipes.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
---
layout: default
title: Recipes
permalink: /recipes/
---
<style type="text/css" media="screen">
.container {
margin: 10px auto;
max-width: 600px;
text-align: center;
}
h1 {
margin: 30px 0;
font-size: 4em;
line-height: 1;
letter-spacing: -1px;
}
</style>
<div class="wrapper">
<h1 style="text-align: center">Recipes</h1>
<div id="putta-da-recipes-here">
<h3>
</h3>
</div>
</div>
<style type="text/css" media="screen">
.underline-on-hover:hover {
text-decoration: underline;
}
input[type="text"] {
padding: 10px;
margin: 20px 0;
border: 1px solid #ddd;
border-radius: 4px;
width: 300px;
font-size: 16px;
}
input[type="text"]:focus {
outline: none;
border-color: #9ecaed;
box-shadow: 0 0 10px #9ecaed;
}
</style>
<script type="module">
// Import h and render from preact
import { h, Component, render } from 'https://esm.sh/preact@10';
import { useState } from 'https://esm.sh/preact@10/hooks';
// Define a component for a collapsible section
function Collapsible({ title, children }) {
const [isOpen, setIsOpen] = useState(false);
const handleClick = () => setIsOpen(!isOpen);
const symbol = isOpen ? '[-]' : '[+]';
return h('div', {}, [
h(
'h2',
{
onClick: handleClick,
style: { cursor: 'pointer' },
class: "underline-on-hover"
},
`${symbol} ${title}`
),
isOpen && h('div', {}, children),
]);
}
// Define a component for a recipe
function Recipe({ recipe }) {
return h(Collapsible, { title: recipe.name }, [
//h('div', {}, `Effort: ${recipe.effort}`),
h('strong', {}, 'Ingredients:'),
h('ul', {}, Object.entries(recipe.ingredients).map(([ingredient, amount]) =>
h('li', {}, `${ingredient}: ${amount}`)
)),
h('strong', {}, 'Cookware:'),
h('ul', {}, recipe.cookware.map(description =>
h('li', {}, description)
)),
h('strong', {}, 'Steps:'),
h('ul', {}, recipe.steps.map(step =>
h('li', {}, step.text)
)),
]);
}
// Define a component for the page
function SearchBar({ onInput }) {
return h('input', { type: 'text', onInput , style: { width: "100%" }});
}
// Define a component for the page
function Page({ recipes }) {
const [search, setSearch] = useState('');
const handleInput = e => setSearch(e.target.value);
const filteredRecipes = recipes.filter(recipe =>
recipe.name.toLowerCase().includes(search.toLowerCase())
);
return h('div', {}, [
h(SearchBar, { onInput: handleInput }),
filteredRecipes.map(recipe => h(Recipe, { recipe })),
]);
}
// Fetch the recipes and render the page
const baseElement = document.getElementById("putta-da-recipes-here");
function renderRecipes(recipes) {
recipes.sort((a, b) => a.name.localeCompare(b.name));
render(h(Page, { recipes }), baseElement)
}
fetch('/recipes.json', {
headers: new Headers({
'If-None-Match': localStorage.getItem('recipesETag') || '',
}),
})
.then(response => {
if (response.status === 304) {
// Not Modified, use cached data
renderRecipes(JSON.parse(localStorage.getItem('recipes')));
} else if (response.ok) {
// OK, cache the new data
const eTag = response.headers.get('ETag');
response.json().then(recipes => {
localStorage.setItem('recipes', JSON.stringify(recipes));
localStorage.setItem('recipesETag', eTag);
renderRecipes(recipes)
});
} else {
// Handle HTTP errors
console.error('HTTP error', response.status);
}
})
.catch(error => {
// Handle network errors
console.error('Network error', error);
});
</script>