-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
600 additions
and
3 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import mailchimp from '@mailchimp/mailchimp_marketing'; | ||
|
||
import { NextResponse } from 'next/server'; | ||
|
||
mailchimp.setConfig({ | ||
apiKey: process.env.MAILCHIMP_API_KEY, | ||
server: process.env.MAILCHIMP_API_SERVER, | ||
}); | ||
|
||
const API_KEY = process.env.MAILCHIMP_API_KEY; | ||
const API_SERVER = process.env.MAILCHIMP_API_SERVER; | ||
const AUDIENCE_ID = process.env.MAILCHIMP_AUDIENCE_ID; | ||
export const POST = async (req: Request, res: Response) => { | ||
const body = await req.json(); | ||
const { email } = body; | ||
|
||
if (!email || !email.length) { | ||
return new NextResponse( | ||
JSON.stringify({ message: 'Adres emial nie może być pusty' }), | ||
{ | ||
status: 400, | ||
} | ||
); | ||
} | ||
|
||
const url = `https://${API_SERVER}.api.mailchimp.com/3.0/lists/${AUDIENCE_ID}/members`; | ||
|
||
const data = { | ||
email_address: email, | ||
status: 'subscribed', | ||
}; | ||
|
||
const options = { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `api_key ${API_KEY}`, | ||
}, | ||
body: JSON.stringify(data), | ||
}; | ||
try { | ||
const response = await fetch(url, options); | ||
const responseData = await response.json(); | ||
|
||
if (responseData.status >= 400) { | ||
return new NextResponse( | ||
JSON.stringify({ | ||
message: | ||
responseData.detail ?? | ||
`Wystąpił bład podczas dodawania emaila do list. Napisz do mnie na [email protected], a dodam Cię do listy. `, | ||
}), | ||
{ | ||
status: responseData.status, | ||
} | ||
); | ||
} | ||
return new NextResponse(JSON.stringify({ message: responseData.detail }), { | ||
status: 201, | ||
}); | ||
} catch (error) { | ||
console.log(error); | ||
return new NextResponse(JSON.stringify({ message: 'Coś poszło nie tak' }), { | ||
status: 500, | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use client'; | ||
import React, { useState } from 'react'; | ||
|
||
//utils | ||
import { isEmailOrEmpty } from '@/utils/helpers'; | ||
//styles | ||
import styles from './subscribePage.module.css'; | ||
|
||
const SubscribePage = () => { | ||
const [email, setEmail] = useState(''); | ||
const [message, setMessage] = useState(''); | ||
const [loading, setLoading] = useState(false); | ||
|
||
const subscribe = async (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
if (!isEmailOrEmpty(email)) { | ||
return setMessage('Wpisz poprawny adres email'); | ||
} | ||
setLoading(true); | ||
const res = await fetch('/api/subscribe', { | ||
body: JSON.stringify({ | ||
email, | ||
}), | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
method: 'POST', | ||
}); | ||
const data = await res.json(); | ||
|
||
const { message } = data; | ||
if (message) { | ||
setMessage(message); | ||
setLoading(false); | ||
return; | ||
} | ||
|
||
setEmail(''); | ||
setMessage('Sukces! 🎉 Twoj adres mailowy został zapisany do newslettera.'); | ||
setLoading(false); | ||
}; | ||
|
||
return ( | ||
<form onSubmit={subscribe} className={styles.form}> | ||
<input | ||
id='email-input' | ||
name='email' | ||
placeholder={'Wpisz swój email'} | ||
value={email} | ||
className={styles.input} | ||
onChange={e => setEmail(e.target.value)} | ||
required | ||
type='email' | ||
/> | ||
<div className={styles.message}> | ||
{message | ||
? message | ||
: ` Zapisz się do newslettera! Wysyłam tylko powiadomienie o nowych wpisach, bez spamu. Możesz zrezygnować w dowolnym momencie. W przypadku zapisania się, a nie otrzymywania wiadomości mailowej, czasem wiadomości wpadają do folderu SPAM.`} | ||
</div> | ||
<button className={styles.button} disabled={loading} type='submit'> | ||
{'Subskrybuj'} | ||
</button> | ||
</form> | ||
); | ||
}; | ||
|
||
export default SubscribePage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
.form { | ||
background-color: var(--softBg); | ||
padding: 3rem; | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: center; | ||
align-items: center; | ||
gap: 1.25rem; | ||
width: 50%; | ||
margin: 0 auto; | ||
} | ||
.input { | ||
border: none; | ||
width: max-content; | ||
border-radius: 0.625rem; | ||
padding: 0.625rem; | ||
outline: none; | ||
text-align: center; | ||
} | ||
.message { | ||
text-align: center; | ||
font-size: 1.125rem; | ||
color: var(--textColor); | ||
font-weight: 500; | ||
margin-bottom: 1.25rem; | ||
width: 60%; | ||
} | ||
.button { | ||
padding: 0.625rem 1.25rem; | ||
border: none; | ||
background-color: #1a8917; | ||
color: #fff; | ||
cursor: pointer; | ||
border-radius: 20px; | ||
align-self: flex-end; | ||
} | ||
.button:disabled { | ||
opacity: 0.5; | ||
cursor: not-allowed; | ||
} | ||
|
||
@media screen and (max-width: 1280px) { | ||
.form { | ||
width: 80%; | ||
} | ||
} | ||
|
||
@media screen and (max-width: 640px) { | ||
.form { | ||
width: 100%; | ||
} | ||
} | ||
|
||
@media screen and (max-width: 480px) { | ||
.message { | ||
width: 100%; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters