Skip to content
This repository has been archived by the owner on Jan 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #29 from mfdavies/fix-images
Browse files Browse the repository at this point in the history
change images to use FB Cloud Storage
  • Loading branch information
owencooke authored Jan 7, 2024
2 parents 98a50ed + 74485cf commit 6fb01be
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
2 changes: 2 additions & 0 deletions frontend/firebaseConfig.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import firebase from "firebase/compat/app";
import "firebase/compat/firestore";
import "firebase/compat/auth";
import "firebase/compat/storage";

const firebaseConfig = {
apiKey: "AIzaSyCdjuKe_DkpM9PT71WMfl7l3SVpCeZfD5c",
Expand All @@ -19,6 +20,7 @@ if (!firebase.apps.length) {

export const db = firebase.firestore();
export const auth = firebase.auth();
export const storage = firebase.storage();
export async function getCurrentUser() {
return new Promise((resolve, reject) => {
if (auth.currentUser) {
Expand Down
63 changes: 42 additions & 21 deletions frontend/src/views/practitioner/components/ExerciseModal.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useState } from "react";
import { getCurrentUser, db } from "../../../../firebaseConfig";
import { getCurrentUser, db, storage } from "../../../../firebaseConfig";
import { ref, uploadBytes, getDownloadURL } from "firebase/storage";
import { v4 as uuidv4 } from "uuid";

const emptyForm = {
title: "",
Expand All @@ -9,6 +11,7 @@ const emptyForm = {

const ExerciseModal = () => {
const [formData, setFormData] = useState(emptyForm);
const [image, setImage] = useState(null);
const [imageString, setImageString] = useState("");

const handleChange = (e) => {
Expand All @@ -27,30 +30,48 @@ const ExerciseModal = () => {
};

reader.readAsDataURL(selectedImage);
setImage(selectedImage);
}
};

const handleSubmit = async (e) => {
e.preventDefault();

// Check if any form fields are empty
if (!formData.title || !formData.description || !formData.steps || !imageString) {
alert("Please fill in all fields and upload an image.");
return;
}

try {
// Add the new exercise to Firestore
const currentUser = await getCurrentUser();
await db
.collection(`practitioners/${currentUser.uid}/exercises`)
.add({ ...formData, image: imageString });
handleClose();
} catch (error) {
console.error("Error adding new exercise:", error);
alert("Failed to add new exercise. Please try again.");
}
};
e.preventDefault();

// Check if any form fields are empty
if (
!formData.title ||
!formData.description ||
!formData.steps ||
!imageString ||
!image
) {
alert("Please fill in all fields and upload an image.");
return;
}

try {
// Upload the image to Firebase Cloud Storage
const currentUser = await getCurrentUser();
const storageRef = ref(
storage,
`practitioners/${currentUser.uid}/images/${uuidv4()}`
);
await uploadBytes(storageRef, image);

// Get the download URL of the uploaded image
const downloadURL = await getDownloadURL(storageRef);

// Add the new exercise to Firestore with the image URL
await db
.collection(`practitioners/${currentUser.uid}/exercises`)
.add({ ...formData, image: downloadURL });

handleClose();
} catch (error) {
console.error("Error adding new exercise:", error);
alert("Failed to add new exercise. Please try again.");
}
};

const handleClose = () => {
setFormData(emptyForm);
Expand Down

0 comments on commit 6fb01be

Please sign in to comment.