Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmad-masud committed Nov 11, 2024
1 parent aa3e3f5 commit abe8c5c
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions src/components/solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,66 +20,69 @@ const Solution = ({ problemId, onClose, theme }) => {
const fetchSolutions = async () => {
setLoading(true);
setFailed(false);
setSolutions([]);

setSolutions([]);
setVideoUrl('');

try {

const headers = {
'Authorization': `token ${process.env.REACT_APP_GITHUB_TOKEN}`,
'Accept': 'application/vnd.github.v3.raw'
'Authorization': `token ${process.env.REACT_APP_GITHUB_TOKEN}`,
'Accept': 'application/vnd.github.v3.raw'
};


const response = await axios.get(`https://api.github.com/repos/ahmad-masud/CompCode-Solutions/contents/${problemId}`, {
headers
});

if (response.status !== 200) {
addAlert('Failed to fetch solutions', 'error');
setFailed(true);
return;
return;
}

const data = response.data;

try {
const videoResponse = await axios.get(`https://raw.githubusercontent.com/ahmad-masud/CompCode-Solutions/main/${problemId}/video.txt`);
setVideoUrl(videoResponse.data.trim()); // Set the video URL (assumes plain text)
} catch (videoError) {
console.warn(`No video.txt found for problem ${problemId}:`, videoError.message);
}

const solutionPromises = data.map(async (solution) => {
if (solution.type === 'dir') {
try {
const infoResponse = await axios.get(`https://raw.githubusercontent.com/ahmad-masud/CompCode-Solutions/main/${problemId}/${solution.name}/info.json`);
const infoData = infoResponse.data;
const codeResponse = await axios.get(`https://raw.githubusercontent.com/ahmad-masud/CompCode-Solutions/main/${problemId}/${solution.name}/code.py`);
const codeData = codeResponse.data;

if (!videoUrl && infoData.video) {
setVideoUrl(infoData.video);
}


return {
...infoData,
code: codeData,
};
} catch (innerError) {
console.error(`Error fetching data for solution ${solution.name}:`, innerError);
return null;
return null;
}
}
return null;
});

const loadedSolutions = await Promise.all(solutionPromises);
setSolutions(loadedSolutions.filter((sol) => sol !== null));
} catch (err) {
console.error('Error fetching solutions:', err);
addAlert('Not available yet', 'warning');
setFailed(true);
} finally {
setLoading(false);
setLoading(false);
}
};

if (problemId) {
fetchSolutions();
}
}, [problemId, addAlert, videoUrl]);
}, [problemId, addAlert]);

const handleCopy = (index) => {
setCopiedIndex(index);
Expand Down

0 comments on commit abe8c5c

Please # to comment.