Skip to content

Commit

Permalink
Add rescript example (#442)
Browse files Browse the repository at this point in the history
* Add rescript build configurations

* Create rescript page components

* Add rescript deps
  • Loading branch information
Yassa-hue committed Nov 3, 2023
1 parent 5762d08 commit 075856e
Show file tree
Hide file tree
Showing 38 changed files with 1,649 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ client/app/libs/i18n/default.js
/yarn-error.log
yarn-debug.log*
.yarn-integrity

lib/bs
1 change: 1 addition & 0 deletions Procfile.dev
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Procfile for development using HMR
# You can run these commands in separate shells
rescript: yarn res:dev
redis: redis-server
rails: bundle exec rails s -p 3000
wp-client: HMR=true RAILS_ENV=development NODE_ENV=development bin/shakapacker-dev-server
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def no_router

def simple; end

def rescript; end

private

def set_comments
Expand Down
1 change: 1 addition & 0 deletions app/views/pages/rescript.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= react_component "RescriptPage" %>
30 changes: 30 additions & 0 deletions bsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "react-webpack-rails-tutorial",
"sources": [
{
"dir": "client/app/bundles/comments/src",
"subdirs": true
}
],
"package-specs": [
{
"module": "es6",
"in-source": true
}
],
"bsc-flags": [
"-bs-super-errors",
"-open JsonCombinators"
],
"suffix": ".bs.mjs",
"bs-dependencies": [
"@rescript/react",
"@rescript/core",
"@glennsl/rescript-fetch",
"@glennsl/rescript-json-combinators"
],
"jsx": {
"version": 4,
"mode": "automatic"
}
}
1 change: 1 addition & 0 deletions client/app/bundles/comments/constants/paths.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const ROUTER_PATH = '/';
export const REACT_ROUTER_PATH = '/react-router';
export const NO_ROUTER_PATH = '/no-router';
export const RESCRIPT_PATH = '/rescript';
export const SIMPLE_REACT_PATH = '/simple';
export const STIMULUS_PATH = '/stimulus';
export const RAILS_PATH = '/comments';
51 changes: 51 additions & 0 deletions client/app/bundles/comments/src/Actions/Actions.bs.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Curry from "rescript/lib/es6/curry.js";
import * as Axios from "axios";
import * as Js_exn from "rescript/lib/es6/js_exn.js";
import ReactOnRails from "react-on-rails";
import * as Json$JsonCombinators from "@glennsl/rescript-json-combinators/src/Json.bs.mjs";
import * as Json_Decode$JsonCombinators from "@glennsl/rescript-json-combinators/src/Json_Decode.bs.mjs";

async function fetchComments(param) {
var response = await fetch("comments.json");
var jsonRes = await response.json();
var jsonComment = Json_Decode$JsonCombinators.object(function (field) {
return {
author: field.required("author", Json_Decode$JsonCombinators.string),
text: field.required("text", Json_Decode$JsonCombinators.string),
id: field.required("id", Json_Decode$JsonCombinators.$$int)
};
});
var jsonComments = Json_Decode$JsonCombinators.object(function (field) {
return {
comments: field.required("comments", Json_Decode$JsonCombinators.array(jsonComment))
};
});
var decodedRes = Json$JsonCombinators.decode(jsonRes, jsonComments);
if (decodedRes.TAG === /* Ok */0) {
return decodedRes._0.comments;
} else {
return Js_exn.raiseError(decodedRes._0);
}
}

async function storeComment(author, text) {
await Axios.post("comments.json", {
author: author,
text: text
}, {
responseType: "json",
headers: Curry._1(ReactOnRails.default.authenticityHeaders, undefined)
});
}

var Api = {
fetchComments: fetchComments,
storeComment: storeComment
};

export {
Api ,
}
/* axios Not a pure module */
91 changes: 91 additions & 0 deletions client/app/bundles/comments/src/Actions/Actions.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// ReactOnRails binding
type rorDefault = {
authenticityHeaders: unit => {.}
}
type rorModule = {
default: rorDefault
}
@module("react-on-rails") external ror: rorModule = "default";

type storeCommentData = {
author: string,
text: string
}

// axios binding
type reqOptions = {
responseType: string,
headers: {.}
}
@module("axios") external post: (string, storeCommentData, reqOptions) => promise<unit> = "post";

module Api = {
type comment = {
author: string,
text: string,
id: int
};

type comments = array<comment>

type commentsRes = {
comments: comments
}

let fetchComments = async (): comments => {
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)
})

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

switch jsonRes->Json.decode(jsonComments) {
| Ok(decodedRes) => decodedRes.comments
| Error(err) => Js.Exn.raiseError(err)
}
}

let storeComment = async (author: string, text: string) => {
let _ = await post(
"comments.json",
{
author: author,
text: text
},
{
responseType: "json",
headers:ror.default.authenticityHeaders()
}
)


// setIsSaving(_ => true)
// try {
// let _ = await post(
// "comments.json",
// {
// author: author,
// text: text
// },
// {
// responseType: "json",
// headers:ror.default.authenticityHeaders()
// }
// )
// setIsSaving(_ => false)
// let comments = await fetchComments()
// setComments(_ => comments)
// } catch {
// | _ => setError(_ => true)
// }
}
}
2 changes: 2 additions & 0 deletions client/app/bundles/comments/src/Actions/Types.bs.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Generated by ReScript, PLEASE EDIT WITH CARE
/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */
Loading

0 comments on commit 075856e

Please sign in to comment.