Skip to content

Commit

Permalink
fix: write page api and fixes #348
Browse files Browse the repository at this point in the history
fixed write page save to draft , publish and fixes
  • Loading branch information
jasirtp committed Nov 3, 2021
1 parent 99f33d9 commit 603734d
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 34 deletions.
6 changes: 3 additions & 3 deletions component/myblogs/MyBlogs.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ export default function MyBlogs({ posts }) {
<div className="w-full">
{posts &&
posts.map((item) => (
<div className={`${MyBlogsstyles.oddBg} w-full`} key={item._id}>
<div className={`${MyBlogsstyles.oddBg} w-full`} key={item.id}>
<div
className={`flex flex-col md:flex-row md:items-center justify-between p-4`}
>
<div className="">
<Link
href={{
pathname: `/posts/write`,
query: { post_id: `${item._id}` }
query: { post_id: `${item.id}` }
}}
className={`text-15 font-semibold mb-1 ${MyBlogsstyles.color_blue_910}`}
>
Expand Down Expand Up @@ -79,7 +79,7 @@ export default function MyBlogs({ posts }) {
<Link
href={{
pathname: `/posts/write`,
query: { post_id: `${item._id}` }
query: { post_id: `${item.id}` }
}}
>
<a target="_blank">
Expand Down
8 changes: 4 additions & 4 deletions component/myblogs/WriteNav.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default function WriteNav({
}, [post]);

useEffect(() => {
// getSettingsTags();
getSettingsTags();
if (userCookie.roles === "admin") {
getIsAdmin();
}
Expand All @@ -81,7 +81,7 @@ export default function WriteNav({
async function getSettingsTags() {
try {
const res = await TagService.getTags();
// console.log("get tags response", res);
console.log("get tags response", res);
if (res && res.length) {
const resTagOptions = res.map((tag) => {
return {
Expand All @@ -108,12 +108,12 @@ export default function WriteNav({
const newTag = e
.filter((tag) => {
if (tag.__isNew__ === true) {
// console.log(tag);
console.log(tag);
return tag;
}
})
.map((fTag) => fTag.value);
// console.log(newTag);
console.log(newTag, 'newtag');
try {
const res = await TagService.postTags(userCookie, newTag);
// console.log({ res });
Expand Down
2 changes: 2 additions & 0 deletions config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const userUrl = `${apiVerUrl}/user`;
const usersUrl = `${apiVerUrl}/users`;
const adminUrl = "api/admin";
const tagUrl = `${apiVerUrl}/tags`;
const CreateTagUrl = `${apiVerUrl}/tag`;
const skillUrl = "api/skills";
const searchUrl = "api/search";
const forgotPasswordUrl = "api/auth/reset-password";
Expand Down Expand Up @@ -47,6 +48,7 @@ const configVars = {
userUrl,
adminUrl,
tagUrl,
CreateTagUrl,
serverUrl,
searchUrl,
forgotPasswordUrl,
Expand Down
10 changes: 5 additions & 5 deletions pages/posts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,18 @@ export default function Posts() {
pageNo: router.query.pageNo,
limit: postData.limit,
tag: router.query.tag,
status: router.query.status
// with_table: ["user"]
status: router.query.status,
with_table: ["user"]
};
// getPosts(newReqData);
getPosts(newReqData);
}, [router.query.pageNo, router.query.tag, router.query.status]);

async function getPosts(reqData) {
// console.log("getposts call");
try {
const data = await PostService.getPostsByUserId(reqData);
const { posts, count } = data;
// console.log({ posts });
const { posts, count } = data.data;
console.log({ posts });
if (data) {
setLoaded(true);
}
Expand Down
30 changes: 17 additions & 13 deletions pages/posts/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ export default function Write({
}
}


async function updatePostById(updateData, newPostId) {
try {
const res = await PostService.updatePostById(updateData, newPostId);
Expand All @@ -171,38 +172,41 @@ export default function Write({
iframeRef.current.contentWindow.postMessage({ msg: "savePost" }, TARGET);
setTimeout(() => {
// wait for the response post message to get the post from the state
// console.log({ postElement });
console.log(postElement.current.scratch );
// console.log(postElement.current.scratch);
const newMobiledoc = postElement.current.scratch;
const title = postElement.current.titleScratch || "[Untitled]";
const newMobiledoc = postElement?.current.scratch;
const title = postElement?.current.titleScratch || "[Untitled]";

if (postId) {
//updatePost
const newUpdatedPost = {
status: 'drafted',
title: title,
mobiledoc: newMobiledoc
mobiledoc: newMobiledoc,
};
console.log({ newUpdatedPost });
updatePostById(newUpdatedPost, postId);
updatePostById(postId, newUpdatedPost);
}
}, 500);

};

async function submitForReview() {
iframeRef.current.contentWindow.postMessage({ msg: "savePost" }, TARGET);
//change status to "pending" if submitted for review
// console.log(postId);
try {
const statusUpdate = {
status: "pending"
status: "pending",
// mobiledoc: postElement.current.scratch,
// mobiledoc: post.mobiledoc,
};
const msg = await PostService.changePostStatus(
userCookie,
const msg = await PostService.updatePostById(
postId,
statusUpdate
);
// console.log(msg);
statusUpdate,
);
console.log(msg);
if (msg) {
// notify(msg);
notify(msg);
return msg;
}
} catch (err) {
Expand Down
8 changes: 4 additions & 4 deletions services/PostService.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { getUrl } from "../lib/getUrl";

async function getPostsByUserId(reqData) {
try {
const { data } = await axios.get(`${baseUrl}/${allPostsUrl}`, {
const { data } = await axios.get(`${baseUrl}/${postsUrl}`, {
params: reqData
});
console.log(data);
Expand Down Expand Up @@ -62,7 +62,7 @@ async function createPost(post) {
async function getLatestPosts(reqParams) {
let url = getUrl();
try {
const res = await axios.get(`${url}/${postsUrl}`, {
const res = await axios.get(`${baseUrl}/${postsUrl}`, {
params: reqParams
});
return res.data.data;
Expand Down Expand Up @@ -94,9 +94,9 @@ async function adminGetLatestPosts(reqParams) {
}
}

async function updatePostById(post, postId) {
async function updatePostById(postId, postDetails) {
try {
const { data } = await axios.put(`${baseUrl}/${postUrl}/${postId}`, post);
const { data } = await axios.put(`${baseUrl}/${postUrl}/${postId}`, postDetails);
return data;
} catch (err) {
console.log(err);
Expand Down
9 changes: 4 additions & 5 deletions services/TagService.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
const axios = require("axios");
import { baseUrl, tagUrl } from "../config/config";
import { baseUrl, tagUrl,CreateTagUrl } from "../config/config";
import { getUrl } from "../lib/getUrl";

async function getTags(filterWhatsNew) {
let url = getUrl();

try {
const { data } = await axios.get(
`${url}/${tagUrl}`
`${baseUrl}/${tagUrl}`
// {
// params: { filterWhatsNew }
// }
);
// console.log(data);
console.log(data);
return data;
} catch (err) {
console.log(err);
Expand All @@ -23,7 +22,7 @@ async function getTags(filterWhatsNew) {
async function postTags(userCookie, newTag) {
try {
const { data } = await axios.post(
`${baseUrl}/${tagUrl}`,
`${baseUrl}/${CreateTagUrl}`,
{ tags: newTag },
{
headers: {
Expand Down

0 comments on commit 603734d

Please sign in to comment.