Skip to content

Commit

Permalink
fix : [bug] wiki pages not showing on the main page. tdjsnelling#63
Browse files Browse the repository at this point in the history
  • Loading branch information
Klaiment committed Mar 7, 2024
1 parent c9385c2 commit f9a4ba0
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 17 deletions.
46 changes: 45 additions & 1 deletion api/src/controllers/wiki.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,51 @@ export const getWiki = async (req, res, next) => {
next(e);
}
};

export const getWikis = async (req, res, next) => {
try {
const result = await Wiki.aggregate([
{
$lookup: {
from: "users",
as: "createdBy",
let: { userId: "$createdBy" },
pipeline: [
{
$match: { $expr: { $eq: ["$_id", "$$userId"] } },
},
{
$project: {
username: 1,
},
},
],
},
},
{
$unwind: {
path: "$createdBy",
preserveNullAndEmptyArrays: true,
},
},
]);
if (!result || result.length === 0) {
res.status(404).send("No wikis found");
return;
}
let page = result[0];
if (process.env.SQ_ALLOW_UNREGISTERED_VIEW && !req.userId && !page.public) {
page = null;
}
const query = {};
if (process.env.SQ_ALLOW_UNREGISTERED_VIEW && !req.userId) {
query.public = true;
}
const allPages = await Wiki.find(query, { slug: 1, title: 1 }).lean();
res.json({ page, allPages });
} catch (e) {
next(e);
}
};
export const deleteWiki = async (req, res, next) => {
try {
if (req.userRole !== "admin") {
Expand Down
2 changes: 2 additions & 0 deletions api/src/routes/wiki.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import express from "express";
import {
createWiki,
getWiki,
getWikis,
deleteWiki,
updateWiki,
} from "../controllers/wiki";
Expand All @@ -11,6 +12,7 @@ const router = express.Router();
export default () => {
router.post("/new", createWiki);
router.post("/update/:wikiId", updateWiki);
router.get("/", getWikis);
router.get("*", getWiki);
router.delete("*", deleteWiki);
return router;
Expand Down
49 changes: 33 additions & 16 deletions client/pages/wiki/[[...slug]].js
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,18 @@ const Wiki = ({ page, allPages, token, userRole, slug }) => {
</form>
)}
</>
) : allPages.length > 0 ? (
<>
{allPages.map((p) => (
<Link key={`page-${p.slug}`} href={`/wiki${p.slug}`} passHref>
<Text as="a" display="block">
{p.title}
</Text>
</Link>
))}
</>
) : (
<Text color="grey">{getLocaleString("wikiThereNothingHereYet")}</Text>
<Text>{getLocaleString("wikiThereNothingHereYet")}</Text>
)}
{showDeleteModal && (
<Modal close={() => setShowDeleteModal(false)}>
Expand All @@ -272,7 +282,6 @@ const Wiki = ({ page, allPages, token, userRole, slug }) => {
export const getServerSideProps = withAuthServerSideProps(
async ({ token, fetchHeaders, isPublicAccess, query: { slug } }) => {
if (!token && !isPublicAccess) return { props: {} };

const parsedSlug = slug?.length ? slug.join("/") : "";

const {
Expand All @@ -281,21 +290,29 @@ export const getServerSideProps = withAuthServerSideProps(
} = getConfig();

const { role } = token ? jwt.verify(token, SQ_JWT_SECRET) : { role: null };

try {
const wikiRes = await fetch(`${SQ_API_URL}/wiki/${parsedSlug}`, {
headers: fetchHeaders,
});
if (
wikiRes.status === 403 &&
(await wikiRes.text()) === "User is banned"
) {
throw "banned";
}
const { page, allPages } = await wikiRes.json();
return {
props: { page, allPages, token, userRole: role, slug: parsedSlug },
};
let page, allPages;
if (parsedSlug.length === 0) {
const wikiRes = await fetch(`${SQ_API_URL}/wiki`, {
headers: fetchHeaders,
});
({ allPages } = await wikiRes.json());

return {
props: {allPages, token, userRole: role, slug: parsedSlug, },
};
} else {
const wikiRes = await fetch(`${SQ_API_URL}/wiki/${parsedSlug}`, {
headers: fetchHeaders,
});
if (wikiRes.status === 403 && (await wikiRes.text()) === "User is banned") {
throw "banned";
}
({ page, allPages } = await wikiRes.json());
return {
props: { page, allPages, token, userRole: role, slug: parsedSlug },
};
}
} catch (e) {
if (e === "banned") throw "banned";
return { props: { token, userRole: role, slug: parsedSlug } };
Expand Down

0 comments on commit f9a4ba0

Please sign in to comment.