Skip to content

Commit

Permalink
Supports new data version 1, pdf upload optional
Browse files Browse the repository at this point in the history
  • Loading branch information
awest25 committed Jan 20, 2024
1 parent 1450a1d commit e7e8b2a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 8 deletions.
5 changes: 4 additions & 1 deletion components/SearchDropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Select from 'react-select';
import { collection, getDocs } from 'firebase/firestore';
import { db } from '../services/initializeFirebase.js';
import styles from '../styles/SearchDropdown.module.css';
import transformData from '../services/transformData.js';

const SearchDropdown = ({ setMatchData }) => {
const [searchTerm, setSearchTerm] = useState('');
Expand All @@ -20,7 +21,9 @@ const SearchDropdown = ({ setMatchData }) => {
}, []);

const handleDropdownItemClick = (selectedOption) => {
setMatchData(selectedOption.value);
let matchData = transformData(selectedOption.value);
console.log(matchData.points);
setMatchData(matchData);
setSearchTerm(selectedOption);
};

Expand Down
4 changes: 2 additions & 2 deletions pages/upload-video.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ export default function UploadVideo() {
setJsonFile(e.target.files[0]);
};

const handlePdfFileChange = (e) => { // New handler for PDF file
const handlePdfFileChange = (e) => {
setPdfFile(e.target.files[0]);
};

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

if (!matchName || !videoId || !jsonFile || !pdfFile) {
if (!matchName || !videoId || !jsonFile) {
console.error("Please fill in all fields.");
return;
}
Expand Down
53 changes: 53 additions & 0 deletions services/transformData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// This takes in matchData
function transformData(matchData) {
let pointsData = matchData.points;
let dataVersion = -1;

// This is the original Dartfish format we used.
if (pointsData[0].Position) {
dataVersion = 0;
}
// This is the new format Leo made.
else if (pointsData[0].serverName) {
dataVersion = 1;
}

if (dataVersion === 0) {
// Do nothing, this is the original format
}
else if (dataVersion === 1) {
matchData.points = transformDataVersion1(pointsData);
}
return matchData;
}

function transformDataVersion1(pointsData) {
const pointsArray = [];
let currentPoint = {};
let shotCounter = 1;

pointsData.forEach((shot, index) => {
// Prefix each key with "Shot N:" and add to currentPoint, with special handling for certain keys
for (let key in shot) {
let newKey = `Shot ${shotCounter}: ${key}`;
if (shotCounter === 1) {
if (key === 'isPointStartTime') newKey = 'Position';
else if (key === 'pointScore') newKey = 'Name';
}
currentPoint[newKey] = shot[key];
}

shotCounter++;

// If it's the last shot in the data or the end of a point
if (index === pointsData.length - 1 || shot.isPointEnd === 1) {
pointsArray.push(currentPoint);
currentPoint = {};
shotCounter = 1;
}
});

return pointsArray;
}

export default transformData;
13 changes: 8 additions & 5 deletions services/uploadMatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ import { db, storage } from '../services/initializeFirebase.js'; // Ensure stora

// Define the uploadMatch function
async function uploadMatch(matchName, videoId, pointsJson, pdfFile) {
if (!matchName || !videoId || !pointsJson || !pdfFile) {
if (!matchName || !videoId || !pointsJson) {
console.error("All fields are required.");
return; // Exit the function if any field is empty
}

try {
// First, upload the PDF to Firebase Storage
const pdfRef = ref(storage, `match-pdfs/${pdfFile.name}`);
const snapshot = await uploadBytes(pdfRef, pdfFile);
const pdfUrl = await getDownloadURL(snapshot.ref);
let pdfUrl = null;
if (pdfFile) {
// First, upload the PDF to Firebase Storage
const pdfRef = ref(storage, `match-pdfs/${pdfFile.name}`);
const snapshot = await uploadBytes(pdfRef, pdfFile);
pdfUrl = await getDownloadURL(snapshot.ref);
}

// Then, save the match data along with the PDF URL to Firestore
const docRef = await addDoc(collection(db, "matches"), {
Expand Down

0 comments on commit e7e8b2a

Please sign in to comment.