Skip to content

Commit

Permalink
fix permissions, add notes
Browse files Browse the repository at this point in the history
  • Loading branch information
GraemeFulton committed Aug 20, 2024
1 parent ae5a7ac commit a759cab
Show file tree
Hide file tree
Showing 14 changed files with 701 additions and 507 deletions.
2 changes: 1 addition & 1 deletion app/typr/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const metadata = {
description: 'A customizable WYSIWYG editor with publishing flows and user state management for React.js. Built with Tiptap and ProseMirror.',
images: [
{
url: `${process.env.NEXTAUTH_URL}/static/images/typr-og.jpg`,
url: `${process.env.NEXTAUTH_URL}/static/images/typr-og.png`,
width: 1200,
height: 630,
alt: 'Tiptypr Editor Preview',
Expand Down
2 changes: 1 addition & 1 deletion components/toolbox/hooks/useLoad.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const useLoad = (user) => {
setLoading(true);

try {
const data = await getUserArticle(user, id);
const data = await getUserArticle({user, id, type:'tool'});

const post = data;

Expand Down
9 changes: 9 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
],
"~/*": [
"./node_modules/*"
],
"paper-interview": [
"./prototypr-packages/paper-interview/src"
],
"react-kofi": [
"./prototypr-packages/react-kofi/src"
],
"tiptypr": [
"./prototypr-packages/tiptypr/src"
]
}
}
Expand Down
5 changes: 4 additions & 1 deletion lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -786,14 +786,17 @@ export async function getPartnerJobs({ user, pageSize, offset }) {
});
return data;
}
export async function getUserArticle(user, id) {
export async function getUserArticle({user, id, type}) {

const data = await fetchAPIAuthenticated(user, userArticle, { id });

const hasPermission = permissionCheck(user, data?.userPostId);

if (hasPermission) {
if(data?.userPostId){
if(data.userPostId.type !== type){
return false;
}
data.userPostId.versioned_content = data.userPostId.draft_content;
data.userPostId.versioned_title = data.userPostId.draft_title;
return data?.userPostId;
Expand Down
4 changes: 2 additions & 2 deletions lib/editor/createPost.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from "axios";
import { checkSessionExpired } from "../account/checkSessionExpired";

export const createPost = async ({ entry, user }) => {
export const createPost = async ({ entry, user, type }) => {

//check if jwt is expired
const sessionExpired = checkSessionExpired(user?.jwt);
Expand All @@ -14,7 +14,7 @@ export const createPost = async ({ entry, user }) => {
let createData = false;

let newPostData = {
type: "article",
type: type ? type : "article",
status: entry.status,
title: entry.title,
content: entry.content,
Expand Down
8 changes: 8 additions & 0 deletions lib/editor/permissionCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ export const permissionCheck = (user, post) => {
if (user && post?.owner == user?.id && post?.type === "article") {
hasPermission = true;
}
if (user && post?.owner == user?.id && post?.type === "tool") {
hasPermission = true;
}
if (user && post?.owner == user?.id && post?.type === "note") {
hasPermission = true;
}



if (user?.isAdmin && post?.id) {
hasPermission = true;
Expand Down
88 changes: 88 additions & 0 deletions lib/editor/typrNotesProps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { signOut } from "next-auth/react";
import fetchJson from "@/lib/iron-session/fetchJson";
import { createPost } from "@/lib/editor/createPost";
import { savePost } from "@/lib/editor/savePost";
import { getUserArticle } from "@/lib/api";
// import { loggedInMenu } from "./menus/loggedInMenu";

export const typrNotesProps = ({ user, userLoading, mutateUser, router }) => ({
enablePublishingFlow: true,
theme: "blue",
editorWidth: "590px",
components: {
nav: {
show:true,
undoRedoButtons: { show: false },
logo: { show:false,image: `/static/images/logo-small.svg`, url: "/" },
userBadge: {
show:false,
// loggedInMenu
},
position: "sticky",
},
userMenu: {
avatarPlaceholder:
"https://prototypr-media.sfo2.digitaloceanspaces.com/strapi/4f9713374ad556ff3b8ca33e241f6c43.png?updated_at=2022-12-14T10:55:38.818Z",
},
settingsPanel: {
featuredImage: {
show:true
},
},
},
user: {
id: user?.id,
slug: user?.profile?.slug,
avatar: user?.profile?.avatar?.url,
isLoggedIn: user?.isLoggedIn,
isAdmin: user?.isAdmin,
loading: userLoading,
jwt: user?.jwt,
signOut: async () => {
await signOut({ redirect: false });
mutateUser(
await fetchJson("/api/auth/logout", { method: "POST" }),
false
);
},
},
postOperations: {
load: async ({ postId, user }) => {
const postObject = await getUserArticle({user, id:postId, type: "note"});
return postObject;
},
save: async ({ entry, postId }) => {
const postObject = await savePost({ entry, postId, user });
return postObject;
},
create: async ({ entry }) => {
const postObject = await createPost({ entry, user, type: "note" });
return postObject;
},
},
mediaHandler:{
config: {
method: "post",
url: `${process.env.NEXT_PUBLIC_API_URL}/api/users-permissions/users/article/image/upload`,
headers: {
Authorization: `Bearer ${user?.jwt}`,
}
},
},
hooks: {
onPostCreated: ({ id }) => {
router.replace(
{
pathname: router.pathname,
query: { slug: id },
as: `/p/${id}`,
},
undefined,
{ shallow: true }
);
},
},
router: {
push: router.push
},
});
2 changes: 1 addition & 1 deletion lib/editor/typrProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const typrProps = ({ user, userLoading, mutateUser, router }) => ({
},
postOperations: {
load: async ({ postId, user }) => {
const postObject = await getUserArticle(user, postId);
const postObject = await getUserArticle({user, id:postId, type: "article"});
return postObject;
},
save: async ({ entry, postId }) => {
Expand Down
Loading

0 comments on commit a759cab

Please sign in to comment.