Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admin Side fixes. #27

Merged
merged 3 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix not updating previous chapter while adding new
  • Loading branch information
amittras-pal committed Dec 22, 2022
commit b17d70e977f973a9af159d56fa59f9e2ff147055
57 changes: 41 additions & 16 deletions components/admin/AddChapter.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@ export default function AddChapter({ onCompleted }) {
}
}, [selectedStory, setValue]);

const refreshPages = async ({ refreshPassword }) => {
const refreshPages = async (values) => {
setProcessing("Refreshing Story...");
try {
await axios.post("/api/revalidate", {
pwd: refreshPassword,
updateType: [`stories/${selectedStory.id}`],
pwd: values.refreshPassword,
paths: [
`stories/${selectedStory.id}`,
`stories/${selectedStory.id}/${values.previousChapter}`,
],
});
setProcessing("Processing Completed.");
setTimeout(() => {
Expand All @@ -77,6 +80,8 @@ export default function AddChapter({ onCompleted }) {
setProcessing("Creating Chapter...");
try {
const fileUrl = await getDownloadURL(snapshotRef);

// Payload for creating new chapter
const chapter = {
author: values.author,
excerpt: values.excerpt,
Expand All @@ -86,28 +91,48 @@ export default function AddChapter({ onCompleted }) {
title: values.title,
content: fileUrl,
};
const chapterRef = doc(
store,
"stories",
selectedStory.id,
"chapters",
values.chapterId
);
await setDoc(chapterRef, chapter);

setProcessing("Updating Story Info...");
const storyUpdatePayload = {
// Payload for updating story
const storyUpdate = {
lastUpdated: Timestamp.fromDate(new Date()),
chapterSlugs: [...selectedStory.chapterSlugs, values.chapterId],
wip: !values.markCompleted,
draft: false,
};
if (chapter.order === 1)
storyUpdatePayload.published = Timestamp.fromDate(new Date());
storyUpdate.published = Timestamp.fromDate(new Date());

// New Chapter Reference
const newChapter = doc(
store,
"stories",
selectedStory.id,
"chapters",
values.chapterId
);
// Previous Chapter Reference
const prevChapter = doc(
store,
"stories",
selectedStory.id,
"chapters",
values.previousChapter
);
// Story Reference
const storyRef = doc(store, "stories", selectedStory.id);
await setDoc(storyRef, storyUpdatePayload, { merge: true });

// Update All in parallel.
await Promise.all([
setDoc(newChapter, chapter),
setDoc(
prevChapter,
{ nextChapter: values.chapterId },
{ merge: true }
),
setDoc(storyRef, storyUpdate, { merge: true }),
]);
setProcessing("Story Updated.");

// Revalidate static pages.
refreshPages(values);
} catch (error) {
console.error(error);
Expand Down
4 changes: 2 additions & 2 deletions components/admin/AddPost.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ export default function AddPost({ onCompleted }) {
if (!formValues.draft)
post.published = Timestamp.fromDate(new Date());
delete post.postId;
delete post.refreshPassword; // TODO:
delete post.refreshPassword;
const docRef = doc(store, "posts", formValues.postId);
await setDoc(docRef, post);

if (!formValues.draft) {
await await axios.post("/api/revalidate", {
pwd: formValues.refreshPassword,
updateType: ["posts"],
paths: ["posts"],
});
}
setProcessing("Completed.");
Expand Down
8 changes: 4 additions & 4 deletions pages/api/revalidate.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ export default async function handler(req, res) {
return res.status(401).json({ error: "Invalid credentials." });

// check if routes are provided.
const { updateType = [] } = req.body;
if (updateType.length === 0)
const { paths = [] } = req.body;
if (paths.length === 0)
return res.status(400).json({ error: "Routes not provided." });

/* MAIN BUSINESS LOGIC */
try {
await Promise.all([
res.revalidate("/"),
...updateType.map((route) => res.revalidate(`/${route}`)),
...paths.map((route) => res.revalidate(`/${route}`)),
]);
return res.json({ message: `Rebuilt routes: ${updateType.join(", ")}` });
return res.json({ message: `Rebuilt routes: ${paths.join(", ")}` });
} catch (error) {
return res.status(500).json({
error: "Something went wrong, revalidation of the routes failed.",
Expand Down
17 changes: 16 additions & 1 deletion utils/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ export const storyFormValues = {
chapterSlugs: [],
};


/**
* The complete Triforce, or one or more components of the Triforce.
* @typedef {Object} ChapterFormValues
* @property {string} author
* @property {string} excerpt
* @property {string} chapterId
* @property {number} order
* @property {string} previousChapter
* @property {string} nextChapter
* @property {string} title
* @property {File} file
* @property {boolean} markCompleted
* @property {string} refreshPassword
*/
export const chapterFormValues = {
author: "",
excerpt: "",
Expand Down Expand Up @@ -267,4 +282,4 @@ export const messageValidator = yup.object().shape({
.required("Message is required")
.min(20, "Message should be between 20-1024 characters in length")
.max(1024, "Message should be between 20-1024 characters in length"),
});
});