Scratch space for learning atproto app development

Remove client side JS

Changed files
+24 -45
src
+10 -9
src/pages/home.ts
···
</div>
</div>`}
</div>
-
<div class="status-options">
+
<form action="/status" method="post" class="status-options">
${STATUS_OPTIONS.map(
(status) =>
-
html`<div
-
class=${myStatus?.status === status
-
? 'status-option selected'
-
: 'status-option'}
-
data-value="${status}"
-
data-authed=${profile ? '1' : '0'}
+
html`<button
+
class=${
+
myStatus?.status === status
+
? 'status-option selected'
+
: 'status-option'
+
}
+
name="status"
+
value="${status}"
>
${status}
</div>`
)}
-
</div>
+
</form>
${statuses.map((status, i) => {
const handle = didHandleMap[status.authorDid] || status.authorDid
const date = ts(status)
···
`
})}
</div>
-
<script src="/public/home.js"></script>
</div>`
}
-32
src/pages/public/home.js
···
-
Array.from(document.querySelectorAll('.status-option'), (el) => {
-
el.addEventListener('click', async (ev) => {
-
setError('')
-
-
if (el.dataset.authed !== '1') {
-
window.location = '/login'
-
return
-
}
-
-
const res = await fetch('/status', {
-
method: 'POST',
-
headers: { 'content-type': 'application/json' },
-
body: JSON.stringify({ status: el.dataset.value }),
-
})
-
const body = await res.json()
-
if (body?.error) {
-
setError(body.error)
-
} else {
-
location.reload()
-
}
-
})
-
})
-
-
function setError(str) {
-
const errMsg = document.querySelector('.error')
-
if (str) {
-
errMsg.classList.add('visible')
-
errMsg.textContent = str
-
} else {
-
errMsg.classList.remove('visible')
-
}
-
}
+1
src/pages/public/styles.css
···
font-size: 2rem;
width: 3rem;
height: 3rem;
+
padding: 0;
background-color: #fff;
border: 1px solid var(--border-color);
border-radius: 3rem;
+13 -4
src/routes.ts
···
// If the user is signed in, get an agent which communicates with their server
const agent = await getSessionAgent(req, res, ctx)
if (!agent) {
-
return res.status(401).json({ error: 'Session required' })
+
return res
+
.status(401)
+
.type('html')
+
.send('<h1>Error: Session required</h1>')
}
// Construct & validate their status record
···
createdAt: new Date().toISOString(),
}
if (!Status.validateRecord(record).success) {
-
return res.status(400).json({ error: 'Invalid status' })
+
return res
+
.status(400)
+
.type('html')
+
.send('<h1>Error: Invalid status</h1>')
}
let uri
···
uri = res.data.uri
} catch (err) {
ctx.logger.warn({ err }, 'failed to write record')
-
return res.status(500).json({ error: 'Failed to write record' })
+
return res
+
.status(500)
+
.type('html')
+
.send('<h1>Error: Failed to write record</h1>')
}
try {
···
)
}
-
res.status(200).json({})
+
return res.redirect('/')
})
)