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

campaign create and list #36

Merged
merged 1 commit into from
Jun 26, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default function Page() {
}, []);
return (
<main className='mx-auto'>
<CampaignForm channels={channels} />
<CampaignForm />
</main>
);
}
85 changes: 85 additions & 0 deletions app/(root)/campaigns/list/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"use client"
import Pagination from '@/components/ui/pagination';
import Search from '@mitech/shared-components/ui/search';
import CampaignListTable from '@/components/ui/campaign-table';
import { useEffect, useState } from 'react';
import { GetFilteredCampaignPages } from '@/lib/data';
import { fetchCampaignList } from '@/lib/api';
import { Campaign } from '@/lib/definitions';
import 'react-toastify/dist/ReactToastify.css';
import Cookies from 'js-cookie';
import { toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import { PlusIcon, PencilIcon, StopIcon, EyeIcon, TrashIcon, ArrowUpOnSquareStackIcon } from '@heroicons/react/24/outline';
import Link from 'next/link';

export default function Page({
searchParams,
}: {
searchParams?: {
query?: string;
page?: string;
};
}) {
const query = searchParams?.query || '';
const currentPage = Number(searchParams?.page) || 1;
const [campaigns, setCampaigns] = useState<Campaign[]>([]);
useEffect(() => {
fetchCampaignList().then(data => setCampaigns(data.result.filter((ad:any) => ad.status !== 'deleted')));
}, []);
useEffect(() => {
const message = Cookies.get('notification_delete_campaign');
function showToast() {
toast.success(message);
}
if (message) {
setTimeout(showToast, 1000);
Cookies.remove('notification_delete_campaign');
}
}, []);
useEffect(() => {
const message = Cookies.get('notification_create_campaign');
function showToast() {
toast.success(message);
}
if (message) {
setTimeout(showToast, 1000);
Cookies.remove('notification_create_campaign');
}
}, []);
useEffect(() => {
const message = Cookies.get('notification_update_campaign');
function showToast() {
toast.success(message);
}
if (message) {
setTimeout(showToast, 1000);
Cookies.remove('notification_update_campaign');
}
}, []);
const totalPages = GetFilteredCampaignPages(campaigns,query);
return (
<div className="w-full">
<header className="flex items-center justify-between px-6 py-4 bg-gray-300 font-bold rounded-t-medium">
<div className="flex items-center space-x-4">
<h2 className="text-lg font-medium">Campaign List</h2>
</div>
</header>
<div className='w-full max-w-[80%] justify-center items-center m-auto'>
<div className="mt-4 flex items-center justify-center gap-2 md:mt-8">
<Search placeholder="Search campaign" />
<Link href="/campaigns/create"
className="flex h-10 items-center rounded-lg bg-blue-600 px-4 text-sm font-medium text-white transition-colors hover:bg-blue-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-600"
>
<span className="hidden md:block">Create New Campaign</span>{' '}
<PlusIcon className="h-5 md:ml-4" />
</Link>
</div>
<CampaignListTable query={query} currentPage={currentPage} />
<div className="mt-5 flex w-full justify-center">
<Pagination totalPages={totalPages} />
</div>
</div>
</div>
);
}
119 changes: 119 additions & 0 deletions components/ui/campaign-table.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
'use client';
import { fetchCampaignList } from '@/lib/api';
import { GetFilteredCampaign } from '@/lib/data';
import { Campaign } from '@/lib/definitions';
import { useEffect, useState } from 'react';

export default function CampaignListTable({
query,
currentPage,
}: {
query: string;
currentPage: number;
}) {
const [sortColumn, setSortColumn] = useState("name");
const [sortOrder, setSortOrder] = useState("asc");
const [campaignAll, setCampaign] = useState<Campaign[]>([]);
useEffect(() => {
fetchCampaignList().then(data => setCampaign(data.result.filter((campaign:any) => campaign.status !== 'deleted')));
}, []);
const handleSort = (column:string) => {
if (sortColumn === column) {
setSortOrder(sortOrder === "asc" ? "desc" : "asc");
} else {
setSortColumn(column);
setSortOrder("asc");
}
};
const sortSvg = (column:string) =>
sortColumn === column ? (
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
width="24"
height="24"
>
{sortOrder === "desc" ? (
<path d="M7 10l5 5 5-5z" />
) : (
<path d="M7 14l5-5 5 5z" />
)}

<path d="M0 0h24v24H0z" fill="none" />
</svg>
) : null;
const campaignList = GetFilteredCampaign(campaignAll, query, currentPage, sortColumn,sortOrder);
return (
<div className="mt-6 flow-root">
<div className="overflow-x-auto">
<div className="inline-block min-w-full align-middle">
<div className="overflow-hidden rounded-md p-2 md:pt-0">
<table className="hidden min-w-full rounded-md text-gray-900 md:table">
<thead className="rounded-md bg-gray-50 text-left text-sm font-normal">
<tr>
<th scope="col" onClick={() => handleSort("name")} className="px-4 py-5 font-medium sm:pl-6">
<p className='inline-flex cursor-pointer font-bold'>Name{sortSvg("name")}</p>
</th>
<th scope="col" onClick={() => handleSort("budget")} className="px-3 py-5 font-medium">
<p className='inline-flex cursor-pointer font-bold'>Budget{sortSvg("budget")}</p>
</th>
<th scope="col" onClick={() => handleSort("startDateTime")} className="px-3 py-5 font-medium">
<p className='inline-flex cursor-pointer font-bold'>Start Date{sortSvg("startDateTime")}</p>
</th>
<th scope="col" onClick={() => handleSort("endDateTime")} className="px-4 py-5 font-medium">
<p className='inline-flex cursor-pointer font-bold'>End Date{sortSvg("endDateTime")}</p>
</th>
<th scope="col" className="relative py-3 pl-2 pr-2 w-0.5">
<span className="sr-only">Detail</span>
</th>
<th scope="col" className="relative py-3 pl-2 pr-2 w-0.5">
<span className="sr-only">Edit</span>
</th>
<th scope="col" className="relative py-3 pl-2 pr-2 w-0.5">
<span className="sr-only">Delete</span>
</th>
</tr>
</thead>

<tbody className="divide-y divide-gray-200 text-gray-900">
{campaignList.map((campaign) => (
<tr key={campaign.id} className="group">
<td className="whitespace-nowrap bg-white py-5 pl-4 pr-3 text-sm text-black group-first-of-type:rounded-md group-last-of-type:rounded-md sm:pl-6">
<div className="flex items-center gap-3">
<p>{campaign.name}</p>
</div>
</td>
<td className="whitespace-nowrap bg-white px-4 py-5 text-sm">
{campaign.budget}
</td>
<td className="whitespace-nowrap bg-white px-4 py-5 text-sm">
{campaign.startDateTime?.split('T')[0]}
</td>
<td className="whitespace-nowrap bg-white px-4 py-5 text-sm group-first-of-type:rounded-md group-last-of-type:rounded-md">
{campaign.endDateTime?.split('T')[0]}
</td>
<td className="whitespace-nowrap py-3 pl-2 pr-2 w-0.5">
<div className="flex gap-3">
{campaign.id && 'View' }
</div>
</td>
<td className="whitespace-nowrap py-3 pl-2 pr-2 w-0.5">
<div className="flex gap-3">
{campaign.id && 'Update'}
</div>
</td>
<td className="whitespace-nowrap py-3 pl-2 pr-2 w-0.5">
<div className="flex gap-3">
{campaign.id && 'Delete'}
</div>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</div>
</div>
);
}
Loading
Loading