Build an app
Write a voting board your team can use today. People suggest ideas, vote once each, and see every change live. One HTML file, no backend.
This tutorial builds Ideas, a small app where your team suggests things to build and votes on them. Along the way you'll use every part of the SDK: who is signed in, what they're allowed to do, shared data, and live updates.
It takes about 15 minutes. You should have finished the Quickstart, so you're signed in.
Start with a page
Make a folder called ideas with an index.html in it:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Ideas</title>
<link rel="stylesheet" href="/_shed/kit.css">
<script src="/_shed/sdk.js"></script>
</head>
<body>
<main>
<p id="who" class="sh-label"> </p>
<h1>Ideas.</h1>
<form id="add" hidden>
<input name="text" placeholder="What should we build next?" aria-label="Idea" required>
<button>Add</button>
</form>
<p id="readonly" class="sh-note" hidden></p>
<ol id="list"></ol>
</main>
<script>
// The next steps go here.
</script>
</body>
</html>Two lines in the <head> do the work. /_shed/sdk.js gives the page a global shed object. /_shed/kit.css is optional and gives the page Shed's look (see Styling). Both are served by every Shed app at its own address, so there's nothing to install.
Deploy it now, so you can watch each step work:
$ npx shedcloud deploy ideas
Deployed ideas v1 (1 file)
https://ideas.shed.example.com/Find out who's here
Shed signs people in before your page loads. shed.me() tells you who they are, and shed.can() tells you what they may do:
let me = null, canEdit = false;
shed.me().then(async ({ user, role, app }) => {
me = user; // { email } or null
canEdit = await shed.can('edit'); // true for editors and owners
document.getElementById('who').textContent = `${user.email} · ${role}`;
document.getElementById('add').hidden = !canEdit;
const note = document.getElementById('readonly');
note.hidden = canEdit;
note.textContent = `You can see the ideas. To add and vote, ask ${app.owner} for edit access.`;
});Hide what people can't use, and tell them who to ask. Shed enforces roles on the server either way: a viewer's write fails with a 403 whatever your page does. Hiding the form just means nobody finds that out the hard way.
Save ideas
Data lives in collections of JSON records. A collection exists as soon as you write to it:
const ideas = shed.db('ideas');
document.getElementById('add').onsubmit = async (e) => {
e.preventDefault();
const text = e.target.text.value.trim();
if (text) await ideas.add({ text });
e.target.reset();
};add gives the record an id and a _meta with who wrote it and when. Every person with access to the app shares the same records.
Show them, live
subscribe calls your function with every record now, and again whenever anyone changes the collection, in every open tab:
const esc = (s) => String(s).replace(/[&<>"]/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c]));
let allIdeas = [];
ideas.subscribe((records) => { allIdeas = records; render(); });
function render() {
document.getElementById('list').innerHTML = allIdeas.map((idea) => `<li>
<span class="text">${esc(idea.text)} <small class="sh-mute">${esc(idea._meta.created_by)}</small></span>
</li>`).join('');
}Deploy, open the app in two windows, and add an idea in one. It appears in the other.
Add votes, one per person
You could store votes inside each idea, but two people voting at once would overwrite each other. Instead, give each vote its own record, and choose its id so a person can only have one vote per idea:
const votes = shed.db('votes');
let allVotes = [];
votes.subscribe((records) => { allVotes = records; render(); });
// "idea-id--ana_example_com": the same person and idea always give the same id.
const voteId = (ideaId) => `${ideaId}--${me.email.replace(/[^A-Za-z0-9_-]/g, '_')}`;
async function toggleVote(ideaId) {
const id = voteId(ideaId);
if (allVotes.some((v) => v.id === id)) await votes.remove(id);
else await votes.put(id, { idea: ideaId });
}put creates or replaces the record with that id, so voting twice can't count twice. remove takes the vote back. Ids can use letters, digits, _ and -, which is why the email is rewritten.
Then count votes in render, sort by them, and add a vote button:
const count = (id) => allVotes.filter((v) => v.idea === id).length;
const mine = (id) => me && allVotes.some((v) => v.id === voteId(id));
// In render(), sort and add the button:
[...allIdeas].sort((a, b) => count(b.id) - count(a.id)).map((idea) => `<li>
<button data-vote="${idea.id}" aria-pressed="${mine(idea.id)}" ${canEdit ? '' : 'disabled'}>▲ ${count(idea.id)}</button>
…
</li>`)
document.getElementById('list').onclick = (e) => {
const b = e.target.closest('[data-vote]');
if (b) toggleVote(b.dataset.vote);
};Let people delete their own ideas
Every record's _meta.created_by is the email of whoever wrote it. Show a Delete button only on your own ideas:
${me && idea._meta.created_by === me.email ? `<button data-delete="${idea.id}">Delete</button>` : ''}
const del = e.target.closest('[data-delete]');
if (del) await ideas.remove(del.dataset.delete);Share it with your team
Deploy the finished app, then open it to everyone at your company as editors:
$ npx shedcloud deploy ideas
$ npx shedcloud share ideas --org editor--org lets in everyone who signs in with an address at your company's email domain. On Shed Cloud that's your own address's domain, so it needs a company address, not a gmail.com one. You can also add people one at a time with --add sam@example.com --invite, and Shed emails them. Sharing and roles covers every option.
The whole file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Ideas</title>
<link rel="stylesheet" href="/_shed/kit.css">
<script src="/_shed/sdk.js"></script>
<style>
main { max-width: 680px; margin: 0 auto; padding: 40px 24px 80px }
form { display: flex; gap: 8px; margin: 24px 0 } form input { flex: 1 }
ol { list-style: none; margin: 0; padding: 0; border-top: 2px solid var(--ink) }
li { display: flex; align-items: center; gap: 14px; padding: 12px 0; border-bottom: 1px solid var(--hair) }
li .text { flex: 1 } li small { display: block }
</style>
</head>
<body>
<main>
<p id="who" class="sh-label"> </p>
<h1>Ideas.</h1>
<form id="add" hidden>
<input name="text" placeholder="What should we build next?" aria-label="Idea" required>
<button>Add</button>
</form>
<p id="readonly" class="sh-note" hidden></p>
<ol id="list"></ol>
</main>
<script>
const ideas = shed.db('ideas');
const votes = shed.db('votes');
const esc = (s) => String(s).replace(/[&<>"]/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c]));
let me = null, canEdit = false, allIdeas = [], allVotes = [];
shed.me().then(async ({ user, role, app }) => {
me = user;
canEdit = await shed.can('edit');
document.getElementById('who').textContent = user ? `${user.email} · ${role}` : 'Viewing as a guest';
document.getElementById('add').hidden = !canEdit;
const note = document.getElementById('readonly');
note.hidden = canEdit;
note.textContent = `You can see the ideas. To add and vote, ask ${app.owner} for edit access.`;
render();
});
const voteId = (ideaId) => `${ideaId}--${me.email.replace(/[^A-Za-z0-9_-]/g, '_')}`;
function render() {
const count = (id) => allVotes.filter((v) => v.idea === id).length;
const mine = (id) => me && allVotes.some((v) => v.id === voteId(id));
document.getElementById('list').innerHTML = [...allIdeas]
.sort((a, b) => count(b.id) - count(a.id))
.map((idea) => `<li>
<button class="${mine(idea.id) ? '' : 'is-quiet'} is-small" data-vote="${idea.id}" ${canEdit ? '' : 'disabled'}
aria-pressed="${mine(idea.id)}">▲ ${count(idea.id)}</button>
<span class="text">${esc(idea.text)}<small class="sh-mono sh-mute">${esc(idea._meta.created_by || 'guest')}</small></span>
${me && idea._meta.created_by === me.email ? `<button class="is-quiet is-small" data-delete="${idea.id}">Delete</button>` : ''}
</li>`).join('');
}
ideas.subscribe((records) => { allIdeas = records; render(); });
votes.subscribe((records) => { allVotes = records; render(); });
document.getElementById('add').onsubmit = async (e) => {
e.preventDefault();
const text = e.target.text.value.trim();
if (text) await ideas.add({ text });
e.target.reset();
};
document.getElementById('list').onclick = async (e) => {
const vote = e.target.closest('[data-vote]');
if (vote) {
const id = voteId(vote.dataset.vote);
if (allVotes.some((v) => v.id === id)) await votes.remove(id);
else await votes.put(id, { idea: vote.dataset.vote });
}
const del = e.target.closest('[data-delete]');
if (del) await ideas.remove(del.dataset.delete);
};
</script>
</body>
</html>