Skip to content

Commit 5958506

Browse files
committed
define typescript interface for state
1 parent ae4df47 commit 5958506

File tree

3 files changed

+335
-19
lines changed

3 files changed

+335
-19
lines changed
Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
1-
import React, { Component } from 'react';
2-
import {CookieTableRow} from './CookieTableRow'
1+
import React, { Component } from "react";
2+
import { CookieTableRow } from "./CookieTableRow";
33

44
class CookieTable extends Component {
55
constructor(props) {
66
super(props);
7-
this.state = {}
7+
this.state = {};
88
}
99

1010
render() {
1111
let cookieRowArray;
1212
if (Array.isArray(this.props.cookies)) {
1313
cookieRowArray = this.props.cookies.map((cookie, i) => {
14-
return <CookieTableRow className='cookieTableRow' cookie={cookie} key={i} ></CookieTableRow>
15-
})
14+
return (
15+
<CookieTableRow className="cookieTableRow" cookie={cookie} key={i} />
16+
);
17+
});
1618
}
1719

1820
return (
19-
<div className='cookieTable'>
20-
<div className='cookieTableHeaders grid-9'>
21-
22-
<div className='cookieTableHeaderCell'>Name</div>
23-
<div className='cookieTableHeaderCell'>Value</div>
24-
<div className='cookieTableHeaderCell'>Domain</div>
25-
<div className='cookieTableHeaderCell'>HostOnly</div>
26-
<div className='cookieTableHeaderCell'>Path</div>
27-
<div className='cookieTableHeaderCell'>Secure</div>
28-
<div className='cookieTableHeaderCell'>HttpOnly</div>
29-
<div className='cookieTableHeaderCell'>Session</div>
30-
<div className='cookieTableHeaderCell'>ExpirationDate</div>
21+
<div className="cookieTable">
22+
<div className="cookieTableHeaders grid-9">
23+
<div className="cookieTableHeaderCell">Name</div>
24+
<div className="cookieTableHeaderCell">Value</div>
25+
<div className="cookieTableHeaderCell">Domain</div>
26+
<div className="cookieTableHeaderCell">HostOnly</div>
27+
<div className="cookieTableHeaderCell">Path</div>
28+
<div className="cookieTableHeaderCell">Secure</div>
29+
<div className="cookieTableHeaderCell">HttpOnly</div>
30+
<div className="cookieTableHeaderCell">Session</div>
31+
<div className="cookieTableHeaderCell">ExpirationDate</div>
3132
</div>
3233
{cookieRowArray}
3334
</div>
34-
)
35+
);
3536
}
3637
}
3738

38-
export default CookieTable;
39+
export default CookieTable;

