diff --git a/src/actions/user/index.ts b/src/actions/user/index.ts index d85f351e7..09a6048c4 100644 --- a/src/actions/user/index.ts +++ b/src/actions/user/index.ts @@ -25,3 +25,39 @@ export const logoutUser = async (email: string, adminPassword: string) => { return { message: 'User logged out' }; }; + +export const searchPayoutMethods = async ( + searchTerm: string, + adminPassword: string, +) => { + if (adminPassword !== process.env.ADMIN_SECRET) { + return { error: 'Unauthorized' }; + } + let user; + const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; + if (emailRegex.test(searchTerm)) { + user = await db.user.findUnique({ + where: { email: searchTerm }, + include: { + upiIds: true, + solanaAddresses: true, + }, + }); + } else { + user = await db.user.findFirst({ + where: { githubUser: { username: searchTerm } }, + select: { + upiIds: true, + solanaAddresses: true, + }, + }); + } + + if (user) { + return { + data: { upiIds: user.upiIds, solAddresses: user.solanaAddresses }, + }; + } else { + return { error: 'User not found' }; + } +}; diff --git a/src/app/admin/user/LogoutUser.tsx b/src/app/admin/user/LogoutUser.tsx deleted file mode 100644 index a9f8f66eb..000000000 --- a/src/app/admin/user/LogoutUser.tsx +++ /dev/null @@ -1,85 +0,0 @@ -'use client'; -import { logoutUser } from '@/actions/user'; -import { Button } from '@/components/ui/button'; -import { Input } from '@/components/ui/input'; -import { Label } from '@radix-ui/react-dropdown-menu'; -import React from 'react'; -import { toast } from 'sonner'; -import { - Accordion, - AccordionContent, - AccordionItem, - AccordionTrigger, -} from "@/components/ui/accordion"; -import { LogOut } from 'lucide-react'; - -const LogoutUserComp = () => { - const formRef = React.useRef(null); - - const handlLogout = async (e: React.FormEvent) => { - e.preventDefault(); - const formData = new FormData(e.target as HTMLFormElement); - - const email = formData.get('email') as string; - const adminPassword = formData.get('adminPassword') as string; - const res = await logoutUser(email, adminPassword); - toast.info(res.message || res.error); - }; - return ( - -
- - - -
- Logout User -
-
- -
-
-
-
- Enter the information below to logout the user -
-
- - - -
-
-
-
-
-
- - ); -}; - -export default LogoutUserComp; diff --git a/src/app/admin/user/Usermanagement.tsx b/src/app/admin/user/Usermanagement.tsx new file mode 100644 index 000000000..7453e8cf7 --- /dev/null +++ b/src/app/admin/user/Usermanagement.tsx @@ -0,0 +1,246 @@ +'use client'; +import { logoutUser, searchPayoutMethods } from '@/actions/user'; +import { Button } from '@/components/ui/button'; +import { Input } from '@/components/ui/input'; +import { Label } from '@radix-ui/react-dropdown-menu'; +import React, { useState } from 'react'; +import { toast } from 'sonner'; +import { + Accordion, + AccordionContent, + AccordionItem, + AccordionTrigger, +} from '@/components/ui/accordion'; +import { Copy, LogOut, Search } from 'lucide-react'; + +const UserManagement = () => { + interface SearchResult { + upiIds: { value: string }[]; + solAddresses: { value: string }[]; + } + + const formRef = React.useRef(null); + + const [loading, setloading] = useState(false); + const [searchResults, setSearchResults] = useState(); + + const handlLogout = async (e: React.FormEvent) => { + e.preventDefault(); + const formData = new FormData(e.target as HTMLFormElement); + const email = formData.get('email') as string; + const adminPassword = formData.get('adminPassword') as string; + const res = await logoutUser(email, adminPassword); + toast.info(res.message || res.error); + }; + + const handlePayoutMethodSearch = async ( + e: React.FormEvent, + ) => { + e.preventDefault(); + const formData = new FormData(e.target as HTMLFormElement); + const searchTerm = formData.get('search-term') as string; + const adminPassword = formData.get('adminPassword') as string; + + setloading(true); + const res = await searchPayoutMethods(searchTerm, adminPassword); + setloading(false); + + setSearchResults(res.data); + if (res.error) { + toast.error(res.error); + } + }; + + const handleCopyToClipboard = (e: any, value: string) => { + e.preventDefault(); + navigator.clipboard + .writeText(value) + .then(() => { + toast.success('Copied to clipboard'); + }) + .catch((e) => { + toast.error('Failed to copy'); + }); + }; + + return ( +
+ + + +
+ Search Payout Methods +
+
+ +
+
+
+
+ Enter user's github / email to get payout methods +
+
+ + +
+ {searchResults && ( +
+
+

UPI IDs

+
    + {searchResults.upiIds.length === 0 ? ( +
  • + No associated upi ids +
  • + ) : ( + searchResults.upiIds.map((upi: any, id: number) => ( +
  • + {upi.value} + +
  • + )) + )} +
+
+
+

Solana Addresses

+
    + {searchResults.solAddresses.length === 0 ? ( +
  • + No associated solana addresses +
  • + ) : ( + searchResults.solAddresses.map( + (sol: any, id: number) => ( +
  • + + {sol.value.slice(0, 20)}... + + + {sol.value} + + +
  • + ), + ) + )} +
+
+
+ )} +
+
+
+ + +
+ Logout User +
+
+ +
+
+
+
+ Enter the information below to logout the user +
+
+ + +
+
+
+
+
+
+ ); +}; + +export default UserManagement; diff --git a/src/app/admin/user/page.tsx b/src/app/admin/user/page.tsx index 370ea914b..c767c585a 100644 --- a/src/app/admin/user/page.tsx +++ b/src/app/admin/user/page.tsx @@ -1,15 +1,17 @@ import React from 'react'; -import LogoutUserComp from './LogoutUser'; import { UserIcon } from 'lucide-react'; +import UserManagement from './Usermanagement'; const UserAdminPage = () => { return ( -
-
+
+
-

User Mangement

+

User Mangement

- +
+ +
); };