Handle the result of server calls
This commit is contained in:
parent
cb2560a813
commit
414627555a
|
@ -1,3 +1,8 @@
|
||||||
|
export type Response<A> =
|
||||||
|
| { type: "Success"; content: A }
|
||||||
|
| { type: "Failure"; content: string }
|
||||||
|
| { type: "Fatal"; content: string };
|
||||||
|
|
||||||
export interface TrackInfo {
|
export interface TrackInfo {
|
||||||
id: string;
|
id: string;
|
||||||
track_number?: number;
|
track_number?: number;
|
||||||
|
@ -8,11 +13,28 @@ export interface TrackInfo {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getTracks = (): Promise<TrackInfo[]> =>
|
export const getTracks = (): Promise<TrackInfo[]> =>
|
||||||
fetch("/api/v1/tracks").then((r) => r.json());
|
fetch("/api/v1/tracks")
|
||||||
|
.then((r) => r.json())
|
||||||
|
.then((result: Response<TrackInfo[]>) => {
|
||||||
|
switch (result.type) {
|
||||||
|
case "Success":
|
||||||
|
return result.content || [];
|
||||||
|
case "Failure":
|
||||||
|
console.log("failed: ", result.content);
|
||||||
|
return [];
|
||||||
|
case "Fatal":
|
||||||
|
console.log("fatal: ", result.content);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
export const playTrack = (id: string): Promise<Response> =>
|
export const playTrack = (id: string): Promise<void> =>
|
||||||
fetch("/api/v1/play", {
|
fetch("/api/v1/play", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "content-type": "application/json" },
|
headers: { "content-type": "application/json" },
|
||||||
body: JSON.stringify({ id: id }),
|
body: JSON.stringify({ id: id }),
|
||||||
|
})
|
||||||
|
.then((r) => r.json())
|
||||||
|
.then((result: Response<null>) => {
|
||||||
|
console.log("result: ", result);
|
||||||
});
|
});
|
||||||
|
|
|
@ -16,11 +16,13 @@ use std::{
|
||||||
use warp::Filter;
|
use warp::Filter;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize)]
|
#[derive(Clone, Debug, Deserialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
struct TrackRequest {
|
struct TrackRequest {
|
||||||
id: String,
|
id: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
|
#[serde(tag = "type", content = "content")]
|
||||||
enum Response<A: Serialize> {
|
enum Response<A: Serialize> {
|
||||||
Success(A),
|
Success(A),
|
||||||
Failure(String),
|
Failure(String),
|
||||||
|
|
Loading…
Reference in New Issue