From 9e5fa87a6a9c75ad2a452385f7d4fe7666d73492 Mon Sep 17 00:00:00 2001 From: Sander Vocke Date: Sun, 12 Jul 2020 22:50:41 +0200 Subject: [PATCH] Can create artists. --- client/src/App.tsx | 64 ++++++++++++++++++++-- client/src/components/ArtistTable.tsx | 34 ++++++++++++ client/src/components/EditArtistDialog.tsx | 46 ++++++++++++++++ 3 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 client/src/components/ArtistTable.tsx create mode 100644 client/src/components/EditArtistDialog.tsx diff --git a/client/src/App.tsx b/client/src/App.tsx index 76137a7..8b1dc43 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -4,15 +4,22 @@ import { Box, Button } from '@material-ui/core'; import * as serverApi from './api'; import SongTable, { Entry as SongEntry } from './components/SongTable'; +import ArtistTable, { Entry as ArtistEntry } from './components/ArtistTable'; import EditSongDialog, { SongProperties } from './components/EditSongDialog'; +import EditArtistDialog, { ArtistProperties } from './components/EditArtistDialog'; function App() { const [songs, setSongs] = useState([]); + const [artists, setArtists] = useState([]); const [editSongDialogOpen, setEditSongDialogOpen] = useState(false); const [songDialogProperties, setSongDialogProperties] = useState({ title: "", artistId: -1, }); + const [editArtistDialogOpen, setEditArtistDialogOpen] = useState(false); + const [artistDialogProperties, setArtistDialogProperties] = useState({ + name: "" + }); const fetchSongs = () => { fetch(serverApi.ListSongsEndpoint) @@ -27,6 +34,18 @@ function App() { }); } + const fetchArtists = () => { + fetch(serverApi.ListArtistsEndpoint) + .then((response: any) => response.json()) + .then((result: serverApi.ListArtistsResponse) => { + setArtists(result.map((item: serverApi.ListArtistsResponseItem) => { + return { + name: item.name, + }; + })); + }); + } + const createSong = (p:SongProperties) => { const request:serverApi.CreateSongRequest = { title: p.title, @@ -40,7 +59,19 @@ function App() { fetch(serverApi.CreateSongEndpoint, requestOpts) } - const onClickCreateSong = () => { + const createArtist = (p:ArtistProperties) => { + const request:serverApi.CreateArtistRequest = { + name: p.name, + } + const requestOpts = { + method: 'POST', + headers: {'Content-Type': 'application/json'}, + body: JSON.stringify(request) + }; + fetch(serverApi.CreateArtistEndpoint, requestOpts) + } + + const openCreateSongDialog = () => { setSongDialogProperties({ title: "", artistId: -1, @@ -48,28 +79,53 @@ function App() { setEditSongDialogOpen(true); } - const onCreateCurrentSong = () => { + const onSubmitCreateSongDialog = () => { createSong(songDialogProperties); fetchSongs(); setEditSongDialogOpen(false); } + const openCreateArtistDialog = () => { + setArtistDialogProperties({ + name: "", + }); + setEditArtistDialogOpen(true); + } + + const onSubmitCreateArtistDialog = () => { + createArtist(artistDialogProperties); + fetchArtists(); + setEditArtistDialogOpen(false); + } + useEffect(fetchSongs, []); + useEffect(fetchArtists, []); return (
- + + + { setEditSongDialogOpen(false); }} onChangeSongProperties={(props:SongProperties) => setSongDialogProperties(props)} songProperties={songDialogProperties} - onSubmit={onCreateCurrentSong} + onSubmit={onSubmitCreateSongDialog} + /> + { setEditArtistDialogOpen(false); }} + onChangeArtistProperties={(props:ArtistProperties) => setArtistDialogProperties(props)} + artistProperties={artistDialogProperties} + onSubmit={onSubmitCreateArtistDialog} />
); diff --git a/client/src/components/ArtistTable.tsx b/client/src/components/ArtistTable.tsx new file mode 100644 index 0000000..dbcfcc7 --- /dev/null +++ b/client/src/components/ArtistTable.tsx @@ -0,0 +1,34 @@ +import React from 'react'; +import MaterialTable from 'material-table'; + +export interface Entry { + name: String +} + +export interface IProps { + artists: Entry[] +} + +export default function ArtistTable(props: IProps) { + const tableTitle = "Artists"; + const tableColumns = [ + { title: "Name", field: 'name' }, + ]; + const tableData = props.artists; + + const options = { + filtering: true, + paging: true, + pageSize: 100, + pageSizeOptions: [ 5, 10, 20, 50, 100 ] + }; + + return ( + + ); +} \ No newline at end of file diff --git a/client/src/components/EditArtistDialog.tsx b/client/src/components/EditArtistDialog.tsx new file mode 100644 index 0000000..760f5bc --- /dev/null +++ b/client/src/components/EditArtistDialog.tsx @@ -0,0 +1,46 @@ +import React from 'react'; +import { Dialog, Grid, Typography, TextField, Button } from '@material-ui/core'; + +var cloneDeep = require('lodash/cloneDeep'); + +export interface ArtistProperties { + name: String, +} + +export interface IProps { + dialogOpen: boolean, + onClose?: () => void, + onChangeArtistProperties?: (props: ArtistProperties) => void, + artistProperties: ArtistProperties, + onSubmit?: () => void, +} + +export default function EditArtistDialog(props: IProps) { + const onNameChange = (name: String) => { + if (props.onChangeArtistProperties) { + const p = cloneDeep(props.artistProperties); + p.name = name; + props.onChangeArtistProperties(p); + } + }; + + return + + Artist Details + + + + onNameChange(i.target.value)} + fullWidth + /> + + + + +} \ No newline at end of file