ghostguild-org/app/composables/useBoardPosts.js
Jennie Robinson Faber 28040f44f4
Some checks failed
Test / vitest (push) Failing after 7m17s
Test / playwright (push) Has been skipped
Test / visual (push) Has been skipped
Test / Notify on failure (push) Successful in 1s
refactor(board): atomic delete + query limit + composable cleanup
Delete uses findOneAndDelete with author match (no TOCTOU window);
existence check only runs on miss to distinguish 403 vs 404. Posts
list capped at 200. Drop unused resolveTagChannel and refreshParams;
route slack URL building through the composable's slackUrl helper.
2026-04-15 12:47:53 +01:00

50 lines
1.1 KiB
JavaScript

export function useBoardPosts() {
const posts = useState('board.posts', () => [])
const loading = useState('board.loading', () => false)
async function fetchPosts(params = {}) {
loading.value = true
try {
const result = await $fetch('/api/board/posts', { params })
posts.value = result?.posts || []
return posts.value
} finally {
loading.value = false
}
}
async function createPost(body) {
const created = await $fetch('/api/board/posts', {
method: 'POST',
body,
})
await fetchPosts()
return created
}
async function updatePost(id, body) {
const updated = await $fetch(`/api/board/posts/${id}`, {
method: 'PATCH',
body,
})
await fetchPosts()
return updated
}
async function deletePost(id) {
const result = await $fetch(`/api/board/posts/${id}`, {
method: 'DELETE',
})
await fetchPosts()
return result
}
return {
posts: readonly(posts),
loading: readonly(loading),
fetchPosts,
createPost,
updatePost,
deletePost,
}
}