Skip to content

Commit e7bde79

Browse files
committed
format rescript files
1 parent 9a848f9 commit e7bde79

File tree

17 files changed

+175
-205
lines changed

17 files changed

+175
-205
lines changed

app/views/pages/rescript.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<%= react_component "RescriptShow", prerender: false %>
1+
<%= react_component "RescriptShow", prerender: true %>
Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
module Create = {
22
type t = {
33
author: string,
4-
text: string
4+
text: string,
55
}
66

77
let storeComment = async (comment: t) => {
88
let _ = await Axios.post(
99
"comments.json",
1010
{
1111
author: comment.author,
12-
text: comment.text
12+
text: comment.text,
1313
},
1414
{
1515
responseType: "json",
16-
headers: { // see https://github.com/shakacode/react_on_rails/blob/249c69812474e0f532df432581bf5e618df0e1ec/node_package/src/Authenticity.ts#L13C1-L18C1
16+
headers: {
17+
// see https://github.com/shakacode/react_on_rails/blob/249c69812474e0f532df432581bf5e618df0e1ec/node_package/src/Authenticity.ts#L13C1-L18C1
1718
"X-CSRF-Token": ReactOnRails.authenticityToken(),
1819
"X-Requested-With": "XMLHttpRequest",
19-
}
20-
}
20+
},
21+
},
2122
)
2223
}
2324
}
@@ -26,34 +27,32 @@ module Fetch = {
2627
type t = {
2728
author: string,
2829
text: string,
29-
id: int
30+
id: int,
3031
}
3132

3233
type comments = array<t>
3334

34-
type commentsRes = {
35-
comments: comments
36-
}
35+
type commentsRes = {comments: comments}
3736

3837
let fetchComments = async (): result<comments, string> => {
3938
open Json.Decode
4039

4140
let response = await Fetch.get("comments.json")
4241
let jsonRes = await response->Fetch.Response.json
43-
42+
4443
let jsonComment = Json.Decode.object(field => {
4544
author: field.required(. "author", string),
4645
text: field.required(. "text", string),
47-
id: field.required(. "id", int)
46+
id: field.required(. "id", int),
4847
})
4948

5049
let jsonComments = Json.Decode.object(field => {
5150
comments: field.required(. "comments", array(jsonComment)),
5251
})
5352

5453
switch jsonRes->Json.decode(jsonComments) {
55-
| Ok(decodedRes) => Ok(decodedRes.comments)
56-
| Error(e) => Error(e)
54+
| Ok(decodedRes) => Ok(decodedRes.comments)
55+
| Error(e) => Error(e)
5756
}
5857
}
5958
}
Lines changed: 60 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,95 @@
11
let reducer = (
2-
state: CommentFormTypes.state,
3-
action: CommentFormTypes.action
2+
state: CommentFormTypes.state,
3+
action: CommentFormTypes.action,
44
): CommentFormTypes.state => {
5-
switch (action) {
5+
switch action {
66
| SetAuthor(author) => {...state, author}
77
| SetText(text) => {...state, text}
8-
| SetFormType(form) => {...state, form: form}
8+
| SetFormType(form) => {...state, form}
99
}
1010
}
1111

12-
1312
@react.component
14-
let make = (~storeComment: ReScriptShowTypes.storeComment, ~disabled: bool, ~storeCommentError: bool) => {
13+
let make = (
14+
~storeComment: ReScriptShowTypes.storeComment,
15+
~disabled: bool,
16+
~storeCommentError: bool,
17+
) => {
1518
let (state, dispatch) = React.useReducer(
16-
reducer, {
19+
reducer,
20+
{
1721
author: "",
1822
text: "",
19-
form: Horizontal
20-
}
23+
form: Horizontal,
24+
},
2125
)
2226

23-
let handleAuthorChange = (event) => {
27+
let handleAuthorChange = event => {
2428
let value = ReactEvent.Form.currentTarget(event)["value"]
2529
SetAuthor(value)->dispatch
2630
}
2731

28-
let handleTextChange = (event) => {
32+
let handleTextChange = event => {
2933
let value = ReactEvent.Form.currentTarget(event)["value"]
3034
SetText(value)->dispatch
3135
}
3236

33-
let handleSubmit = (event) => {
37+
let handleSubmit = event => {
3438
ReactEvent.Form.preventDefault(event)
3539
storeComment({author: state.author, text: state.text})
3640
}
3741

38-
let forms: array<CommentFormTypes.formData> =
39-
[
42+
let forms: array<CommentFormTypes.formData> = [
4043
{formName: "Horizontal Form", formType: Horizontal},
4144
{formName: "Inline Form", formType: Inline},
42-
{formName: "Stacked Form", formType: Stacked}
45+
{formName: "Stacked Form", formType: Stacked},
4346
]
4447

4548
<div>
4649
<div className="flex gap-1 not-prose">
47-
{
48-
forms
49-
->Array.map(form
50-
=> (
51-
<button
52-
key={`form_${form.formName}`}
53-
className={`px-6 py-2 font-semibold border-0 rounded ${state.form == form.formType ? "text-sky-50 bg-sky-600" : "text-sky-600 hover:bg-gray-100"}`}
54-
onClick={event => SetFormType(form.formType)->dispatch}
55-
>
56-
{form.formName->React.string}
57-
</button>
58-
)
59-
)->React.array
60-
}
50+
{forms
51+
->Array.map(form =>
52+
<button
53+
key={`form_${form.formName}`}
54+
className={`px-6 py-2 font-semibold border-0 rounded ${state.form == form.formType
55+
? "text-sky-50 bg-sky-600"
56+
: "text-sky-600 hover:bg-gray-100"}`}
57+
onClick={event => SetFormType(form.formType)->dispatch}>
58+
{form.formName->React.string}
59+
</button>
60+
)
61+
->React.array}
6162
</div>
6263
<hr />
63-
{
64-
switch state.form {
65-
| Horizontal
66-
=> <HorizontalForm
67-
author={state.author}
68-
handleAuthorChange
69-
text={state.text}
70-
handleTextChange
71-
handleSubmit
72-
disabled
73-
/>
74-
| Stacked
75-
=> <StackedFrom
76-
author={state.author}
77-
handleAuthorChange
78-
text={state.text}
79-
handleTextChange
80-
handleSubmit
81-
disabled
82-
/>
83-
| Inline
84-
=> <InlineForm
85-
author={state.author}
86-
handleAuthorChange
87-
text={state.text}
88-
handleTextChange
89-
handleSubmit
90-
disabled
91-
/>
92-
}
93-
}
94-
95-
{
96-
storeCommentError ? <AlertError errorMsg="Can't store the comment!" /> : React.null
97-
}
64+
{switch state.form {
65+
| Horizontal =>
66+
<HorizontalForm
67+
author={state.author}
68+
handleAuthorChange
69+
text={state.text}
70+
handleTextChange
71+
handleSubmit
72+
disabled
73+
/>
74+
| Stacked =>
75+
<StackedFrom
76+
author={state.author}
77+
handleAuthorChange
78+
text={state.text}
79+
handleTextChange
80+
handleSubmit
81+
disabled
82+
/>
83+
| Inline =>
84+
<InlineForm
85+
author={state.author}
86+
handleAuthorChange
87+
text={state.text}
88+
handleTextChange
89+
handleSubmit
90+
disabled
91+
/>
92+
}}
93+
{storeCommentError ? <AlertError errorMsg="Can't store the comment!" /> : React.null}
9894
</div>
9995
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ type formDisplay = Horizontal | Inline | Stacked
22

33
type formData = {
44
formName: string,
5-
formType: formDisplay
5+
formType: formDisplay,
66
}
77

88
type state = {
99
author: string,
1010
text: string,
11-
form: formDisplay
11+
form: formDisplay,
1212
}
1313

1414
type action =
15-
| SetAuthor(string)
16-
| SetText(string)
17-
| SetFormType(formDisplay)
15+
| SetAuthor(string)
16+
| SetText(string)
17+
| SetFormType(formDisplay)

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

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,33 @@
11
@react.component
2-
let make = (
3-
~author,
4-
~handleAuthorChange,
5-
~text,
6-
~handleTextChange,
7-
~handleSubmit,
8-
~disabled
9-
) => {
10-
<form className="form-horizontal flex flex-col gap-4" onSubmit=handleSubmit disabled>
2+
let make = (~author, ~handleAuthorChange, ~text, ~handleTextChange, ~handleSubmit, ~disabled) => {
3+
<form className="form-horizontal flex flex-col gap-4" onSubmit=handleSubmit disabled>
114
<div className="flex flex-col gap-0 items-center lg:gap-4 lg:flex-row">
12-
<label className="w-full lg:w-2/12 lg:text-end shrink-0">{"Name"->React.string}</label>
13-
<input
5+
<label className="w-full lg:w-2/12 lg:text-end shrink-0"> {"Name"->React.string} </label>
6+
<input
147
type_="text"
158
className="px-3 py-1 leading-4 border border-gray-300 rounded w-full"
169
placeholder="Your Name"
1710
name="comment_author"
1811
id="comment_author"
19-
onChange=handleAuthorChange value={author}
12+
onChange=handleAuthorChange
13+
value={author}
2014
/>
2115
</div>
22-
2316
<div className="flex flex-col gap-0 items-center lg:gap-4 lg:flex-row">
24-
<label className="w-full lg:w-2/12 lg:text-end shrink-0">{"Text"->React.string}</label>
17+
<label className="w-full lg:w-2/12 lg:text-end shrink-0"> {"Text"->React.string} </label>
2518
<input
2619
type_="text"
2720
className="px-3 py-1 leading-4 border border-gray-300 rounded w-full"
2821
placeholder="Say something using markdown..."
2922
name="comment_text"
3023
id="comment_text"
31-
onChange=handleTextChange value={text}
24+
onChange=handleTextChange
25+
value={text}
3226
/>
3327
</div>
34-
3528
<div className="flex flex-col gap-0 lg:gap-4 lg:flex-row">
36-
<div className="hidden lg:block lg:w-2/12 grow-0"></div>
37-
<input
29+
<div className="hidden lg:block lg:w-2/12 grow-0" />
30+
<input
3831
type_="submit"
3932
className="self-start px-3 py-1 font-semibold border-0 rounded text-sky-50 bg-sky-600 hover:bg-sky-800"
4033
value="Post"

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
@react.component
2-
let make = (
3-
~author,
4-
~handleAuthorChange,
5-
~text,
6-
~handleTextChange,
7-
~handleSubmit,
8-
~disabled
9-
) => {
10-
<form
2+
let make = (~author, ~handleAuthorChange, ~text, ~handleTextChange, ~handleSubmit, ~disabled) => {
3+
<form
114
className="form-inline flex flex-col lg:flex-row flex-wrap gap-4"
125
onSubmit=handleSubmit
13-
disabled
14-
>
6+
disabled>
157
<div className="flex gap-2 items-center">
168
<label> {"Name"->React.string} </label>
179
<input
@@ -24,7 +16,6 @@ let make = (
2416
onChange=handleAuthorChange
2517
/>
2618
</div>
27-
2819
<div className="flex gap-2 items-center">
2920
<label> {"Text"->React.string} </label>
3021
<input
@@ -37,7 +28,6 @@ let make = (
3728
value={text}
3829
/>
3930
</div>
40-
4131
<div className="flex gap-2">
4232
<input
4333
type_="submit"

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

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,20 @@
11
@react.component
2-
let make = (
3-
~author,
4-
~handleAuthorChange,
5-
~text,
6-
~handleTextChange,
7-
~handleSubmit,
8-
~disabled
9-
) => {
10-
<form
11-
onSubmit=handleSubmit
12-
disabled
13-
className="flex flex-col gap-4"
14-
>
2+
let make = (~author, ~handleAuthorChange, ~text, ~handleTextChange, ~handleSubmit, ~disabled) => {
3+
<form onSubmit=handleSubmit disabled className="flex flex-col gap-4">
154
<div className="flex flex-col gap-0">
16-
<label className="w-full" > {"Name"->React.string} </label>
17-
<input
5+
<label className="w-full"> {"Name"->React.string} </label>
6+
<input
187
type_="text"
198
className="px-3 py-1 leading-4 border border-gray-300 rounded w-full"
209
placeholder="Your Name"
2110
name="comment_author"
2211
id="comment_author"
23-
onChange=handleAuthorChange value={author}
12+
onChange=handleAuthorChange
13+
value={author}
2414
/>
2515
</div>
26-
2716
<div className="flex flex-col gap-0">
28-
<label className="w-full" > {"Name"->React.string} </label>
17+
<label className="w-full"> {"Name"->React.string} </label>
2918
<input
3019
type_="text"
3120
className="px-3 py-1 leading-4 border border-gray-300 rounded w-full"
@@ -36,7 +25,6 @@ let make = (
3625
value={text}
3726
/>
3827
</div>
39-
4028
<div className="flex flex-col gap-0">
4129
<input
4230
type_="submit"

0 commit comments

Comments
 (0)