Skip to content

Commit

Permalink
Chore: change firebase api syntax to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
everywherejacobkim committed Sep 30, 2023
1 parent 277fc50 commit 963576a
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 101 deletions.
28 changes: 0 additions & 28 deletions src/lib/api/firebase.js

This file was deleted.

31 changes: 31 additions & 0 deletions src/lib/api/firebase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {firebaseConfig} from '$lib/firebaseConfig'
import {initializeApp} from 'firebase/app'
import {Firestore, getFirestore, collection, addDoc} from 'firebase/firestore'

let firebaseApp
let db: Firestore

if (!firebaseApp) {
firebaseApp = initializeApp(firebaseConfig)
}

// Initialize Firestore
db = getFirestore()

interface FormData {
name: string
email: string
occupation: string
work: string
message: string
}

export const saveFormData = async (formData: FormData) => {
try {
const collectionRef = collection(db, 'registrant')
await addDoc(collectionRef, formData)
console.log('Form data saved to Firestore')
} catch (error) {
console.error('Error saving form data to Firestore:', error)
}
}
15 changes: 7 additions & 8 deletions src/lib/components/CheckoutForm.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script lang="ts">
import { saveFormData } from '$lib/api/firebase';
import {saveFormData} from '$lib/api/firebase'
export let isOpen = false
let name = ''
Expand All @@ -16,18 +15,18 @@
occupation,
work,
message,
};
}
// Save the form data to Firebase
saveFormData(formData)
.then(() => {
console.log('Form data saved successfully');
console.log('Form data saved successfully')
// Redirect to the main page
window.location.href = '/';
window.location.href = '/'
})
.catch((error) => {
console.error('Error saving form data:', error);
});
console.error('Error saving form data:', error)
})
}
</script>

Expand Down Expand Up @@ -88,7 +87,7 @@
<div class="text-right">
<button
type="submit"
class="px-4 py-2 bg-[#794bc4] text-white rounded-md hover:bg-royalBlue-500"
class="px-4 py-2 bg-royalBlue-500 text-white rounded-md hover:bg-[#794bc4]"
>Submit</button>
</div>
</form>
Expand Down
Loading

0 comments on commit 963576a

Please sign in to comment.