Skip to content

Commit 972d286

Browse files
committed
Enum actions
1 parent ae0111e commit 972d286

3 files changed

Lines changed: 77 additions & 77 deletions

File tree

src/state/GitreeContext.tsx

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import { fetchRepoNames, fetchBranches, fetchFiles } from "../api/api";
88
import { TreeRenderer } from "../domain/TreeRenderer";
99

1010
import { GitreeContextType } from "./types";
11-
import { actions } from "./actions";
1211
import { reducer } from "./reducer";
1312
import { initialState } from "./initialState";
1413
import { storeOwner, storeToken } from "./storage";
14+
import { ActionTypes } from "./actions";
1515

1616
export const GitreeContext = React.createContext<GitreeContextType | null>(
1717
null,
@@ -22,35 +22,35 @@ export const GitreeProvider = ({ children }: { children: React.ReactNode }) => {
2222
const value: GitreeContextType = {
2323
state,
2424
setOwnerFormCollapsed: (collapsed: boolean) => {
25-
dispatch({ type: actions.SET_OWNER_FORM_COLLAPSED, collapsed });
25+
dispatch({ type: ActionTypes.SET_OWNER_FORM_COLLAPSED, collapsed });
2626
},
2727
setOwner: (owner: string) => {
28-
dispatch({ type: actions.SET_OWNER, owner });
28+
dispatch({ type: ActionTypes.SET_OWNER, owner });
2929
},
3030
setToken: (token: string) => {
31-
dispatch({ type: actions.SET_TOKEN, token });
31+
dispatch({ type: ActionTypes.SET_TOKEN, token });
3232
},
3333
getRepos: async () => {
34-
dispatch({ type: actions.FETCH_REPOS });
34+
dispatch({ type: ActionTypes.FETCH_REPOS });
3535
const { owner, token } = state.ownerData;
3636
try {
3737
const repos = await fetchRepoNames(owner, token);
3838
if (!repos.length) {
3939
dispatch({
40-
type: actions.SET_REPOS,
40+
type: ActionTypes.SET_REPOS,
4141
error:
4242
"This user appears to have no repos. Why don't you try another one?",
4343
repos,
4444
});
4545
} else {
46-
dispatch({ type: actions.SET_REPOS, error: null, repos });
46+
dispatch({ type: ActionTypes.SET_REPOS, error: null, repos });
4747
storeOwner(owner);
4848
storeToken(token);
4949
}
5050
} catch (e) {
5151
if (e instanceof Error) {
5252
dispatch({
53-
type: actions.SET_REPOS,
53+
type: ActionTypes.SET_REPOS,
5454
error: `Can't fetch repos: ${e.message}.`,
5555
repos: [],
5656
});
@@ -61,13 +61,13 @@ export const GitreeProvider = ({ children }: { children: React.ReactNode }) => {
6161
},
6262

6363
setRepoFormCollapsed: (collapsed: boolean) => {
64-
dispatch({ type: actions.SET_REPO_FORM_COLLAPSED, collapsed });
64+
dispatch({ type: ActionTypes.SET_REPO_FORM_COLLAPSED, collapsed });
6565
},
6666
setRepo: (repo: string) => {
67-
dispatch({ type: actions.SET_REPO, repo });
67+
dispatch({ type: ActionTypes.SET_REPO, repo });
6868
},
6969
getBranches: async () => {
70-
dispatch({ type: actions.FETCH_BRANCHES });
70+
dispatch({ type: ActionTypes.FETCH_BRANCHES });
7171
const { owner, token } = state.ownerData;
7272
const { repo } = state.repoData;
7373
try {
@@ -78,7 +78,7 @@ export const GitreeProvider = ({ children }: { children: React.ReactNode }) => {
7878
const branches = await fetchBranches(owner, token, repo);
7979
if (!branches.length) {
8080
dispatch({
81-
type: actions.SET_BRANCHES,
81+
type: ActionTypes.SET_BRANCHES,
8282
error:
8383
"This repo appears to have no branches. Why don't you try another one?",
8484
branches,
@@ -89,7 +89,7 @@ export const GitreeProvider = ({ children }: { children: React.ReactNode }) => {
8989
(b: Branch) => b.name === "master" || b.name === "main",
9090
);
9191
dispatch({
92-
type: actions.SET_BRANCHES,
92+
type: ActionTypes.SET_BRANCHES,
9393
error: null,
9494
branches,
9595
branch: masterBranch ?? null,
@@ -98,7 +98,7 @@ export const GitreeProvider = ({ children }: { children: React.ReactNode }) => {
9898
} catch (e) {
9999
if (e instanceof Error) {
100100
dispatch({
101-
type: actions.SET_BRANCHES,
101+
type: ActionTypes.SET_BRANCHES,
102102
error: `Can't fetch branches: ${e.message}.`,
103103
branches: [],
104104
branch: null,
@@ -110,13 +110,13 @@ export const GitreeProvider = ({ children }: { children: React.ReactNode }) => {
110110
},
111111

112112
setBranchFormCollapsed: (collapsed: boolean) => {
113-
dispatch({ type: actions.SET_BRANCH_FORM_COLLAPSED, collapsed });
113+
dispatch({ type: ActionTypes.SET_BRANCH_FORM_COLLAPSED, collapsed });
114114
},
115115
setBranch: (branch: Branch) => {
116-
dispatch({ type: actions.SET_BRANCH, branch });
116+
dispatch({ type: ActionTypes.SET_BRANCH, branch });
117117
},
118118
buildTree: async () => {
119-
dispatch({ type: actions.FETCH_FILES });
119+
dispatch({ type: ActionTypes.FETCH_FILES });
120120
const { owner, token } = state.ownerData;
121121
const { repo } = state.repoData;
122122
const { branch } = state.branchData;
@@ -133,7 +133,7 @@ export const GitreeProvider = ({ children }: { children: React.ReactNode }) => {
133133
);
134134

135135
dispatch({
136-
type: actions.BUILD_TREE,
136+
type: ActionTypes.BUILD_TREE,
137137
error: null,
138138
files,
139139
tree: buildTree(`${repo}@${branch!.name}`, files),
@@ -142,7 +142,7 @@ export const GitreeProvider = ({ children }: { children: React.ReactNode }) => {
142142
} catch (e) {
143143
if (e instanceof Error) {
144144
dispatch({
145-
type: actions.BUILD_TREE,
145+
type: ActionTypes.BUILD_TREE,
146146
error: `Can't fetch files: ${e.message}.`,
147147
files: [],
148148
tree: null,
@@ -155,13 +155,13 @@ export const GitreeProvider = ({ children }: { children: React.ReactNode }) => {
155155
},
156156

157157
setRenderer: (renderer: TreeRenderer) => {
158-
dispatch({ type: actions.SET_RENDERER, renderer });
158+
dispatch({ type: ActionTypes.SET_RENDERER, renderer });
159159
},
160160
setHoveredNode: (hoveredNode: Node | null) => {
161-
dispatch({ type: actions.SET_HOVERED_NODE, hoveredNode });
161+
dispatch({ type: ActionTypes.SET_HOVERED_NODE, hoveredNode });
162162
},
163163
setMainNode: (mainNode: Node | null) => {
164-
dispatch({ type: actions.SET_MAIN_NODE, mainNode });
164+
dispatch({ type: ActionTypes.SET_MAIN_NODE, mainNode });
165165
},
166166
};
167167

src/state/actions.ts

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,109 +3,109 @@ import { Branch } from "../types/Branch";
33
import { FileList } from "../types/FileList";
44
import { Node } from "../types/Node";
55

6-
export const actions = {
7-
SET_OWNER_FORM_COLLAPSED: "SET_OWNER_FORM_COLLAPSED",
8-
SET_OWNER: "SET_OWNER",
9-
SET_TOKEN: "SET_TOKEN",
10-
FETCH_REPOS: "FETCH_REPOS",
11-
SET_REPOS: "SET_REPOS",
12-
13-
SET_REPO_FORM_COLLAPSED: "SET_REPO_FORM_COLLAPSED",
14-
SET_REPO: "SET_REPO",
15-
FETCH_BRANCHES: "FETCH_BRANCHES",
16-
SET_BRANCHES: "SET_BRANCHES",
17-
18-
SET_BRANCH_FORM_COLLAPSED: "SET_BRANCH_FORM_COLLAPSED",
19-
SET_BRANCH: "SET_BRANCH",
20-
FETCH_FILES: "FETCH_FILES",
21-
BUILD_TREE: "BUILD_TREE",
22-
23-
SET_RENDERER: "SET_RENDERER",
24-
REDRAW: "REDRAW",
25-
SET_HOVERED_NODE: "SET_HOVERED_NODE",
26-
SET_MAIN_NODE: "SET_MAIN_NODE",
27-
} as const;
6+
export enum ActionTypes {
7+
SET_OWNER_FORM_COLLAPSED,
8+
SET_OWNER,
9+
SET_TOKEN,
10+
FETCH_REPOS,
11+
SET_REPOS,
12+
13+
SET_REPO_FORM_COLLAPSED,
14+
SET_REPO,
15+
FETCH_BRANCHES,
16+
SET_BRANCHES,
17+
18+
SET_BRANCH_FORM_COLLAPSED,
19+
SET_BRANCH,
20+
FETCH_FILES,
21+
BUILD_TREE,
22+
23+
SET_RENDERER,
24+
REDRAW,
25+
SET_HOVERED_NODE,
26+
SET_MAIN_NODE,
27+
}
2828

2929
type SetOwnerFormCollapsedAction = {
30-
type: "SET_OWNER_FORM_COLLAPSED";
30+
type: ActionTypes.SET_OWNER_FORM_COLLAPSED;
3131
collapsed: boolean;
3232
};
3333

3434
type SetOwnerAction = {
35-
type: "SET_OWNER";
35+
type: ActionTypes.SET_OWNER;
3636
owner: string;
3737
};
3838

3939
type SetTokenAction = {
40-
type: "SET_TOKEN";
40+
type: ActionTypes.SET_TOKEN;
4141
token: string;
4242
};
4343

4444
type FetchReposAction = {
45-
type: "FETCH_REPOS";
45+
type: ActionTypes.FETCH_REPOS;
4646
};
4747

4848
type SetReposAction = {
49-
type: "SET_REPOS";
49+
type: ActionTypes.SET_REPOS;
5050
error: string | null;
5151
repos: string[] | null;
5252
};
5353

5454
type SetRepoFormCollapsedAction = {
55-
type: "SET_REPO_FORM_COLLAPSED";
55+
type: ActionTypes.SET_REPO_FORM_COLLAPSED;
5656
collapsed: boolean;
5757
};
5858

5959
type SetRepoAction = {
60-
type: "SET_REPO";
60+
type: ActionTypes.SET_REPO;
6161
repo: string;
6262
};
6363

6464
type FetchBranchesAction = {
65-
type: "FETCH_BRANCHES";
65+
type: ActionTypes.FETCH_BRANCHES;
6666
};
6767

6868
type SetBranchesAction = {
69-
type: "SET_BRANCHES";
69+
type: ActionTypes.SET_BRANCHES;
7070
error: string | null;
7171
branches: Branch[];
7272
branch: Branch | null;
7373
};
7474

7575
type SetBranchCollapsedAction = {
76-
type: "SET_BRANCH_FORM_COLLAPSED";
76+
type: ActionTypes.SET_BRANCH_FORM_COLLAPSED;
7777
collapsed: boolean;
7878
};
7979

8080
type SetBranchAction = {
81-
type: "SET_BRANCH";
81+
type: ActionTypes.SET_BRANCH;
8282
branch: Branch;
8383
};
8484

8585
type FetchFilesAction = {
86-
type: "FETCH_FILES";
86+
type: ActionTypes.FETCH_FILES;
8787
};
8888

8989
type BuildTreeAction = {
90-
type: "BUILD_TREE";
90+
type: ActionTypes.BUILD_TREE;
9191
tree: Node | null;
9292
files: FileList["files"];
9393
error: string | null;
9494
truncated: boolean;
9595
};
9696

9797
type SetHoveredNodeAction = {
98-
type: "SET_HOVERED_NODE";
98+
type: ActionTypes.SET_HOVERED_NODE;
9999
hoveredNode: Node | null;
100100
};
101101

102102
type SetMainNodeAction = {
103-
type: "SET_MAIN_NODE";
103+
type: ActionTypes.SET_MAIN_NODE;
104104
mainNode: Node | null;
105105
};
106106

107107
type SetRendererAction = {
108-
type: "SET_RENDERER";
108+
type: ActionTypes.SET_RENDERER;
109109
renderer: TreeRenderer;
110110
};
111111

0 commit comments

Comments
 (0)