src/client/reducers/business.ts

Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
import * from "../../types"
2+
import format from "date-fns/format";
3+
import * as types from "../actions/actionTypes";
4+
5+
const initialState = {
6+
currentTab: "First Tab";
7+
reqResArray: [];
8+
history: [];
9+
collections: [];
10+
warningMessage: "";
11+
newRequestFields: {
12+
protocol: "";
13+
url: "http://";
14+
method: "GET";
15+
graphQL: false;
16+
gRPC: false;
17+
};
18+
newRequestHeaders: {
19+
headersArr: [];
20+
count: 0;
21+
};
22+
newRequestStreams: {
23+
streamsArr: [];
24+
count: 0;
25+
streamContent: [];
26+
selectedPackage: null;
27+
selectedRequest: null;
28+
selectedService: null;
29+
selectedStreamingType: null;
30+
initialQuery: null;
31+
queryArr: null;
32+
protoPath: null;
33+
services: null;
34+
protoContent: "";
35+
};
36+
newRequestCookies: {
37+
cookiesArr: [];
38+
count: 0;
39+
};
40+
newRequestBody: {
41+
bodyContent: "";
42+
bodyVariables: "";
43+
bodyType: "raw";
44+
rawType: "Text (text/plain)";
45+
JSONFormatted: true;
46+
};
47+
newRequestSSE: {
48+
isSSE: false;
49+
};
50+
}
51+
52+
const businessReducer = (state = initialState, action) => {
53+
switch (action.type) {
54+
case types.GET_HISTORY: {
55+
return {
56+
...state,
57+
history: action.payload,
58+
};
59+
}
60+
61+
case types.DELETE_HISTORY: {
62+
const deleteId = action.payload.id;
63+
const deleteDate = format(action.payload.created_at, "MM/DD/YYYY");
64+
const newHistory = JSON.parse(JSON.stringify(state.history));
65+
newHistory.forEach((obj, i) => {
66+
if (obj.date === deleteDate)
67+
obj.history = obj.history.filter((hist) => hist.id !== deleteId);
68+
if (obj.history.length === 0) {
69+
newHistory.splice(i, 1);
70+
}
71+
});
72+
return {
73+
...state,
74+
history: newHistory,
75+
};
76+
}
77+
78+
case types.CLEAR_HISTORY: {
79+
return {
80+
...state,
81+
history: [],
82+
};
83+
}
84+
85+
case types.GET_COLLECTIONS: {
86+
return {
87+
...state,
88+
collections: action.payload,
89+
};
90+
}
91+
92+
case types.DELETE_COLLECTION: {
93+
const deleteId = action.payload.id;
94+
const newCollections = JSON.parse(JSON.stringify(state.collections));
95+
newCollections.forEach((obj, i) => {
96+
if (obj.id === deleteId) {
97+
newCollections.splice(i, 1);
98+
}
99+
});
100+
return {
101+
...state,
102+
collections: newCollections,
103+
};
104+
}
105+
106+
case types.COLLECTION_TO_REQRES: {
107+
const reqResArray = [...action.payload];
108+
return {
109+
...state,
110+
reqResArray,
111+
};
112+
}
113+
114+
case types.COLLECTION_ADD: {
115+
//add to collection to array in state
116+
return {
117+
...state,
118+
collections: [action.payload, ...state.collections],
119+
};
120+
}
121+
122+
case types.REQRES_CLEAR: {
123+
return {
124+
...state,
125+
reqResArray: [],
126+
};
127+
}
128+
129+
case types.REQRES_ADD: {
130+
const reqResArray = JSON.parse(JSON.stringify(state.reqResArray));
131+
reqResArray.push(action.payload);
132+
const addDate = format(action.payload.created_at, "MM/DD/YYYY");
133+
const newHistory = JSON.parse(JSON.stringify(state.history));
134+
let updated = false;
135+
//if there is history for added date, add query to beginning of history
136+
newHistory.forEach((obj) => {
137+
if (obj.date === addDate) {
138+
obj.history.unshift(action.payload);
139+
updated = true;
140+
}
141+
});
142+
//if there is not history at added date, create new history with new query
143+
if (!updated) {
144+
newHistory.unshift({
145+
date: addDate,
146+
history: [action.payload],
147+
});
148+
}
149+
return {
150+
...state,
151+
reqResArray,
152+
history: newHistory,
153+
};
154+
}
155+
156+
case types.REQRES_DELETE: {
157+
const deleteId = action.payload.id;
158+
const newReqResArray = state.reqResArray.filter(
159+
(reqRes) => reqRes.id !== deleteId
160+
);
161+
return {
162+
...state,
163+
reqResArray: newReqResArray,
164+
};
165+
}
166+
167+
case types.SET_CHECKS_AND_MINIS: {
168+
return {
169+
...state,
170+
reqResArray: JSON.parse(JSON.stringify(action.payload)),
171+
};
172+
}
173+
174+
case types.REQRES_UPDATE: {
175+
const reqResDeepCopy = JSON.parse(JSON.stringify(state.reqResArray));
176+
let indexToBeUpdated;
177+
reqResDeepCopy.forEach((reqRes, index) => {
178+
if (reqRes.id === action.payload.id) indexToBeUpdated = index;
179+
});
180+
if (indexToBeUpdated !== undefined) {
181+
action.payload.checked = state.reqResArray[indexToBeUpdated].checked;
182+
action.payload.minimized =
183+
state.reqResArray[indexToBeUpdated].minimized;
184+
reqResDeepCopy.splice(
185+
indexToBeUpdated,
186+
1,
187+
JSON.parse(JSON.stringify(action.payload))
188+
); //FOR SOME REASON THIS IS NECESSARY, MESSES UP CHECKS OTHERWISE
189+
}
190+
return {
191+
...state,
192+
reqResArray: reqResDeepCopy,
193+
};
194+
}
195+
196+
case types.SET_COMPOSER_WARNING_MESSAGE: {
197+
return {
198+
...state,
199+
warningMessage: action.payload,
200+
};
201+
}
202+
203+
case types.SET_NEW_REQUEST_FIELDS: {
204+
return {
205+
...state,
206+
newRequestFields: action.payload,
207+
};
208+
}
209+
210+
case types.SET_NEW_REQUEST_HEADERS: {
211+
return {
212+
...state,
213+
newRequestHeaders: action.payload,
214+
};
215+
}
216+
217+
case types.SET_NEW_REQUEST_STREAMS: {
218+
return {
219+
...state,
220+
newRequestStreams: action.payload,
221+
};
222+
}
223+
224+
case types.SET_NEW_REQUEST_BODY: {
225+
return {
226+
...state,
227+
newRequestBody: action.payload,
228+
};
229+
}
230+
231+
case types.SET_NEW_REQUEST_COOKIES: {
232+
return {
233+
...state,
234+
newRequestCookies: action.payload,
235+
};
236+
}
237+
238+
case types.SET_NEW_REQUEST_SSE: {
239+
return {
240+
...state,
241+
newRequestSSE: { isSSE: action.payload },
242+
};
243+
}
244+
245+
case types.SET_CURRENT_TAB: {
246+
return {
247+
...state,
248+
currentTab: action.payload,
249+
};
250+
}
251+
252+
default:
253+
return state;
254+
}
255+
};
256+
257+
export default businessReducer;

