Unverified Commit 83c09e5b authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Remove with-reasonml-todo example (#62812)

## What?

Removes this outdated example.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes NEXT-2683
parent 2af956e1
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz
# testing
/coverage
# next.js
/.next/
/out/
# production
/build
# misc
.DS_Store
*.pem
# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# local env files
.env*.local
# vercel
.vercel
# typescript
*.tsbuildinfo
next-env.d.ts
# Example app using ReasonML & ReasonReact components
This example builds upon the original `with-reasonml` example to show how a
global state object can be used to track state across page within the application.
It is intended to show how to build a simple, stateful application using hooks
without the added complexity of a redux type library.
This example features:
- An app that mixes together JavaScript and ReasonML components and functions
- An app with two pages which has a common Counter component
- That Counter component maintain the counter inside its module. This is used
primarily to illustrate that modules get initialized once and their state
variables persist in runtime
## Deploy your own
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example):
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-reasonml-todo&project-name=with-reasonml-todo&repository-name=with-reasonml-todo)
## How to use
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example:
```bash
npx create-next-app --example with-reasonml-todo with-reasonml-app
# or
yarn create next-app --example with-reasonml-todo with-reasonml-app
# or
pnpm create next-app --example with-reasonml-todo with-reasonml-app
```
Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
### Recommendation:
Run BuckleScript build system `bsb -w` and `next -w` separately. For the sake
of simple convention, `npm run dev` run both `bsb` and `next` concurrently.
However, this doesn't offer the full [colorful and very, very, veeeery nice
error
output](https://reasonml.github.io/blog/2017/08/25/way-nicer-error-messages.html)
experience that ReasonML can offer, don't miss it!
There are 2 convenience scripts to facilitate running these separate processes:
1. `npm run dev:reason` - This script will start the ReasonML toolchain in
watch mode to re-compile whenever you make changes.
2. `npm run dev:next` - This script will start the next.js development server
so that you will be able to access your site at the location output by the
script. This will also hot reload as you make changes.
You should start the scripts in the presented order.
module Link = {
[@bs.module "next/link"] [@react.component]
external make:
(
~href: string=?,
~_as: string=?,
~prefetch: option(bool)=?,
~replace: option(bool)=?,
~shallow: option(bool)=?,
~passHref: option(bool)=?,
~children: React.element
) =>
React.element =
"default";
};
module Head = {
[@bs.module "next/head"] [@react.component]
external make: (~children: React.element) => React.element = "default";
};
module Error = {
[@bs.module "next/head"] [@react.component]
external make: (~statusCode: int, ~children: React.element) => React.element =
"default";
};
{
"name": "with-reasonml",
"sources": [
{
"dir": "components",
"subdirs": true
},
{
"dir": "pages",
"subdirs": true
},
{
"dir": "bindings",
"subdirs": true
}
],
"bs-dependencies": ["reason-react"],
"reason": { "react-jsx": 3 },
"package-specs": {
"module": "es6",
"in-source": true
},
"suffix": ".bs.js",
"bsc-flags": ["-bs-super-errors"],
"refmt": 3
}
let styles = ReactDOMRe.Style.make(~marginRight="10px", ());
[@react.component]
let make = () => {
<div>
<Next.Link href="/">
<a style=styles> {ReasonReact.string("Home")} </a>
</Next.Link>
<Next.Link href="/about">
<a style=styles> {ReasonReact.string("About")} </a>
</Next.Link>
</div>;
};
let default = make;
module TodoId = {
type t = string;
let prefix = ref(0);
let make = () => {
let prefix = Js.Math.random_int(1000000, 9999999)->string_of_int;
let suffix = Js.Date.now()->int_of_float->string_of_int;
{j|$prefix::$suffix|j}
}
};
module Todo = {
[@bs.deriving abstract]
type t = {
id: TodoId.t,
mutable finished: bool,
text: string,
};
let make = (~finished=false, text) => t(~id=TodoId.make(), ~finished, ~text);
let complete = t => {
t->finishedSet(true);
t
}
let isSame = (t1, t2) => t1->idGet === t2->idGet
};
module TodoList = {
type t = array(Todo.t);
let contains = (list, todo) => list->Belt.Array.some(Todo.isSame(todo));
let complete = (list, target) =>
list->Belt.Array.map(todo =>
todo->Todo.isSame(target) ? todo->Todo.complete : todo
);
let add = (list, todo) => {
if (!list->contains(todo)) {
list->Belt.Array.concat([|todo|])
} else {
// panic - should be unreachable
let id = todo->Todo.idGet;
Js.Exn.raiseError({j|Could not add todo $id, it already exists.|j});
}
}
let remove = (list, todo) =>
list->Belt.Array.keep(current => !todo->Todo.isSame(current));
}
type t = {
today: TodoList.t,
tomorrow: TodoList.t,
}
type day =
| Today
| Tomorrow;
type action =
| Add(day, Todo.t)
| Complete(day, Todo.t)
| Remove(day, Todo.t)
let appState = ref({ today: [||], tomorrow: [||] });
let add = (state, day, todo) => {
switch(day) {
| Today => { ...state, today: state.today->TodoList.add(todo) }
| Tomorrow => { ...state, tomorrow: state.tomorrow->TodoList.add(todo) }
}
}
let complete = (state, day, todo) => {
switch(day) {
| Today => { ...state, today: state.today->TodoList.complete(todo) }
| Tomorrow => { ...state, tomorrow: state.tomorrow->TodoList.complete(todo) }
}
}
let remove = (state, day, todo) => {
switch(day) {
| Today => { ...state, today: state.today->TodoList.remove(todo) }
| Tomorrow => { ...state, tomorrow: state.tomorrow->TodoList.remove(todo) }
}
}
let getDay = (state, day) => {
switch(day) {
| Today => state.today
| Tomorrow => state.tomorrow
}
}
let reducer = (state, action) => {
let newState = switch(action) {
| Add(day, todo) => add(state, day, todo)
| Complete(day, todo) => complete(state, day, todo)
| Remove(day, todo) => remove(state, day, todo)
};
appState := newState;
newState;
}
let useTodoReducer = () => React.useReducer(reducer, appState^);
type action =
| Change(string)
| NoChange
| Submit(TodoApp.day, TodoApp.action => unit)
type state = {
newTodo: string,
};
let reducer = (state, action) =>
switch(action) {
| Change(string) => { newTodo: string }
| NoChange => state
| Submit(day, appDispatch) =>
appDispatch(TodoApp.Add(day, TodoApp.Todo.make(state.newTodo)));
{ newTodo: "" }
};
[@react.component]
let make = (~day) => {
let (state, dispatch) = React.useReducer(reducer, { newTodo: "" });
let (appState, appDispatch) = TodoApp.useTodoReducer();
let todos = appState->TodoApp.getDay(day);
<div>
<ul>
(
todos->Belt.Array.map(todo => {
<li key=TodoApp.Todo.idGet(todo)>
<p> {ReasonReact.string(todo->TodoApp.Todo.textGet)}</p>
</li>
})
)->ReasonReact.array
</ul>
<p>
<input
placeholder="What needs to be done?"
value=state.newTodo
onChange={ev => dispatch(Change(ReactEvent.Form.target(ev)##value))}
onKeyDown={
ev =>
if (ReactEvent.Keyboard.keyCode(ev) === 13) {
ReactEvent.Keyboard.preventDefault(ev);
dispatch(Submit(day, appDispatch));
}
}
/>
<button onClick={ _ => dispatch(Submit(day, appDispatch)) }>
{React.string("Add")}
</button>
</p>
</div>
}
const withTM = require("next-transpile-modules")(["bs-platform"]);
module.exports = withTM({
pageExtensions: ["jsx", "js", "bs.js"],
});
{
"private": true,
"scripts": {
"dev": "concurrently \"bsb -clean-world -make-world -w\" \"next dev\"",
"dev:reason": "bsb -clean-world -make-world -w",
"dev:next": "next dev",
"build": "bsb -clean-world -make-world && next build",
"start": "next start"
},
"dependencies": {
"next": "latest",
"next-transpile-modules": "^9.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"reason-react": "^0.7.0"
},
"devDependencies": {
"@babel/core": "7.22.5",
"bs-platform": "7.2.2",
"concurrently": "^4.1.0"
}
}
[@react.component]
let make = () => {
<div>
<Header />
<p> {ReasonReact.string("Things to do TOMORROW!")} </p>
<TodoList day=TodoApp.Tomorrow />
</div>;
};
let default = make;
[@react.component]
let make = (~onServer) => {
<div>
<Header />
<p> {ReasonReact.string("Things todo TODAY!")} </p>
<p> {ReasonReact.string("onServer: " ++ string_of_bool(onServer))} </p>
<TodoList day=TodoApp.Today />
</div>;
};
let default = make;
let getInitialProps = context =>
Js.Promise.make((~resolve, ~reject as _) => {
let onServer =
switch (Js.Nullable.toOption(context##req)) {
| None => false
| Some(_) => true
};
resolve(. {"onServer": onServer});
});
let inject:
(
Js.t('a) => React.element,
{. "req": Js.Nullable.t(Js.t('a))} => Js.Promise.t(Js.t('a))
) =>
unit = [%bs.raw
{| (cls, fn) => cls.getInitialProps = fn |}
];
inject(default, getInitialProps);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment