Skip to content

Commit

Permalink
Merge pull request #168 from awest25/downloadData
Browse files Browse the repository at this point in the history
download functinoality for match-list
  • Loading branch information
Fredenck authored May 30, 2024
2 parents 8893b91 + f55eca4 commit 1f1a58c
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion app/(interactive)/match-list/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React, { useEffect, useState } from 'react';
import Link from 'next/link';
import { db } from '../../services/initializeFirebase.js';
import { collection, getDocs, deleteDoc, setDoc, doc } from 'firebase/firestore';
import { collection, getDoc, getDocs, deleteDoc, setDoc, doc } from 'firebase/firestore';

export default function MatchList() {
const [matchData, setMatchData] = useState([]);
Expand All @@ -24,6 +24,33 @@ export default function MatchList() {
await deleteDoc(doc(db, "matches", id));
setMatchData(matchData.filter(match => match.id !== id));
};
const handleDownload = async (id) => {
try {
const matchDoc = await getDoc(doc(db, "matches", id));
if (matchDoc.exists()) {
const matchData = matchDoc.data();
const points = matchData.points;
if (points) {
const jsonString = JSON.stringify(points, null, 2);
const blob = new Blob([jsonString], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `${matchDoc.id}_points.json`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
} else {
console.error("No points field found in document!");
}
} else {
console.error("No such document!");
}
} catch (error) {
console.error("Error fetching document: ", error);
}
};
const handleRename = async (id) => {
const docRef = doc(db, "matches", id);
setDoc(docRef, {name:newName}, { merge: true })
Expand All @@ -45,6 +72,7 @@ export default function MatchList() {
<div key={match.id}>
<li>
<span>{match.name}<button onClick={() => handleDelete(match.id)}>Delete</button></span>
<span><button onClick={() => handleDownload(match.id)}>Download</button></span>
<Link href={`/tag-match/${match.id}`}><button>Tag Match</button></Link>
<br/>
<input onChange={(e) => setNewName(e.target.value)}/>
Expand Down

0 comments on commit 1f1a58c

Please sign in to comment.