2025-11-23 09:08:01 +00:00
|
|
|
import axios from "axios";
|
2025-11-24 19:48:46 +00:00
|
|
|
import { NoteRead } from "./folders";
|
2025-12-08 22:08:30 +00:00
|
|
|
import { encryptString, decryptString } from "./encryption";
|
|
|
|
|
import { useAuthStore } from "../stores/authStore";
|
|
|
|
|
axios.defaults.withCredentials = true;
|
|
|
|
|
const API_URL = (import.meta as any).env.PROD
|
|
|
|
|
? "/api"
|
|
|
|
|
: "http://localhost:8000/api";
|
2025-11-23 09:08:01 +00:00
|
|
|
|
|
|
|
|
export interface Note {
|
|
|
|
|
id: number;
|
|
|
|
|
title: string;
|
|
|
|
|
folder_id?: number;
|
|
|
|
|
content: string;
|
|
|
|
|
created_at: string;
|
|
|
|
|
updated_at: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface NoteCreate {
|
|
|
|
|
title: string;
|
|
|
|
|
content: string;
|
|
|
|
|
folder_id: number | null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-23 15:14:48 +00:00
|
|
|
const createNote = async (note: NoteCreate) => {
|
2025-12-08 22:08:30 +00:00
|
|
|
const encryptionKey = useAuthStore.getState().encryptionKey;
|
|
|
|
|
if (!encryptionKey) throw new Error("Not authenticated");
|
|
|
|
|
|
|
|
|
|
var noteContent = await encryptString(note.content, encryptionKey);
|
|
|
|
|
var noteTitle = await encryptString(note.title, encryptionKey);
|
2025-11-24 19:48:46 +00:00
|
|
|
|
|
|
|
|
var encryptedNote = {
|
|
|
|
|
title: noteTitle,
|
|
|
|
|
content: noteContent,
|
|
|
|
|
folder_id: note.folder_id,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
console.log(encryptedNote);
|
2025-12-08 22:08:30 +00:00
|
|
|
return axios.post(`${API_URL}/notes/`, encryptedNote);
|
2025-11-24 19:48:46 +00:00
|
|
|
};
|
|
|
|
|
const fetchNotes = async () => {
|
2025-12-08 22:08:30 +00:00
|
|
|
const encryptionKey = useAuthStore.getState().encryptionKey;
|
|
|
|
|
if (!encryptionKey) throw new Error("Not authenticated");
|
|
|
|
|
|
|
|
|
|
const { data } = await axios.get(`${API_URL}/notes/`);
|
2025-11-24 19:48:46 +00:00
|
|
|
|
|
|
|
|
console.log(data);
|
|
|
|
|
const decryptedNotes = await Promise.all(
|
|
|
|
|
data.map(async (note: Note) => ({
|
|
|
|
|
...note,
|
2025-12-08 22:08:30 +00:00
|
|
|
title: await decryptString(note.title, encryptionKey),
|
|
|
|
|
content: await decryptString(note.content, encryptionKey),
|
2025-11-24 19:48:46 +00:00
|
|
|
})),
|
|
|
|
|
);
|
2025-11-23 15:14:48 +00:00
|
|
|
|
2025-11-24 19:48:46 +00:00
|
|
|
return decryptedNotes;
|
2025-11-23 15:14:48 +00:00
|
|
|
};
|
|
|
|
|
|
2025-11-25 22:09:02 +00:00
|
|
|
const updateNote = async (id: number, note: Partial<Note>) => {
|
2025-12-08 22:08:30 +00:00
|
|
|
const encryptionKey = useAuthStore.getState().encryptionKey;
|
|
|
|
|
if (!encryptionKey) throw new Error("Not authenticated");
|
|
|
|
|
|
2025-11-25 22:09:02 +00:00
|
|
|
var encryptedNote: Partial<Note> = {};
|
|
|
|
|
if (note.content) {
|
2025-12-08 22:08:30 +00:00
|
|
|
encryptedNote.content = await encryptString(note.content, encryptionKey);
|
2025-11-25 22:09:02 +00:00
|
|
|
}
|
|
|
|
|
if (note.title) {
|
2025-12-08 22:08:30 +00:00
|
|
|
encryptedNote.title = await encryptString(note.title, encryptionKey);
|
2025-11-25 22:09:02 +00:00
|
|
|
}
|
|
|
|
|
if (note.folder_id) {
|
|
|
|
|
encryptedNote.folder_id = note.folder_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return axios.patch(`${API_URL}/notes/${id}`, encryptedNote);
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-23 09:08:01 +00:00
|
|
|
export const notesApi = {
|
2025-11-24 19:48:46 +00:00
|
|
|
list: () => fetchNotes(),
|
2025-11-23 09:08:01 +00:00
|
|
|
get: (id: number) => axios.get(`${API_URL}/notes/${id}`),
|
2025-11-23 15:14:48 +00:00
|
|
|
create: (note: NoteCreate) => createNote(note),
|
2025-11-25 22:09:02 +00:00
|
|
|
update: (id: number, note: Partial<Note>) => updateNote(id, note),
|
2025-11-23 09:08:01 +00:00
|
|
|
delete: (id: number) => axios.delete(`${API_URL}/notes/${id}`),
|
|
|
|
|
};
|