aboutsummaryrefslogtreecommitdiffstats
path: root/src/store/form
diff options
context:
space:
mode:
authorGravatar ks129 <[email protected]>2021-02-21 07:35:29 +0200
committerGravatar ks129 <[email protected]>2021-02-21 07:35:29 +0200
commitb1f05fa57c862ce8219e5ca464e794353261f842 (patch)
treeff67e7265ad52099181ceb0bf2a0af36f0525fdd /src/store/form
parentMove hCaptcha types library to dev-dependencies (diff)
Migrate from public state map to Redux
Diffstat (limited to 'src/store/form')
-rw-r--r--src/store/form/actions.ts79
-rw-r--r--src/store/form/reducers.ts29
-rw-r--r--src/store/form/store.ts4
-rw-r--r--src/store/form/types.ts5
4 files changed, 117 insertions, 0 deletions
diff --git a/src/store/form/actions.ts b/src/store/form/actions.ts
new file mode 100644
index 0000000..8b48a37
--- /dev/null
+++ b/src/store/form/actions.ts
@@ -0,0 +1,79 @@
+import { Question } from "../../api/question";
+
+// All Redux actions that can be triggered
+export enum FormAction {
+ SET_VALUE = "SET_VALUE",
+ SET_ERROR = "SET_ERROR",
+ SET_VALID = "SET_VALID",
+ CLEAN = "CLEAN"
+}
+
+// This is base for all actions
+export interface DefaultFormAction {
+ type: FormAction
+}
+
+// Return values for actions
+export interface SetValueAction extends DefaultFormAction {
+ type: FormAction.SET_VALUE,
+ payload: {
+ question: Question,
+ value: string | Map<string, boolean> | null
+ }
+}
+
+export interface SetErrorAction extends DefaultFormAction {
+ type: FormAction.SET_ERROR,
+ payload: {
+ question: Question,
+ error: string
+ }
+}
+
+export interface SetValidAction extends DefaultFormAction {
+ type: FormAction.SET_VALID,
+ payload: {
+ question: Question,
+ valid: boolean
+ }
+}
+
+export interface CleanAction extends DefaultFormAction {
+ type: FormAction.CLEAN
+}
+
+export type Action = SetValueAction | SetErrorAction | SetValidAction | CleanAction;
+
+export function setValue(question: Question, value: string | Map<string, boolean> | null): SetValueAction {
+ return {
+ type: FormAction.SET_VALUE,
+ payload: {
+ question: question,
+ value: value
+ }
+ };
+}
+
+export function setError(question: Question, error: string): SetErrorAction {
+ return {
+ type: FormAction.SET_ERROR,
+ payload: {
+ question: question,
+ error: error
+ }
+ };
+}
+
+export function setValid(question: Question, valid: boolean): SetValidAction {
+ return {
+ type: FormAction.SET_VALID,
+ payload: {
+ question: question,
+ valid: valid
+ }
+ };
+}
+
+export function clean(): CleanAction {
+ return { type: FormAction.CLEAN };
+}
diff --git a/src/store/form/reducers.ts b/src/store/form/reducers.ts
new file mode 100644
index 0000000..fcbb33a
--- /dev/null
+++ b/src/store/form/reducers.ts
@@ -0,0 +1,29 @@
+import {Action, FormAction} from "./actions";
+import {FormState} from "./types";
+
+export const initialState: FormState = {
+ values: new Map(),
+ errors: new Map(),
+ valid: new Map()
+};
+
+export function formReducer(state = initialState, action: Action): FormState {
+ const new_state = state;
+ switch (action.type) {
+ case FormAction.SET_VALUE:
+ new_state.values.set(action.payload.question.id, action.payload.value);
+ break;
+
+ case FormAction.SET_ERROR:
+ new_state.errors.set(action.payload.question.id, action.payload.error);
+ break;
+
+ case FormAction.SET_VALID:
+ new_state.valid.set(action.payload.question.id, action.payload.valid);
+ break;
+
+ case FormAction.CLEAN:
+ return initialState;
+ }
+ return new_state;
+}
diff --git a/src/store/form/store.ts b/src/store/form/store.ts
new file mode 100644
index 0000000..4311a2f
--- /dev/null
+++ b/src/store/form/store.ts
@@ -0,0 +1,4 @@
+import { createStore } from "redux";
+import { formReducer } from "./reducers";
+
+export const store = createStore(formReducer);
diff --git a/src/store/form/types.ts b/src/store/form/types.ts
new file mode 100644
index 0000000..1d3e594
--- /dev/null
+++ b/src/store/form/types.ts
@@ -0,0 +1,5 @@
+export interface FormState {
+ values: Map<string, string | Map<string, boolean> | null>,
+ errors: Map<string, string>,
+ valid: Map<string, boolean>
+}