|
|
|
@ -18,14 +18,54 @@ const darkTheme = createMuiTheme({ |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
export default function QueryWindow(props: any) { |
|
|
|
|
interface ResultsFor { |
|
|
|
|
for: QueryElem, |
|
|
|
|
results: any[], |
|
|
|
|
}; |
|
|
|
|
export interface ResultsForQuery { |
|
|
|
|
for: QueryElem, |
|
|
|
|
results: any[], |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const [query, setQuery] = useState<QueryElem | null>(null); |
|
|
|
|
const [resultsFor, setResultsFor] = useState<ResultsFor | null>(null); |
|
|
|
|
export interface QueryWindowState { |
|
|
|
|
editingQuery: boolean, |
|
|
|
|
query: QueryElem | null, |
|
|
|
|
resultsForQuery: ResultsForQuery | null, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export enum QueryWindowStateActions { |
|
|
|
|
SetQuery = "setQuery", |
|
|
|
|
SetEditingQuery = "setEditingQuery", |
|
|
|
|
SetResultsForQuery = "setResultsForQuery", |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function QueryWindowReducer(state: QueryWindowState, action: any) { |
|
|
|
|
switch (action.type) { |
|
|
|
|
case QueryWindowStateActions.SetQuery: |
|
|
|
|
return { ...state, query: action.value } |
|
|
|
|
case QueryWindowStateActions.SetEditingQuery: |
|
|
|
|
return { ...state, editingQuery: action.value } |
|
|
|
|
case QueryWindowStateActions.SetResultsForQuery: |
|
|
|
|
return { ...state, resultsForQuery: action.value } |
|
|
|
|
default: |
|
|
|
|
throw new Error("Unimplemented QueryWindow state update.") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export interface IProps { |
|
|
|
|
state: QueryWindowState, |
|
|
|
|
dispatch: (action: any) => void |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export default function QueryWindow(props: IProps) { |
|
|
|
|
let query = props.state.query; |
|
|
|
|
let editing = props.state.editingQuery; |
|
|
|
|
let resultsFor = props.state.resultsForQuery; |
|
|
|
|
let setQuery = (q: QueryElem | null) => { |
|
|
|
|
props.dispatch({ type: QueryWindowStateActions.SetQuery, value: q }); |
|
|
|
|
} |
|
|
|
|
let setEditingQuery = (e: boolean) => { |
|
|
|
|
props.dispatch({ type: QueryWindowStateActions.SetEditingQuery, value: e }); |
|
|
|
|
} |
|
|
|
|
let setResultsForQuery = (r: ResultsForQuery | null) => { |
|
|
|
|
props.dispatch({ type: QueryWindowStateActions.SetResultsForQuery, value: r }); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const loading = query && (!resultsFor || !_.isEqual(resultsFor.for, query)); |
|
|
|
|
const showResults = (query && resultsFor && query == resultsFor.for) ? resultsFor.results : []; |
|
|
|
@ -71,7 +111,7 @@ export default function QueryWindow(props: any) { |
|
|
|
|
const response = await fetch((process.env.REACT_APP_BACKEND || "") + serverApi.QueryEndpoint, requestOpts) |
|
|
|
|
let json: any = await response.json(); |
|
|
|
|
if (_.isEqual(query, _query)) { |
|
|
|
|
setResultsFor({ |
|
|
|
|
setResultsForQuery({ |
|
|
|
|
for: _query, |
|
|
|
|
results: json.songs, |
|
|
|
|
}) |
|
|
|
@ -83,7 +123,7 @@ export default function QueryWindow(props: any) { |
|
|
|
|
if (query) { |
|
|
|
|
doQuery(query); |
|
|
|
|
} else { |
|
|
|
|
setResultsFor(null); |
|
|
|
|
setResultsForQuery(null); |
|
|
|
|
} |
|
|
|
|
}, [query]); |
|
|
|
|
|
|
|
|
@ -95,6 +135,8 @@ export default function QueryWindow(props: any) { |
|
|
|
|
<QueryBuilder |
|
|
|
|
query={query} |
|
|
|
|
onChangeQuery={setQuery} |
|
|
|
|
editing={editing} |
|
|
|
|
onChangeEditing={setEditingQuery} |
|
|
|
|
requestFunctions={{ |
|
|
|
|
getArtists: getArtists, |
|
|
|
|
getSongTitles: getSongTitles, |
|
|
|
|