Skip to content

Commit d331401

Browse files
committed
Typescript Dispatch typing
1 parent 83cd5b3 commit d331401

File tree

5 files changed

+26
-23
lines changed

5 files changed

+26
-23
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Created by https://www.gitignore.io/api/node,linux,macos,windows,visualstudio,yarn
2-
2+
yarn.lock
33
### Linux ###
44
*~
55

src/actions/components.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
ComponentInt, ComponentsInt, PropInt, ChildInt,
2+
ComponentInt, ComponentsInt, PropInt, ChildInt, Action
33
} from '../utils/Interfaces.ts';
44

55
import {
@@ -35,10 +35,10 @@ import createApplicationUtil from '../utils/createApplication.util.ts';
3535

3636
export const changeImagePath = (imageSource: string) => ({
3737
type: CHANGE_IMAGE_SOURCE,
38-
payload: imageSource,
38+
payload: { imageSource },
3939
})
4040

41-
export const loadInitData = () => (dispatch: any) => {
41+
export const loadInitData = () => (dispatch: (arg: Action) => void) => {
4242
loadState().then((data: any) => dispatch({
4343
type: LOAD_INIT_DATA,
4444
payload: {
@@ -47,7 +47,7 @@ export const loadInitData = () => (dispatch: any) => {
4747
}));
4848
};
4949

50-
export const addComponent = ({ title }: { title: string }) => (dispatch: any) => {
50+
export const addComponent = ({ title }: { title: string }) => (dispatch: (arg: Action) => void) => {
5151
dispatch({ type: ADD_COMPONENT, payload: { title } });
5252
};
5353

@@ -59,11 +59,11 @@ export const addChild = ({
5959
title: string;
6060
childType: string;
6161
HTMLInfo: object;
62-
}) => (dispatch: any) => {
62+
}) => (dispatch: (arg: Action) => void) => {
6363
dispatch({ type: ADD_CHILD, payload: { title, childType, HTMLInfo } });
6464
};
6565

66-
export const deleteChild = ({}) => (dispatch: any) => {
66+
export const deleteChild = ({}) => (dispatch: (arg: Action) => void) => {
6767
// with no payload, it will delete focusd child
6868
dispatch({ type: DELETE_CHILD, payload: {} });
6969
};
@@ -74,7 +74,7 @@ export const deleteComponent = ({
7474
}: {
7575
componentId: number;
7676
stateComponents: ComponentsInt;
77-
}) => (dispatch: any) => {
77+
}) => (dispatch: (arg: Action) => void) => {
7878
// find all places where the "to be deleted" is a child and do what u gotta do
7979
stateComponents.forEach((parent: ComponentInt) => {
8080
parent.childrenArray
@@ -97,12 +97,12 @@ stateComponents: ComponentsInt;
9797
dispatch({ type: DELETE_COMPONENT, payload: { componentId } });
9898
};
9999

100-
export const changeFocusComponent = ({ title }: { title: string }) => (dispatch: any) => {
100+
export const changeFocusComponent = ({ title }: { title: string }) => (dispatch: (arg: Action) => void) => {
101101
dispatch({ type: CHANGE_FOCUS_COMPONENT, payload: { title } });
102102
};
103103

104104
// make sure childId is being sent in
105-
export const changeFocusChild = ({ childId }: { childId: number }) => (dispatch: any) => {
105+
export const changeFocusChild = ({ childId }: { childId: number }) => (dispatch: (arg: Action) => void) => {
106106
dispatch({ type: CHANGE_FOCUS_CHILD, payload: { childId } });
107107
};
108108

@@ -112,16 +112,15 @@ export const changeComponentFocusChild = ({
112112
}: {
113113
componentId: number;
114114
childId: number;
115-
}) => (dispatch: any) => {
115+
}) => (dispatch: (arg: Action) => void) => {
116116
dispatch({
117117
type: CHANGE_COMPONENT_FOCUS_CHILD,
118118
payload: { componentId, childId },
119119
});
120120
};
121121

122122
export const deleteImage = () => ({
123-
type: DELETE_IMAGE,
124-
payload: ''
123+
type: DELETE_IMAGE
125124
})
126125

127126

@@ -135,7 +134,7 @@ components: ComponentsInt;
135134
path: string;
136135
appName: string;
137136
exportAppBool: boolean;
138-
}) => (dispatch: any) => {
137+
}) => (dispatch: (arg: Action) => void) => {
139138
// this dispatch sets the global state property 'loading' to true until the createFiles call resolves below
140139
dispatch({
141140
type: EXPORT_FILES,
@@ -187,7 +186,7 @@ components: ComponentsInt;
187186
genOption: number;
188187
appName: string;
189188
exportAppBool: boolean;
190-
}) => (dispatch: any) => {
189+
}) => (dispatch: (arg: Action) => void) => {
191190
if (genOption === 0) {
192191
exportAppBool = false;
193192
dispatch(
@@ -207,7 +206,6 @@ exportAppBool: boolean;
207206
path,
208207
appName,
209208
genOption,
210-
// exportAppBool
211209
})
212210
.then(() => {
213211
dispatch({
@@ -238,7 +236,7 @@ export const deleteAllData = () => ({
238236
type: DELETE_ALL_DATA,
239237
});
240238

241-
export const deleteProp = (propId: number) => (dispatch: any) => {
239+
export const deleteProp = (propId: number) => (dispatch: (arg: Action) => void) => {
242240
dispatch({ type: DELETE_PROP, payload: propId });
243241
};
244242

@@ -248,7 +246,7 @@ export const addProp = (prop: PropInt) => ({
248246
});
249247

250248
export const updateHtmlAttr = ({ attr, value }: { attr: string; value: string }) => (
251-
dispatch: any,
249+
dispatch: (arg: Action) => void,
252250
) => {
253251
dispatch({
254252
type: UPDATE_HTML_ATTR,
@@ -257,7 +255,7 @@ export const updateHtmlAttr = ({ attr, value }: { attr: string; value: string })
257255
};
258256

259257
export const updateChildrenSort = ({ newSortValues }: { newSortValues: any }) => (
260-
dispatch: any,
258+
dispatch: (arg: Action) => void,
261259
) => {
262260
dispatch({
263261
type: UPDATE_CHILDREN_SORT,

src/containers/AppContainer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ type Props = {
1919
totalComponents: number;
2020
loading: boolean;
2121
selectableChildren: Array<number>;
22-
loadInitData: any;
23-
changeImagePath: any;
22+
loadInitData: () => void;
23+
changeImagePath: () => void;
2424
changed: boolean;
2525
};
2626

src/utils/Interfaces.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,8 @@ export interface ApplicationStateInt {
5757
appDir: string;
5858
loading: boolean;
5959
}
60+
61+
export interface Action {
62+
type: string;
63+
payload: object;
64+
}

src/utils/componentReducer.util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,10 @@ export const handleTransform = (
319319
};
320320

321321
//change image source
322-
export const changeImageSource = (state: ApplicationStateInt, src :string) => {
322+
export const changeImageSource = (state: ApplicationStateInt, { imageSource }: { imageSource: string }) => {
323323
return {
324324
...state,
325-
imageSource: src
325+
imageSource
326326
}
327327
}
328328

0 commit comments

Comments
 (0)