Fix snippets API integration and database table creation
This commit is contained in:
@@ -242,6 +242,17 @@ async function initializeCompleteDatabase(): Promise<void> {
|
||||
FOREIGN KEY (user_id) REFERENCES users (id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS snippets (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
content TEXT NOT NULL,
|
||||
description TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES users (id)
|
||||
);
|
||||
|
||||
`);
|
||||
|
||||
migrateSchema();
|
||||
|
||||
@@ -11,10 +11,15 @@ import {
|
||||
CardTitle,
|
||||
} from "@/components/ui/card";
|
||||
import { Plus, Play, Edit, Trash2, X } from "lucide-react";
|
||||
import axios from "axios";
|
||||
import { toast } from "sonner";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useConfirmation } from "@/hooks/use-confirmation.ts";
|
||||
import {
|
||||
getSnippets,
|
||||
createSnippet,
|
||||
updateSnippet,
|
||||
deleteSnippet,
|
||||
} from "@/ui/main-axios";
|
||||
import type { Snippet, SnippetData } from "../../../../types/index.js";
|
||||
|
||||
interface SnippetsSidebarProps {
|
||||
@@ -53,9 +58,9 @@ export function SnippetsSidebar({
|
||||
const fetchSnippets = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const response = await axios.get("/snippets");
|
||||
// Defensive: ensure response.data is an array
|
||||
setSnippets(Array.isArray(response.data) ? response.data : []);
|
||||
const data = await getSnippets();
|
||||
// Defensive: ensure data is an array
|
||||
setSnippets(Array.isArray(data) ? data : []);
|
||||
} catch (err) {
|
||||
toast.error(t("snippets.failedToFetch"));
|
||||
setSnippets([]);
|
||||
@@ -82,23 +87,20 @@ export function SnippetsSidebar({
|
||||
setShowDialog(true);
|
||||
};
|
||||
|
||||
const handleDelete = async (snippet: Snippet) => {
|
||||
const confirmed = await confirmWithToast({
|
||||
title: t("snippets.deleteConfirmTitle"),
|
||||
description: t("snippets.deleteConfirmDescription", {
|
||||
name: snippet.name,
|
||||
}),
|
||||
});
|
||||
|
||||
if (confirmed) {
|
||||
try {
|
||||
await axios.delete(`/snippets/${snippet.id}`);
|
||||
toast.success(t("snippets.deleteSuccess"));
|
||||
fetchSnippets();
|
||||
} catch (err) {
|
||||
toast.error(t("snippets.deleteFailed"));
|
||||
}
|
||||
}
|
||||
const handleDelete = (snippet: Snippet) => {
|
||||
confirmWithToast(
|
||||
t("snippets.deleteConfirmDescription", { name: snippet.name }),
|
||||
async () => {
|
||||
try {
|
||||
await deleteSnippet(snippet.id);
|
||||
toast.success(t("snippets.deleteSuccess"));
|
||||
fetchSnippets();
|
||||
} catch (err) {
|
||||
toast.error(t("snippets.deleteFailed"));
|
||||
}
|
||||
},
|
||||
"destructive",
|
||||
);
|
||||
};
|
||||
|
||||
const handleSubmit = async () => {
|
||||
@@ -116,10 +118,10 @@ export function SnippetsSidebar({
|
||||
|
||||
try {
|
||||
if (editingSnippet) {
|
||||
await axios.put(`/snippets/${editingSnippet.id}`, formData);
|
||||
await updateSnippet(editingSnippet.id, formData);
|
||||
toast.success(t("snippets.updateSuccess"));
|
||||
} else {
|
||||
await axios.post("/snippets", formData);
|
||||
await createSnippet(formData);
|
||||
toast.success(t("snippets.createSuccess"));
|
||||
}
|
||||
setShowDialog(false);
|
||||
|
||||
@@ -2155,3 +2155,46 @@ export async function deployCredentialToHost(
|
||||
throw handleApiError(error, "deploy credential to host");
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// SNIPPETS API
|
||||
// ============================================================================
|
||||
|
||||
export async function getSnippets(): Promise<any> {
|
||||
try {
|
||||
const response = await authApi.get("/snippets");
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw handleApiError(error, "fetch snippets");
|
||||
}
|
||||
}
|
||||
|
||||
export async function createSnippet(snippetData: any): Promise<any> {
|
||||
try {
|
||||
const response = await authApi.post("/snippets", snippetData);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw handleApiError(error, "create snippet");
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateSnippet(
|
||||
snippetId: number,
|
||||
snippetData: any,
|
||||
): Promise<any> {
|
||||
try {
|
||||
const response = await authApi.put(`/snippets/${snippetId}`, snippetData);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw handleApiError(error, "update snippet");
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteSnippet(snippetId: number): Promise<any> {
|
||||
try {
|
||||
const response = await authApi.delete(`/snippets/${snippetId}`);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw handleApiError(error, "delete snippet");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user