fixing up and finishing the api sside histories

This commit is contained in:
Shoofle 2024-10-05 17:38:48 -04:00
parent 6355e86777
commit 13fb4db1d1
4 changed files with 21 additions and 14 deletions

View File

@ -27,11 +27,9 @@ const defaults = {
}; };
export async function postNewPage() { export async function postNewPage() {
return fetch(`${apiUrl}/page/new`, { return shoofetch(`${apiUrl}/page/new`, {
method: 'POST', method: 'POST',
...defaults })
}).then((res) => res.json())
.then((data) => data.id)
} }
export async function fetchPageList() { export async function fetchPageList() {

View File

@ -17,10 +17,10 @@ function Landing() {
const navigate = useNavigate(); const navigate = useNavigate();
const makeNewPage = useMutation({ // for changing the value when we're done with it const makeNewPage = useMutation({ // for changing the value when we're done with it
mutationFn: () => postNewPage() mutationFn: () => postNewPage(),
.catch((error) => { console.log("got an error", error.json()) }),
onSettled: async (data, error, variables) => { onSettled: async (data, error, variables) => {
// Invalidate and navigate to the new page // Invalidate and navigate to the new page
console.log(data, error, variables);
await queryClient.invalidateQueries({ queryKey: ['pages'] }); await queryClient.invalidateQueries({ queryKey: ['pages'] });
navigate(`/${data}/edit`); navigate(`/${data}/edit`);
}, },

View File

@ -5,8 +5,7 @@ const { circular } = require('graphology-layout');
function graphFromList(allTheStuff) { function graphFromList(allTheStuff) {
const graph = new graphology.Graph(); const graph = new graphology.Graph();
for (const {number, html} of allTheStuff) { for (const {number, html} of allTheStuff) {
if (!graph.hasNode(number)) graph.addNode(number);
graph.addNode(number);
} }
for (const {number, html} of allTheStuff) { for (const {number, html} of allTheStuff) {

View File

@ -9,9 +9,19 @@ const { loginRequired } = require('../authStuff.js');
const showdown = require('showdown'); const showdown = require('showdown');
const converter = new showdown.Converter(); const converter = new showdown.Converter();
const pageListQuery = db.prepare(`
select p.number, p.title, p.time
from pages p
where time >= (
select max(s.time)
from pages s
where s.number = p.number
)
order by p.time desc
`);
app.get('/pages', (req, res) => { app.get('/pages', (req, res) => {
try { try {
const pages = db.prepare('select number, title from pages').all(); const pages = pageListQuery.all();
res.status(200).json(pages); res.status(200).json(pages);
} catch (error) { } catch (error) {
res.status(500).json({"error": error}); res.status(500).json({"error": error});
@ -20,11 +30,11 @@ app.get('/pages', (req, res) => {
app.post('/page/new', loginRequired, (req, res) => { app.post('/page/new', loginRequired, (req, res) => {
try { try {
const maxPageNumber = db.prepare('select max(number) from pages').get() const maxPageNumber = db.prepare('select max(number) as maximum from pages').get()
console.log('maxPageNumber is ', maxPageNumber); const newPageNumber = maxPageNumber.maximum + 1;
const newPage = db.prepare('insert into pages (title, description, author) values (?, ?, ?) returning number') const newPage = db.prepare('insert into pages (number, title, description, author) values (?, ?, ?, ?) returning number')
.get("new page", "this page is new!", req.session.userId); .get(newPageNumber, "new page", "this page is new!", req.session.userId);
res.status(200).json(newPage); res.status(200).json(newPageNumber);
} catch (error) { } catch (error) {
res.status(500).json({"error": error}); res.status(500).json({"error": error});
} }