Skip to content

Commit 9a848f9

Browse files
committed
refactor the types
1 parent ef879b1 commit 9a848f9

File tree

7 files changed

+47
-50
lines changed

7 files changed

+47
-50
lines changed

.eslintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ public/
44
client/app/libs/i18n/translations.js
55
client/app/libs/i18n/default.js
66
postcss.config.js
7-
client/app/bundles/comments/src/
7+
client/app/bundles/comments/rescript/

client/app/bundles/comments/rescript/Actions/Actions.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module Create = {
1313
},
1414
{
1515
responseType: "json",
16-
headers: {
16+
headers: { // see https://github.com/shakacode/react_on_rails/blob/249c69812474e0f532df432581bf5e618df0e1ec/node_package/src/Authenticity.ts#L13C1-L18C1
1717
"X-CSRF-Token": ReactOnRails.authenticityToken(),
1818
"X-Requested-With": "XMLHttpRequest",
1919
}

client/app/bundles/comments/rescript/CommentForm/CommentForm.res

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
let reducer = (
2-
state: Types.commentFormState,
3-
action: Types.commentFormAction
4-
): Types.commentFormState => {
2+
state: CommentFormTypes.state,
3+
action: CommentFormTypes.action
4+
): CommentFormTypes.state => {
55
switch (action) {
66
| SetAuthor(author) => {...state, author}
77
| SetText(text) => {...state, text}
@@ -11,7 +11,7 @@ let reducer = (
1111

1212

1313
@react.component
14-
let make = (~storeComment: Types.storeCommentAction, ~disabled: bool, ~storeCommentError: bool) => {
14+
let make = (~storeComment: ReScriptShowTypes.storeComment, ~disabled: bool, ~storeCommentError: bool) => {
1515
let (state, dispatch) = React.useReducer(
1616
reducer, {
1717
author: "",
@@ -32,10 +32,10 @@ let make = (~storeComment: Types.storeCommentAction, ~disabled: bool, ~storeComm
3232

3333
let handleSubmit = (event) => {
3434
ReactEvent.Form.preventDefault(event)
35-
storeComment(state.author, state.text)
35+
storeComment({author: state.author, text: state.text})
3636
}
3737

38-
let forms: array<Types.formData> =
38+
let forms: array<CommentFormTypes.formData> =
3939
[
4040
{formName: "Horizontal Form", formType: Horizontal},
4141
{formName: "Inline Form", formType: Inline},
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
11
type formDisplay = Horizontal | Inline | Stacked
22

3-
type commentFormState = {
4-
author: string,
5-
text: string,
6-
form: formDisplay
7-
}
8-
9-
type commentFormAction =
10-
| SetAuthor(string)
11-
| SetText(string)
12-
| SetFormType(formDisplay)
13-
143
type formData = {
154
formName: string,
165
formType: formDisplay
176
}
187

19-
type storeCommentAction = (string, string) => unit
20-
21-
type storeCommentData = {
8+
type state = {
229
author: string,
23-
text: string
10+
text: string,
11+
form: formDisplay
2412
}
2513

26-
type error = NoError | FailedToSaveComment | FailedToFetchComments
14+
type action =
15+
| SetAuthor(string)
16+
| SetText(string)
17+
| SetFormType(formDisplay)

client/app/bundles/comments/rescript/ReScriptShow.res

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,7 @@
1-
type savingStatus = Free | BusySaving
2-
3-
type status = {
4-
commentsFetchError: bool,
5-
commentStoreError: bool,
6-
saving: savingStatus
7-
}
8-
9-
type state = {
10-
comments: Actions.Fetch.comments,
11-
status: status
12-
}
13-
14-
type action =
15-
| SetComments(Actions.Fetch.comments)
16-
| SetFetchError(bool)
17-
| SetStoreError(bool)
18-
| SetSavingStatus(savingStatus)
19-
20-
211
let reducer = (
22-
state: state,
23-
action: action
24-
): state => {
2+
state: ReScriptShowTypes.state,
3+
action: ReScriptShowTypes.action
4+
): ReScriptShowTypes.state => {
255
switch (action) {
266
| SetComments(comments) => {comments, status: {...state.status, commentsFetchError: false}}
277
| SetFetchError(error) => {...state, status: {...state.status, commentsFetchError: error}}
@@ -43,12 +23,12 @@ let default = () => {
4323
}
4424
)
4525

46-
let storeComment = (author, text) => {
26+
let storeComment: ReScriptShowTypes.storeComment = (newComment: Actions.Create.t) => {
4727
SetStoreError(false)->dispatch
4828
SetSavingStatus(BusySaving)->dispatch
4929
let saveAndFetchComments = async () => {
5030
try {
51-
let _ = await Actions.Create.storeComment({author, text})
31+
let _ = await Actions.Create.storeComment(newComment)
5232
SetSavingStatus(Free)->dispatch
5333

5434
let comments = await Actions.Fetch.fetchComments()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
type savingStatus = Free | BusySaving
2+
3+
type status = {
4+
commentsFetchError: bool,
5+
commentStoreError: bool,
6+
saving: savingStatus
7+
}
8+
9+
type state = {
10+
comments: Actions.Fetch.comments,
11+
status: status
12+
}
13+
14+
type action =
15+
| SetComments(Actions.Fetch.comments)
16+
| SetFetchError(bool)
17+
| SetStoreError(bool)
18+
| SetSavingStatus(savingStatus)
19+
20+
type storeComment = (Actions.Create.t) => unit

client/app/bundles/comments/rescript/bindings/Axios.res

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,10 @@ type reqOptions = {
55
"X-Requested-With": string
66
}
77
}
8-
@module("axios") external post: (string, Types.storeCommentData, reqOptions) => promise<unit> = "post"
8+
9+
type commentData = {
10+
author: string,
11+
text: string
12+
}
13+
14+
@module("axios") external post: (string, commentData, reqOptions) => promise<unit> = "post"

0 commit comments

Comments
 (0)