質問編集履歴
1
ソースコードの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -8,4 +8,140 @@
|
|
8
8
|
|
9
9
|
Next.jsの公式も読んだのですが、該当箇所がよくわかりませんでした。
|
10
10
|
|
11
|
-
全体的な理解が足りていないのかもしれませんが、このあたりの実装方法のヒントや参考文献についてお知恵を拝借できますと助かります。よろしくお願いします。
|
11
|
+
全体的な理解が足りていないのかもしれませんが、このあたりの実装方法のヒントや参考文献についてお知恵を拝借できますと助かります。よろしくお願いします。
|
12
|
+
|
13
|
+
contact.js
|
14
|
+
```javascript
|
15
|
+
import Layout from "../components/layout"
|
16
|
+
import Tags from "./tags"
|
17
|
+
import SearchWindow from "./search_window"
|
18
|
+
import { getUniqTags } from "../lib/posts"
|
19
|
+
import axios from 'axios'
|
20
|
+
|
21
|
+
export async function getStaticProps() {
|
22
|
+
|
23
|
+
const uniqTags = getUniqTags()
|
24
|
+
|
25
|
+
return {
|
26
|
+
props:{
|
27
|
+
uniqTags
|
28
|
+
}
|
29
|
+
}
|
30
|
+
|
31
|
+
}
|
32
|
+
|
33
|
+
const submitFormData = (e) => {
|
34
|
+
e.preventDefault()
|
35
|
+
|
36
|
+
axios.post('/api/mailSender',
|
37
|
+
{
|
38
|
+
'company': company,
|
39
|
+
'nickname': nickname,
|
40
|
+
'email': email,
|
41
|
+
'question': question
|
42
|
+
}
|
43
|
+
).then(() => {
|
44
|
+
var myModal = new bootstrap.Modal(document.getElementById('myModal'))
|
45
|
+
myModal.toggle()
|
46
|
+
}).catch((error) => {
|
47
|
+
console.log(error)
|
48
|
+
})
|
49
|
+
}
|
50
|
+
|
51
|
+
export default function Contact({ uniqTags }) {
|
52
|
+
return(
|
53
|
+
<Layout>
|
54
|
+
<div className="container content">
|
55
|
+
<div className="row">
|
56
|
+
<div className="col-md-9 mb-5">
|
57
|
+
<form onSubmit={submitFormData}>
|
58
|
+
<label htmlFor="company" className="form-label fw-bold">会社名 <span className="badge bg-secondary">任意</span></label>
|
59
|
+
<input maxLength="30" type="text" className="form-control mb-3" id="company" placeholder="会社名を記入してください"/>
|
60
|
+
<label htmlFor="nickname" className="form-label fw-bold">お名前(ニックネーム可) <span className="badge bg-danger">必須</span></label>
|
61
|
+
<input required maxLength="30" type="text" className="form-control mb-3" id="nickname" placeholder="お名前を記入してください"/>
|
62
|
+
<label htmlFor="email" className="form-label fw-bold">Eメールアドレス <span className="badge bg-danger">必須</span></label>
|
63
|
+
<input required maxLength="50" type="email" className="form-control mb-3" id="email" placeholder="メールアドレスを記入してください"/>
|
64
|
+
<label htmlFor="question" className="form-label fw-bold">お問い合わせ内容 <span className="badge bg-danger">必須</span></label>
|
65
|
+
<textarea required maxLength="1000" className="form-control mb-5" id="question" rows="7" placeholder="お問い合わせ、ご相談内容を記入してください"/>
|
66
|
+
<div className="d-flex justify-content-center">
|
67
|
+
<input id="submitBtn" className="btn btn-success w-50" type="submit" value="この内容で送信する"/>
|
68
|
+
</div>
|
69
|
+
<div className="modal fade" id="myModal" data-bs-backdrop="static" data-bs-keyboard="false" tabIndex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
|
70
|
+
<div className="modal-dialog">
|
71
|
+
<div className="modal-content">
|
72
|
+
<div className="modal-header">
|
73
|
+
<h5 className="modal-title" id="staticBackdropLabel">送信完了</h5>
|
74
|
+
<button type="button" className="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
75
|
+
</div>
|
76
|
+
<div className="modal-body">
|
77
|
+
お問い合わせいただきありがとうございます。
|
78
|
+
</div>
|
79
|
+
<div className="modal-footer">
|
80
|
+
<button type="button" className="btn btn-secondary" data-bs-dismiss="modal" onClick={closeModal}>Close</button>
|
81
|
+
</div>
|
82
|
+
</div>
|
83
|
+
</div>
|
84
|
+
</div>
|
85
|
+
</form>
|
86
|
+
</div>
|
87
|
+
<div className="col-md-3">
|
88
|
+
<Tags uniqTags={uniqTags}/>
|
89
|
+
<SearchWindow />
|
90
|
+
</div>
|
91
|
+
</div>
|
92
|
+
</div>
|
93
|
+
</Layout>
|
94
|
+
)
|
95
|
+
}
|
96
|
+
```
|
97
|
+
/api/mailSender.js
|
98
|
+
```javascript
|
99
|
+
import sgMail from '@sendgrid/mail'
|
100
|
+
import Cors from 'cors'
|
101
|
+
import initMiddleware from '../../lib/init-middleware'
|
102
|
+
|
103
|
+
const mailto = process.env.NEXT_PUBLIC_MAILTO
|
104
|
+
const mailkey = process.env.NEXT_PUBLIC_MAILKEY
|
105
|
+
|
106
|
+
const cors = initMiddleware(
|
107
|
+
Cors({
|
108
|
+
methods: ['POST'],
|
109
|
+
})
|
110
|
+
)
|
111
|
+
|
112
|
+
export default async function handler(req, res) {
|
113
|
+
|
114
|
+
await cors(req, res)
|
115
|
+
|
116
|
+
if (req.method === 'POST') {
|
117
|
+
|
118
|
+
const company = req.body.company
|
119
|
+
const nickname = req.body.nickname
|
120
|
+
const email = req.body.email
|
121
|
+
const question = req.body.question
|
122
|
+
|
123
|
+
sgMail.setApiKey(mailkey)
|
124
|
+
const msg = {
|
125
|
+
to: mailto,
|
126
|
+
from: email,
|
127
|
+
subject: "フォームからのお問い合わせ",
|
128
|
+
html: `<p>お名前:${nickname}</p>
|
129
|
+
<p>会社名:${company}</p>
|
130
|
+
<p>お問い合わせ内容:${question}</p>`,
|
131
|
+
}
|
132
|
+
|
133
|
+
try {
|
134
|
+
await sgMail.send(msg)
|
135
|
+
console.log('Success to send mail')
|
136
|
+
res.status(200).send()
|
137
|
+
} catch(error) {
|
138
|
+
console.log(error)
|
139
|
+
res.status(500).send()
|
140
|
+
}
|
141
|
+
|
142
|
+
} else {
|
143
|
+
res.status(400).send()
|
144
|
+
}
|
145
|
+
|
146
|
+
};
|
147
|
+
```
|