website refactor
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import type { Result } from '@/lib/contracts/Result';
|
||||
import type { SponsorshipRequestsViewData } from '@/lib/view-data/SponsorshipRequestsViewData';
|
||||
import { SponsorshipRequestsTemplate } from '@/templates/SponsorshipRequestsTemplate';
|
||||
import { InlineNotice } from '@/components/shared/ux/InlineNotice';
|
||||
import { ProgressLine } from '@/components/shared/ux/ProgressLine';
|
||||
import { Box } from '@/ui/Box';
|
||||
|
||||
interface SponsorshipRequestsClientProps {
|
||||
viewData: SponsorshipRequestsViewData;
|
||||
@@ -11,25 +16,54 @@ interface SponsorshipRequestsClientProps {
|
||||
}
|
||||
|
||||
export function SponsorshipRequestsClient({ viewData, onAccept, onReject }: SponsorshipRequestsClientProps) {
|
||||
const router = useRouter();
|
||||
const [isProcessing, setIsProcessing] = useState<string | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const handleAccept = async (requestId: string) => {
|
||||
setIsProcessing(requestId);
|
||||
setError(null);
|
||||
const result = await onAccept(requestId);
|
||||
if (result.isErr()) {
|
||||
console.error('Failed to accept request:', result.getError());
|
||||
setError(result.getError());
|
||||
setIsProcessing(null);
|
||||
} else {
|
||||
router.refresh();
|
||||
setIsProcessing(null);
|
||||
}
|
||||
};
|
||||
|
||||
const handleReject = async (requestId: string, reason?: string) => {
|
||||
setIsProcessing(requestId);
|
||||
setError(null);
|
||||
const result = await onReject(requestId, reason);
|
||||
if (result.isErr()) {
|
||||
console.error('Failed to reject request:', result.getError());
|
||||
setError(result.getError());
|
||||
setIsProcessing(null);
|
||||
} else {
|
||||
router.refresh();
|
||||
setIsProcessing(null);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<SponsorshipRequestsTemplate
|
||||
viewData={viewData}
|
||||
onAccept={handleAccept}
|
||||
onReject={handleReject}
|
||||
/>
|
||||
<>
|
||||
<ProgressLine isLoading={!!isProcessing} />
|
||||
{error && (
|
||||
<Box position="fixed" top={4} right={4} zIndex={50} maxWidth="md">
|
||||
<InlineNotice
|
||||
variant="error"
|
||||
title="Action Failed"
|
||||
message={error}
|
||||
/>
|
||||
</Box>
|
||||
)}
|
||||
<SponsorshipRequestsTemplate
|
||||
viewData={viewData}
|
||||
onAccept={handleAccept}
|
||||
onReject={handleReject}
|
||||
processingId={isProcessing}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
'use server';
|
||||
|
||||
import { AcceptSponsorshipRequestMutation } from '@/lib/mutations/sponsors/AcceptSponsorshipRequestMutation';
|
||||
import { RejectSponsorshipRequestMutation } from '@/lib/mutations/sponsors/RejectSponsorshipRequestMutation';
|
||||
import { SessionGateway } from '@/lib/gateways/SessionGateway';
|
||||
import { revalidatePath } from 'next/cache';
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
import { routes } from '@/lib/routing/RouteConfig';
|
||||
|
||||
export async function acceptSponsorshipRequest(
|
||||
requestId: string,
|
||||
): Promise<Result<void, string>> {
|
||||
// Get session for actorDriverId
|
||||
const sessionGateway = new SessionGateway();
|
||||
const session = await sessionGateway.getSession();
|
||||
const actorDriverId = session?.user?.primaryDriverId;
|
||||
|
||||
if (!actorDriverId) {
|
||||
return Result.err('Not authenticated');
|
||||
}
|
||||
|
||||
const mutation = new AcceptSponsorshipRequestMutation();
|
||||
const result = await mutation.execute({ requestId, actorDriverId });
|
||||
|
||||
if (result.isErr()) {
|
||||
console.error('Failed to accept sponsorship request:', result.getError());
|
||||
return Result.err(result.getError());
|
||||
}
|
||||
|
||||
revalidatePath(routes.protected.profileSponsorshipRequests);
|
||||
return Result.ok(undefined);
|
||||
}
|
||||
|
||||
export async function rejectSponsorshipRequest(
|
||||
requestId: string,
|
||||
reason?: string,
|
||||
): Promise<Result<void, string>> {
|
||||
// Get session for actorDriverId
|
||||
const sessionGateway = new SessionGateway();
|
||||
const session = await sessionGateway.getSession();
|
||||
const actorDriverId = session?.user?.primaryDriverId;
|
||||
|
||||
if (!actorDriverId) {
|
||||
return Result.err('Not authenticated');
|
||||
}
|
||||
|
||||
const mutation = new RejectSponsorshipRequestMutation();
|
||||
const result = await mutation.execute({ requestId, actorDriverId, reason: reason || null });
|
||||
|
||||
if (result.isErr()) {
|
||||
console.error('Failed to reject sponsorship request:', result.getError());
|
||||
return Result.err(result.getError());
|
||||
}
|
||||
|
||||
revalidatePath(routes.protected.profileSponsorshipRequests);
|
||||
return Result.ok(undefined);
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { notFound } from 'next/navigation';
|
||||
import { SponsorshipRequestsPageQuery } from '@/lib/page-queries/SponsorshipRequestsPageQuery';
|
||||
import { SponsorshipRequestsClient } from './SponsorshipRequestsClient';
|
||||
import { acceptSponsorshipRequest, rejectSponsorshipRequest } from './actions';
|
||||
import { acceptSponsorshipRequest, rejectSponsorshipRequest } from '@/app/actions/sponsorshipActions';
|
||||
|
||||
export default async function SponsorshipRequestsPage() {
|
||||
// Execute PageQuery
|
||||
|
||||
Reference in New Issue
Block a user