Skip to content

Commit

Permalink
admin feature initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
effy971 committed Nov 10, 2020
1 parent 896e6ff commit f49f4ff
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 4 deletions.
29 changes: 29 additions & 0 deletions admindeletequestion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { MongoClient, ObjectId } = require("mongodb");

module.exports = async function (id) {
const uri = process.env.DB_CREDENTIALS;

const client = new MongoClient(uri);
let successOrFail = false;
try {
await client.connect();

const database = client.db("4learn");
const collection = database.collection("Admin");

// create js obj received by /register
console.log(id);
const doc = { id: id };
const result = await collection.deleteOne({ _id: ObjectId(id) });

console.log(`${result.insertedCount} documents were inserted into db`);
console.log(result.insertedCount > 0);

if (result.insertedCount > 0) {
successOrFail = true;
return true;
}
} finally {
await client.close();
}
};
29 changes: 29 additions & 0 deletions adminquestion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { MongoClient } = require("mongodb");

module.exports = async function (title, area, detail) {
const uri = process.env.DB_CREDENTIALS;

const client = new MongoClient(uri);
let successOrFail = false;
try {
await client.connect();

const database = client.db("4learn");
const collection = database.collection("Admin");

// create js obj received by /register

const doc = { title: title, area: area, detail: detail };
const result = await collection.insertOne(doc);

console.log(`${result.insertedCount} documents were inserted into db`);
console.log(result.insertedCount > 0);

if (result.insertedCount > 0) {
successOrFail = true;
return true;
}
} finally {
await client.close();
}
};
24 changes: 24 additions & 0 deletions adminviewquestion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { MongoClient } = require("mongodb");

module.exports = async function (questionArray) {
const uri = process.env.DB_CREDENTIALS;

const client = new MongoClient(uri, { useUnifiedTopology: true });
let successOrFail = false;

try {
await client.connect();

const database = client.db("4learn");
const collection = database.collection("Admin");

// create js obj received by /login

const result = await collection.find();

await result.forEach((result) => questionArray.push(result));
} finally {
await client.close();
return questionArray;
}
};
53 changes: 50 additions & 3 deletions frontend/src/Components/Admin/Admin.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,56 @@
import React from "react";
import React, { useState, useEffect } from "react";
import AdminQuestion from "./AdminQuestion";

const Admin = () => {
const [result, setResult] = useState([]);

// const [title, setTitle] = useState([]);
// const [area, setArea] = useState([]);
// const [detail, setDetail] = useState([]);

const [loading, setLoading] = useState(true);

useEffect(() => {
let isMounted = true;
if (isMounted) {
async function fetchData() {
const databaseResult = await fetch("/adminviewquestion");
const resultObj = await databaseResult.json();
console.log(resultObj);
const tempResult = [];

// push title into result state

await resultObj.forEach((element) => {
tempResult.push(element);
});
// setTitle(tempTitle);
// setArea(tempArea);
// setDetail(tempDetail);
setResult(tempResult);
setLoading(false);
}
fetchData();
}
return () => {
isMounted = false;
};
}, []);

return (
<div>
<h1>admin page</h1>
<div className="container-question">
{loading ? (
<h4>Loading ...</h4>
) : (
result.map((element) => (
<AdminQuestion
title={element.title}
area={element.area}
detail={element.detail}
id={element._id}
/>
))
)}
</div>
);
};
Expand Down
41 changes: 41 additions & 0 deletions frontend/src/Components/Admin/AdminQuestion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from "react";
import parse from "html-react-parser";
const AdminQuestion = (props) => {
const handleClickAccept = () => {
fetch("/askquestion", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
title: props.title,
area: props.area,
detail: props.detail,
}),
});
};

const handleClickReject = () => {
fetch("/admindeletequestion", {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
id: props.id,
}),
});
};

return (
<div className="container-view-question">
<h4>Title: {props.title}</h4>

<p>{parse(props.detail)}</p>
<button onClick={handleClickAccept}>Accept</button>
<button onClick={handleClickReject}>Reject</button>
</div>
);
};

export default AdminQuestion;
2 changes: 1 addition & 1 deletion frontend/src/Components/Question/AskQuestion.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from "react";
const AskQuestion = () => {
return (
<div>
<form action="/askquestion" method="POST">
<form action="/adminquestion" method="POST">
<label for="title">Question Title</label>
<input type="text" name="title"></input>
<label for="area">Question Area</label>
Expand Down
27 changes: 27 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const getChatroomList = require("./getchatroomlist");
const getQuestionList = require("./getquestionlist");
const saveUserWork = require("./saveuserwork");
const hostAddQuestion = require("./hostaddquestion");
const adminQuestion = require("./adminquestion");
const adminDeletequestion = require("./admindeletequestion");
const adminViewquestion = require("./adminviewquestion");

const auth = require("./auth");
const jwt = require("jsonwebtoken");
Expand Down Expand Up @@ -130,6 +133,30 @@ app.post("/askquestion", async (req, res) => {
: res.send("there was an error while posting");
});

app.get("/adminviewquestion", async (req, res) => {
const data = [];
const result = await adminViewquestion(data);

res.json(result);
});

app.post("/adminquestion", async (req, res) => {
const result = await adminQuestion(
req.body.title,
req.body.area,
req.body.detail
);
result
? res.send("question posted success")
: res.send("there was an error while posting");
});

app.delete("/admindeletequestion", async (req, res) => {
const result = await adminDeletequestion(req.body.id);

res.send("deleted");
});

app.get("/answerquestion", async (req, res) => {
const data = [];
const result = await getQuestion(data);
Expand Down

0 comments on commit f49f4ff

Please sign in to comment.