Skip to content

Commit

Permalink
feat(landing): use mailgun compatible email API
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Carlier committed Jun 5, 2022
1 parent bb22cf2 commit a7a29f4
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 25 deletions.
1 change: 0 additions & 1 deletion landing/config/readflow.js → landing/config/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

const endpoint = process.env.READFLOW_ENDPOINT || 'https://api.readflow.app'
const clientID = process.env.READFLOW_CLIENT_ID || 'subscription-management'
const clientSecret = process.env.READFLOW_CLIENT_SECRET
Expand Down
9 changes: 9 additions & 0 deletions landing/config/sendmail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const serviceURL = process.env.SENDMAIL_SERVICE_URL
const contactMail = process.env.SENDMAIL_CONTACT_MAIL

const config = {
serviceURL,
contactMail,
}

export default config
7 changes: 7 additions & 0 deletions landing/config/site.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const baseURL = process.env.NODE_ENV === "development" ? "http://localhost:3000" : "https://about.readflow.app"

const config = {
baseURL,
}

export default config
4 changes: 0 additions & 4 deletions landing/config/url.js

This file was deleted.

2 changes: 1 addition & 1 deletion landing/context/AppAuthProvider.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { AuthProvider, AuthContext } from 'oidc-react'
import { AuthProvider } from 'oidc-react'

import oidcConfig from '@/config/oidc'

Expand Down
12 changes: 6 additions & 6 deletions landing/helpers/readflow.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import readflow from '@/config/readflow'
import api from '@/config/api'
import oidcConfig from '@/config/oidc'

const getRegisterUserMutation = (username) => `
Expand Down Expand Up @@ -40,8 +40,8 @@ const getAccessToken = async () => {
},
body: new URLSearchParams({
'grant_type': 'client_credentials',
'client_id': readflow.clientID,
'client_secret': readflow.clientSecret
'client_id': api.clientID,
'client_secret': api.clientSecret
})
})
if (res.error) {
Expand All @@ -68,7 +68,7 @@ export const updateUser = async (id, payload) => {
query: getUpdateUserMutation(id, payload),
variables: null
}
const res = await fetch(`${readflow.endpoint}/admin`, {
const res = await fetch(`${api.endpoint}/admin`, {
method: 'POST',
headers: new Headers({
'Content-Type': 'application/json',
Expand All @@ -90,7 +90,7 @@ export const updateUser = async (id, payload) => {
}

/**
* Get or register readfloe user.
* Get or register readflow user.
* @param {string} username
*/
export const getOrRegisterUser = async (username) => {
Expand All @@ -99,7 +99,7 @@ export const getOrRegisterUser = async (username) => {
query: getRegisterUserMutation(username),
variables: null
}
const res = await fetch(`${readflow.endpoint}/admin`, {
const res = await fetch(`${api.endpoint}/admin`, {
method: 'POST',
headers: new Headers({
'Content-Type': 'application/json',
Expand Down
16 changes: 9 additions & 7 deletions landing/pages/api/contact.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import validate from 'deep-email-validator'

import { sendmail_url } from '@/config/url'
import { serviceURL, contactMail } from '@/config/mail'

/**
* Post contact form to HTTP endpoint.
Expand Down Expand Up @@ -32,15 +32,17 @@ const contactForm = async (req, res) => {
return res.redirect(302, `/result?variant=contact-error&reason=${message}`)
}

const url = new URL(sendmail_url)
url.searchParams.set('subject', '[readflow-contact] ' + subject)
url.searchParams.set('from', from)
const resp = await fetch(url, {
const formData = new FormData()
formData.append('subject', '[readflow-contact] ' + subject)
formData.append('to', contactMail)
formData.append('from', from)
formData.append('text', body)
const resp = await fetch(serviceURL, {
method: 'POST',
headers: new Headers({
'Content-Type': 'application/json'
'Content-Type': 'application/x-www-form-urlencoded',
}),
body
body: formData
})

if (resp.error) {
Expand Down
6 changes: 3 additions & 3 deletions landing/pages/api/create-checkout-session.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { base_url } from '@/config/url'
import { baseURL } from '@/config/site'
import { pricing } from '@/config/stripe'
import { stripe } from '@/helpers/stripe-server'
import { decodeToken } from '@/helpers/token'
Expand Down Expand Up @@ -53,8 +53,8 @@ const createCheckoutSession = async (req, res) => {
quantity: 1
}
],
success_url: `${base_url}/result?variant=subscription-success&session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${base_url}/pricing`
success_url: `${baseURL}/result?variant=subscription-success&session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${baseURL}/pricing`
})
return res.status(200).json({ sessionId: session.id })
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions landing/pages/api/create-portal-link.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { decodeToken } from '@/helpers/token'
import { base_url } from '@/config/url'
import { baseURL } from '@/config/site'
import { getOrRegisterUser } from '@/helpers/readflow'
import { stripe } from '@/helpers/stripe-server'

Expand Down Expand Up @@ -28,7 +28,7 @@ const createPortalLink = async (req, res) => {
}
const { url } = await stripe.billingPortal.sessions.create({
customer,
return_url: `${base_url}/account`
return_url: `${baseURL}/account`
})
return res.status(200).json({ url })
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion landing/pages/pricing.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const PricingSection = () => {
const auth = useAuth()

const handlePlanCheckout = async (plan) => {
console.log(`choosed plan: ${plan}`)
console.log(`chosen plan: ${plan}`)
if (plan === 'free') {
return document.location = 'https://readflow.app/login'
}
Expand Down

0 comments on commit a7a29f4

Please sign in to comment.