From c3ea4dcd6d9df14d69bb678499bb7158666998f8 Mon Sep 17 00:00:00 2001 From: Rama Priyanto Date: Fri, 7 Feb 2025 16:37:39 +0700 Subject: [PATCH] feat:activity log --- app/auth/page.tsx | 14 +++++++------- app/setup-password/page.tsx | 2 +- components/form/login.tsx | 11 ++++++++++- components/form/qudo-login.tsx | 16 +--------------- components/icons.tsx | 18 +++++++++--------- components/layout/navbar/NavbarHumas.tsx | 12 ++++++------ components/layout/sidebar/sidebar.tsx | 2 +- components/main/detail/comment.tsx | 23 ++++++++++++++++++++++- components/main/detail/new-detail.tsx | 22 +++++++++++++++++++++- components/page/detail-news.tsx | 22 ++++++++++++++++++++-- service/activity-log.ts | 20 ++++++++++++++++++++ service/master-user.ts | 12 ++++++++---- 12 files changed, 126 insertions(+), 48 deletions(-) create mode 100644 service/activity-log.ts diff --git a/app/auth/page.tsx b/app/auth/page.tsx index d42d7ce..cf09ce9 100644 --- a/app/auth/page.tsx +++ b/app/auth/page.tsx @@ -1,4 +1,4 @@ -"use client" +"use client"; import Login from "@/components/form/login"; import QudoLogin from "@/components/form/qudo-login"; @@ -6,11 +6,10 @@ import Cookies from "js-cookie"; import React, { useEffect, useState } from "react"; export default function AuthPage() { + // const isAuthenticated = Cookies.get("is_authenticated") || "false"; - const isAuthenticated = Cookies.get("is_authenticated") || "false"; + // console.log("isAuthenticated : ", isAuthenticated); - console.log("isAuthenticated : ", isAuthenticated); - const [hasMounted, setHasMounted] = useState(false); useEffect(() => { @@ -21,8 +20,9 @@ export default function AuthPage() { if (!hasMounted) return null; return ( - isAuthenticated == "true" ? - : - + // isAuthenticated == "true" ? + + // : + // ); } diff --git a/app/setup-password/page.tsx b/app/setup-password/page.tsx index 4edd919..fcd226e 100644 --- a/app/setup-password/page.tsx +++ b/app/setup-password/page.tsx @@ -5,7 +5,7 @@ import { Button } from "@nextui-org/button"; import Link from "next/link"; import Cookies from "js-cookie"; import { close, error, loading } from "@/config/swal"; -import { getProfile, postSignIn, resetPassword } from "@/service/master-user"; +import { postSignIn, resetPassword } from "@/service/master-user"; import { useRouter, useSearchParams } from "next/navigation"; import Swal from "sweetalert2"; import withReactContent from "sweetalert2-react-content"; diff --git a/components/form/login.tsx b/components/form/login.tsx index 8e808d0..142ef14 100644 --- a/components/form/login.tsx +++ b/components/form/login.tsx @@ -10,6 +10,7 @@ import { checkUsernames, getProfile, postSignIn } from "@/service/master-user"; import { useRouter } from "next/navigation"; import Swal from "sweetalert2"; import withReactContent from "sweetalert2-react-content"; +import { saveActivity } from "@/service/activity-log"; export default function Login() { const router = useRouter(); @@ -53,6 +54,14 @@ export default function Login() { sameSite: "strict", }); const profile = await getProfile(access_token); + const resActivity = await saveActivity( + { + activityTypeId: 1, + url: "https://kontenhumas.com/auth", + userId: profile?.data?.data?.id, + }, + response?.data?.data?.id_token + ); console.log("PROFILE : ", profile?.data); Cookies.set("profile_picture", profile?.data?.data?.profilePictureUrl, { expires: 1, @@ -85,8 +94,8 @@ export default function Login() { expires: 1, }); - close(); router.push("/admin/dashboard"); + close(); Cookies.set("status", "login", { expires: 1, }); diff --git a/components/form/qudo-login.tsx b/components/form/qudo-login.tsx index f9a8f82..665e24f 100644 --- a/components/form/qudo-login.tsx +++ b/components/form/qudo-login.tsx @@ -7,7 +7,7 @@ import { error, loading, close } from "@/config/swal"; import { Button, Input } from "@nextui-org/react"; import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; -import { getProfile, postSignIn } from "@/service/master-user"; +import { postSignIn } from "@/service/master-user"; const images = ["bg1.jpg", "bg2.jpg"]; @@ -66,20 +66,6 @@ export default function QudoLogin() { secure: true, sameSite: "strict", }); - const profile = await getProfile(); - console.log("PROFILE : ", profile?.data); - Cookies.set("profile_picture", profile?.data?.data?.profilePictureUrl, { - expires: 1, - }); - Cookies.set("uie", profile?.data?.data?.id, { - expires: 1, - }); - Cookies.set("ufne", profile?.data?.data?.fullname, { - expires: 1, - }); - Cookies.set("username", profile?.data?.data?.username, { - expires: 1, - }); close(); router.push("/"); diff --git a/components/icons.tsx b/components/icons.tsx index aab1136..e12ac35 100644 --- a/components/icons.tsx +++ b/components/icons.tsx @@ -108,7 +108,7 @@ export const SendIcon: React.FC = ({ xmlns="http://www.w3.org/2000/svg" > - + = (props) => { xmlns="http://www.w3.org/2000/svg" > - + - + - + - + = ({ width={size || width} {...props} > - + - + - + - + state.locale); const setLanguage = storedLanguage((state) => state.setLocale); - useEffect(() => { - if (!isAuthenticated) { - onLogout(); - } - }, [token]); + // useEffect(() => { + // if (!isAuthenticated) { + // onLogout(); + // } + // }, [token]); const onLogout = () => { Object.keys(Cookies.get()).forEach((cookieName) => { - Cookies.remove(cookieName); + Cookies.remove(cookieName, { path: "/" }); }); router.push("/auth"); }; diff --git a/components/layout/sidebar/sidebar.tsx b/components/layout/sidebar/sidebar.tsx index 763f9ed..2c88504 100644 --- a/components/layout/sidebar/sidebar.tsx +++ b/components/layout/sidebar/sidebar.tsx @@ -262,7 +262,7 @@ const Sidebar: React.FC = ({ updateSidebarData }) => { const onLogout = () => { Object.keys(Cookies.get()).forEach((cookieName) => { - Cookies.remove(cookieName); + Cookies.remove(cookieName, { path: "/" }); }); router.push("/auth"); }; diff --git a/components/main/detail/comment.tsx b/components/main/detail/comment.tsx index 0bf3e30..581a1ff 100644 --- a/components/main/detail/comment.tsx +++ b/components/main/detail/comment.tsx @@ -20,8 +20,11 @@ import OTPInput from "react-otp-input"; import Swal from "sweetalert2"; import withReactContent from "sweetalert2-react-content"; import { SendIcon, TimesIcon } from "@/components/icons"; +import { saveActivity } from "@/service/activity-log"; +import { usePathname } from "next/navigation"; const userId = Cookies.get("uie"); +const token = Cookies.get("access_token"); const commentSchema = z.object({ name: z.string().min(1, { @@ -43,7 +46,7 @@ const commentSchema = z.object({ export default function Comment(props: { id: string | null }) { const { id } = props; const MySwal = withReactContent(Swal); - + const pathname = usePathname(); const [needOtp, setNeedOtp] = useState(false); const [otpValue, setOtpValue] = useState(""); const [commentList, setCommentList] = useState([]); @@ -98,6 +101,13 @@ export default function Comment(props: { id: string | null }) { error(res?.message); return false; } + const req: any = { + activityTypeId: 5, + url: "https://kontenhumas.com/" + pathname, + articleId: Number(id), + }; + + const resActivity = await saveActivity(req); reset(); fetchData(); setNeedOtp(false); @@ -151,8 +161,19 @@ export default function Comment(props: { id: string | null }) { error(res?.message); return false; } + const req: any = { + activityTypeId: 5, + url: "https://kontenhumas.com/" + pathname, + articleId: Number(id), + userId: Number(userId), + }; + + const resActivity = await saveActivity(req, token); + fetchData(); }; + + // const sendActivity = async () => {}; const editComment = async (idComment: number, parentId: number) => { const data = { articleId: Number(id), diff --git a/components/main/detail/new-detail.tsx b/components/main/detail/new-detail.tsx index 9054b61..8f57a04 100644 --- a/components/main/detail/new-detail.tsx +++ b/components/main/detail/new-detail.tsx @@ -5,20 +5,27 @@ import SidebarDetail from "../../page/sidebar-detail"; import RelatedNews from "../../page/related-news"; import Comment from "./comment"; import { getArticleById, getListArticle } from "@/service/article"; -import { useParams } from "next/navigation"; +import { useParams, usePathname } from "next/navigation"; import Link from "next/link"; import { ChevronRightIcon, UserIcon } from "@/components/icons"; import { close, loading } from "@/config/swal"; +import { saveActivity } from "@/service/activity-log"; +import Cookies from "js-cookie"; + +const token = Cookies.get("access_token"); +const uid = Cookies.get("uie"); export default function NewsDetailPage() { const params = useParams(); const id: any = params?.id; + const pathname = usePathname(); const [detailArticle, setDetailArticle] = useState(); const [articles, setArticles] = useState([]); useEffect(() => { initFetch(); getArticles(); + sendActivity(); }, []); async function getArticles() { @@ -34,6 +41,19 @@ export default function NewsDetailPage() { setDetailArticle(data); close(); }; + + const sendActivity = async () => { + let req: any = { + activityTypeId: 2, + url: "https://kontenhumas.com/" + pathname, + articleId: Number(id?.split("-")[0]), + }; + if (uid) { + req.userId = Number(uid); + } + + const resActivity = await saveActivity(req, token); + }; return (
diff --git a/components/page/detail-news.tsx b/components/page/detail-news.tsx index a4ba980..ee83b17 100644 --- a/components/page/detail-news.tsx +++ b/components/page/detail-news.tsx @@ -20,10 +20,15 @@ import { UserIcon, } from "../icons"; import { Button } from "@nextui-org/button"; -import { usePathname } from "next/navigation"; +import { useParams, usePathname } from "next/navigation"; import Link from "next/link"; import { useEffect, useState } from "react"; import { image } from "@nextui-org/theme"; +import Cookies from "js-cookie"; +import { saveActivity } from "@/service/activity-log"; + +const token = Cookies.get("access_token"); +const uid = Cookies.get("uie"); export default function DetailNews(props: { data: any; listArticle: any }) { const { data, listArticle } = props; @@ -31,12 +36,25 @@ export default function DetailNews(props: { data: any; listArticle: any }) { const [nextArticle, setNextArticle] = useState(""); const [imageNow, setImageNow] = useState(0); const pathname = usePathname(); + const params = useParams(); + const id: any = params?.id; const shareText = "Humas Polri"; - const handleShare = (platform: string) => { + const handleShare = async (platform: string) => { let shareLink = ""; const urls = "https://kontenhumas.com/" + pathname; + let req: any = { + activityTypeId: 3, + url: "https://kontenhumas.com/" + pathname, + articleId: Number(id?.split("-")[0]), + }; + if (uid) { + req.userId = Number(uid); + } + + const resActivity = await saveActivity(req, token); + switch (platform) { case "facebook": shareLink = `https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent( diff --git a/service/activity-log.ts b/service/activity-log.ts new file mode 100644 index 0000000..287d862 --- /dev/null +++ b/service/activity-log.ts @@ -0,0 +1,20 @@ +import { PaginationRequest } from "@/types/globals"; +import { + httpDeleteInterceptor, + httpGet, + httpPost, + httpPut, +} from "./http-config/axios-base-service"; + +export async function saveActivity(data: any, token?: string) { + const headers = token + ? { + "content-type": "application/json", + Authorization: `Bearer ${token}`, + } + : { + "content-type": "application/json", + }; + const pathUrl = `/activity-logs`; + return await httpPost(pathUrl, headers, data); +} diff --git a/service/master-user.ts b/service/master-user.ts index aa38710..9ef15ca 100644 --- a/service/master-user.ts +++ b/service/master-user.ts @@ -88,10 +88,14 @@ export async function otpValidation(email: string, otpCode: string) { } export async function postArticleComment(data: any) { - const headers = { - "content-type": "application/json", - Authorization: `Bearer ${token}`, - }; + const headers = token + ? { + "content-type": "application/json", + Authorization: `Bearer ${token}`, + } + : { + "content-type": "application/json", + }; return await httpPost(`/article-comments`, headers, data); }