Skip to content

Commit

Permalink
format rescript files
Browse files Browse the repository at this point in the history
  • Loading branch information
Yassa-hue committed Nov 3, 2023
1 parent d1a6ad2 commit 30033ff
Show file tree
Hide file tree
Showing 17 changed files with 175 additions and 205 deletions.
2 changes: 1 addition & 1 deletion app/views/pages/rescript.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= react_component "RescriptShow", prerender: false %>
<%= react_component "RescriptShow", prerender: true %>
25 changes: 12 additions & 13 deletions client/app/bundles/comments/rescript/Actions/Actions.res
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
module Create = {
type t = {
author: string,
text: string
text: string,
}

let storeComment = async (comment: t) => {
let _ = await Axios.post(
"comments.json",
{
author: comment.author,
text: comment.text
text: comment.text,
},
{
responseType: "json",
headers: { // see https://github.com/shakacode/react_on_rails/blob/249c69812474e0f532df432581bf5e618df0e1ec/node_package/src/Authenticity.ts#L13C1-L18C1
headers: {
// see https://github.com/shakacode/react_on_rails/blob/249c69812474e0f532df432581bf5e618df0e1ec/node_package/src/Authenticity.ts#L13C1-L18C1
"X-CSRF-Token": ReactOnRails.authenticityToken(),
"X-Requested-With": "XMLHttpRequest",
}
}
},
},
)
}
}
Expand All @@ -26,34 +27,32 @@ module Fetch = {
type t = {
author: string,
text: string,
id: int
id: int,
}

type comments = array<t>

type commentsRes = {
comments: comments
}
type commentsRes = {comments: comments}

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

let response = await Fetch.get("comments.json")
let jsonRes = await response->Fetch.Response.json

let jsonComment = Json.Decode.object(field => {
author: field.required(. "author", string),
text: field.required(. "text", string),
id: field.required(. "id", int)
id: field.required(. "id", int),
})

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

switch jsonRes->Json.decode(jsonComments) {
| Ok(decodedRes) => Ok(decodedRes.comments)
| Error(e) => Error(e)
| Ok(decodedRes) => Ok(decodedRes.comments)
| Error(e) => Error(e)
}
}
}
124 changes: 60 additions & 64 deletions client/app/bundles/comments/rescript/CommentForm/CommentForm.res
Original file line number Diff line number Diff line change
@@ -1,99 +1,95 @@
let reducer = (
state: CommentFormTypes.state,
action: CommentFormTypes.action
state: CommentFormTypes.state,
action: CommentFormTypes.action,
): CommentFormTypes.state => {
switch (action) {
switch action {
| SetAuthor(author) => {...state, author}
| SetText(text) => {...state, text}
| SetFormType(form) => {...state, form: form}
| SetFormType(form) => {...state, form}
}
}


@react.component
let make = (~storeComment: ReScriptShowTypes.storeComment, ~disabled: bool, ~storeCommentError: bool) => {
let make = (
~storeComment: ReScriptShowTypes.storeComment,
~disabled: bool,
~storeCommentError: bool,
) => {
let (state, dispatch) = React.useReducer(
reducer, {
reducer,
{
author: "",
text: "",
form: Horizontal
}
form: Horizontal,
},
)

let handleAuthorChange = (event) => {
let handleAuthorChange = event => {
let value = ReactEvent.Form.currentTarget(event)["value"]
SetAuthor(value)->dispatch
}

let handleTextChange = (event) => {
let handleTextChange = event => {
let value = ReactEvent.Form.currentTarget(event)["value"]
SetText(value)->dispatch
}

let handleSubmit = (event) => {
let handleSubmit = event => {
ReactEvent.Form.preventDefault(event)
storeComment({author: state.author, text: state.text})
}

let forms: array<CommentFormTypes.formData> =
[
let forms: array<CommentFormTypes.formData> = [
{formName: "Horizontal Form", formType: Horizontal},
{formName: "Inline Form", formType: Inline},
{formName: "Stacked Form", formType: Stacked}
{formName: "Stacked Form", formType: Stacked},
]

<div>
<div className="flex gap-1 not-prose">
{
forms
->Array.map(form
=> (
<button
key={`form_${form.formName}`}
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"}`}
onClick={event => SetFormType(form.formType)->dispatch}
>
{form.formName->React.string}
</button>
)
)->React.array
}
{forms
->Array.map(form =>
<button
key={`form_${form.formName}`}
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"}`}
onClick={event => SetFormType(form.formType)->dispatch}>
{form.formName->React.string}
</button>
)
->React.array}
</div>
<hr />
{
switch state.form {
| Horizontal
=> <HorizontalForm
author={state.author}
handleAuthorChange
text={state.text}
handleTextChange
handleSubmit
disabled
/>
| Stacked
=> <StackedFrom
author={state.author}
handleAuthorChange
text={state.text}
handleTextChange
handleSubmit
disabled
/>
| Inline
=> <InlineForm
author={state.author}
handleAuthorChange
text={state.text}
handleTextChange
handleSubmit
disabled
/>
}
}

{
storeCommentError ? <AlertError errorMsg="Can't store the comment!" /> : React.null
}
{switch state.form {
| Horizontal =>
<HorizontalForm
author={state.author}
handleAuthorChange
text={state.text}
handleTextChange
handleSubmit
disabled
/>
| Stacked =>
<StackedFrom
author={state.author}
handleAuthorChange
text={state.text}
handleTextChange
handleSubmit
disabled
/>
| Inline =>
<InlineForm
author={state.author}
handleAuthorChange
text={state.text}
handleTextChange
handleSubmit
disabled
/>
}}
{storeCommentError ? <AlertError errorMsg="Can't store the comment!" /> : React.null}
</div>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ type formDisplay = Horizontal | Inline | Stacked

type formData = {
formName: string,
formType: formDisplay
formType: formDisplay,
}

type state = {
author: string,
text: string,
form: formDisplay
form: formDisplay,
}

type action =
| SetAuthor(string)
| SetText(string)
| SetFormType(formDisplay)
| SetAuthor(string)
| SetText(string)
| SetFormType(formDisplay)
Original file line number Diff line number Diff line change
@@ -1,40 +1,33 @@
@react.component
let make = (
~author,
~handleAuthorChange,
~text,
~handleTextChange,
~handleSubmit,
~disabled
) => {
<form className="form-horizontal flex flex-col gap-4" onSubmit=handleSubmit disabled>
let make = (~author, ~handleAuthorChange, ~text, ~handleTextChange, ~handleSubmit, ~disabled) => {
<form className="form-horizontal flex flex-col gap-4" onSubmit=handleSubmit disabled>
<div className="flex flex-col gap-0 items-center lg:gap-4 lg:flex-row">
<label className="w-full lg:w-2/12 lg:text-end shrink-0">{"Name"->React.string}</label>
<input
<label className="w-full lg:w-2/12 lg:text-end shrink-0"> {"Name"->React.string} </label>
<input
type_="text"
className="px-3 py-1 leading-4 border border-gray-300 rounded w-full"
placeholder="Your Name"
name="comment_author"
id="comment_author"
onChange=handleAuthorChange value={author}
onChange=handleAuthorChange
value={author}
/>
</div>

<div className="flex flex-col gap-0 items-center lg:gap-4 lg:flex-row">
<label className="w-full lg:w-2/12 lg:text-end shrink-0">{"Text"->React.string}</label>
<label className="w-full lg:w-2/12 lg:text-end shrink-0"> {"Text"->React.string} </label>
<input
type_="text"
className="px-3 py-1 leading-4 border border-gray-300 rounded w-full"
placeholder="Say something using markdown..."
name="comment_text"
id="comment_text"
onChange=handleTextChange value={text}
onChange=handleTextChange
value={text}
/>
</div>

<div className="flex flex-col gap-0 lg:gap-4 lg:flex-row">
<div className="hidden lg:block lg:w-2/12 grow-0"></div>
<input
<div className="hidden lg:block lg:w-2/12 grow-0" />
<input
type_="submit"
className="self-start px-3 py-1 font-semibold border-0 rounded text-sky-50 bg-sky-600 hover:bg-sky-800"
value="Post"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
@react.component
let make = (
~author,
~handleAuthorChange,
~text,
~handleTextChange,
~handleSubmit,
~disabled
) => {
<form
let make = (~author, ~handleAuthorChange, ~text, ~handleTextChange, ~handleSubmit, ~disabled) => {
<form
className="form-inline flex flex-col lg:flex-row flex-wrap gap-4"
onSubmit=handleSubmit
disabled
>
disabled>
<div className="flex gap-2 items-center">
<label> {"Name"->React.string} </label>
<input
Expand All @@ -24,7 +16,6 @@ let make = (
onChange=handleAuthorChange
/>
</div>

<div className="flex gap-2 items-center">
<label> {"Text"->React.string} </label>
<input
Expand All @@ -37,7 +28,6 @@ let make = (
value={text}
/>
</div>

<div className="flex gap-2">
<input
type_="submit"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
@react.component
let make = (
~author,
~handleAuthorChange,
~text,
~handleTextChange,
~handleSubmit,
~disabled
) => {
<form
onSubmit=handleSubmit
disabled
className="flex flex-col gap-4"
>
let make = (~author, ~handleAuthorChange, ~text, ~handleTextChange, ~handleSubmit, ~disabled) => {
<form onSubmit=handleSubmit disabled className="flex flex-col gap-4">
<div className="flex flex-col gap-0">
<label className="w-full" > {"Name"->React.string} </label>
<input
<label className="w-full"> {"Name"->React.string} </label>
<input
type_="text"
className="px-3 py-1 leading-4 border border-gray-300 rounded w-full"
placeholder="Your Name"
name="comment_author"
id="comment_author"
onChange=handleAuthorChange value={author}
onChange=handleAuthorChange
value={author}
/>
</div>

<div className="flex flex-col gap-0">
<label className="w-full" > {"Name"->React.string} </label>
<label className="w-full"> {"Name"->React.string} </label>
<input
type_="text"
className="px-3 py-1 leading-4 border border-gray-300 rounded w-full"
Expand All @@ -36,7 +25,6 @@ let make = (
value={text}
/>
</div>

<div className="flex flex-col gap-0">
<input
type_="submit"
Expand Down
Loading

0 comments on commit 30033ff

Please sign in to comment.