Skip to content
Commits on Source (8)
......@@ -7,7 +7,6 @@ export default function Navigation() {
<div className={styles.link}>
<Link href="/appSettings">AppSettings</Link>
</div>
<div className={styles.link}>
<Link href="/analytics">Analytics</Link>
</div>
......@@ -23,6 +22,9 @@ export default function Navigation() {
<div className={styles.link}>
<Link href="/categories">Catégories</Link>
</div>
<div className={styles.link}>
<Link href="/contextSettings">ContextSettings</Link>
</div>
<div className={styles.link}>
<Link href="/defaultSpaces">DefaultSpaces</Link>
</div>
......
import mongoose from "mongoose";
const ContextSettingsSchema = new mongoose.Schema(
{
_id: { type: String },
key: { type: String },
type: { type: String },
value: { type: Array },
'value.$': {
type: { any: {} },
}
},
{ collection: "contextsettings" },
{ timestamps: true }
);
export default mongoose.models.ContextSettings ||
mongoose.model("ContextSettings", ContextSettingsSchema);
This diff is collapsed.
{
"name": "supercrudv2",
"version": "1.3.0",
"version": "1.4.0-testing.1",
"private": true,
"scripts": {
"dev": "next dev -p 3050",
......
import { getServerSession } from "next-auth/next";
import { authOptions } from "../auth/[...nextauth]";
import connectDB from "../../../database/connectDB";
import ContextSettings from "../../../database/models/ContextSettings";
function BooleansToStrings(setting) {
if (setting.type === 'boolean') {
return {...setting._doc, value: setting.value.map((val) => val.toString())};
}
return setting;
}
function convertStringToExpectedType(setting) {
if (setting.type === 'boolean') {
return {...setting, value: setting.value.map((val) => val === "true")};
}
if (setting.type === 'number') {
return {...setting, value: setting.value.map((val) => parseInt(val))};
}
return setting;
}
export default async function handler(req, res) {
const session = await getServerSession(req, res, authOptions);
const { method } = req;
const { tableName } = req.query;
if (!session)
return res.end(
"You must be signed in to view the protected content on this page."
);
if (!tableName) return res.end("Cannot access API by URL");
await connectDB();
switch (method) {
case "GET":
try {
const contextSettings = await ContextSettings.find({});
const convertedSettings = contextSettings.map((setting) => BooleansToStrings(setting));
res.status(200).json({ data: convertedSettings });
} catch (error) {
res.status(400).json({ success: false, error: error.message });
}
break;
case "DELETE":
try {
const bodyParsed = JSON.parse(req.body);
const id = bodyParsed.id;
const result = await ContextSettings.findOneAndDelete({ _id: id });
res.status(200).json({
success: true,
result,
message: "supprimé de la Base de Donnée !",
});
} catch (error) {
res.status(400).json({ success: false, error: error.message });
}
break;
case "PUT":
try {
const bodyParsed = JSON.parse(req.body);
const id = bodyParsed._id;
const convertedBody = convertStringToExpectedType(bodyParsed)
const result = await ContextSettings.findByIdAndUpdate(
{ _id: id },
{ ...convertedBody }
);
res.status(200).json({
success: true,
result,
message: "Modifié en Base de Donnée !",
});
} catch (error) {
res.status(400).json({ success: false, error: error.message });
}
break;
}
}
import { useMemo } from "react";
import DashBoard from "../components/DashBoard/DashBoard";
const ContextSettings = () => {
const columns = useMemo(
() => [
{
accessorKey: "_id",
header: "id",
enableEditing: false,
size: 200,
},
{
accessorFn: (row) => row?.key,
id: "key",
header: "key",
enableEditing: false,
},
{
accessorFn: (row) => row?.type,
id: "type",
header: "type",
enableEditing: true,
},
{
accessorFn: (row) => row?.value,
id: "value",
header: "value",
enableEditing: true,
},
],
[]
);
return (
<>
<DashBoard columns={columns} apiRouteName={"contextSettings"} />
</>
);
};
export default ContextSettings;