Last active
May 5, 2020 14:28
-
-
Save Kazuki-tam/5f31c2c4d98b687a5bb43fd637a19505 to your computer and use it in GitHub Desktop.
firebase-form sample code2
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
"use strict"; | |
// Firebase 初期化 | |
import * as firebase from "firebase"; | |
const config = { | |
apiKey: "api-key", | |
authDomain: "project-id.firebaseapp.com", | |
databaseURL: "https://project-id.firebaseio.com", | |
projectId: "project-id", | |
storageBucket: "project-id.appspot.com", | |
messagingSenderId: "sender-id", | |
}; | |
firebase.initializeApp(config); | |
export const functions = firebase.functions(); | |
// フォームの値取得 | |
function getVal(id: string): string { | |
const element: HTMLInputElement = document.getElementById( | |
id | |
) as HTMLInputElement; | |
return element.value; | |
} | |
// フォーム送信 | |
function submitForm(e: any): void { | |
e.preventDefault(); | |
const getName = getVal("name"); | |
const getMail = getVal("email"); | |
const mailer = functions.httpsCallable("sendMail"); | |
mailer({ name: getName, email: getMail }) | |
.then(() => { | |
alert("お問い合わせ内容を送信しました!"); | |
}) | |
.catch(error => { | |
console.log(error); | |
}) | |
.finally(() => { | |
console.log("処理が実行されました"); | |
}); | |
} | |
document.getElementById("contactForm").addEventListener("submit", submitForm); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment