Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update base project by adding add chart route in admin #3

Merged
merged 1 commit into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Update base project by adding add chart route in admin
  • Loading branch information
nesmon committed Mar 22, 2023
commit c8730b04f6b9b05537c8d2bbbeea597a71396cc2
2 changes: 0 additions & 2 deletions src/Components/addChart/addChart.js

This file was deleted.

4 changes: 2 additions & 2 deletions src/Components/chart/chart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Chart = ({ data }) => {
return (
<div className="chart">
<div className='song'>
<img className='chartImg inline' src={data.imageCover} alt={data.name} width="80%"/>
<img className='chartImg inline' src={data.imageCover} alt={data.title} width="80%"/>
<div className='difficulty'>
{data.difficulty.map((item, index) => {
return (
Expand All @@ -21,7 +21,7 @@ const Chart = ({ data }) => {
</div>
</div>
<div className='songInfo'>
<div>{data.name}</div>
<div>{data.title}</div>
<div>{data.artist}</div>
</div>
</div>
Expand Down
95 changes: 95 additions & 0 deletions src/Route/Admin/AddChart/AddChart.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { getFirestore, collection, addDoc } from 'firebase/firestore';
import './AddChart.css';

function AddChart() {
function handleSubmit(e) {
e.preventDefault();

const form = e.target;
const formData = new FormData(form);

let date = new Date();
let id = `${formData.get('title').slice(0, 3).toLowerCase()}` +
`${formData.get('title').length}` +
`${formData.get('artist').length}` +
`${formData.get('artist').slice(0, 3).toLowerCase()}` +
`${formData.get('category').length}`;

const formatedData = {
"id": id,
"title": formData.get('title'),
"artist": formData.get('artist'),
"category": formData.get('category'),
"difficulty": [
{
"Easy": formData.get('easy'),
"Basic": formData.get('basic'),
"Advance": formData.get('advance'),
"Expert": formData.get('expert'),
"Master": formData.get('master'),
"Remaster": formData.get('remaster'),
}
],
"imageCover": formData.get('image'),
}

const db = getFirestore();
addDoc(collection(db, "Chart"), formatedData).then(() => {
console.log("Document successfully written!");
window.location.href = "/charts";
}).catch((error) => {
console.error("Error writing document: ", error);
});
}


return (
<div className="addChart">
<h1>Add Chart</h1>
<form method="post" onSubmit={handleSubmit}>
<div>
<label htmlFor="title">Title</label>
<input type="text" name="title" id="title" />
</div>
<div>
<label htmlFor="artist">Artist</label>
<input type="text" name="artist" id="artist" />
</div>
<div>
<label htmlFor="category">Category</label>
<select name="category" id="category">
<option value="pop">Pops & Anime</option>
<option value="niconico">niconico & Vocaloid</option>
<option value="touhou">Touhou Project</option>
<option value="game">Game & Variety</option>
<option value="maimai">Maimai</option>
<option value="original">Original & Joypolis</option>
<option value="utage">Utage</option>
</select>
</div>
<div>
<p>Difficulty: </p>
<label htmlFor="easy">Easy</label>
<input type="string" name="easy" id="easy" />
<label htmlFor="basic">Basic</label>
<input type="string" name="basic" id="basic" />
<label htmlFor="advance">Advance</label>
<input type="string" name="advance" id="advance" />
<label htmlFor="expert">Expert</label>
<input type="string" name="expert" id="expert" />
<label htmlFor="master">Master</label>
<input type="string" name="master" id="master" />
<label htmlFor="remaster">Remaster</label>
<input type="string" name="remaster" id="remaster" />
</div>
<div>
<label htmlFor="image">Image Cover</label>
<input type="string" name="image" id="image" />
</div>
<button type="submit">Submit</button>
</form>
</div>
);
}

export default AddChart;
2 changes: 1 addition & 1 deletion src/Route/Charts/Charts.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function Charts() {
<div className="songList">
{charts.map((item, index) => {
return (
<Link to={`/score/${item.name}`} key={index}>
<Link to={`/charts/${item.id}/score`} key={index}>
<Chart key={index} data={item} />
</Link>
)
Expand Down
7 changes: 6 additions & 1 deletion src/router.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Index from './Route/Index/Index';
import Charts from './Route/Charts/Charts';
import ChartScore from './Route/ChartScore/ChartScore';
import AddChart from './Route/Admin/AddChart/AddChart';
import NotFound from './Route/NotFound/NotFound';

const router = [
Expand All @@ -13,9 +14,13 @@ const router = [
element: <Charts />,
},
{
path: "/charts/:chartId",
path: "/charts/:chartId/score",
element: <ChartScore />,
},
{
path: "/admin/charts/add",
element: <AddChart />,
},
{
path: "*",
element: <NotFound />,
Expand Down