mirror of
https://github.com/allankoechke/mantisbase-admin.git
synced 2026-12-01 01:15:36 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c05a27ae0e |
BIN
app/favicon.ico
BIN
app/favicon.ico
Binary file not shown.
|
Before Width: | Height: | Size: 182 KiB |
@@ -83,7 +83,7 @@ export function AdminDashboard({ token, onLogout }: AdminDashboardProps) {
|
||||
}, [])
|
||||
|
||||
const [apiClient, setApiClient] = React.useState(
|
||||
() => new ApiClient(token, handleUnauthorized, mode, settings?.baseUrl, showError),
|
||||
() => new ApiClient(token, handleUnauthorized, showError),
|
||||
)
|
||||
|
||||
React.useEffect(() => {
|
||||
@@ -93,9 +93,9 @@ export function AdminDashboard({ token, onLogout }: AdminDashboardProps) {
|
||||
|
||||
// Update API client when mode or settings change
|
||||
React.useEffect(() => {
|
||||
const newApiClient = new ApiClient(token, handleUnauthorized, mode, settings?.baseUrl, showError)
|
||||
const newApiClient = new ApiClient(token, handleUnauthorized, showError)
|
||||
setApiClient(newApiClient)
|
||||
}, [mode, settings?.baseUrl, token, handleUnauthorized, showError])
|
||||
}, [token, handleUnauthorized, showError])
|
||||
|
||||
const loadData = async () => {
|
||||
// Set default settings if loading fails
|
||||
|
||||
@@ -104,8 +104,16 @@ export function AdminsSection({ admins, apiClient, onAdminsUpdate }: AdminsSecti
|
||||
|
||||
const handleDeleteAdmin = async (adminId: string) => {
|
||||
try {
|
||||
await apiClient.call(`/api/v1/admins/${adminId}`, { method: "DELETE" })
|
||||
const res: any = await apiClient.call(`/api/v1/admins/${adminId}`, { method: "DELETE" })
|
||||
|
||||
// If the request failed, throw the error here
|
||||
if (res?.error?.length > 0) throw res.error
|
||||
|
||||
const updatedAdmins = await apiClient.call<Admin[]>("/api/v1/admins")
|
||||
|
||||
// If the request failed, throw the error here
|
||||
if (updatedAdmins?.error?.length > 0) throw updatedAdmins.error
|
||||
|
||||
onAdminsUpdate(updatedAdmins)
|
||||
|
||||
toast({
|
||||
|
||||
@@ -38,12 +38,16 @@ export function ChangePasswordDialog({ admin, apiClient, onClose }: ChangePasswo
|
||||
setError("")
|
||||
|
||||
try {
|
||||
await apiClient.call(`/api/v1/admins/${admin.id}`, {
|
||||
const res: any = await apiClient.call(`/api/v1/admins/${admin.id}`, {
|
||||
method: "PATCH",
|
||||
body: JSON.stringify({ password: newPassword }),
|
||||
})
|
||||
|
||||
// If the request failed, throw the error here
|
||||
if (res?.error?.length > 0) throw res.error
|
||||
|
||||
onClose()
|
||||
|
||||
|
||||
toast({
|
||||
variant: "default",
|
||||
title: "Admin Updated",
|
||||
|
||||
@@ -51,9 +51,13 @@ export function AddItemDrawer({ table, apiClient, open, onClose, onItemAdded }:
|
||||
method: "POST",
|
||||
body: JSON.stringify(formData),
|
||||
})
|
||||
|
||||
// If the request failed, throw the error here
|
||||
if (createdItem?.error?.length > 0) throw createdItem.error
|
||||
|
||||
onItemAdded(createdItem)
|
||||
onClose()
|
||||
|
||||
|
||||
toast({
|
||||
title: "Table Created",
|
||||
description: `A table ${table.name} has been created successfully.`,
|
||||
|
||||
@@ -148,20 +148,27 @@ export function AddTableDialog({ apiClient, onTablesUpdate, children }: AddTable
|
||||
)
|
||||
}
|
||||
|
||||
await apiClient.call("/api/v1/tables", {
|
||||
const res: any = await apiClient.call("/api/v1/tables", {
|
||||
method: "POST",
|
||||
body: JSON.stringify(tableData),
|
||||
})
|
||||
|
||||
// If the request failed, throw the error here
|
||||
if (res?.error?.length > 0) throw res.error
|
||||
|
||||
// Refresh tables list
|
||||
const updatedTables = await apiClient.call<TableMetadata[]>("/api/v1/tables")
|
||||
|
||||
// If the request failed, throw the error here
|
||||
if (updatedTables?.error?.length > 0) throw updatedTables.error
|
||||
|
||||
onTablesUpdate(updatedTables)
|
||||
|
||||
// Reset form
|
||||
setTableName("")
|
||||
setSqlQuery("")
|
||||
setOpen(false)
|
||||
|
||||
|
||||
toast({
|
||||
title: "Table Created",
|
||||
description: `The table '${tableData.name}' been updated successfully.`,
|
||||
@@ -338,12 +345,12 @@ export function AddTableDialog({ apiClient, onTablesUpdate, children }: AddTable
|
||||
</div>
|
||||
|
||||
<div className="text-sm text-muted-foreground mt-2">
|
||||
<p>
|
||||
<div>
|
||||
<Badge variant="outline" className="text-xs mr-2">
|
||||
System
|
||||
</Badge>
|
||||
System columns are automatically managed and cannot be modified.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -48,8 +48,18 @@ export function DatabaseSection({ apiClient, tables, onTablesUpdate }: DatabaseS
|
||||
|
||||
const handleDeleteTable = async (tableId: string) => {
|
||||
try {
|
||||
await apiClient.call(`/api/v1/tables/${tableId}`, { method: "DELETE" })
|
||||
const res: any = await apiClient.call(`/api/v1/tables/${tableId}`, { method: "DELETE" })
|
||||
|
||||
// If the request failed, throw the error here
|
||||
if (res?.error?.length > 0) throw res.error
|
||||
|
||||
// Fetch new tables
|
||||
const updatedTables = await apiClient.call<TableMetadata[]>("/api/v1/tables")
|
||||
|
||||
// If the request failed, throw the error here
|
||||
if (updatedTables?.error?.length > 0) throw updatedTables.error
|
||||
|
||||
// Set the new tables
|
||||
onTablesUpdate(updatedTables)
|
||||
|
||||
toast({
|
||||
@@ -73,6 +83,10 @@ export function DatabaseSection({ apiClient, tables, onTablesUpdate }: DatabaseS
|
||||
setIsRefreshing(true)
|
||||
try {
|
||||
const updatedTables = await apiClient.call<TableMetadata[]>("/api/v1/tables")
|
||||
|
||||
// If the request failed, throw the error here
|
||||
if (updatedTables?.error?.length > 0) throw updatedTables.error
|
||||
|
||||
onTablesUpdate(updatedTables)
|
||||
} catch (error) {
|
||||
console.error("Failed to refresh tables:", error)
|
||||
|
||||
@@ -49,9 +49,13 @@ export function EditItemDrawer({ table, item, apiClient, open, onClose, onItemUp
|
||||
method: "PATCH",
|
||||
body: JSON.stringify(formData),
|
||||
})
|
||||
|
||||
// If the request failed, throw the error here
|
||||
if (updatedItem?.error?.length > 0) throw updatedItem.error
|
||||
|
||||
onItemUpdate(updatedItem)
|
||||
onClose()
|
||||
|
||||
|
||||
toast({
|
||||
title: "Table Updated",
|
||||
description: "Table data has been updated successfully.",
|
||||
|
||||
@@ -126,6 +126,10 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
|
||||
method: "PATCH",
|
||||
body: JSON.stringify({ fields: columns, deletedFields: deletedColumns }),
|
||||
})
|
||||
|
||||
// If the request failed, throw the error here
|
||||
if (updatedTable?.error?.length > 0) throw updatedTable.error
|
||||
|
||||
onTableUpdate(updatedTable)
|
||||
toast({
|
||||
title: "Table Updated",
|
||||
@@ -152,6 +156,10 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
|
||||
deleteRule: rules.deleteRule,
|
||||
}),
|
||||
})
|
||||
|
||||
// If the request failed, throw the error here
|
||||
if (updatedTable?.error?.length > 0) throw updatedTable.error
|
||||
|
||||
onTableUpdate(updatedTable)
|
||||
toast({
|
||||
title: "Table Updated",
|
||||
|
||||
@@ -60,6 +60,10 @@ export function TableDetailView({ table, onBack, apiClient, onTableUpdate }: Tab
|
||||
|
||||
try {
|
||||
const tableData = await apiClient.call<any>(`/api/v1/${table.name}`)
|
||||
|
||||
// If the request failed, throw the error here
|
||||
if (tableData?.error?.length > 0) throw tableData.error
|
||||
|
||||
setTableData(tableData)
|
||||
|
||||
// setTotalPages(3) // Mock pagination
|
||||
@@ -136,10 +140,10 @@ export function TableDetailView({ table, onBack, apiClient, onTableUpdate }: Tab
|
||||
// Remove deleted items from table data
|
||||
setTableData((prevData) => prevData.filter((item) => !selectedItems.includes(item.id)))
|
||||
setSelectedItems([])
|
||||
toast({
|
||||
title: "Record(s) Deleted",
|
||||
description: "Table records have been updated successfully.",
|
||||
})
|
||||
// toast({
|
||||
// title: "Record(s) Deleted",
|
||||
// description: "Table records have been updated successfully.",
|
||||
// })
|
||||
} catch (error) {
|
||||
console.error("Failed to delete items:", error)
|
||||
} finally {
|
||||
|
||||
@@ -66,6 +66,10 @@ export function SettingsSection({ apiClient, settings, onSettingsUpdate, onModeC
|
||||
method: "PATCH",
|
||||
body: JSON.stringify(formData),
|
||||
})
|
||||
|
||||
// If the request failed, throw the error here
|
||||
if (updatedSettings?.error?.length > 0) throw updatedSettings.error
|
||||
|
||||
onSettingsUpdate(updatedSettings)
|
||||
setHasChanges(false)
|
||||
|
||||
@@ -84,6 +88,10 @@ export function SettingsSection({ apiClient, settings, onSettingsUpdate, onModeC
|
||||
setIsRefreshing(true)
|
||||
try {
|
||||
const updatedSettings = await apiClient.call<AppSettings>("/api/v1/settings/config")
|
||||
|
||||
// If the request failed, throw the error here
|
||||
if (updatedSettings?.error?.length > 0) throw updatedSettings.error
|
||||
|
||||
onSettingsUpdate(updatedSettings)
|
||||
} catch (error) {
|
||||
console.error("Failed to refresh settings:", error)
|
||||
|
||||
65
lib/api.ts
65
lib/api.ts
@@ -1,7 +1,7 @@
|
||||
"use client"
|
||||
|
||||
import type { AppMode } from "./app-state"
|
||||
import { useRouter } from "@/lib/router"
|
||||
|
||||
|
||||
// Updated interfaces to match the API response format
|
||||
export interface TableField {
|
||||
@@ -51,6 +51,24 @@ export interface Admin {
|
||||
updated: string
|
||||
}
|
||||
|
||||
export function getApiBaseUrl(): string {
|
||||
|
||||
const mode = process?.env?.NODE_ENV || "production";
|
||||
const port = process?.env?.MANTIS_PORT || 7070;
|
||||
|
||||
if (mode === "development") {
|
||||
// For debug mode, or non window sessions, pick port from env
|
||||
return `http://localhost:${port}`;
|
||||
}
|
||||
|
||||
if (typeof window !== "undefined") {
|
||||
const { origin } = window.location;
|
||||
return `${origin}`;
|
||||
}
|
||||
|
||||
return `http://localhost:${port}`;
|
||||
}
|
||||
|
||||
export interface AppSettings {
|
||||
allowRegistration: boolean
|
||||
appName: string
|
||||
@@ -75,45 +93,20 @@ export class ApiClient {
|
||||
private token: string
|
||||
private onUnauthorized: (error: string) => void
|
||||
private onError?: (error: string, type?: "error" | "warning") => void
|
||||
private requestCount = 0
|
||||
private maxRequests = 100
|
||||
private mode: AppMode
|
||||
private baseUrl: string
|
||||
|
||||
constructor(
|
||||
token: string,
|
||||
onUnauthorized: (reason?: string | "") => void,
|
||||
mode: AppMode = "PROD",
|
||||
baseUrl = "http://127.0.0.1:7070",
|
||||
onError?: (error: string, type?: "error" | "warning") => void,
|
||||
) {
|
||||
this.token = token
|
||||
this.onUnauthorized = onUnauthorized
|
||||
this.onError = onError
|
||||
this.mode = mode
|
||||
this.baseUrl = baseUrl
|
||||
}
|
||||
|
||||
private getApiBaseUrl(appMode: AppMode): string {
|
||||
if (appMode === "TEST") {
|
||||
// For debug mode, or non window sessions, pick port from env
|
||||
const port = process.env.MANTIS_PORT || 7070; // match your server default
|
||||
return `http://localhost:${port}`;
|
||||
}
|
||||
|
||||
if (typeof window !== "undefined") {
|
||||
const { origin } = window.location;
|
||||
console.log("ORIGIN: ", origin)
|
||||
return `${origin}`;
|
||||
} else {
|
||||
const port = process.env.MANTIS_PORT || 7070; // match your server default
|
||||
return `http://localhost:${port}`;
|
||||
}
|
||||
}
|
||||
|
||||
private async realApiCall<T>(endpoint: string, options: RequestInit = {}): Promise<ApiResponse<T>> {
|
||||
try {
|
||||
const url = `${this.getApiBaseUrl(this.mode)}${endpoint}`
|
||||
const url = `${getApiBaseUrl()}${endpoint}`
|
||||
const headers = {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${this.token}`,
|
||||
@@ -169,28 +162,18 @@ export class ApiClient {
|
||||
data: null as T,
|
||||
error: error.message || "Could not connect to the server!",
|
||||
status: 500, // could use 500 if you prefer
|
||||
} as T
|
||||
}
|
||||
}
|
||||
|
||||
// Method to update mode
|
||||
updateMode(mode: AppMode, baseUrl?: string) {
|
||||
this.mode = mode
|
||||
if (baseUrl) {
|
||||
this.baseUrl = baseUrl
|
||||
} as any
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function loginWithPassword(
|
||||
email: string,
|
||||
password: string,
|
||||
mode: AppMode = "PROD",
|
||||
baseUrl = "http://127.0.0.1:7070",
|
||||
password: string
|
||||
) {
|
||||
// Real API call
|
||||
try {
|
||||
const response = await fetch(`${baseUrl}/api/v1/admins/auth-with-password`, {
|
||||
const response = await fetch(`${getApiBaseUrl()}/api/v1/admins/auth-with-password`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json", },
|
||||
body: JSON.stringify({ email, password }),
|
||||
@@ -203,7 +186,7 @@ export async function loginWithPassword(
|
||||
if (responseData.data) {
|
||||
return responseData.data
|
||||
}
|
||||
return responseData
|
||||
return responseData.data
|
||||
} else {
|
||||
throw new Error(responseData.error || responseData.message || "Login failed")
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "mantis-admin",
|
||||
"version": "0.1.5",
|
||||
"version": "0.1.6",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"export": "next build && next export",
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 182 KiB After Width: | Height: | Size: 183 KiB |
Reference in New Issue
Block a user