Skip to content
This repository was archived by the owner on Dec 13, 2023. It is now read-only.

Commit 25f8822

Browse files
author
Marcelo
committed
feature: added a error store
1 parent f4dfea8 commit 25f8822

File tree

9 files changed

+60
-23
lines changed

9 files changed

+60
-23
lines changed

src/client/modules/hello_world/ui/crud/CreateAnimal.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import "./main.scss";
1010
import { rootStoreContext } from "../../../../stores/RootStore";
1111

1212
export const CreateAnimal: React.FunctionComponent = observer(() => {
13-
const { animalsStore } = React.useContext(rootStoreContext);
13+
const [submitting, setSubmitting] = React.useState(false);
14+
const { animalsStore, errorStore } = React.useContext(rootStoreContext);
1415

1516
return (
1617
<Mutation<CreateAnimalMutation, CreateAnimalVariables>
@@ -40,15 +41,20 @@ export const CreateAnimal: React.FunctionComponent = observer(() => {
4041
<button
4142
className="button"
4243
onClick={async () => {
44+
setSubmitting(true);
4345
await mutate({
4446
variables: {
4547
species: animalsStore.species,
4648
favoriteFood: animalsStore.favoriteFood
4749
}
48-
});
50+
})
51+
.catch(error => errorStore.setErrorMessage(error.message))
52+
.finally(() => {
53+
setSubmitting(false);
54+
});
4955
}}
5056
>
51-
Submit
57+
<span>{submitting ? "Submitting..." : "Submit"}</span>
5258
</button>
5359
</div>
5460
</form>

src/client/modules/hello_world/ui/index.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { inject, observer } from "mobx-react";
22
import * as React from "react";
3-
import { AnimalsStore } from "../../../stores/AnimalsStore";
3+
import { AnimalsStore, ErrorStore } from "../../../stores";
44
import { CreateAnimal } from "./crud/CreateAnimal";
55
import { ListAnimals } from "./crud/ListAnimals";
66
import "./main.scss";
7+
import { ErrorMessage } from "./misc/ErrorMessage";
78

89
interface Props {
910
animalsStore?: AnimalsStore;
11+
errorStore?: ErrorStore;
1012
}
1113

12-
@inject("animalsStore")
14+
@inject("animalsStore", "errorStore")
1315
@observer
1416
export class HelloWorld extends React.Component<Props> {
1517
private handleCreate = () => {
@@ -27,6 +29,9 @@ export class HelloWorld extends React.Component<Props> {
2729
public render() {
2830
return (
2931
<>
32+
{this.props.errorStore.errorMessage && (
33+
<ErrorMessage errorMessage={this.props.errorStore.errorMessage} />
34+
)}
3035
{this.props.animalsStore.helloworld && (
3136
<>
3237
<div className="hello-world">
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as React from "react";
2+
3+
interface Props {
4+
errorMessage: string;
5+
}
6+
7+
export const ErrorMessage: React.FC<Props> = ({ errorMessage }) => {
8+
return (
9+
<div className="error-wrapper">
10+
<span>{errorMessage}</span>
11+
</div>
12+
);
13+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.error-wrapper {
2+
position: absolute;
3+
top: 0;
4+
right: 0;
5+
left: 0;
6+
text-align: center;
7+
height: 40px;
8+
}

src/client/stores/AnimalsStore.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ export class AnimalsStore {
2121
@observable public delete = false;
2222

2323
@action public handleMutation = () => {
24-
if (this.mutation) {
25-
this.mutation = true;
26-
}
24+
this.mutation = true;
2725
};
2826
@action public handleCreate = () => {
2927
this.create = true;

src/client/stores/ErrorStore.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { action, observable } from "mobx";
2+
import { RootStore } from "./RootStore";
3+
4+
export class ErrorStore {
5+
protected rootStore: RootStore;
6+
7+
public constructor(rootStore: RootStore) {
8+
this.rootStore = rootStore;
9+
}
10+
11+
@observable public errorMessage = "";
12+
13+
@action public setErrorMessage = (errorMessage: string) => {
14+
this.errorMessage = errorMessage;
15+
};
16+
}

src/client/stores/RootStore.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
import { AnimalsStore } from "./AnimalsStore";
1+
import { AnimalsStore, ErrorStore } from ".";
22
import { createContext } from "react";
33

44
export class RootStore {
55
public animalsStore: AnimalsStore;
6+
public errorStore: ErrorStore;
67

78
public constructor() {
89
this.animalsStore = new AnimalsStore(this);
10+
this.errorStore = new ErrorStore(this);
911

1012
return {
11-
animalsStore: this.animalsStore
13+
animalsStore: this.animalsStore,
14+
errorStore: this.errorStore
1215
};
1316
}
1417
}

src/client/stores/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { AnimalsStore } from "./AnimalsStore";
22
export { RootStore } from "./RootStore";
3+
export { ErrorStore } from "./ErrorStore";

yarn.lock

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6793,12 +6793,10 @@ lodash.isequal@^4.5.0:
67936793
resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"
67946794
integrity sha1-QVxEePK8wwEgwizhDtMib30+GOA=
67956795

6796-
<<<<<<< HEAD
67976796
lodash.memoize@4.x:
67986797
version "4.1.2"
67996798
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
68006799
integrity sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=
6801-
=======
68026800
lodash.merge@^4.6.1:
68036801
version "4.6.2"
68046802
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
@@ -6808,7 +6806,6 @@ lodash.mergewith@^4.6.0:
68086806
version "4.6.2"
68096807
resolved "https://registry.yarnpkg.com/lodash.mergewith/-/lodash.mergewith-4.6.2.tgz#617121f89ac55f59047c7aec1ccd6654c6590f55"
68106808
integrity sha512-GK3g5RPZWTRSeLSpgP8Xhra+pnjBC56q9FZYe1d5RN3TJ35dbkGy3YqBSMbyCrlbi+CM9Z3Jk5yTL7RCsqboyQ==
6811-
>>>>>>> 6db02bc986617364d3e8b22c134552dcf92841b8
68126809

68136810
lodash.once@^4.1.1:
68146811
version "4.1.1"
@@ -6820,30 +6817,20 @@ lodash.sortby@^4.7.0:
68206817
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
68216818
integrity sha1-7dFMgk4sycHgsKG0K7UhBRakJDg=
68226819

6823-
<<<<<<< HEAD
6824-
=======
68256820
lodash.tail@^4.1.1:
68266821
version "4.1.1"
68276822
resolved "https://registry.yarnpkg.com/lodash.tail/-/lodash.tail-4.1.1.tgz#d2333a36d9e7717c8ad2f7cacafec7c32b444664"
68286823
integrity sha1-0jM6NtnncXyK0vfKyv7HwytERmQ=
68296824

6830-
>>>>>>> 6db02bc986617364d3e8b22c134552dcf92841b8
68316825
lodash@4.17.11:
68326826
version "4.17.11"
68336827
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d"
68346828
integrity sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==
68356829

6836-
<<<<<<< HEAD
68376830
lodash@4.17.15, lodash@^4.0.0, lodash@^4.15.0, lodash@^4.17.10, lodash@^4.17.11, lodash@^4.17.12, lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.2.0, lodash@~4.17.10:
68386831
version "4.17.15"
68396832
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548"
68406833
integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==
6841-
=======
6842-
lodash@^4.0.0, lodash@^4.15.0, lodash@^4.17.10, lodash@^4.17.11, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.2.0, lodash@~4.17.10:
6843-
version "4.17.14"
6844-
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.14.tgz#9ce487ae66c96254fe20b599f21b6816028078ba"
6845-
integrity sha512-mmKYbW3GLuJeX+iGP+Y7Gp1AiGHGbXHCOh/jZmrawMmsE7MS4znI3RL2FsjbqOyMayHInjOeykW7PEajUk1/xw==
6846-
>>>>>>> 6db02bc986617364d3e8b22c134552dcf92841b8
68476834

68486835
log-symbols@2.2.0:
68496836
version "2.2.0"

0 commit comments

Comments
 (0)