-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
1,360 additions
and
156 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.