refactor: Simplify API calls
API calls can now make with a simple wrapper function. Additionally, the error code "200" now means success for all API calls.
This commit is contained in:
@@ -28,7 +28,7 @@ import { IVocab, VocabType } from "../models/vocab";
|
||||
|
||||
interface IProps {
|
||||
id: string;
|
||||
levelVocab: (id: string) => Promise<IVocab[]>;
|
||||
levelVocab: (id: string) => Promise<any>;
|
||||
|
||||
history: any;
|
||||
|
||||
@@ -64,7 +64,8 @@ const LevelPageWithRouter = withRouter(
|
||||
this.props.setLoading(true);
|
||||
|
||||
// TODO: Error handling
|
||||
this.props.levelVocab(this.props.id).then(vocab => {
|
||||
this.props.levelVocab(this.props.id).then(data => {
|
||||
const { vocab } = data;
|
||||
this.props.setVocab(vocab);
|
||||
this.props.setCurrentVocab(vocab[0]);
|
||||
this.props.setLookedAt([vocab[0].id]);
|
||||
|
||||
@@ -6,7 +6,6 @@ import Button from "@material-ui/core/Button";
|
||||
import Card from '@material-ui/core/Card';
|
||||
import CardActions from '@material-ui/core/CardActions';
|
||||
import CardContent from '@material-ui/core/CardContent';
|
||||
import Paper from "@material-ui/core/Paper";
|
||||
import Snackbar from "@material-ui/core/Snackbar";
|
||||
|
||||
import Loader from "../components/loading";
|
||||
@@ -17,7 +16,7 @@ import { ILevel } from "../models/level";
|
||||
import { IUser } from "../models/user";
|
||||
|
||||
interface IProps {
|
||||
getLevels: () => Promise<ILevel[]>;
|
||||
getLevels: () => Promise<any>;
|
||||
|
||||
history: any;
|
||||
|
||||
@@ -37,7 +36,7 @@ const LevelListWithRouter = withRouter(
|
||||
|
||||
// Fetch the levels
|
||||
this.props.getLevels().then(res => {
|
||||
this.props.setLevels(res);
|
||||
this.props.setLevels(res.levels);
|
||||
this.props.setLoading(false);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -40,8 +40,8 @@ import { Queue } from "../utils/queue";
|
||||
|
||||
interface IProps {
|
||||
levelId?: number;
|
||||
vocabByLevel?: (level: number) => Promise<IVocab[]>;
|
||||
vocabByQueue?: () => Promise<IVocab[]>;
|
||||
vocabByLevel?: (level: number) => Promise<any>;
|
||||
vocabByQueue?: () => Promise<any>;
|
||||
updateDoneLevels?: (id: string) => void;
|
||||
setLastReview: (meta: IReviewMetadata, sm2: any, delta: number) => void;
|
||||
reviewType: ReviewType;
|
||||
@@ -110,19 +110,19 @@ const ReviewPageWithRouter = withRouter(
|
||||
// Track the start of a session
|
||||
trackAction(TrackerEvent.START_LEARNING);
|
||||
|
||||
getVocab().then((res: IVocab[]) => {
|
||||
getVocab().then((res: any) => {
|
||||
// Check if we received any vocabulary
|
||||
if (res.length === 0) {
|
||||
if (res.vocab.length === 0) {
|
||||
this.openModal();
|
||||
return;
|
||||
}
|
||||
|
||||
// Stop the loading
|
||||
this.props.setLoading(false);
|
||||
this.vocab = res;
|
||||
this.vocab = res.vocab;
|
||||
|
||||
// Convert the vocab items into review queue cards
|
||||
res.forEach(vocab => {
|
||||
res.vocab.forEach((vocab: IVocab) => {
|
||||
// Set the error data for the group
|
||||
this.error_data[vocab.id] = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user