* Add logging placeholder functions, fix notes.js uids * Fix areas.js uids and remove ids * Add UIDs to inbox items. Includes migration. * id -> uid for task-events.js --------- Co-authored-by: antanst <>
80 lines
2.5 KiB
TypeScript
80 lines
2.5 KiB
TypeScript
import { Note } from '../entities/Note';
|
|
import {
|
|
handleAuthResponse,
|
|
getDefaultHeaders,
|
|
getPostHeaders,
|
|
} from './authUtils';
|
|
|
|
export const fetchNotes = async (): Promise<Note[]> => {
|
|
const response = await fetch('/api/notes', {
|
|
credentials: 'include',
|
|
headers: getDefaultHeaders(),
|
|
});
|
|
await handleAuthResponse(response, 'Failed to fetch notes.');
|
|
|
|
return await response.json();
|
|
};
|
|
|
|
export const createNote = async (noteData: Note): Promise<Note> => {
|
|
const response = await fetch('/api/note', {
|
|
method: 'POST',
|
|
credentials: 'include',
|
|
headers: getPostHeaders(),
|
|
body: JSON.stringify(noteData),
|
|
});
|
|
|
|
await handleAuthResponse(response, 'Failed to create note.');
|
|
return await response.json();
|
|
};
|
|
|
|
export const updateNote = async (
|
|
noteUid: string,
|
|
noteData: Note
|
|
): Promise<Note> => {
|
|
// Transform project_id to project_uid if needed
|
|
const requestData = { ...noteData };
|
|
if (noteData.project && noteData.project.uid) {
|
|
requestData.project_uid = noteData.project.uid;
|
|
} else if (noteData.project_uid) {
|
|
// project_uid is already set, use it as-is
|
|
} else if (noteData.project_id && !noteData.project_uid) {
|
|
// Legacy: if only project_id is provided, we can't convert it to uid here
|
|
// This should not happen with the new implementation, but keeping for safety
|
|
console.warn(
|
|
'Note update with project_id but no project_uid - this may fail'
|
|
);
|
|
}
|
|
|
|
// Use the provided noteUid
|
|
const noteIdentifier = noteUid;
|
|
|
|
const response = await fetch(`/api/note/${noteIdentifier}`, {
|
|
method: 'PATCH',
|
|
credentials: 'include',
|
|
headers: getPostHeaders(),
|
|
body: JSON.stringify(requestData),
|
|
});
|
|
|
|
await handleAuthResponse(response, 'Failed to update note.');
|
|
return await response.json();
|
|
};
|
|
|
|
export const deleteNote = async (noteUid: string): Promise<void> => {
|
|
const response = await fetch(`/api/note/${noteUid}`, {
|
|
method: 'DELETE',
|
|
credentials: 'include',
|
|
headers: getDefaultHeaders(),
|
|
});
|
|
|
|
await handleAuthResponse(response, 'Failed to delete note.');
|
|
};
|
|
|
|
export const fetchNoteBySlug = async (uidSlug: string): Promise<Note> => {
|
|
const response = await fetch(`/api/note/${uidSlug}`, {
|
|
credentials: 'include',
|
|
headers: getDefaultHeaders(),
|
|
});
|
|
|
|
await handleAuthResponse(response, 'Failed to fetch note.');
|
|
return await response.json();
|
|
};
|