-
Notifications
You must be signed in to change notification settings - Fork 0
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
Publish blog posts 464 #473
base: staging
Are you sure you want to change the base?
Changes from all commits
6bfc425
d676c8e
8d10bb5
e01e764
31b8aa2
bdd50c2
9d310e5
496cf7a
be78c92
091596d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use client'; | ||
|
||
import { useMutation, useQueryClient } from '@tanstack/react-query'; | ||
import { AxiosError } from 'axios'; | ||
import { toast } from 'react-toastify'; | ||
import { useAxios } from '../../AxiosProvider'; | ||
import ENDPOINTS from '../../endpoints'; | ||
import QUERY_KEYS from '../../queryKeys'; | ||
|
||
type UpdatePostStatusPayload = { | ||
id: string; | ||
status: string; | ||
}; | ||
|
||
type ErrorResponse = { | ||
message: string; | ||
statusCode?: number; | ||
}; | ||
|
||
const useUpdatePostStatus = () => { | ||
const queryClient = useQueryClient(); | ||
const axios = useAxios(); | ||
|
||
return useMutation({ | ||
mutationFn: async ({ id, status }: UpdatePostStatusPayload) => { | ||
const response = await axios.patch(ENDPOINTS.BLOGS.UPDATE_STATUS(id), { status }); | ||
return response.data; | ||
}, | ||
onError: (error: AxiosError<ErrorResponse>) => { | ||
toast.error( | ||
error?.response?.data?.message || 'Настана грешка при ажурирање на статусот на статијата.' | ||
); | ||
}, | ||
onSuccess: () => { | ||
queryClient.invalidateQueries({ queryKey: QUERY_KEYS.BLOGS.ALL }); | ||
toast.success('Статусот на статијата е успешно ажуриран!'); | ||
}, | ||
}); | ||
}; | ||
|
||
export default useUpdatePostStatus; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,25 @@ | ||
'use client'; | ||
|
||
/* eslint-disable jsx-a11y/label-has-associated-control */ | ||
|
||
import React from 'react'; | ||
import { useSession } from 'next-auth/react'; | ||
import styles from './createArticlePage.module.scss'; | ||
import PublishArticleForm from '../../../../components/module-components/blog/PublishArticleForm'; | ||
import { UserRole } from '../../../../Types'; | ||
|
||
const PostArticle = () => { | ||
const { data: session } = useSession(); | ||
|
||
// console.log('Session data:', session); | ||
const userRole = session?.user.role as UserRole; | ||
|
||
return ( | ||
<div className={styles.container}> | ||
<h2>Објави статија</h2> | ||
|
||
<div className={styles.controlsContainer}> | ||
<PublishArticleForm /> | ||
<PublishArticleForm userRole={userRole} /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The main purpose of the create blog page should be to create/add new blog. On create we should call this endpoint: https://api.learnhub.mk/docs/#content-POSTcontent-blog-posts |
||
</div> | ||
</div> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,17 @@ import styles from './PublishArticleForm.module.scss'; | |
import TiptapEditor from '../../editor/TiptapEditor'; | ||
import TagManager from './TagManager'; | ||
import Button from '../../reusable-components/button/Button'; | ||
import { UserRole } from '../../../Types'; | ||
import UpdatePostStatus from '../../../apis/mutations/blogs/updatePostStatus'; | ||
|
||
const PublishArticleForm = () => { | ||
interface PublishArticleFormProps { | ||
userRole: UserRole; | ||
postId?: string; | ||
} | ||
|
||
const PublishArticleForm: React.FC<PublishArticleFormProps> = ({ userRole, postId }) => { | ||
const addNewPostMutation = useAddNewPost(); | ||
const updatePostStatusMutation = UpdatePostStatus(); | ||
const [selectedTags, setSelectedTags] = useState<TagObject[]>([]); | ||
|
||
const validationSchema = Yup.object({ | ||
|
@@ -28,8 +36,12 @@ const PublishArticleForm = () => { | |
.min(1, 'Мора да селектираш барем еден таг.'), | ||
}); | ||
|
||
const handleAddPost = (values: NewPost) => { | ||
addNewPostMutation.mutate(values); | ||
Comment on lines
-31
to
-32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is handleAddPost deleted? |
||
const handleSubmit = (values: NewPost) => { | ||
if (postId) { | ||
updatePostStatusMutation.mutate({ id: postId, status: values.status }); | ||
} else { | ||
addNewPostMutation.mutate(values); | ||
} | ||
}; | ||
|
||
return ( | ||
|
@@ -40,8 +52,9 @@ const PublishArticleForm = () => { | |
excerpt: '', | ||
content: '', | ||
tags: [], | ||
status: 'draft', | ||
}} | ||
onSubmit={handleAddPost} | ||
onSubmit={handleSubmit} | ||
> | ||
{({ values, setFieldValue, touched, errors }) => ( | ||
<Form className={styles.form}> | ||
|
@@ -104,6 +117,18 @@ const PublishArticleForm = () => { | |
{touched.tags && errors.tags && <div className={styles.error}>{errors.tags}</div>} | ||
</div> | ||
|
||
<div className={styles.fields}> | ||
<label htmlFor="status" className={styles.inputLabel}> | ||
Статус | ||
</label> | ||
<Field as="select" name="status" classname={styles.input}> | ||
<option value="draft">Draft</option> | ||
<option value="in_review">In Review</option> | ||
{userRole === UserRole.admin && <option value="published">Published</option>} | ||
</Field> | ||
{touched.status && errors.status && <div className={styles.error}>{errors.status}</div>} | ||
</div> | ||
|
||
{addNewPostMutation.isPending ? ( | ||
<Button | ||
disabled | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { Role } from '../Types'; | ||
import { UserRole } from '../Types'; | ||
|
||
export default function getAuthUrl(baseUrl: string, role: Role): string { | ||
export default function getAuthUrl(baseUrl: string, role: UserRole): string { | ||
let url = baseUrl; | ||
if (role === 'member') url = `${baseUrl}`; | ||
else if (role === 'content_manager' || role === 'content') url = `${baseUrl}/content`; | ||
else if (role === 'admin') url = `${baseUrl}/admin`; | ||
if (role === UserRole.member) url = `${baseUrl}`; | ||
else if (role === UserRole.content_manager) url = `${baseUrl}/content`; | ||
else if (role === UserRole.admin) url = `${baseUrl}/admin`; | ||
return url; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove console.log