Skip to content

Commit

Permalink
Merge branch 'dev' into test-radar
Browse files Browse the repository at this point in the history
  • Loading branch information
Flepsz authored May 15, 2024
2 parents ded6d13 + 109d005 commit 6fbb24f
Show file tree
Hide file tree
Showing 36 changed files with 1,360 additions and 156 deletions.
99 changes: 98 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
"@azure/msal-react": "^2.0.12",
"@hookform/resolvers": "^3.3.4",
"@meghoshpritam/react-image-file-resizer": "^1.0.3",
"@radix-ui/react-alert-dialog": "^1.0.5",
"@radix-ui/react-avatar": "^1.0.4",
"@radix-ui/react-context-menu": "^2.1.5",
"@radix-ui/react-dialog": "^1.0.5",
"@radix-ui/react-dropdown-menu": "^2.0.6",
"@radix-ui/react-icons": "^1.3.0",
"@radix-ui/react-label": "^2.0.2",
"@radix-ui/react-menubar": "^1.0.4",
"@radix-ui/react-popover": "^1.0.7",
"@radix-ui/react-slot": "^1.0.2",
"@tanstack/react-query": "^5.22.2",
"@tanstack/react-query-devtools": "^5.28.14",
Expand Down
Binary file added public/imageDefault.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed public/newsNotFound.png
Binary file not shown.
19 changes: 14 additions & 5 deletions src/api/hooks/news/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ async function getNewsFilterScroll(ctx: QueryFunctionContext) {
const pageParam = ctx.pageParam;
const filterParam = usefilter({ filters: { tags, title } });

const url = tags || title ? `${filterParam}` : "";

const { data } = await apiNews.get<ContentNews>(`news/preview?size=9${url}`, {
const url = tags || title ? `${filterParam}` : undefined;
const { data } = await api.get<ContentNews>(`news/preview?size=9${url}`, {
params: {
page: pageParam,
},
});

console.log(data)
return data;
}

Expand All @@ -52,7 +51,7 @@ export function useFetchGetNewsScroll(tags?: string, title?: string) {
}

// GET News By ID
async function getIdNews(ctx: QueryFunctionContext) {
export async function getIdNews(ctx: QueryFunctionContext) {
const [, id] = ctx.queryKey;
const { data } = await apiNews.get<News>(`news/${id}`);
return data;
Expand All @@ -65,6 +64,16 @@ export function useFetchGetNewsId(id: string) {
});
}


export function useFetchGetNewsIdRefetch(id: string) {
return useQuery<News, Error>({
queryKey: ["newsRead", id],
queryFn: getIdNews,
enabled: false,
refetchOnWindowFocus: false
});
}

// GET Other News
async function getNewsOtherNews() {
const { data } = await apiNews.get<ContentNews>(
Expand Down
136 changes: 129 additions & 7 deletions src/api/hooks/user/queries.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,139 @@
import { useQuery } from "@tanstack/react-query";
import { ContentNews } from "@/api/types/news/type";
import { apiNews } from "@/services/api";
import {
QueryFunctionContext,
useMutation,
useQuery,
useQueryClient,
} from "@tanstack/react-query";
import toast from "react-hot-toast";
import { ContentComment, ContentNews, News } from "@/api/types/news/type";
import api from "@/services/api";
import { getIdNews } from "../news/queries";

// News
// GET user news
async function getUserNews() {
const { data } = await apiNews.get<ContentNews>(`news/author`);
export async function getUserNews(ctx: QueryFunctionContext) {
const [, status] = ctx.queryKey;
const filter = status === undefined ? "published" : status;
const { data } = await api.get<ContentNews>(`news/author?sortBy=${filter}`);
return data;
}

export function useFetchGetUserNews() {
export function useFetchGetUserNews(status?: string) {
return useQuery<ContentNews, Error>({
queryKey: ["news"],
queryKey: ["userNews", status],
queryFn: getUserNews,
});
}

// PATCH user news
async function patchNews({
newsObject,
id,
}: {
newsObject: FormData;
id: string;
}) {
const promise = api.patch<News>(`news/${id}`, newsObject);

toast.promise(promise, {
loading: "Updated news",
success: "Updated news with success",
error: (error) => {
console.log(error);
return error.response.data
? `${error.message}:\n${error.response.data.message}`
: `${error.message}`;
},
});

return await promise;
}

export function useMutationPatchNews() {
return useMutation({
mutationFn: patchNews,
});
}

// ARCHIVE user news

async function patchArchive(id: string) {
const archive = api.patch<News>(`news/${id}/archive`);

toast.promise(archive, {
loading: "Delete news",
success: "Delete news with success",
error: (error) => {
console.log(error);
return error.response.data
? `${error.message}:\n${error.response.data.message}`
: `${error.message}`;
},
});
return await archive;
}

async function patchPublish(id: string) {
const publish = api.patch<News>(`news/${id}/publish`);

toast.promise(publish, {
loading: "Publish news",
success: "Publish news with success",
error: (error) => {
console.log(error);
return error.response.data
? `${error.message}:\n${error.response.data.message}`
: `${error.message}`;
},
});
return await publish;
}

export function useMutationPatchArchive() {
return useMutation({
mutationFn: patchArchive,
});
}

export function useMutationPatchPublish() {
return useMutation({
mutationFn: patchPublish,
});
}

// News
// GET user comments

export async function getUserComments() {
const { data } = await api.get<ContentComment>(
`comments/author`
);
return data;
}

export function useFetchGetUserComments(){
return useQuery<ContentComment, Error>({
queryKey: ["userComments"],
queryFn: getUserComments
})
}

async function patchDelete(id: number) {
const deleteComment = api.delete(`comments/${id}`);

toast.promise(deleteComment, {
loading: "Delete Comment",
success: "Delete Comment with success",
error: (error) => {
console.log(id);
return "Delete Comment Error";
},
});
return await deleteComment;
}

export function useMutationPatchDelete() {
return useMutation({
mutationFn: patchDelete,
});
}
36 changes: 36 additions & 0 deletions src/api/types/all/type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { title } from "process";
import { AppRouterInstance } from "next/dist/shared/lib/app-router-context.shared-runtime";
import { ReadonlyURLSearchParams } from "next/navigation";

Expand All @@ -21,3 +22,38 @@ export interface FilterPath {
searchParams: ReadonlyURLSearchParams;
router: AppRouterInstance;
}

export interface AlertType {
variantButton:
| "delete"
| "ghost"
| "bdpurple"
| "bdlight"
| "default"
| "outline";
nameButton: string | any;
type?: "submit" | "reset" | "button" | undefined;
title: string;
description: string;
nameButtonAction: string;
Action?: () => void;
idForm?: string;
}

interface DefaultValuesType {
title?: string;
body?: string;
image?: File | undefined;
tags?: string[];
}

export interface FormType {
title: string | any;
open: boolean;
openUpdate?: boolean;
OnSubmit: (values: any) => void;
idForm?: string;
idNews?: string;
defaultValues?: DefaultValuesType;
alertSubmit: JSX.Element
}
Loading

0 comments on commit 6fbb24f

Please sign in to comment.