Skip to content

Conversation

Hammadhammad2
Copy link
Owner

No description provided.

"use server";
import instance from "../utils/axios";

export async function fetchCommentsByPostId(postId) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just return the promise from this function and use the try catch block in the component making the API call

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

app/api/posts.js Outdated

export async function createPost(data, userId) {
try {
await instance.post("/posts", { ...data, userId });

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pass the payload from the component which is calling this function

app/api/posts.js Outdated
} catch (error) {
return { message: "Failed to edit post" };
}
revalidatePath(`/users/posts/${userId}`);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this logic should be in the case of API success

app/api/posts.js Outdated

export async function fetchPostsPages(userId, query) {
try {
const response = await instance.get(`/posts?userId=${userId}&q=${query}`);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to make the number of pages in the same API call when fetching posts for users

};

const allPages = generatePagination(currentPage, totalPages);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compute the position here

<>
<div
className="cursor-pointer"
onClick={() => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

{({ handleSubmit, handleChange, values, errors, touched }) => (
<form onSubmit={handleSubmit}>
<div className=" rounded-md bg-gray-50 p-4 md:p-6 ">
<TextField

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map

return (
<main>
<Breadcrumbs
breadcrumbs={[

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

);
}

export function OnCloseModal({ userId }) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CloseModalButton

<h2 className="text-center">Something went wrong!</h2>
<button
className="mt-4 rounded-md bg-blue-500 px-4 py-2 text-sm text-white transition-colors hover:bg-blue-400"
onClick={() => reset()}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onClick={reset}

"use server";
import instance from "../utils/axios";

export async function fetchCommentsByPostId(postId) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

app/api/posts.js Outdated
const offset = (currentPage - 1) * ITEMS_PER_PAGE;
const [response, allData] = await Promise.all([
instance.get(
`/posts?userId=${userId}&_start=${offset}&_limit=${ITEMS_PER_PAGE}&q=${query}`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use axios query params

app/api/posts.js Outdated
query
) {
const offset = (currentPage - 1) * ITEMS_PER_PAGE;
const [response, allData] = await Promise.all([

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

paginatedPosts, allPosts

app/api/posts.js Outdated

export async function createPost(data) {
await instance.post("/posts", data);
revalidatePath(`/users/posts/${data?.userId}`);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the redirects from here

app/api/users.js Outdated
@@ -0,0 +1,31 @@
"use server";
Copy link

@usmannafzal usmannafzal Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this directive

"flex h-[48px] grow items-center justify-center gap-2 rounded-md bg-gray-50 p-3 text-sm font-medium hover:bg-sky-100 hover:text-blue-600 md:flex-none md:justify-start md:p-2 md:px-3",
{
"bg-sky-100 text-blue-600":
`/${pathname.split("/")?.[1]}` === link.href,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compute this expression above

const term = e.target.value;
const params = new URLSearchParams(searchParams);
params.set(PAGE, "1");
if (term) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (term) params.set(QUERY, term);
else params.delete(QUERY);

<div className="flex h-full flex-col px-3 py-4 md:px-2">
<Link
className="mb-2 flex h-20 items-end justify-start rounded-md bg-blue-600 p-4 md:h-40"
href="/"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a constant here, HOME maybe

return (
<main>
<Breadcrumbs
breadcrumbs={[

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put this array in DataHelpers file


export default function UserForm({ user }) {
return (
<Formik

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change it to useFormik hook

<Formik
initialValues={user || generateInitialValues(createUserFormData)}
validationSchema={UserValidationSchema.createUser()}
onSubmit={async (values, { resetForm }) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never create inline functions, always create a separate function

<main>
<Breadcrumbs
breadcrumbs={[
{ label: USERS, href: urls.USERS },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

const [loading, setLoading] = useState(true);

useEffect(() => {
const fetchCommentsData = async () => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this function out of the useEffect hook

export const TRY_AGAIN = "Try again";
export const CANCEL = "Cancel";
//urls
export const urls = {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put this constants inside RouteContants file

},
];

export const createPostFormData = [

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

}
};

const formik = useFormik({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants