Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

Feature/automatic admins publication #45

26 changes: 19 additions & 7 deletions client/src/pages/Suggest/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import clsx from "clsx";
import PropTypes from "prop-types";
import { useCallback, useEffect, useState } from "react";

import { usePrincipal } from "../../authContext";
import { Form, FormControls } from "../../components";
import { ResourceService, TopicService, useService } from "../../services";

Expand All @@ -12,6 +13,7 @@ export default function Suggest() {
const [topics, setTopics] = useState(undefined);
const resourceService = useService(ResourceService);
const topicService = useService(TopicService);
const principal = usePrincipal();

useEffect(() => {
topicService.getTopics().then(setTopics);
Expand All @@ -26,7 +28,9 @@ export default function Suggest() {
await resourceService.suggest(suggestion);
setMessage({
success: true,
text: "Thank you for suggesting a resource!",
text: principal?.is_admin
? "Thank you for suggesting a resource. It has been published."
: "Thank you for suggesting a resource! It will be reviewd by an administrator",
});
} catch (err) {
setMessage({
Expand All @@ -36,17 +40,25 @@ export default function Suggest() {
throw err;
}
},
[resourceService]
[resourceService, principal]
);

return (
<>
<h2>Suggest a resource</h2>
<p>
Please use the form below to submit a suggestion. Note that it will not
appear on the home page immediately, as it needs to be reviewed by an
administrator.
</p>
{principal?.is_admin ? (
<p>
Please use the form below to submit a suggestion. Note that, your
suggestion will be published immediately.
</p>
) : (
<p>
Please use the form below to submit a suggestion. Note that it will
not appear on the home page immediately, as it needs to be reviewed by
an administrator.
</p>
)}

<section>
{message && <Message {...message} />}
<Form
Expand Down
12 changes: 11 additions & 1 deletion server/resources/resourcesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,19 @@ router
}),
asyncHandler(async (req, res) => {
const { id: source } = req.user;
let suggetedUser = req.user?.is_admin;

try {
const resource = await service.create({ ...req.body, source });
res.status(201).send(resource);
if (suggetedUser) {
const publishedResponse = await service.publish(
resource.id,
req.user?.id ?? null
);
res.status(201).send(publishedResponse);
} else {
res.status(201).send(resource);
}
} catch (err) {
if (err instanceof DuplicateResource) {
return res.sendStatus(409);
Expand Down