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:
Alexander Polynomdivision
2018-10-17 18:28:29 +02:00
parent 7de3cedb15
commit 3b7e55d957
14 changed files with 410 additions and 234 deletions

View File

@@ -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]);

View File

@@ -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);
});
}

View File

@@ -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;