Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add search payout method functionality for admin #1356

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/actions/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' };
}
};
85 changes: 0 additions & 85 deletions src/app/admin/user/LogoutUser.tsx

This file was deleted.

246 changes: 246 additions & 0 deletions src/app/admin/user/Usermanagement.tsx
Original file line number Diff line number Diff line change
@@ -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<HTMLFormElement>(null);

const [loading, setloading] = useState<boolean>(false);
const [searchResults, setSearchResults] = useState<SearchResult>();

const handlLogout = async (e: React.FormEvent<HTMLFormElement>) => {
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<HTMLFormElement>,
) => {
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 (
<div className="h-full w-full">
<Accordion
defaultValue="search-payout-methods"
className="rounded-2xl border-2 p-4"
type="single"
collapsible
>
<AccordionItem className="p-6" value="search-payout-methods">
<AccordionTrigger className="p-6 text-lg font-bold lg:text-2xl">
<div className="flex flex-col gap-4 text-nowrap">
<Search size={40} /> Search Payout Methods
</div>
</AccordionTrigger>
<AccordionContent>
<form
className="h-full w-full"
onSubmit={handlePayoutMethodSearch}
ref={formRef}
>
<div className="grid w-full grid-cols-1 rounded-lg dark:border-gray-800 md:grid-cols-7">
<div className="col-span-1 grid gap-2 p-6 lg:col-span-3">
<div className="text-sm font-medium leading-none text-gray-500 dark:text-gray-400">
Enter user&apos;s github / email to get payout methods
</div>
</div>

<aside className="col-span-1 flex w-full flex-col gap-6 lg:col-span-4">
<div className="">
<Label className="sr-only">EMAIL / GITHUB</Label>
<Input
className="h-14 w-full px-2"
id="search-term"
name="search-term"
placeholder="github username / [email protected]"
style={{
minWidth: '0',
}}
/>
</div>
<div className="">
<Label className="sr-only">Admin password</Label>
<Input
className="h-14 w-full px-2"
id="adminPassword"
name="adminPassword"
placeholder="Admin password"
style={{
minWidth: '0',
}}
/>
</div>
<Button className="w-full lg:w-[20%]">
{loading ? 'Searching..' : 'Search'}
</Button>
</aside>
</div>
{searchResults && (
<div className="flex w-full flex-col items-start justify-between pt-10 md:flex-row md:px-6">
<div className="w-full">
<p className="text-lg font-semibold">UPI IDs</p>
<ul>
{searchResults.upiIds.length === 0 ? (
<li className="text-[#9BA3AF]">
No associated upi ids
</li>
) : (
searchResults.upiIds.map((upi: any, id: number) => (
<li
className="my-2 flex h-fit items-center justify-between rounded-lg border p-3 md:w-5/6"
key={id}
>
{upi.value}
<Button
size={'iconSM'}
onClick={(e) =>
handleCopyToClipboard(e, upi.value)
}
>
<Copy className="h-3 w-3" />
</Button>
</li>
))
)}
</ul>
</div>
<div className="w-full">
<p className="text-lg font-semibold">Solana Addresses</p>
<ul>
{searchResults.solAddresses.length === 0 ? (
<li className="text-[#9BA3AF]">
No associated solana addresses
</li>
) : (
searchResults.solAddresses.map(
(sol: any, id: number) => (
<li
className="my-2 flex h-fit items-center justify-between text-ellipsis rounded-lg border p-3 md:w-5/6"
key={id}
>
<span className="block md:hidden">
{sol.value.slice(0, 20)}...
</span>
<span className="hidden md:block">
{sol.value}
</span>
<Button
size={'iconSM'}
onClick={(e) =>
handleCopyToClipboard(e, sol.value)
}
>
<Copy className="h-3 w-3" />
</Button>
</li>
),
)
)}
</ul>
</div>
</div>
)}
</form>
</AccordionContent>
</AccordionItem>
<AccordionItem className="border-none p-6" value="logout-user">
<AccordionTrigger className="p-6 text-lg font-bold lg:text-2xl">
<div className="flex flex-col gap-4">
<LogOut size={40} /> Logout User
</div>
</AccordionTrigger>
<AccordionContent>
<form
className="h-full w-full"
onSubmit={handlLogout}
ref={formRef}
>
<div className="grid w-full grid-cols-1 rounded-lg dark:border-gray-800 md:grid-cols-7">
<div className="col-span-1 grid gap-2 p-6 lg:col-span-3">
<div className="text-sm font-medium leading-none text-gray-500 dark:text-gray-400">
Enter the information below to logout the user
</div>
</div>

<aside className="col-span-1 flex w-full flex-col gap-6 lg:col-span-4">
<div className="">
<Label className="sr-only">EMAIL</Label>
<Input
className="h-14 w-full px-2"
id="email"
name="email"
placeholder="[email protected]"
style={{
minWidth: '0',
}}
/>
</div>
<div className="">
<Label className="sr-only">Admin password</Label>
<Input
className="h-14 w-full px-2"
id="adminPassword"
name="adminPassword"
placeholder="Admin password"
style={{
minWidth: '0',
}}
/>
</div>
<Button className="w-full lg:w-[20%]">Logout</Button>
</aside>
</div>
</form>
</AccordionContent>
</AccordionItem>
</Accordion>
</div>
);
};

export default UserManagement;
12 changes: 7 additions & 5 deletions src/app/admin/user/page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import React from 'react';
import LogoutUserComp from './LogoutUser';
import { UserIcon } from 'lucide-react';
import UserManagement from './Usermanagement';

const UserAdminPage = () => {
return (
<div className="flex flex-col gap-4 mx-auto px-4 max-w-7xl h-[100dvh]">
<section className='flex gap-2 border-2 p-4 bg-primary/5 rounded-lg my-4 items-center'>
<div className="mx-auto flex h-[100dvh] max-w-7xl flex-col gap-4 px-4 pb-5">
<section className="my-4 flex items-center gap-2 rounded-lg border-2 bg-primary/5 p-4">
<UserIcon size={18} />
<h2 className='text-md font-bold'>User Mangement</h2>
<h2 className="text-md font-bold">User Mangement</h2>
</section>
<LogoutUserComp />
<div className='flex h-full w-full flex-col'>
<UserManagement />
</div>
</div>
);
};
Expand Down
Loading