Merge pull request #74 from Chroma-Case/front/api-wrapper

Front/api wrapper
This commit is contained in:
Arthur Jamet
2022-09-09 16:41:10 +02:00
committed by GitHub
15 changed files with 263 additions and 2 deletions
+135
View File
@@ -0,0 +1,135 @@
import AuthToken from "./models/AuthToken";
import Chapter from "./models/Chapter";
import Lesson from "./models/Lesson";
import LessonHistory from "./models/LessonHistory";
import Song from "./models/Song";
import SongHistory from "./models/SongHistory";
import User from "./models/User";
declare type AuthenticationInput = { email: string, password: string };
export default class API {
/***
* Retrieve information of the currently authentified user
*/
static async getUserInfo(): Promise<User> {
return {
name: "User",
email: "user@chromacase.com",
xp: 0,
premium: false,
metrics: {},
settings: {},
id: 1
}
}
/**
* Logs the user in, with an email and a password
* @param _credentials the credentials to get an authentication token
* @returns an authentication token, that must be used for authentified requests
*/
static async login(_credentials: AuthenticationInput): Promise<AuthToken> {
return "12345";
}
/**
* Create a new user profile, with an email and a password
* @param _credentials the credentials to create a new profile
* @returns an empty promise. On error, the promise will not be resolved
*/
static async register(_credentials: AuthenticationInput): Promise<void> {
return;
}
/**
* Authentify a new user through Google
*/
static async authWithGoogle(): Promise<AuthToken> {
return "11111";
}
/**
* Retrive a song
* @param songId the id to find the song
*/
static async getSong(songId: number): Promise<Song> {
return {
title: "Song",
description: "A song",
album: "Album",
metrics: {},
id: songId
};
}
/**
* Retrive a song's chapters
* @param songId the id to find the song
*/
static async getSongChapters(songId: number): Promise<Chapter[]> {
return [{
start: 0,
end: 100,
songId: songId,
name: "Chapter",
type: "chorus",
key_aspect: "rhythm",
difficulty: 1,
id: 1
}];
}
/**
* Retrieve a song's play history
* @param songId the id to find the song
*/
static async getSongHistory(songId: number): Promise<SongHistory[]> {
return [{
songId: songId,
userId: 1,
score: 2
}];
}
/**
* Search a song by its name
* @param query the string used to find the songs
*/
static async searchSongs(query: string): Promise<Song[]> {
return [{
title: "Song",
description: "A song",
album: "Album",
metrics: {},
id: 1
}];
}
/**
* Retrieve a lesson
* @param lessonId the id to find the lesson
*/
static async getLesson(lessonId: number): Promise<Lesson> {
return {
title: "Song",
description: "A song",
requiredLevel: 1,
mainSkill: 'lead-head-change',
id: lessonId
};
}
/**
* Retrieve a lesson's history
* @param lessonId the id to find the lesson
*/
static async getLessonHistory(lessonId: number): Promise<LessonHistory[]> {
return [{
lessonId,
userId: 1
}];
}
}
+7
View File
@@ -0,0 +1,7 @@
import Model from "./Model";
interface Artist extends Model {
name: string;
}
export default Artist;
+3
View File
@@ -0,0 +1,3 @@
type AuthToken = string;
export default AuthToken;
+14
View File
@@ -0,0 +1,14 @@
import Skill from "./Skill";
import Model from "./Model";
interface Chapter extends Model {
start: number;
end: number;
songId: number;
name: string;
type: 'chorus' | 'verse';
key_aspect: Skill;
difficulty: number
}
export default Chapter;
+7
View File
@@ -0,0 +1,7 @@
import Model from "./Model";
interface Genre extends Model {
name: string;
}
export default Genre;
+26
View File
@@ -0,0 +1,26 @@
import Skill from "./Skill";
import Model from "./Model";
/**
* A Lesson is an exercice that the user can try to practice a skill
*/
interface Lesson extends Model {
/**
* The title of the lesson
*/
title: string,
/**
* Short description of the lesson
*/
description: string;
/**
* The minimum level required for the user to access this lesson
*/
requiredLevel: number;
/**
* The main skill learnt in this lesson
*/
mainSkill: Skill;
}
export default Lesson;
+6
View File
@@ -0,0 +1,6 @@
interface LessonHistory {
lessonId: number;
userId: number
}
export default LessonHistory;
+5
View File
@@ -0,0 +1,5 @@
interface Metrics {
}
export default Metrics;
+5
View File
@@ -0,0 +1,5 @@
interface Model {
id: number;
}
export default Model;
+15
View File
@@ -0,0 +1,15 @@
type Skill = 'rhythm'
| 'two-hands'
| 'combos'
| 'arpeggio'
| 'distance'
| 'left-hand'
| 'right-hand'
| 'lead-head-change'
| 'chord-complexity'
| 'chord-timing'
| 'pedal'
| 'precision'
export default Skill;
+11
View File
@@ -0,0 +1,11 @@
import Metrics from "./Metrics";
import Model from "./Model";
interface Song extends Model {
title: string;
description: string;
album: string
metrics: Metrics;
}
export default Song;
+7
View File
@@ -0,0 +1,7 @@
interface LessonHistory {
songId: number;
userId: number;
score: number;
}
export default LessonHistory;
+14
View File
@@ -0,0 +1,14 @@
import Metrics from "./Metrics";
import Model from "./Model";
import UserSettings from "./UserSettings";
interface User extends Model {
name: string;
email: string;
xp: number;
premium: boolean;
metrics: Metrics;
settings: UserSettings;
}
export default User;
+5
View File
@@ -0,0 +1,5 @@
interface UserSettings {
}
export default UserSettings
+3 -2
View File
@@ -1,12 +1,13 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import AuthToken from '../models/AuthToken';
export const userSlice = createSlice({
name: 'user',
initialState: {
token: undefined as string | undefined
token: undefined as AuthToken | undefined
},
reducers: {
setUserToken: (state, action: PayloadAction<string>) => {
setUserToken: (state, action: PayloadAction<AuthToken>) => {
state.token = action.payload;
},
unsetUserToken: (state) => {