Skip to content
This repository has been archived by the owner on Jul 1, 2023. It is now read-only.

Commit

Permalink
fix: fetch vocabulary for each category
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyloi committed Feb 21, 2020
1 parent bd3d8c9 commit da05992
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 143 deletions.
31 changes: 3 additions & 28 deletions packages/ulangi-action/src/ActionPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import {
SetStatus,
SyncTask,
Theme,
VocabularyDueType,
VocabularyStatus,
} from '@ulangi/ulangi-common/enums';
import {
AutoArchiveSettings,
Expand Down Expand Up @@ -262,20 +260,7 @@ export interface ActionPayload {
readonly LIBRARY__GET_PUBLIC_SET_COUNT_SUCCEEDED: { count: number };
readonly LIBRARY__GET_PUBLIC_SET_COUNT_FAILED: ErrorBag;

readonly MANAGE__PREPARE_FETCH_VOCABULARY:
| {
filterBy: 'VocabularyStatus';
setId: string;
vocabularyStatus: VocabularyStatus;
categoryName?: string;
}
| {
filterBy: 'VocabularyDueType';
setId: string;
initialInterval: number;
dueType: VocabularyDueType;
categoryName?: string;
};
readonly MANAGE__PREPARE_FETCH_VOCABULARY: VocabularyFilterCondition;
readonly MANAGE__PREPARING_FETCH_VOCABULARY: null;
readonly MANAGE__PREPARE_FETCH_VOCABULARY_SUCCEEDED: null;
readonly MANAGE__PREPARE_FETCH_VOCABULARY_FAILED: ErrorBag;
Expand All @@ -287,18 +272,8 @@ export interface ActionPayload {
};
readonly MANAGE__FETCH_VOCABULARY_FAILED: ErrorBag;
readonly MANAGE__CLEAR_FETCH_VOCABULARY: null;
readonly MANAGE__PREPARE_FETCH_CATEGORY:
| {
filterBy: 'VocabularyStatus';
setId: string;
vocabularyStatus: VocabularyStatus;
}
| {
filterBy: 'VocabularyDueType';
setId: string;
initialInterval: number;
dueType: VocabularyDueType;
};
readonly MANAGE__PREPARE_FETCH_CATEGORY: VocabularyFilterCondition;

readonly MANAGE__PREPARING_FETCH_CATEGORY: null;
readonly MANAGE__PREPARE_FETCH_CATEGORY_SUCCEEDED: null;
readonly MANAGE__PREPARE_FETCH_CATEGORY_FAILED: ErrorBag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ export class CategoryDetailScreenDelegate {
}

public prepareAndFetch(filterType: VocabularyFilterType): void {
this.vocabularyListDelegate.prepareAndFetch(
filterType,
this.vocabularyListDelegate.prepareAndFetch(filterType, [
this.observableScreen.category.categoryName,
);
]);
}

public fetch(): void {
Expand All @@ -84,10 +83,9 @@ export class CategoryDetailScreenDelegate {
}

public refresh(filterType: VocabularyFilterType): void {
this.vocabularyListDelegate.refresh(
filterType,
this.vocabularyListDelegate.refresh(filterType, [
this.observableScreen.category.categoryName,
);
]);
}

public refreshCurrentList(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
ActivityState,
VocabularyDueType,
VocabularyFilterType,
VocabularyStatus,
} from '@ulangi/ulangi-common/enums';
import { VocabularyFilterCondition } from '@ulangi/ulangi-common/types';
import {
isVocabularyDueType,
isVocabularyStatus,
Expand Down Expand Up @@ -151,23 +151,13 @@ export class CategoryListDelegate {

private createPrepareFetchPayload(
filterType: VocabularyFilterType,
):
| {
filterBy: 'VocabularyStatus';
setId: string;
vocabularyStatus: VocabularyStatus;
}
| {
filterBy: 'VocabularyDueType';
setId: string;
initialInterval: number;
dueType: VocabularyDueType;
} {
): VocabularyFilterCondition {
if (isVocabularyStatus(filterType)) {
return {
filterBy: 'VocabularyStatus',
setId: this.setStore.existingCurrentSetId,
vocabularyStatus: filterType,
categoryNames: undefined,
};
} else if (isVocabularyDueType(filterType)) {
return {
Expand All @@ -179,6 +169,7 @@ export class CategoryListDelegate {
.initialInterval
: this.writingSettingsDelegate.getCurrentSettings().initialInterval,
dueType: filterType,
categoryNames: undefined,
};
} else {
throw new Error('Invalid filter type');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
ActivityState,
VocabularyDueType,
VocabularyFilterType,
VocabularyStatus,
} from '@ulangi/ulangi-common/enums';
import { VocabularyFilterCondition } from '@ulangi/ulangi-common/types';
import {
isVocabularyDueType,
isVocabularyStatus,
Expand Down Expand Up @@ -57,14 +57,14 @@ export class VocabularyListDelegate {

public prepareAndFetch(
filterType: VocabularyFilterType,
categoryName?: string,
categoryNames?: undefined | string[],
): void {
this.eventBus.pubsub(
createAction(
this.forManageScreen
? ActionType.MANAGE__PREPARE_FETCH_VOCABULARY
: ActionType.VOCABULARY__PREPARE_FETCH,
this.createPrepareFetchPayload(filterType, categoryName),
this.createPrepareFetchPayload(filterType, categoryNames),
),
group(
on(
Expand Down Expand Up @@ -98,12 +98,12 @@ export class VocabularyListDelegate {

public refresh(
filterType: VocabularyFilterType,
categoryName?: string,
categoryNames?: undefined | string[],
): void {
this.vocabularyListState.isRefreshing.set(true);
this.vocabularyListState.shouldShowRefreshNotice.set(false);
this.clearFetch();
this.prepareAndFetch(filterType, categoryName);
this.prepareAndFetch(filterType, categoryNames);
}

public fetch(): void {
Expand Down Expand Up @@ -171,39 +171,26 @@ export class VocabularyListDelegate {

public refreshIfEmpty(
filterType: VocabularyFilterType,
categoryName?: string,
categoryNames?: undefined | string[],
): void {
if (
this.vocabularyListState.vocabularyList !== null &&
this.vocabularyListState.vocabularyList.size === 0
) {
this.refresh(filterType, categoryName);
this.refresh(filterType, categoryNames);
}
}

private createPrepareFetchPayload(
filterType: VocabularyFilterType,
categoryName?: string,
):
| {
filterBy: 'VocabularyStatus';
setId: string;
vocabularyStatus: VocabularyStatus;
categoryName: undefined | string;
}
| {
filterBy: 'VocabularyDueType';
setId: string;
initialInterval: number;
dueType: VocabularyDueType;
categoryName: undefined | string;
} {
categoryNames?: undefined | string[],
): VocabularyFilterCondition {
if (isVocabularyStatus(filterType)) {
return {
filterBy: 'VocabularyStatus',
setId: this.setStore.existingCurrentSetId,
vocabularyStatus: filterType,
categoryName,
categoryNames,
};
} else if (isVocabularyDueType(filterType)) {
return {
Expand All @@ -215,7 +202,7 @@ export class VocabularyListDelegate {
.initialInterval
: this.writingSettingsDelegate.getCurrentSettings().initialInterval,
dueType: filterType,
categoryName,
categoryNames,
};
} else {
throw new Error('Invalid filter type');
Expand Down
50 changes: 12 additions & 38 deletions packages/ulangi-saga/src/sagas/ManageSaga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@

import { SQLiteDatabase } from '@ulangi/sqlite-adapter';
import { Action, ActionType, createAction } from '@ulangi/ulangi-action';
import {
VocabularyDueType,
VocabularyStatus,
} from '@ulangi/ulangi-common/enums';
import { VocabularyDueType } from '@ulangi/ulangi-common/enums';
import { Category } from '@ulangi/ulangi-common/interfaces';
import { VocabularyFilterCondition } from '@ulangi/ulangi-common/types';
import {
CategoryModel,
SpacedRepetitionModel,
Expand Down Expand Up @@ -145,20 +143,7 @@ export class ManageSaga extends ProtectedSaga {
}

private *allowFetchVocabulary(
payload:
| {
filterBy: 'VocabularyStatus';
setId: string;
vocabularyStatus: VocabularyStatus;
categoryName?: string;
}
| {
filterBy: 'VocabularyDueType';
setId: string;
initialInterval: number;
dueType: VocabularyDueType;
categoryName?: string;
},
payload: VocabularyFilterCondition,
limit: number,
spacedRepetitionMaxLevel: number,
writingMaxLevel: number
Expand All @@ -177,20 +162,20 @@ export class ManageSaga extends ProtectedSaga {
>
>;
if (payload.filterBy === 'VocabularyStatus') {
const { setId, vocabularyStatus, categoryName } = payload;
const { setId, vocabularyStatus, categoryNames } = payload;

result = yield call(
[this.vocabularyModel, 'getVocabularyList'],
this.userDb,
setId,
vocabularyStatus,
typeof categoryName !== 'undefined' ? [categoryName] : undefined,
typeof categoryNames !== 'undefined' ? categoryNames : undefined,
limit,
offset,
true
);
} else {
const { setId, initialInterval, dueType, categoryName } = payload;
const { setId, initialInterval, dueType, categoryNames } = payload;

if (dueType === VocabularyDueType.DUE_BY_SPACED_REPETITION) {
result = yield call(
Expand All @@ -199,7 +184,7 @@ export class ManageSaga extends ProtectedSaga {
setId,
initialInterval,
spacedRepetitionMaxLevel,
typeof categoryName !== 'undefined' ? [categoryName] : undefined,
typeof categoryNames !== 'undefined' ? categoryNames : undefined,
limit,
offset,
true
Expand All @@ -211,7 +196,7 @@ export class ManageSaga extends ProtectedSaga {
setId,
initialInterval,
writingMaxLevel,
typeof categoryName !== 'undefined' ? [categoryName] : undefined,
typeof categoryNames !== 'undefined' ? categoryNames : undefined,
limit,
offset,
true
Expand Down Expand Up @@ -321,18 +306,7 @@ export class ManageSaga extends ProtectedSaga {
}

private *allowFetchCategory(
payload:
| {
filterBy: 'VocabularyStatus';
setId: string;
vocabularyStatus: VocabularyStatus;
}
| {
filterBy: 'VocabularyDueType';
setId: string;
initialInterval: number;
dueType: VocabularyDueType;
},
condition: VocabularyFilterCondition,
limitOfCategorized: number,
spacedRepetitionMaxLevel: number,
writingMaxLevel: number
Expand All @@ -346,8 +320,8 @@ export class ManageSaga extends ProtectedSaga {
yield put(createAction(ActionType.MANAGE__FETCHING_CATEGORY, null));

let categoryList;
if (payload.filterBy === 'VocabularyStatus') {
const { setId, vocabularyStatus } = payload;
if (condition.filterBy === 'VocabularyStatus') {
const { setId, vocabularyStatus } = condition;

const result: PromiseType<
ReturnType<CategoryModel['getCategoryListByVocabularyStatus']>
Expand All @@ -363,7 +337,7 @@ export class ManageSaga extends ProtectedSaga {

categoryList = result.categoryList;
} else {
const { setId, initialInterval, dueType } = payload;
const { setId, initialInterval, dueType } = condition;
if (dueType === VocabularyDueType.DUE_BY_SPACED_REPETITION) {
const result: PromiseType<
ReturnType<SpacedRepetitionModel['getDueCategoryList']>
Expand Down
Loading

0 comments on commit da05992

Please sign in to comment.