parent
ffb82d6784
commit
7a137d7012
11 changed files with 316 additions and 112 deletions
@ -0,0 +1,88 @@ |
|||||||
|
import React from 'react'; |
||||||
|
|
||||||
|
import { |
||||||
|
TextField, |
||||||
|
Paper, |
||||||
|
Select, |
||||||
|
MenuItem, |
||||||
|
Typography |
||||||
|
} from '@material-ui/core'; |
||||||
|
|
||||||
|
import { |
||||||
|
TitleQuery, |
||||||
|
ArtistQuery, |
||||||
|
isTitleQuery, |
||||||
|
isArtistQuery, |
||||||
|
SongQuery |
||||||
|
} from '../types/Query'; |
||||||
|
|
||||||
|
|
||||||
|
interface TitleFilterControlProps { |
||||||
|
query: TitleQuery, |
||||||
|
onChangeQuery: (q: SongQuery) => void, |
||||||
|
} |
||||||
|
function TitleFilterControl(props: TitleFilterControlProps) { |
||||||
|
return <TextField |
||||||
|
label="Title" |
||||||
|
value={props.query.titleLike} |
||||||
|
onChange={(i: any) => props.onChangeQuery({ |
||||||
|
titleLike: i.target.value |
||||||
|
})} |
||||||
|
/> |
||||||
|
} |
||||||
|
|
||||||
|
interface ArtistFilterControlProps { |
||||||
|
query: ArtistQuery, |
||||||
|
onChangeQuery: (q: SongQuery) => void, |
||||||
|
} |
||||||
|
function ArtistFilterControl(props: ArtistFilterControlProps) { |
||||||
|
return <TextField |
||||||
|
label="Name" |
||||||
|
value={props.query.artistLike} |
||||||
|
onChange={(i: any) => props.onChangeQuery({ |
||||||
|
artistLike: i.target.value |
||||||
|
})} |
||||||
|
/> |
||||||
|
} |
||||||
|
|
||||||
|
export interface IProps { |
||||||
|
query: SongQuery, |
||||||
|
onChangeQuery: (query: SongQuery) => void, |
||||||
|
} |
||||||
|
|
||||||
|
export default function FilterControl(props: IProps) { |
||||||
|
const selectOptions: string[] = ['Title', 'Artist']; |
||||||
|
const selectOption: string = (isTitleQuery(props.query) && 'Title') || |
||||||
|
(isArtistQuery(props.query) && 'Artist') || |
||||||
|
"Unknown"; |
||||||
|
|
||||||
|
const handleQueryOnChange = (event: any) => { |
||||||
|
switch (event.target.value) { |
||||||
|
case 'Title': { |
||||||
|
props.onChangeQuery({ |
||||||
|
titleLike: '' |
||||||
|
}) |
||||||
|
break; |
||||||
|
} |
||||||
|
case 'Artist': { |
||||||
|
props.onChangeQuery({ |
||||||
|
artistLike: '' |
||||||
|
}) |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return <Paper> |
||||||
|
<Select |
||||||
|
value={selectOption} |
||||||
|
onChange={handleQueryOnChange} |
||||||
|
> |
||||||
|
{selectOptions.map((option: string) => { |
||||||
|
return <MenuItem value={option}>{option}</MenuItem> |
||||||
|
})} |
||||||
|
</Select> |
||||||
|
{isTitleQuery(props.query) && <TitleFilterControl query={props.query} onChangeQuery={props.onChangeQuery} />} |
||||||
|
{isArtistQuery(props.query) && <ArtistFilterControl query={props.query} onChangeQuery={props.onChangeQuery} />} |
||||||
|
</Paper>; |
||||||
|
} |
@ -0,0 +1,32 @@ |
|||||||
|
import { SongQueryElemProperty, SongQueryFilterOp } from '../api'; |
||||||
|
|
||||||
|
export interface TitleQuery { |
||||||
|
titleLike: String |
||||||
|
}; |
||||||
|
export function isTitleQuery(q: SongQuery): q is TitleQuery { |
||||||
|
return "titleLike" in q; |
||||||
|
} |
||||||
|
export function TitleToApiQuery(q: TitleQuery) { |
||||||
|
return { |
||||||
|
'prop': SongQueryElemProperty.title, |
||||||
|
'propOperand': '%' + q.titleLike + '%', |
||||||
|
'propOperator': SongQueryFilterOp.Like, |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export interface ArtistQuery { |
||||||
|
artistLike: String |
||||||
|
}; |
||||||
|
export function isArtistQuery(q: SongQuery): q is ArtistQuery { |
||||||
|
return "artistLike" in q; |
||||||
|
} |
||||||
|
export function ArtistToApiQuery(q: ArtistQuery) { |
||||||
|
return { |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export type SongQuery = TitleQuery | ArtistQuery; |
||||||
|
export function toApiQuery(q: SongQuery) { |
||||||
|
return (isTitleQuery(q) && TitleToApiQuery(q)) || |
||||||
|
(isArtistQuery(q) && ArtistToApiQuery(q)) || {}; |
||||||
|
} |
Loading…
Reference in new issue