add loading state model and component
This commit is contained in:
parent
644f839933
commit
32e520a7dd
28
src/components/LoadingComponent.tsx
Normal file
28
src/components/LoadingComponent.tsx
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import * as React from 'react';
|
||||||
|
import { FunctionComponent } from 'react';
|
||||||
|
import { Context } from '../context/context';
|
||||||
|
import { LoadingState } from '../models/LoadingState';
|
||||||
|
import { getColorByBrightness } from '../util/ColorUtil';
|
||||||
|
import CircularPanel from './CircularPanel';
|
||||||
|
|
||||||
|
const LoadingComponent: FunctionComponent<{ context: Context, loadingState: LoadingState<any> }> = ({ context, loadingState }) => {
|
||||||
|
if (loadingState.isUnset() || loadingState.isLoaded()) {
|
||||||
|
return <></>
|
||||||
|
}
|
||||||
|
if (loadingState.isLoading() || loadingState.isError()) {
|
||||||
|
const textColorOnBoard = getColorByBrightness(
|
||||||
|
context.themeManager.theme.boardColor,
|
||||||
|
context.themeManager.theme.textColor,
|
||||||
|
context.themeManager.theme.textLightColor
|
||||||
|
);
|
||||||
|
const text = loadingState.isLoading() ? context.texts.Loading +"..." : loadingState.errorMessage;
|
||||||
|
return (
|
||||||
|
<CircularPanel color={context.themeManager.theme.boardColor}>
|
||||||
|
<h4 style={{ margin: "0", color: textColorOnBoard }}>{`${text}`}</h4>
|
||||||
|
</CircularPanel>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return <></>
|
||||||
|
}
|
||||||
|
|
||||||
|
export default LoadingComponent;
|
||||||
@ -22,6 +22,8 @@ export type Texts = {
|
|||||||
PleaseWait : string,
|
PleaseWait : string,
|
||||||
GameDraw : string,
|
GameDraw : string,
|
||||||
Anonymous: string,
|
Anonymous: string,
|
||||||
|
GameNotFound: string,
|
||||||
|
Loading: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const EnUs: Texts = {
|
export const EnUs: Texts = {
|
||||||
@ -47,6 +49,8 @@ export const EnUs: Texts = {
|
|||||||
PleaseWait : "Please Wait",
|
PleaseWait : "Please Wait",
|
||||||
GameDraw : "Draw",
|
GameDraw : "Draw",
|
||||||
Anonymous: "Anonymous",
|
Anonymous: "Anonymous",
|
||||||
|
GameNotFound: "Game Not Found",
|
||||||
|
Loading: "Loading",
|
||||||
}
|
}
|
||||||
|
|
||||||
export const TrTr: Texts = {
|
export const TrTr: Texts = {
|
||||||
@ -71,5 +75,7 @@ export const TrTr: Texts = {
|
|||||||
SearchingOpponent: "Rakip Aranıyor",
|
SearchingOpponent: "Rakip Aranıyor",
|
||||||
PleaseWait: "Lütfen Bekleyin",
|
PleaseWait: "Lütfen Bekleyin",
|
||||||
GameDraw : "Berabere",
|
GameDraw : "Berabere",
|
||||||
Anonymous: "Anonim"
|
Anonymous: "Anonim",
|
||||||
|
GameNotFound: "Oyun Bulunamadı",
|
||||||
|
Loading: "Yükleniyor",
|
||||||
}
|
}
|
||||||
46
src/models/LoadingState.tsx
Normal file
46
src/models/LoadingState.tsx
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
export type LoadingStateType = "unset" | "loading" | "loaded" | "error";
|
||||||
|
|
||||||
|
export class LoadingState<T> {
|
||||||
|
state: LoadingStateType;
|
||||||
|
|
||||||
|
errorMessage?: string;
|
||||||
|
|
||||||
|
value?: T;
|
||||||
|
|
||||||
|
constructor(props: { state?: LoadingStateType, errorMessage?: string, value?: T }) {
|
||||||
|
this.state = props.state ? props.state : "unset";
|
||||||
|
this.errorMessage = props.errorMessage;
|
||||||
|
this.value = props.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Unset<T>() {
|
||||||
|
return new LoadingState<T>({ state: "unset" });
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Loading<T>() {
|
||||||
|
return new LoadingState<T>({ state: "loading" });
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Error<T>(props: { errorMessage: string }) {
|
||||||
|
const { errorMessage } = props;
|
||||||
|
return new LoadingState<T>({ state: "error", errorMessage });
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Loaded<T>(props: { value?: T }) {
|
||||||
|
const { value } = props;
|
||||||
|
return new LoadingState<T>({ state: "loaded", value });
|
||||||
|
}
|
||||||
|
|
||||||
|
public isUnset() : boolean {
|
||||||
|
return this.state === "unset";
|
||||||
|
}
|
||||||
|
public isLoading() : boolean {
|
||||||
|
return this.state === "loading";
|
||||||
|
}
|
||||||
|
public isError() : boolean {
|
||||||
|
return this.state === "error";
|
||||||
|
}
|
||||||
|
public isLoaded() : boolean {
|
||||||
|
return this.state === "loaded";
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user