Skip to content

Commit 2781900

Browse files
committed
Add rescript example (#442)
* Add rescript build configurations * Create rescript page components * Add rescript deps
1 parent 9d919d5 commit 2781900

29 files changed

+1220
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,5 @@ client/app/libs/i18n/default.js
4747
/yarn-error.log
4848
yarn-debug.log*
4949
.yarn-integrity
50+
51+
lib/bs

Procfile.dev

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Procfile for development using HMR
22
# You can run these commands in separate shells
3+
rescript: yarn res:dev
34
redis: redis-server
45
rails: bundle exec rails s -p 3000
56
wp-client: HMR=true RAILS_ENV=development NODE_ENV=development bin/shakapacker-dev-server

app/controllers/pages_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ def no_router
3636

3737
def simple; end
3838

39+
def rescript; end
40+
3941
private
4042

4143
def set_comments

app/views/pages/rescript.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= react_component "RescriptPage" %>

bsconfig.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "react-webpack-rails-tutorial",
3+
"sources": [
4+
{
5+
"dir": "client/app/bundles/comments/src",
6+
"subdirs": true
7+
}
8+
],
9+
"package-specs": [
10+
{
11+
"module": "es6",
12+
"in-source": true
13+
}
14+
],
15+
"bsc-flags": [
16+
"-bs-super-errors",
17+
"-open JsonCombinators"
18+
],
19+
"suffix": ".bs.mjs",
20+
"bs-dependencies": [
21+
"@rescript/react",
22+
"@rescript/core",
23+
"@glennsl/rescript-fetch",
24+
"@glennsl/rescript-json-combinators"
25+
],
26+
"jsx": {
27+
"version": 4,
28+
"mode": "automatic"
29+
}
30+
}

client/app/bundles/comments/components/NavigationBar/NavigationBar.jsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ function NavigationBar(props) {
4141
>
4242
<a href={paths.ROUTER_PATH}>React Router Demo</a>
4343
</li>
44+
<li className={classNames({ active: pathname === paths.RESCRIPT_PATH })}>
45+
<a href={paths.RESCRIPT_PATH}>Rescript Demo</a>
46+
</li>
4447
<li className={classNames({ active: pathname === paths.NO_ROUTER_PATH })}>
4548
<a href={paths.NO_ROUTER_PATH}>React Demo</a>
4649
</li>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export const ROUTER_PATH = '/';
22
export const REACT_ROUTER_PATH = '/react-router';
33
export const NO_ROUTER_PATH = '/no-router';
4+
export const RESCRIPT_PATH = '/rescript';
45
export const SIMPLE_REACT_PATH = '/simple';
56
export const STIMULUS_PATH = '/stimulus';
67
export const RAILS_PATH = '/comments';
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Curry from "rescript/lib/es6/curry.js";
4+
import * as Axios from "axios";
5+
import * as Js_exn from "rescript/lib/es6/js_exn.js";
6+
import ReactOnRails from "react-on-rails";
7+
import * as Json$JsonCombinators from "@glennsl/rescript-json-combinators/src/Json.bs.mjs";
8+
import * as Json_Decode$JsonCombinators from "@glennsl/rescript-json-combinators/src/Json_Decode.bs.mjs";
9+
10+
async function fetchComments(param) {
11+
var response = await fetch("comments.json");
12+
var jsonRes = await response.json();
13+
var jsonComment = Json_Decode$JsonCombinators.object(function (field) {
14+
return {
15+
author: field.required("author", Json_Decode$JsonCombinators.string),
16+
text: field.required("text", Json_Decode$JsonCombinators.string),
17+
id: field.required("id", Json_Decode$JsonCombinators.$$int)
18+
};
19+
});
20+
var jsonComments = Json_Decode$JsonCombinators.object(function (field) {
21+
return {
22+
comments: field.required("comments", Json_Decode$JsonCombinators.array(jsonComment))
23+
};
24+
});
25+
var decodedRes = Json$JsonCombinators.decode(jsonRes, jsonComments);
26+
if (decodedRes.TAG === /* Ok */0) {
27+
return decodedRes._0.comments;
28+
} else {
29+
return Js_exn.raiseError(decodedRes._0);
30+
}
31+
}
32+
33+
async function storeComment(author, text) {
34+
await Axios.post("comments.json", {
35+
author: author,
36+
text: text
37+
}, {
38+
responseType: "json",
39+
headers: Curry._1(ReactOnRails.default.authenticityHeaders, undefined)
40+
});
41+
}
42+
43+
var Api = {
44+
fetchComments: fetchComments,
45+
storeComment: storeComment
46+
};
47+
48+
export {
49+
Api ,
50+
}
51+
/* axios Not a pure module */
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// ReactOnRails binding
2+
type rorDefault = {
3+
authenticityHeaders: unit => {.}
4+
}
5+
type rorModule = {
6+
default: rorDefault
7+
}
8+
@module("react-on-rails") external ror: rorModule = "default";
9+
10+
type storeCommentData = {
11+
author: string,
12+
text: string
13+
}
14+
15+
// axios binding
16+
type reqOptions = {
17+
responseType: string,
18+
headers: {.}
19+
}
20+
@module("axios") external post: (string, storeCommentData, reqOptions) => promise<unit> = "post";
21+
22+
module Api = {
23+
type comment = {
24+
author: string,
25+
text: string,
26+
id: int
27+
};
28+
29+
type comments = array<comment>
30+
31+
type commentsRes = {
32+
comments: comments
33+
}
34+
35+
let fetchComments = async (): comments => {
36+
open Json.Decode;
37+
38+
let response = await Fetch.get("comments.json")
39+
let jsonRes = await response->Fetch.Response.json
40+
41+
let jsonComment = Json.Decode.object(field => {
42+
author: field.required(. "author", string),
43+
text: field.required(. "text", string),
44+
id: field.required(. "id", int)
45+
})
46+
47+
let jsonComments = Json.Decode.object(field => {
48+
comments: field.required(. "comments", array(jsonComment)),
49+
})
50+
51+
switch jsonRes->Json.decode(jsonComments) {
52+
| Ok(decodedRes) => decodedRes.comments
53+
| Error(err) => Js.Exn.raiseError(err)
54+
}
55+
}
56+
57+
let storeComment = async (author: string, text: string) => {
58+
let _ = await post(
59+
"comments.json",
60+
{
61+
author: author,
62+
text: text
63+
},
64+
{
65+
responseType: "json",
66+
headers:ror.default.authenticityHeaders()
67+
}
68+
)
69+
70+
71+
// setIsSaving(_ => true)
72+
// try {
73+
// let _ = await post(
74+
// "comments.json",
75+
// {
76+
// author: author,
77+
// text: text
78+
// },
79+
// {
80+
// responseType: "json",
81+
// headers:ror.default.authenticityHeaders()
82+
// }
83+
// )
84+
// setIsSaving(_ => false)
85+
// let comments = await fetchComments()
86+
// setComments(_ => comments)
87+
// } catch {
88+
// | _ => setError(_ => true)
89+
// }
90+
}
91+
}

0 commit comments

Comments
 (0)