chore: code cleaning (#21)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import prisma from '@prisma/prisma';
|
||||
import { ApiResponse } from '@utils/apiResponse';
|
||||
import { formatApiResponse } from '@utils/formatApiResponse';
|
||||
import {
|
||||
BAD_REQUEST,
|
||||
INTERNAL_SERVER_ERROR,
|
||||
@@ -21,7 +21,7 @@ export async function POST(request: NextRequest) {
|
||||
const body = await request.json();
|
||||
const validation = ConfirmationSchema.safeParse(body);
|
||||
if (!validation.success || !validation.data.code) {
|
||||
return ApiResponse(STATUS_BAD_REQUEST, BAD_REQUEST);
|
||||
return formatApiResponse(STATUS_BAD_REQUEST, BAD_REQUEST);
|
||||
}
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
@@ -53,10 +53,13 @@ export async function POST(request: NextRequest) {
|
||||
message: `Thank you for confirming the subscription, ${user.email}!`
|
||||
};
|
||||
|
||||
return ApiResponse(STATUS_OK, message);
|
||||
return formatApiResponse(STATUS_OK, message);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return ApiResponse(STATUS_INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR);
|
||||
return formatApiResponse(
|
||||
STATUS_INTERNAL_SERVER_ERROR,
|
||||
INTERNAL_SERVER_ERROR
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import prisma from '@prisma/prisma';
|
||||
import { ApiResponse } from '@utils/apiResponse';
|
||||
import { formatApiResponse } from '@utils/formatApiResponse';
|
||||
import {
|
||||
INTERNAL_SERVER_ERROR,
|
||||
STATUS_INTERNAL_SERVER_ERROR,
|
||||
@@ -15,7 +15,7 @@ export async function GET(request: NextRequest) {
|
||||
if (
|
||||
request.headers.get('Authorization') !== `Bearer ${process.env.CRON_SECRET}`
|
||||
) {
|
||||
return ApiResponse(STATUS_UNAUTHORIZED, 'Unauthorized');
|
||||
return formatApiResponse(STATUS_UNAUTHORIZED, 'Unauthorized');
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -76,9 +76,15 @@ export async function GET(request: NextRequest) {
|
||||
});
|
||||
}
|
||||
|
||||
return ApiResponse(STATUS_OK, `Imported ${newsPromises.length} news.`);
|
||||
return formatApiResponse(
|
||||
STATUS_OK,
|
||||
`Imported ${newsPromises.length} news.`
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return ApiResponse(STATUS_INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR);
|
||||
return formatApiResponse(
|
||||
STATUS_INTERNAL_SERVER_ERROR,
|
||||
INTERNAL_SERVER_ERROR
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import NewsletterTemplate from '@components/email/Newsletter';
|
||||
import { NewsletterTemplate } from '@components/email/Newsletter';
|
||||
import prisma from '@prisma/prisma';
|
||||
import { ApiResponse } from '@utils/apiResponse';
|
||||
import { sender } from '@utils/sender';
|
||||
import { formatApiResponse } from '@utils/formatApiResponse';
|
||||
import { sender } from '@utils/resendClient';
|
||||
import {
|
||||
INTERNAL_SERVER_ERROR,
|
||||
STATUS_INTERNAL_SERVER_ERROR,
|
||||
@@ -17,7 +17,7 @@ export async function GET(request: NextRequest) {
|
||||
if (
|
||||
request.headers.get('Authorization') !== `Bearer ${process.env.CRON_SECRET}`
|
||||
) {
|
||||
return ApiResponse(STATUS_UNAUTHORIZED, 'Unauthorized');
|
||||
return formatApiResponse(STATUS_UNAUTHORIZED, 'Unauthorized');
|
||||
}
|
||||
|
||||
if (!process.env.NEWS_TO_USE) {
|
||||
@@ -52,7 +52,7 @@ export async function GET(request: NextRequest) {
|
||||
console.info(`Found ${users.length} users to mail to.`);
|
||||
|
||||
if (users.length === 0) {
|
||||
return ApiResponse(STATUS_OK, 'No user to mail to.');
|
||||
return formatApiResponse(STATUS_OK, 'No user to mail to.');
|
||||
}
|
||||
|
||||
const news = await prisma.news.findMany({
|
||||
@@ -70,7 +70,7 @@ export async function GET(request: NextRequest) {
|
||||
console.info(`Found ${news.length} news to include in the newsletter.`);
|
||||
|
||||
if (news.length === 0) {
|
||||
return ApiResponse(STATUS_OK, 'No news to include in newsletter.');
|
||||
return formatApiResponse(STATUS_OK, 'No news to include in newsletter.');
|
||||
}
|
||||
|
||||
const validRankedNews = news.sort((a, b) => b.score - a.score);
|
||||
@@ -83,7 +83,10 @@ export async function GET(request: NextRequest) {
|
||||
);
|
||||
|
||||
if (!sent) {
|
||||
return ApiResponse(STATUS_INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR);
|
||||
return formatApiResponse(
|
||||
STATUS_INTERNAL_SERVER_ERROR,
|
||||
INTERNAL_SERVER_ERROR
|
||||
);
|
||||
}
|
||||
|
||||
// update users so they don't get the newsletter again
|
||||
@@ -98,12 +101,15 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
});
|
||||
|
||||
return ApiResponse(
|
||||
return formatApiResponse(
|
||||
STATUS_OK,
|
||||
`Newsletter sent to ${users.length} addresses.`
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return ApiResponse(STATUS_INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR);
|
||||
return formatApiResponse(
|
||||
STATUS_INTERNAL_SERVER_ERROR,
|
||||
INTERNAL_SERVER_ERROR
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import prisma from '@prisma/prisma';
|
||||
import { ApiResponse } from '@utils/apiResponse';
|
||||
import { formatApiResponse } from '@utils/formatApiResponse';
|
||||
import {
|
||||
INTERNAL_SERVER_ERROR,
|
||||
STATUS_INTERNAL_SERVER_ERROR,
|
||||
@@ -21,10 +21,13 @@ export async function GET() {
|
||||
});
|
||||
|
||||
if (news) {
|
||||
return ApiResponse(STATUS_OK, news);
|
||||
return formatApiResponse(STATUS_OK, news);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return ApiResponse(STATUS_INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR);
|
||||
return formatApiResponse(
|
||||
STATUS_INTERNAL_SERVER_ERROR,
|
||||
INTERNAL_SERVER_ERROR
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import ConfirmationTemplate from '@components/email/Confirmation';
|
||||
import { ConfirmationTemplate } from '@components/email/Confirmation';
|
||||
import prisma from '@prisma/prisma';
|
||||
import { ApiResponse } from '@utils/apiResponse';
|
||||
import { sender } from '@utils/sender';
|
||||
import { formatApiResponse } from '@utils/formatApiResponse';
|
||||
import { sender } from '@utils/resendClient';
|
||||
import {
|
||||
BAD_REQUEST,
|
||||
INTERNAL_SERVER_ERROR,
|
||||
@@ -25,7 +25,7 @@ export async function POST(request: NextRequest) {
|
||||
const validation = SubscribeFormSchema.safeParse(body);
|
||||
|
||||
if (!validation.success) {
|
||||
return ApiResponse(STATUS_BAD_REQUEST, BAD_REQUEST);
|
||||
return formatApiResponse(STATUS_BAD_REQUEST, BAD_REQUEST);
|
||||
}
|
||||
|
||||
const { email } = validation.data;
|
||||
@@ -73,7 +73,7 @@ export async function POST(request: NextRequest) {
|
||||
message: `Thank you for subscribing!`
|
||||
};
|
||||
|
||||
return ApiResponse(STATUS_OK, message);
|
||||
return formatApiResponse(STATUS_OK, message);
|
||||
} else if (user && !user.confirmed) {
|
||||
await prisma.user.update({
|
||||
where: {
|
||||
@@ -106,7 +106,10 @@ export async function POST(request: NextRequest) {
|
||||
const sent = await sender([email], ConfirmationTemplate(code));
|
||||
|
||||
if (!sent) {
|
||||
return ApiResponse(STATUS_INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR);
|
||||
return formatApiResponse(
|
||||
STATUS_INTERNAL_SERVER_ERROR,
|
||||
INTERNAL_SERVER_ERROR
|
||||
);
|
||||
}
|
||||
|
||||
const message: ResponseType = {
|
||||
@@ -114,9 +117,12 @@ export async function POST(request: NextRequest) {
|
||||
message: `Thank you! You will now receive an email to ${email} to confirm the subscription.`
|
||||
};
|
||||
|
||||
return ApiResponse(STATUS_OK, message);
|
||||
return formatApiResponse(STATUS_OK, message);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return ApiResponse(STATUS_INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR);
|
||||
return formatApiResponse(
|
||||
STATUS_INTERNAL_SERVER_ERROR,
|
||||
INTERNAL_SERVER_ERROR
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import UnsubscribeTemplate from '@components/email/Unsubscribe';
|
||||
import { UnsubscribeTemplate } from '@components/email/Unsubscribe';
|
||||
import prisma from '@prisma/prisma';
|
||||
import { ApiResponse } from '@utils/apiResponse';
|
||||
import { sender } from '@utils/sender';
|
||||
import { formatApiResponse } from '@utils/formatApiResponse';
|
||||
import { sender } from '@utils/resendClient';
|
||||
import {
|
||||
BAD_REQUEST,
|
||||
INTERNAL_SERVER_ERROR,
|
||||
@@ -23,7 +23,7 @@ export async function POST(request: NextRequest) {
|
||||
const body = await request.json();
|
||||
const validation = UnsubscribeFormSchema.safeParse(body);
|
||||
if (!validation.success) {
|
||||
return ApiResponse(STATUS_BAD_REQUEST, BAD_REQUEST);
|
||||
return formatApiResponse(STATUS_BAD_REQUEST, BAD_REQUEST);
|
||||
}
|
||||
|
||||
const { email } = validation.data;
|
||||
@@ -55,7 +55,7 @@ export async function POST(request: NextRequest) {
|
||||
const sent = await sender([email], UnsubscribeTemplate());
|
||||
|
||||
if (!sent) {
|
||||
return ApiResponse(
|
||||
return formatApiResponse(
|
||||
STATUS_INTERNAL_SERVER_ERROR,
|
||||
'Internal server error'
|
||||
);
|
||||
@@ -67,9 +67,12 @@ export async function POST(request: NextRequest) {
|
||||
message: `${email} unsubscribed.`
|
||||
};
|
||||
|
||||
return ApiResponse(STATUS_OK, message);
|
||||
return formatApiResponse(STATUS_OK, message);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return ApiResponse(STATUS_INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR);
|
||||
return formatApiResponse(
|
||||
STATUS_INTERNAL_SERVER_ERROR,
|
||||
INTERNAL_SERVER_ERROR
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
'use client';
|
||||
|
||||
import { CardDescription } from '@components/Card';
|
||||
import CustomCard from '@components/CustomCard';
|
||||
import Schema from '@components/SchemaOrg';
|
||||
import { CustomCard } from '@components/CustomCard';
|
||||
import { SchemaOrg } from '@components/SchemaOrg';
|
||||
import { ResponseType } from '@utils/validationSchemas';
|
||||
import { useRouter, useSearchParams } from 'next/navigation';
|
||||
import { Suspense, useEffect, useState } from 'react';
|
||||
|
||||
function ConfirmationPage() {
|
||||
const ConfirmationPage = () => {
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
const [loading, setLoading] = useState(true);
|
||||
@@ -78,9 +78,9 @@ function ConfirmationPage() {
|
||||
footer={false}
|
||||
/>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export default function Confirmation() {
|
||||
const Confirmation = () => {
|
||||
const schema = {
|
||||
'@context': 'https://schema.org',
|
||||
'@type': 'WebSite',
|
||||
@@ -91,10 +91,12 @@ export default function Confirmation() {
|
||||
|
||||
return (
|
||||
<>
|
||||
<Schema schema={schema} />
|
||||
<SchemaOrg schema={schema} />
|
||||
<Suspense fallback={<>Loading...</>}>
|
||||
<ConfirmationPage />
|
||||
</Suspense>
|
||||
</>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export default Confirmation;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import Tiles from '@components/tiles/Tiles';
|
||||
import { Tiles } from '@components/tiles/Tiles';
|
||||
import { cn } from '@utils/cn';
|
||||
import { Analytics } from '@vercel/analytics/react';
|
||||
import type { Metadata } from 'next';
|
||||
@@ -6,7 +6,7 @@ import { Inter as FontSans } from 'next/font/google';
|
||||
import './globals.css';
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: 'Hacker News newsletter by FromPixels',
|
||||
title: `Hacker News newsletter by ${process.env.NEXT_PUBLIC_BRAND_NAME}`,
|
||||
description: 'Newsletter delivering the best posts from Hacker News',
|
||||
keywords: 'newsletter, hackernews, technology, coding, programming, news'
|
||||
};
|
||||
|
||||
14
app/page.tsx
14
app/page.tsx
@@ -2,12 +2,12 @@
|
||||
|
||||
import { Button } from '@components/Button';
|
||||
import { CardDescription } from '@components/Card';
|
||||
import CustomCard from '@components/CustomCard';
|
||||
import ErrorMessage from '@components/ErrorMessage';
|
||||
import { CustomCard } from '@components/CustomCard';
|
||||
import { ErrorMessage } from '@components/ErrorMessage';
|
||||
import { FormControl } from '@components/form/FormControl';
|
||||
import { FormMessage } from '@components/form/FormMessage';
|
||||
import { Input } from '@components/Input';
|
||||
import Schema from '@components/SchemaOrg';
|
||||
import { SchemaOrg } from '@components/SchemaOrg';
|
||||
import { FormField } from '@contexts/FormField/FormFieldProvider';
|
||||
import { FormItem } from '@contexts/FormItem/FormItemProvider';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
@@ -19,7 +19,7 @@ import {
|
||||
import { useState } from 'react';
|
||||
import { FormProvider, useForm } from 'react-hook-form';
|
||||
|
||||
export default function Home() {
|
||||
export const Home = () => {
|
||||
const [completed, setCompleted] = useState(false);
|
||||
const [message, setMessage] = useState('');
|
||||
const [error, setError] = useState(false);
|
||||
@@ -119,7 +119,7 @@ export default function Home() {
|
||||
|
||||
return (
|
||||
<>
|
||||
<Schema schema={schema} />
|
||||
<SchemaOrg schema={schema} />
|
||||
<CustomCard
|
||||
className='max-90vw w-96'
|
||||
title='Interested in keeping up with the latest from the tech world? 👩💻'
|
||||
@@ -128,4 +128,6 @@ export default function Home() {
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export default Home;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
'use client';
|
||||
|
||||
import CustomCard from '@components/CustomCard';
|
||||
import Schema from '@components/SchemaOrg';
|
||||
import { CustomCard } from '@components/CustomCard';
|
||||
import { SchemaOrg } from '@components/SchemaOrg';
|
||||
import Link from 'next/link';
|
||||
|
||||
export default function Privacy() {
|
||||
const Privacy = () => {
|
||||
const schema = {
|
||||
'@context': 'https://schema.org',
|
||||
'@type': 'WebSite',
|
||||
@@ -449,7 +449,7 @@ export default function Privacy() {
|
||||
|
||||
return (
|
||||
<>
|
||||
<Schema schema={schema} />
|
||||
<SchemaOrg schema={schema} />
|
||||
<CustomCard
|
||||
className='max-90vh max-90vw'
|
||||
title='Privacy Policy'
|
||||
@@ -458,4 +458,6 @@ export default function Privacy() {
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export default Privacy;
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
|
||||
import { Button } from '@components/Button';
|
||||
import { CardDescription } from '@components/Card';
|
||||
import CustomCard from '@components/CustomCard';
|
||||
import ErrorMessage from '@components/ErrorMessage';
|
||||
import { CustomCard } from '@components/CustomCard';
|
||||
import { ErrorMessage } from '@components/ErrorMessage';
|
||||
import { FormControl } from '@components/form/FormControl';
|
||||
import { FormMessage } from '@components/form/FormMessage';
|
||||
import { Input } from '@components/Input';
|
||||
import Schema from '@components/SchemaOrg';
|
||||
import { SchemaOrg } from '@components/SchemaOrg';
|
||||
import { FormField } from '@contexts/FormField/FormFieldProvider';
|
||||
import { FormItem } from '@contexts/FormItem/FormItemProvider';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
@@ -19,7 +19,7 @@ import {
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { FormProvider, useForm } from 'react-hook-form';
|
||||
|
||||
export default function Unsubscribe() {
|
||||
const Unsubscribe = () => {
|
||||
const [completed, setCompleted] = useState(false);
|
||||
const [message, setMessage] = useState('');
|
||||
const [error, setError] = useState(false);
|
||||
@@ -122,7 +122,7 @@ export default function Unsubscribe() {
|
||||
|
||||
return (
|
||||
<>
|
||||
<Schema schema={schema} />
|
||||
<SchemaOrg schema={schema} />
|
||||
<CustomCard
|
||||
className='max-90vw w-96'
|
||||
title='Unsubscribe'
|
||||
@@ -131,4 +131,6 @@ export default function Unsubscribe() {
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export default Unsubscribe;
|
||||
|
||||
Reference in New Issue
Block a user