-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from davewalker5/genre-editor
Added the Genre Editor
- Loading branch information
Showing
10 changed files
with
338 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/music-catalogue-ui/components/genres/deleteGenreActionIcon.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useCallback } from "react"; | ||
import { apiDeleteGenre, apiFetchGenres } from "@/helpers/api/apiGenres"; | ||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; | ||
import { faTrashAlt } from "@fortawesome/free-solid-svg-icons"; | ||
|
||
/** | ||
* Icon and associated action to delete a genre | ||
* @param {*} genre | ||
* @param {*} logout | ||
* @param {*} setGenres | ||
* @param {*} setError | ||
* @returns | ||
*/ | ||
const DeleteGenreActionIcon = ({ genre, logout, setGenres, setError }) => { | ||
/* Callback to prompt for confirmation and delete a genre */ | ||
const confirmDeleteGenre = useCallback( | ||
async (e) => { | ||
// Prevent the default action associated with the click event | ||
e.preventDefault(); | ||
|
||
// Show a confirmation message and get the user response | ||
const message = `This will delete the genre "${genre.name}" - click OK to confirm`; | ||
const result = confirm(message); | ||
|
||
// If they've confirmed the deletion ... | ||
if (result) { | ||
try { | ||
// ... delete the genre and confirm the API call was successful | ||
const result = await apiDeleteGenre(genre.id, logout); | ||
if (result) { | ||
// Successful, so refresh the genre list | ||
const fetchedGenres = await apiFetchGenres(null, logout); | ||
setGenres(fetchedGenres); | ||
} else { | ||
setError(`Failed to delete genre ${genre.name}`); | ||
} | ||
} catch (ex) { | ||
setError(`Error deleting the genre: ${ex.message}`); | ||
} | ||
} | ||
}, | ||
[genre, logout, setGenres, setError] | ||
); | ||
|
||
return ( | ||
<FontAwesomeIcon icon={faTrashAlt} onClick={(e) => confirmDeleteGenre(e)} /> | ||
); | ||
}; | ||
|
||
export default DeleteGenreActionIcon; |
101 changes: 101 additions & 0 deletions
101
src/music-catalogue-ui/components/genres/genreEditor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import styles from "./genreEditor.module.css"; | ||
import pages from "@/helpers/navigation"; | ||
import FormInputField from "../common/formInputField"; | ||
import { apiCreateGenre, apiUpdateGenre } from "@/helpers/api/apiGenres"; | ||
import { useState, useCallback } from "react"; | ||
|
||
/** | ||
* Component to render a genre editor | ||
* @param {*} genre | ||
* @param {*} navigate | ||
* @param {*} logout | ||
* @returns | ||
*/ | ||
const GenreEditor = ({ genre, navigate, logout }) => { | ||
// Setup state | ||
const initialName = genre != null ? genre.name : ""; | ||
const [name, setName] = useState(initialName); | ||
const [error, setError] = useState(""); | ||
|
||
const saveGenre = useCallback( | ||
async (e) => { | ||
// Prevent the default action associated with the click event | ||
e.preventDefault(); | ||
|
||
// Clear pre-existing errors | ||
setError(""); | ||
|
||
try { | ||
// Either add or update the genre, depending on whether there's an | ||
// existing album or not | ||
let updatedGenre = null; | ||
if (genre == null) { | ||
// Create the genre | ||
updatedGenre = await apiCreateGenre(name, logout); | ||
} else { | ||
// Update the existing genre | ||
updatedGenre = await apiUpdateGenre(genre.id, name, logout); | ||
} | ||
|
||
// Go back to the genre, which should reflect the updated details | ||
navigate({ | ||
page: pages.genres, | ||
}); | ||
} catch (ex) { | ||
setError(`Error saving the updated genre details: ${ex.message}`); | ||
} | ||
}, | ||
[genre, name, navigate, logout] | ||
); | ||
|
||
// Set the page title | ||
const pageTitle = genre != null ? `Genre - ${genre.name}` : `New Genre`; | ||
|
||
return ( | ||
<> | ||
<div className="row mb-2 pageTitle"> | ||
<h5 className="themeFontColor text-center">{pageTitle}</h5> | ||
</div> | ||
<div className={styles.genreEditorFormContainer}> | ||
<form className={styles.genreEditorForm}> | ||
<div className="row"> | ||
{error != "" ? ( | ||
<div className={styles.genreEditorError}>{error}</div> | ||
) : ( | ||
<></> | ||
)} | ||
</div> | ||
<div className="row align-items-start"> | ||
<FormInputField | ||
label="Name" | ||
name="name" | ||
value={name} | ||
setValue={setName} | ||
/> | ||
</div> | ||
<div className="d-grid gap-2 mt-3"></div> | ||
<div className="d-grid gap-2 mt-3"></div> | ||
<div className={styles.genreEditorButton}> | ||
<button className="btn btn-primary" onClick={(e) => saveGenre(e)}> | ||
Save | ||
</button> | ||
</div> | ||
<div className={styles.genreEditorButton}> | ||
<button | ||
className="btn btn-primary" | ||
onClick={() => | ||
navigate({ | ||
page: pages.genres, | ||
}) | ||
} | ||
> | ||
Cancel | ||
</button> | ||
</div> | ||
</form> | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export default GenreEditor; |
27 changes: 27 additions & 0 deletions
27
src/music-catalogue-ui/components/genres/genreEditor.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
.genreEditorFormContainer { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
} | ||
|
||
.genreEditorForm { | ||
width: 80%; | ||
padding-top: 20px; | ||
padding-bottom: 20px; | ||
} | ||
|
||
.genreEditorFormLabel { | ||
font-size: 14px; | ||
font-weight: 600; | ||
color: rgb(34, 34, 34); | ||
} | ||
|
||
.genreEditorButton { | ||
margin-left: 10px; | ||
float: right; | ||
} | ||
|
||
.genreEditorError { | ||
font-weight: bold; | ||
color: red; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/music-catalogue-ui/components/genres/genreList.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.genreListAddButton { | ||
float: right; | ||
} | ||
|
||
.genreListError { | ||
font-weight: bold; | ||
color: red; | ||
text-align: center; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.