Skip to content

Commit 638f155

Browse files
committed
Export enum instead of const for action types
1 parent d12df3c commit 638f155

File tree

8 files changed

+39
-15
lines changed

8 files changed

+39
-15
lines changed

CHANGE-NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
- v0.2.1 Export enum for action types
12
- v0.2.0 Add `Add Redux Reducer` Action
23
- v0.1.0 Add `Add React Component` Action

resources/META-INF/plugin.xml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<idea-plugin>
22
<id>com.faebeee.reactcomponentcreator</id>
33
<name>React Component Creator</name>
4-
<version>0.3.0</version>
4+
<version>0.3.1</version>
55
<vendor email="[email protected]" url="http://fabs.io">Fabio Gianini</vendor>
66

77
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
@@ -24,8 +24,10 @@
2424

2525
<change-notes>
2626
<![CDATA[
27-
<ul>
28-
<li>v0.2.0 Add <code>Add Redux Reducer</code> Action</li>
27+
<ul>
28+
<li>v0.3.1 Export enum for action types</li>
29+
<li>v0.3.0 Add <code>Add Redux Reducer</code> Action</li>
30+
<li>v0.2.0 Update UI & improve code</li>
2931
<li>v0.1.0 Add <code>Add React Component</code> Action</li>
3032
</ul>
3133
]]>
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
export const {{ mutationType }} = '{{ moduleName }}/{{ mutationType }}';
1+
export enum {{ actionTypesEnumName }} {
2+
{{ mutationType }} = '{{ moduleName }}/{{ mutationType }}',
3+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Dispatch } from 'redux';
2-
import { {{ mutationType }} } from './action-types';
2+
import { {{ actionTypesEnumName }} } from './action-types';
33
import { {{ actionTypeName }} } from './types';
44

5-
export const {{ actionFunctionName }} = () => async (dispatch: Dispatch<{{ actionTypeName }}>) => {
5+
export const {{ actionFunctionName }} = () => async (dispatch: Dispatch<{{ actionTypesEnumName }}.{{ actionTypeName }}>) => {
66
dispatch({ type: {{ mutationType }} , data });
77
};

resources/templates/reducer/index.ts.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { {{ moduleNamePascalCase }}Actions, {{ stateName }} } from './types';
2-
import { {{ mutationType }} } from './action-types';
2+
import { {{ actionTypesEnumName }} } from './action-types';
33

44
export const initialState: {{ stateName }} = {
55
};
66

7-
export default function {{ moduleName }}Reducer(state = initialState, action: {{ moduleNamePascalCase }}Actions): {{ stateName }} {
7+
export default function {{ moduleNamePascalCase }}Reducer(state = initialState, action: {{ moduleNamePascalCase }}Actions): {{ stateName }} {
88
switch (action.type) {
9-
case {{ mutationType }}:
9+
case {{ actionTypesEnumName }}.{{ mutationType }}:
1010
return {
1111
...state,
1212
};
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Action } from 'redux';
2-
import { {{ mutationType }} } from './action-types';
2+
import { {{ actionTypesEnumName }} } from './action-types';
33

44
export interface {{ stateName }} {
55
}
66

7-
export type {{ actionTypeName }} = Action<{{ mutationType }}> & {
7+
export type {{ actionTypeName }} = Action< {{ actionTypesEnumName }}.{{ mutationType }}> & {
88
}
99

1010
export type {{ moduleNamePascalCase }}Actions = {{ actionTypeName }};

src/fabs/reducer/ReducerDialog.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,15 @@ public ArrayList<String> getFiles() {
6060
public Map<String, Object> getTemplateVars() {
6161
Map<String, Object> templateModel = new HashMap<>();
6262
String actionFunctionName = actionNameTextField.getText();
63+
String moduleName = moduleNameTextField.getText();
6364

6465
templateModel.put("actionFunctionName", actionFunctionName);
65-
templateModel.put("moduleName", StringFormatter.toCamelCase(moduleNameTextField.getText()));
66-
templateModel.put("moduleNamePascalCase", StringFormatter.toCamelCase(moduleNameTextField.getText()));
67-
templateModel.put("stateName", StringFormatter.toCamelCase(moduleNameTextField.getText()) + "State");
66+
templateModel.put("moduleName", moduleName);
67+
templateModel.put("moduleNamePascalCase", StringFormatter.toCamelCase(moduleName));
68+
templateModel.put("stateName", StringFormatter.toCamelCase(moduleName) + "State");
6869
templateModel.put("mutationType", mutationNametextField.getText());
69-
templateModel.put("actionTypeName", StringFormatter.capitalizeFirst(actionFunctionName)+"Action");
70+
templateModel.put("actionTypeName", StringFormatter.capitalizeFirst(actionFunctionName) + "Action");
71+
templateModel.put("actionTypesEnumName", StringFormatter.toDashCase(moduleName).toUpperCase() + "_ACTIONS");
7072

7173
return templateModel;
7274
}

src/fabs/util/StringFormatter.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ public static String toCamelCase(String input) {
2323
return sb.toString();
2424
}
2525

26+
public static String toDashCase(String input) {
27+
StringBuffer sb = new StringBuffer();
28+
int c = 0;
29+
for (String s : input.split("-")) {
30+
if (c != 0) {
31+
sb.append("_");
32+
}
33+
34+
sb.append(Character.toLowerCase(s.charAt(0)));
35+
if (s.length() > 1) {
36+
sb.append(s.substring(1, s.length()).toLowerCase());
37+
}
38+
c++;
39+
}
40+
return sb.toString();
41+
}
42+
2643
public static String capitalizeFirst(String input) {
2744
return input.substring(0, 1).toUpperCase() + input.substring(1);
2845
}

0 commit comments

Comments
 (0)