Update party template
This commit is contained in:
parent
613a8cae1f
commit
f105e36f15
@ -1,7 +1,13 @@
|
|||||||
import { APIEndPoint, PartyStatus, SelfStatus, UpdatableSelfStatus } from "./PartyContext";
|
import { APIEndPoint, PartyStatus, SelfStatus, UpdatableSelfStatus } from "./PartyContext";
|
||||||
|
|
||||||
|
let debugMode = false;
|
||||||
|
|
||||||
|
export const enableDebugMode = () => {
|
||||||
|
debugMode = true;
|
||||||
|
};
|
||||||
|
|
||||||
export const parseURI = (uri: string): APIEndPoint | undefined => {
|
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;
|
if (x === null || x.groups === undefined) return;
|
||||||
const partyName = x.groups["partyName"];
|
const partyName = x.groups["partyName"];
|
||||||
const token = x.groups["token"];
|
const token = x.groups["token"];
|
||||||
@ -9,11 +15,12 @@ export const parseURI = (uri: string): APIEndPoint | undefined => {
|
|||||||
return { partyName, token };
|
return { partyName, token };
|
||||||
};
|
};
|
||||||
|
|
||||||
const apiUrl = (apiEndPoint : APIEndPoint): string => {
|
const apiUrl = (apiEndPoint: APIEndPoint): string => {
|
||||||
return `https://party.leafbla.de/api/${apiEndPoint.partyName}/${apiEndPoint.token}`;
|
return `https://party.leafbla.de/api/${apiEndPoint.partyName}/${apiEndPoint.token}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getSelfStatusRequest = async (apiEndpoint: APIEndPoint): Promise<SelfStatus> => {
|
export const getSelfStatusRequest = async (apiEndpoint: APIEndPoint): Promise<SelfStatus> => {
|
||||||
|
if (debugMode) return debugData["me"];
|
||||||
const result = await fetch(`${apiUrl(apiEndpoint)}/me`);
|
const result = await fetch(`${apiUrl(apiEndpoint)}/me`);
|
||||||
if (!result.ok) throw new Error("Error sending getSelfRequest");
|
if (!result.ok) throw new Error("Error sending getSelfRequest");
|
||||||
const data = await result.json();
|
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> => {
|
export const getPartyStatusRequest = async (apiEndpoint: APIEndPoint): Promise<PartyStatus> => {
|
||||||
|
if (debugMode) return debugData["status"];
|
||||||
const result = await fetch(`${apiUrl(apiEndpoint)}/status`);
|
const result = await fetch(`${apiUrl(apiEndpoint)}/status`);
|
||||||
if (!result.ok) throw new Error("Error sending getPartyRequest");
|
if (!result.ok) throw new Error("Error sending getPartyRequest");
|
||||||
const data = await result.json();
|
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> => {
|
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" } });
|
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");
|
if (!result.ok) throw new Error("Error sending modifySelfRequest");
|
||||||
const data = await result.json();
|
const data = await result.json();
|
||||||
return data as SelfStatus;
|
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