Could not understand what is wrong while trying to update a param of an object in DB through custom component in adminjs. Receiving
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
Probably need something additional with data manipulation on mongoDB. Can’t understand what exactly with this custom actions. Maybe someone can help with this
index.js
import AdminJS from "adminjs"
import { buildRouter as AdminJSExpress } from "@adminjs/express"
import express from "express"
import mongoose from "mongoose"
import * as AdminJSMongoose from "@adminjs/mongoose"
import { componentLoader } from "./componentLoader.js"
import { OfferStatus } from "./offerStatus.js"
const PORT = 3000
const DB = "..."
AdminJS.registerAdapter({
Resource: AdminJSMongoose.Resource,
Database: AdminJSMongoose.Database,
})
const startAdminJS = async () => {
try {
await mongoose.connect(DB, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
console.log("Connected to MongoDB")
const app = express()
const admin = new AdminJS({
resources: [OfferStatus],
componentLoader: componentLoader,
})
const adminRouter = AdminJSExpress(admin)
app.use(admin.options.rootPath, adminRouter)
admin.watch()
app.listen(PORT, () => {
console.log(
`Listening on port ${PORT}, AdminJS server started on URL: http://localhost:${PORT}${admin.options.rootPath}`
)
})
} catch (error) {
console.error("Error starting AdminJS", error)
}
}
startAdminJS()
entity
import mongoose from "mongoose"
const orderSchema = new mongoose.Schema({
network_id: {
type: Number,
required: true,
},
offer_status: { type: Boolean, required: true },
})
export const Order = mongoose.model("Order", orderSchema)
resource options
import { Order } from "./order.entity.js"
import { Components } from "./componentLoader.js"
const toggleStatusAction = {
name: "toggleStatus",
actionType: "record",
isVisible: true,
handler: async (request, context) => {
const { record, resource } = context
if (record.isValid() && record.param("offer_status") !== undefined) {
await resource.update(request.params.recordId, {
offer_status: !record.param("offer_status"),
})
return {
record: record.toJSON(),
notice: {
message: "Status successfully toggled",
type: "success",
},
}
} else {
throw new AdminJS.ValidationError(
{
offer_status: {
message: "Status could not be toggled",
},
},
{
message: "An error occurred during status toggle",
}
)
}
},
components: {
list: Components.CustomCheckbox,
},
}
export const OfferStatus = {
resource: Order,
options: {
properties: {
_id: {
type: "string",
isVisible: {
edit: false,
show: false,
list: false,
filter: false,
},
},
offer_status: {
type: "boolean",
components: {
list: Components.CustomCheckbox,
},
},
},
},
actions: {
toggleStatus: toggleStatusAction,
},
}
custom component
import React, { useState } from "react"
import { CheckBox } from "@adminjs/design-system"
const CustomCheckbox = ({ record, property }) => {
const [checked, setChecked] = useState(record.params[property.name])
const handleChange = async () => {
const newValue = !checked
setChecked(newValue)
try {
await fetch(
`/admin/api/resources/Order/records/${record.id}/toggleStatus`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
recordId: record.id,
}),
}
)
} catch (error) {
console.error("Error to change status", error)
setChecked(!newValue)
}
}
return <CheckBox checked={checked} onChange={handleChange} />
}
export default CustomCheckbox