-
{count}
-
+ {error &&
{error}
}
+
+ {loading ? (
+
+ Loading...
+
+ ) : (
+
+ {count}
+
+ )}
+
+
+
+
+
);
diff --git a/template/src/features/counter/CounterActions.tsx b/template/src/features/counter/CounterActions.tsx
new file mode 100644
index 0000000..89b21c3
--- /dev/null
+++ b/template/src/features/counter/CounterActions.tsx
@@ -0,0 +1,26 @@
+import { AppDispatch, AppThunk } from "../../store";
+import {
+ fetchValueStart,
+ fetchValueSuccess,
+ fetchValueError
+} from "./CounterSlice";
+import axios from "axios";
+
+export const sleep = (t = Math.random() * 200 + 300) =>
+ new Promise(resolve => setTimeout(resolve, t));
+
+export const fetchInitialValue = (): AppThunk => async (
+ dispatch: AppDispatch
+) => {
+ dispatch(fetchValueStart());
+ try {
+ // For demo purpose let's say out value is the length
+ // of project name in manifest and the api call is slow
+ await sleep();
+ const response = await axios.get("/manifest.json");
+ const value = response.data.name.length;
+ dispatch(fetchValueSuccess(value));
+ } catch (e) {
+ dispatch(fetchValueError("Something went wrong."));
+ }
+};
diff --git a/template/src/features/counter/CounterSlice.tsx b/template/src/features/counter/CounterSlice.tsx
index 7e0d6c6..76d3352 100644
--- a/template/src/features/counter/CounterSlice.tsx
+++ b/template/src/features/counter/CounterSlice.tsx
@@ -1,17 +1,32 @@
-import { createSlice } from "@reduxjs/toolkit";
+import { createSlice, PayloadAction } from "@reduxjs/toolkit";
interface InitialState {
value: number;
+ loading: boolean;
+ error: string | null;
}
export const initialState: InitialState = {
- value: 0
+ value: 0,
+ loading: false,
+ error: null
};
export const slice = createSlice({
name: "counter",
initialState,
reducers: {
+ fetchValueStart: state => {
+ state.loading = true;
+ },
+ fetchValueSuccess: (state, action: PayloadAction