Update party template
This commit is contained in:
parent
613a8cae1f
commit
f105e36f15
@ -1,7 +1,13 @@
|
||||
import { APIEndPoint, PartyStatus, SelfStatus, UpdatableSelfStatus } from "./PartyContext";
|
||||
|
||||
let debugMode = false;
|
||||
|
||||
export const enableDebugMode = () => {
|
||||
debugMode = true;
|
||||
};
|
||||
|
||||
export const parseURI = (uri: string): APIEndPoint | undefined => {
|
||||
const x = uri.match(/https?:\/\/(?<partyName>.+)\.party\.leafbla\.de\/(?<token>.+)/);
|
||||
const x = uri.match(/https?:\/\/(?<partyName>\w+)\.(?<host>.+)\/(?<token>.+)/);
|
||||
if (x === null || x.groups === undefined) return;
|
||||
const partyName = x.groups["partyName"];
|
||||
const token = x.groups["token"];
|
||||
@ -9,11 +15,12 @@ export const parseURI = (uri: string): APIEndPoint | undefined => {
|
||||
return { partyName, token };
|
||||
};
|
||||
|
||||
const apiUrl = (apiEndPoint : APIEndPoint): string => {
|
||||
const apiUrl = (apiEndPoint: APIEndPoint): string => {
|
||||
return `https://party.leafbla.de/api/${apiEndPoint.partyName}/${apiEndPoint.token}`;
|
||||
};
|
||||
|
||||
export const getSelfStatusRequest = async (apiEndpoint: APIEndPoint): Promise<SelfStatus> => {
|
||||
if (debugMode) return debugData["me"];
|
||||
const result = await fetch(`${apiUrl(apiEndpoint)}/me`);
|
||||
if (!result.ok) throw new Error("Error sending getSelfRequest");
|
||||
const data = await result.json();
|
||||
@ -21,6 +28,7 @@ export const getSelfStatusRequest = async (apiEndpoint: APIEndPoint): Promise<Se
|
||||
};
|
||||
|
||||
export const getPartyStatusRequest = async (apiEndpoint: APIEndPoint): Promise<PartyStatus> => {
|
||||
if (debugMode) return debugData["status"];
|
||||
const result = await fetch(`${apiUrl(apiEndpoint)}/status`);
|
||||
if (!result.ok) throw new Error("Error sending getPartyRequest");
|
||||
const data = await result.json();
|
||||
@ -28,8 +36,23 @@ export const getPartyStatusRequest = async (apiEndpoint: APIEndPoint): Promise<P
|
||||
};
|
||||
|
||||
export const modifySelfRequest = async (apiEndpoint: APIEndPoint, payload: UpdatableSelfStatus): Promise<SelfStatus> => {
|
||||
if (debugMode) return debugData["me"];
|
||||
const result = await fetch(`${apiUrl(apiEndpoint)}/me`, { method: "PATCH", body: JSON.stringify(payload), headers: { "Content-Type": "application/json" } });
|
||||
if (!result.ok) throw new Error("Error sending modifySelfRequest");
|
||||
const data = await result.json();
|
||||
return data as SelfStatus;
|
||||
};
|
||||
|
||||
const debugData: { me: SelfStatus, status: PartyStatus } = {
|
||||
me: {
|
||||
coming: "maybe",
|
||||
grammatical_gender: "f",
|
||||
name: "Test user",
|
||||
token: "testusertoken",
|
||||
extra: {},
|
||||
},
|
||||
status: {
|
||||
definitely_coming: 10,
|
||||
maybe_coming: 5,
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user