src/types.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
export interface initialState {
2+
currentTab: string;
3+
reqResArray: object[];
4+
history: object[];
5+
collections: object[];
6+
warningMessage: string;
7+
newRequestFields: NewRequestFields;
8+
newRequestHeaders: NewRequestHeaders;
9+
newRequestStreams: NewRequestStreams;
10+
newRequestCookies: NewRequestCookies;
11+
newRequestBody: NewRequestBody;
12+
newRequestSSE: NewRequestSSE;
13+
}
14+
15+
export interface NewRequestFields {
16+
protocol: string;
17+
url: string;
18+
method: string;
19+
graphQL: boolean;
20+
gRPC: boolean;
21+
}
22+
23+
export interface NewRequestHeaders {
24+
headersArr: object[];
25+
count: number;
26+
}
27+
28+
export interface NewRequestStreams {
29+
streamsArr: object[];
30+
count: number;
31+
streamContent: object[];
32+
selectedPackage: string | null;
33+
selectedRequest: string | null;
34+
selectedService: string | null;
35+
selectedStreamingType: string | null;
36+
initialQuery: any | null;
37+
queryArr: object[] | null;
38+
protoPath: any | null;
39+
services: object | null;
40+
protoContent: string;
41+
}
42+
43+
export interface NewRequestCookies {
44+
cookiesArr: object[];
45+
count: number;
46+
}
47+
48+
export interface NewRequestBody {
49+
bodyContent: string;
50+
bodyVariables: string;
51+
bodyType: string;
52+
rawType: string;
53+
JSONFormatted: boolean;
54+
};
55+
56+
export interface NewRequestSSE {
57+
isSSE: boolean;
58+
};

0 commit comments

Comments
 (0)