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

Add feature "Ask a question" using semantic search API #1652

Merged
merged 11 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion common/api-urls-constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ module.exports = {
},
CEREMONIES: '//api.sikhitothemax.org/ceremonies/',
DOODLE: '//api.sikhitothemax.org/doodle/',
WRITERS: '//api.banidb.com/v2/writers/'
WRITERS: '//api.banidb.com/v2/writers/',
GURBANIBOT: '//fastersemanticsearchapi.sevaa.win/'
};
19 changes: 10 additions & 9 deletions src/js/components/Autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { setMultipleShabads, setMultiViewPanel } from '@/features/actions';
import Larivaar from '../components/Larivaar';
import { toSearchURL } from '@/util';
import { ShabadButtonWrapper } from "./ShabadButtonWrapper";
import { SEARCH_TYPES } from "@/constants";

class Autocomplete extends Component {
static propTypes = {
Expand Down Expand Up @@ -77,10 +78,10 @@ class Autocomplete extends Component {
// Closing suggestions on mouse down
onMouseDown = e => {
e.stopPropagation();
(this.state.showSuggestions && !this.wrapperRef.current.contains(e.target)) &&
this.setState({
showSuggestions: false,
});
(this.state.showSuggestions && !this.wrapperRef.current.contains(e.target)) &&
this.setState({
showSuggestions: false,
});
Gauravjeetsingh marked this conversation as resolved.
Show resolved Hide resolved
}

componentDidMount() {
Expand Down Expand Up @@ -117,7 +118,6 @@ class Autocomplete extends Component {

getSuggestions(userInput, searchOptions)
.then(suggestions => {

// if any suggestion exists, only then add this as final result item
if (isShowFullResults && suggestions.length) {
suggestions.push({
Expand Down Expand Up @@ -168,6 +168,7 @@ class Autocomplete extends Component {
} = this;

let suggestionsListComponent;
const isChatBot = searchOptions.type === SEARCH_TYPES.ASK_A_QUESTION;

if (showSuggestions && value) {
if (filteredSuggestions.length) {
Expand Down Expand Up @@ -218,14 +219,14 @@ class Autocomplete extends Component {
{searchOptions.type === 3 ? suggestion.translation : suggestion.pankti}
</Larivaar>
{searchOptions.type === 3 && (<p className="gurbani-font">{suggestion.pankti}</p>)}
</a>
</a>
{
<div className="add-shabad-wrapper">
<ShabadButtonWrapper shabad={suggestion} />
</div>
}
}
</>
}
}
</li>
);
})}
Expand All @@ -242,7 +243,7 @@ class Autocomplete extends Component {

return (
<Fragment>
{suggestionsListComponent}
{!isChatBot && suggestionsListComponent}
Gauravjeetsingh marked this conversation as resolved.
Show resolved Hide resolved
</Fragment>
);
}
Expand Down
24 changes: 13 additions & 11 deletions src/js/components/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ class Header extends React.PureComponent {

onFormSubmit =
({ handleSubmit, ...data }) =>
(e) => {
e.preventDefault();
e.stopPropagation();
handleSubmit();
this.handleFormSubmit(data);
};
(e) => {
e.preventDefault();
e.stopPropagation();
handleSubmit();
this.handleFormSubmit(data);
};

handleFormSubmit = (data) => {
this.props.history.push(toSearchURL(data));
Expand Down Expand Up @@ -371,6 +371,7 @@ class Header extends React.PureComponent {
value={source}
onChange={handleSearchSourceChange}
className={[isSourceChanged ? 'selected' : null]}
disabled={type === SEARCH_TYPES.ASK_A_QUESTION}
>
{Object.entries(SOURCES).map(
([value, children]) => (
Expand All @@ -386,18 +387,19 @@ class Header extends React.PureComponent {
value={writer}
onChange={handleSearchWriterChange}
className={[isWriterChanged ? 'selected' : null]}
disabled={type === SEARCH_TYPES.ASK_A_QUESTION}
>
{writers
?.filter((e) =>
source === 'G' || source === 'A'
? !SOURCE_WRITER_FILTER[source].includes(
e.writerID
)
e.writerID
)
: source !== 'all'
? SOURCE_WRITER_FILTER[source].includes(
? SOURCE_WRITER_FILTER[source].includes(
e.writerID
)
: true
: true
)
.map((writer) => (
<option
Expand Down Expand Up @@ -429,7 +431,7 @@ class Header extends React.PureComponent {
}
}

const mapStateToProps = ({}) => ({});
const mapStateToProps = ({ }) => ({});

const mapDispatchToProps = {
toggleSettingsPanel,
Expand Down
38 changes: 19 additions & 19 deletions src/js/components/SearchForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export default class SearchForm extends React.PureComponent {
localStorage.getItem(LOCAL_STORAGE_KEY_FOR_SEARCH_SOURCE) ||
DEFAULT_SEARCH_SOURCE,
writer:
this.props.defaultWriter ||
this.props.defaultWriter ||
localStorage.getItem(LOCAL_STORAGE_KEY_FOR_SEARCH_WRITER) ||
DEFAULT_SEARCH_WRITER,
writers: DEFAULT_SEARCH_WRITERS,
Expand Down Expand Up @@ -155,7 +155,7 @@ export default class SearchForm extends React.PureComponent {
getWriterList()
.then(writersData => {
this._setState({ writers: writersData })
})
})
}

selectHighlight = () => {
Expand Down Expand Up @@ -345,7 +345,7 @@ export default class SearchForm extends React.PureComponent {
}
};

handleSearchSourceChange = ({target}) => {
handleSearchSourceChange = ({ target }) => {
const source = target.value
this.setState(
{
Expand All @@ -363,7 +363,7 @@ export default class SearchForm extends React.PureComponent {
localStorage.setItem(
LOCAL_STORAGE_KEY_FOR_SEARCH_SOURCE,
this.state.source
);
);
}
);
}
Expand Down Expand Up @@ -412,7 +412,7 @@ export default class SearchForm extends React.PureComponent {
);
}

handleSearchWriterChange = ({target}) => {
handleSearchWriterChange = ({ target }) => {
const writer = target.value
this.setState({
writer,
Expand All @@ -421,16 +421,16 @@ export default class SearchForm extends React.PureComponent {
this.state.query !== '',
isWriterChanged: writer !== DEFAULT_SEARCH_WRITER
},
() => {
clickEvent({
action: ACTIONS.SEARCH_WRITER,
label: this.state.writer,
});
localStorage.setItem(
LOCAL_STORAGE_KEY_FOR_SEARCH_WRITER,
this.state.writer
);
})
() => {
clickEvent({
action: ACTIONS.SEARCH_WRITER,
label: this.state.writer,
});
localStorage.setItem(
LOCAL_STORAGE_KEY_FOR_SEARCH_WRITER,
this.state.writer
);
})
}

handleReset = (e) => {
Expand All @@ -446,10 +446,10 @@ export default class SearchForm extends React.PureComponent {
this.props.submitOnChangeOf.includes('writer') &&
this.state.query !== ''
},
() => {
localStorage.setItem(LOCAL_STORAGE_KEY_FOR_SEARCH_SOURCE, this.state.source);
localStorage.removeItem(LOCAL_STORAGE_KEY_FOR_SEARCH_WRITER);
})
() => {
localStorage.setItem(LOCAL_STORAGE_KEY_FOR_SEARCH_SOURCE, this.state.source);
localStorage.removeItem(LOCAL_STORAGE_KEY_FOR_SEARCH_WRITER);
})
}

handleSubmit = () => {
Expand Down
3 changes: 3 additions & 0 deletions src/js/constants/misc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { SOURCES, SOURCES_WITH_ANG, TYPES as _TYPES } from '@sttm/banidb';
import { SEARCH_TYPES } from './search-types';

// TODO: Need to move this to @sttm/banidb module
_TYPES.push('Ask a Question (English)');

export { SOURCES, SOURCES_WITH_ANG };

export const BANI_LENGTH_COLS = {
Expand Down
3 changes: 2 additions & 1 deletion src/js/constants/placeholders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export const PLACEHOLDERS = {
[SEARCH_TYPES.ROMANIZED]: ['jo mange thakur apne te soi', true], // romanized
[SEARCH_TYPES.ANG]: ['123', true], // ang
[SEARCH_TYPES.MAIN_LETTERS]: ['j mgh Tkr Apn q'], // main letters
[SEARCH_TYPES.ROMANIZED_FIRST_LETTERS_ANYWHERE]: ['jmtatssd', true] //romanized first letter anywhere
[SEARCH_TYPES.ROMANIZED_FIRST_LETTERS_ANYWHERE]: ['jmtatssd', true], //romanized first letter anywhere
[SEARCH_TYPES.ASK_A_QUESTION]: ['What is the best time to meditate', true] // ask a question
};
3 changes: 2 additions & 1 deletion src/js/constants/search-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export const SEARCH_TYPES: ISearchTypes = {
ROMANIZED: 4,
ANG: 5,
MAIN_LETTERS: 6,
ROMANIZED_FIRST_LETTERS_ANYWHERE: 7
ROMANIZED_FIRST_LETTERS_ANYWHERE: 7,
ASK_A_QUESTION: 8
};

export const SEARCH_TYPES_NOT_ALLOWED_KEYS = {
Expand Down
5 changes: 4 additions & 1 deletion src/js/pages/Home/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class Home extends React.PureComponent {
<b className='new-text-blue'>NEW{" "}</b>
Listen to every Shabad in Sri Guru Granth Sahib in raag keertan!
</span>
</div>
</div>
</div>

<div id="search-container" className={displayGurmukhiKeyboard ? "kb-active" : ''}>
Expand Down Expand Up @@ -194,6 +194,7 @@ class Home extends React.PureComponent {
value={Object.keys(SOURCES_WITH_ANG).includes(source) ? source : 'G'}
className={[isSourceChanged ? 'selected' : null]}
onChange={handleSearchSourceChange}
disabled={type === SEARCH_TYPES.ASK_A_QUESTION}
>
{Object.entries(SOURCES_WITH_ANG).map(([value, children]) => (
<option key={value} value={value}>
Expand All @@ -207,6 +208,7 @@ class Home extends React.PureComponent {
value={source}
className={[isSourceChanged ? 'selected' : null]}
onChange={handleSearchSourceChange}
disabled={type === SEARCH_TYPES.ASK_A_QUESTION}
>
{Object.entries(SOURCES).map(([value, children]) => (
<option key={value} value={value}>
Expand All @@ -223,6 +225,7 @@ class Home extends React.PureComponent {
name="writer"
value={writer}
className={[isWriterChanged ? 'selected' : null]}
disabled={type === SEARCH_TYPES.ASK_A_QUESTION}
onChange={handleSearchWriterChange}>
{
writers?.filter(e =>
Expand Down
26 changes: 15 additions & 11 deletions src/js/pages/PageLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@ import React from 'react';
import PropTypes from 'prop-types';
import Fetch from '../components/Fetch';
import { throwError } from '../util';
import { Stub } from './Search/Layout';
export default class PageLoader extends React.PureComponent {
static propTypes = {
url: PropTypes.string.isRequired,
children: PropTypes.func.isRequired,
};
render() {
const { url, children } = this.props;
return (
<Fetch url={url}>
{({ data, error, loading }) =>
loading
? children({ loading })
: error
? throwError("Can't load page", error)
: children({ data })
}
</Fetch>
);
if (url) {
return (
<Fetch url={url}>
{({ data, error, loading }) =>
loading
? children({ loading })
: error
? throwError("Can't load page", error)
: children({ data })
}
</Fetch>
);
}
return <Stub />
}
}
Loading