have only one function to turn a query into a card.

use the appactionfactory
This commit is contained in:
Jason Wall 2020-08-23 08:27:48 -04:00
parent 17713c1bc7
commit a9436d43bb
2 changed files with 59 additions and 137 deletions

View File

@ -118,23 +118,7 @@ export class SearchPage extends SubscriberBase implements OnInit {
for (const term of terms) {
const q = term.trim();
if (q !== '') {
if (q.startsWith('note:')) {
await this.appService.findNotes(q.replace('note:', ''));
} else if (q.search(/[0-9]/i) === -1) {
// // its a search term.
await this.appService.getWords(q);
} else if (q.search(/(H|G)[0-9]/i) !== -1) {
// its a strongs lookup
const dict = q.substring(0, 1).search(/h/i) !== -1 ? 'heb' : 'grk';
const strongsNumber = q.substring(1, q.length);
await this.appService.getStrongs(strongsNumber, dict);
} else {
// its a verse reference.
if (q !== '') {
const myref = new BibleReference(q.trim());
await this.appService.getPassage(myref);
}
}
await this.appService.addCard(q);
}
}
} catch (error) {

View File

@ -51,20 +51,6 @@ export class AppService extends createStateService(reducer, initialState) {
//#region General
removeCard(card: CardItem) {
this.dispatch({
type: 'REMOVE_CARD',
card,
});
}
moveCard(card: CardItem, direction: MoveDirection) {
this.dispatch({
type: 'MOVE_CARD',
card,
direction,
});
}
dispatchError(msg: string) {
this.dispatch({
type: 'UPDATE_ERROR',
@ -84,7 +70,22 @@ export class AppService extends createStateService(reducer, initialState) {
//#endregion
async getCardByQuery(qry: string): Promise<CardItem> {
//#region Cards
removeCard(card: CardItem) {
this.dispatch(AppActionFactory.newRemoveCard(card));
}
moveCard(card: CardItem, direction: MoveDirection) {
this.dispatch(AppActionFactory.newMoveCard(card, direction));
}
async addCard(qry: string, nextToItem: CardItem = null) {
const card = await this.getCardByQuery(qry);
this.dispatch(AppActionFactory.newAddCard(card, nextToItem));
}
private async getCardByQuery(qry: string): Promise<CardItem> {
if (qry.startsWith('note:')) {
const id = qry.replace('note:', '');
const data = this.getState().notes.value.find((o) => o.id === id);
@ -120,6 +121,8 @@ export class AppService extends createStateService(reducer, initialState) {
}
}
//#endregion
//#region Saved Pages
async getSavedPage(pageid: string) {
@ -130,38 +133,25 @@ export class AppService extends createStateService(reducer, initialState) {
cards.push(await this.getCardByQuery(ref.qry));
}
this.dispatch({
type: 'UPDATE_CARDS',
cards,
});
this.dispatch(AppActionFactory.newUpdateCards(cards));
}
savePage(title: string) {
this.dispatch({
type: 'SAVE_PAGE',
title,
});
this.dispatch(AppActionFactory.newSavePage(title));
}
updateCurrentSavedPage() {
this.dispatch({
type: 'UPDATE_CURRENT_PAGE',
});
this.dispatch(AppActionFactory.newUpdateCurrentPage());
}
updateSavedPages(savedPages: IStorable<readonly SavedPage[]>) {
this.dispatch({
type: 'UPDATE_SAVED_PAGES',
savedPages,
});
this.dispatch(AppActionFactory.newUpdateSavedPages(savedPages));
}
updateSavedPage(savedPage: SavedPage) {
this.dispatch({
type: 'UPDATE_SAVED_PAGE',
savedPage,
});
this.dispatch(AppActionFactory.newUpdateSavedPage(savedPage));
}
removeSavedPage(savedPage: SavedPage) {
this.dispatch(AppActionFactory.newRemoveSavedPage(savedPage));
}
@ -176,100 +166,69 @@ export class AppService extends createStateService(reducer, initialState) {
//#endregion
//#region Saved Page Settings
//#region Settings
updateCardMergeStrategy(strategy: Overlap) {
this.dispatch({
type: 'UPDATE_CARD_MERGE_STRATEGY',
cardMergeStrategy: strategy,
});
this.dispatch(AppActionFactory.newUpdateCardMergeStrategy(strategy));
}
//#endregion
//#region Display Settings
changeCardFontFamily(cardFont: string) {
this.dispatch({
type: 'UPDATE_CARD_FONT_FAMILY',
cardFontFamily: cardFont,
});
this.dispatch(AppActionFactory.newUpdateCardFontFamily(cardFont));
}
changeCardFontSize(size: number) {
this.dispatch({
type: 'UPDATE_CARD_FONT_SIZE',
cardFontSize: size,
});
this.dispatch(AppActionFactory.newUpdateCardFontSize(size));
}
updateSettings(settings: IStorable<Settings>) {
this.dispatch({
type: 'UPDATE_SETTINGS',
settings,
});
this.dispatch(AppActionFactory.newUpdateSettings(settings));
}
updateDisplaySettings(displaySettings: DisplaySettings) {
const state = this.getState();
this.dispatch({
type: 'UPDATE_SETTINGS',
settings: new Storable<Settings>({
...state.settings.value,
displaySettings,
}),
});
this.dispatch(
AppActionFactory.newUpdateSettings(
new Storable<Settings>({
...state.settings.value,
displaySettings,
})
)
);
}
updatePageSettings(pageSettings: PageSettings) {
const state = this.getState();
this.dispatch({
type: 'UPDATE_SETTINGS',
settings: new Storable<Settings>({
...state.settings.value,
pageSettings,
}),
});
this.dispatch(
AppActionFactory.newUpdateSettings(
new Storable<Settings>({
...state.settings.value,
pageSettings,
})
)
);
}
//#endregion
//#region Notes
findNotes(qry: string, nextToItem: CardItem = null) {
this.dispatch({
type: 'FIND_NOTES',
qry,
nextToItem,
});
this.dispatch(AppActionFactory.newFindNotes(qry, nextToItem));
}
async getNote(noteId: string, nextToItem: CardItem = null) {
this.dispatch({
type: 'GET_NOTE',
noteId,
nextToItem,
});
this.dispatch(AppActionFactory.newGetNote(noteId, nextToItem));
}
updateNotes(notes: IStorable<readonly NoteItem[]>) {
this.dispatch({
type: 'UPDATE_NOTES',
notes,
});
this.dispatch(AppActionFactory.newUpdateNotes(notes));
}
saveNote(note: NoteItem, nextToItem: CardItem = null) {
this.dispatch({
type: 'SAVE_NOTE',
note,
});
saveNote(note: NoteItem) {
this.dispatch(AppActionFactory.newSaveNote(note));
}
deleteNote(note: NoteItem) {
this.dispatch({
type: 'DELETE_NOTE',
note,
});
this.dispatch(AppActionFactory.newDeleteNote(note));
}
//#endregion
@ -282,11 +241,7 @@ export class AppService extends createStateService(reducer, initialState) {
return; // nothing was returned. so an error occurred.
}
this.dispatch({
type: 'ADD_CARD',
card,
nextToItem,
});
this.dispatch(AppActionFactory.newAddCard(card, nextToItem));
}
async getStrongsCard(strongsNumber: string, dict: StrongsDictionary) {
@ -397,20 +352,13 @@ export class AppService extends createStateService(reducer, initialState) {
return; // nothing was returned. so an error occurred.
}
this.dispatch({
type: 'ADD_CARD',
card,
nextToItem,
});
this.dispatch(AppActionFactory.newAddCard(card, nextToItem));
}
async updatePassage(oldCard: CardItem, ref: BibleReference) {
const newCard = await this.composeBiblePassageCardItem(ref);
this.dispatch({
type: 'UPDATE_CARD',
oldCard,
newCard,
});
this.dispatch(AppActionFactory.newUpdateCard(oldCard, newCard));
}
private async composeBiblePassageCardItem(ref: BibleReference) {
@ -584,11 +532,7 @@ export class AppService extends createStateService(reducer, initialState) {
data: result,
} as CardItem;
this.dispatch({
type: 'ADD_CARD',
card,
nextToItem,
});
this.dispatch(AppActionFactory.newAddCard(card, nextToItem));
}
private async getWordsFromApi(qry: string): Promise<WordLookupResult> {
@ -1059,10 +1003,7 @@ export class AppService extends createStateService(reducer, initialState) {
}
}
this.dispatch({
type: 'UPDATE_AUTOCOMPLETE',
words,
});
this.dispatch(AppActionFactory.newUpdateAutocomplete(words));
} else if (qry.search(/(H|G)[0-9]/i) !== -1) {
// its a strongs lookup
if (qry.substr(0, 1).toUpperCase() === 'H') {
@ -1077,10 +1018,7 @@ export class AppService extends createStateService(reducer, initialState) {
}
}
this.dispatch({
type: 'UPDATE_AUTOCOMPLETE',
words,
});
this.dispatch(AppActionFactory.newUpdateAutocomplete(words));
}
}