Skip to content

Commit 59b515f

Browse files
authored
Rename tutorial Param to Slug (ocaml#544)
1 parent 0d29cc8 commit 59b515f

File tree

3 files changed

+49
-33
lines changed

3 files changed

+49
-33
lines changed

pages/[lang]/resources/tutorials/[tutorial].res

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ module T = {
99
}
1010
include Jsonable.Unsafe
1111

12-
module Params = Pages.Params.Lang.Tutorial
12+
module Params = Pages.Params.Lang.Slug({
13+
let name = "tutorial"
14+
})
1315

1416
@react.component
1517
let make = (
1618
~content as {source, title, pageDescription, tableOfContents},
17-
~params as {Params.lang: _, tutorial: _},
19+
~params as {Params.lang: _, slug: _},
1820
) => {
1921
<>
2022
// TODO: should this have a constrained width? what does tailwind do?
@@ -39,7 +41,7 @@ module T = {
3941
{Ood.Tutorial.slug: slug, body_html, title, description, toc_html}: Ood.Tutorial.t,
4042
) => {
4143
(
42-
{Params.lang: #en, tutorial: slug},
44+
{Params.lang: #en, slug: slug},
4345
{
4446
source: body_html,
4547
title: title,

src/Pages.res

+36-26
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ module Params = {
3434
}
3535

3636
let lang = P.field("lang", Lang.ofJson)
37-
let tutorial = P.field("tutorial", Js.Json.decodeString)
3837

3938
module Lang = {
4039
type t = {lang: Lang.t}
@@ -44,15 +43,20 @@ module Params = {
4443
let toJson = ({lang}: t): Js.Json.t =>
4544
Js.Json.object_(Js.Dict.fromArray([("lang", lang->Lang.toJson)]))
4645

47-
module Tutorial = {
48-
type t = {lang: Lang.t, tutorial: string}
49-
let make = (lang, tutorial) => {lang: lang, tutorial: tutorial}
46+
module Slug = (
47+
Arg: {
48+
let name: string
49+
},
50+
) => {
51+
type t = {lang: Lang.t, slug: string}
52+
let slug = P.field(Arg.name, Js.Json.decodeString)
53+
let make = (lang, slug) => {lang: lang, slug: slug}
5054

5155
let ofJson = (json: Js.Json.t): option<t> =>
52-
P.return(make)->P.ap(lang)->P.ap(tutorial)->P.parse(json)
53-
let toJson = ({lang, tutorial}: t): Js.Json.t =>
56+
P.return(make)->P.ap(lang)->P.ap(slug)->P.parse(json)
57+
let toJson = ({lang, slug}: t): Js.Json.t =>
5458
Js.Json.object_(
55-
Js.Dict.fromArray([("lang", lang->Lang.toJson), ("tutorial", tutorial->Js.Json.string)]),
59+
Js.Dict.fromArray([("lang", lang->Lang.toJson), (Arg.name, slug->Js.Json.string)]),
5660
)
5761
}
5862
}
@@ -62,11 +66,12 @@ module type S = {
6266
type t
6367
type props<'content, 'params>
6468
type params
69+
type json1<'a> = Js.Json.t
6570

6671
@react.component
6772
let make: (~content: t, ~params: params) => React.element
68-
let getStaticProps: Next.GetStaticProps.t<props<t, params>, params, void>
69-
let getStaticPaths: Next.GetStaticPaths.t<params>
73+
let getStaticProps: Next.GetStaticProps.t<props<t, params>, json1<params>, void>
74+
let getStaticPaths: Next.GetStaticPaths.t<json1<params>>
7075
let default: props<Js.Json.t, Js.Json.t> => React.element
7176
}
7277

@@ -106,21 +111,26 @@ module Make = (Arg: Arg): (S with type t := Arg.t and type params = Arg.Params.t
106111

107112
type props<'a, 'b> = Props.t<'a, 'b>
108113
type params = Arg.Params.t
109-
110-
let getStaticProps: Next.GetStaticProps.t<props<Arg.t, params>, params, void> = ctx => {
111-
Arg.getContent(ctx.params) |> Js.Promise.then_(content => {
112-
switch content {
113-
| None =>
114-
failwith(
115-
"BUG: No content found for params: " ++ ctx.params->Arg.Params.toJson->Js.Json.stringify,
116-
)
117-
| Some(content) =>
118-
let props = {Props.content: content, params: ctx.params}
119-
Js.Promise.resolve({
120-
"props": props->Props.toJson(Arg.toJson, Arg.Params.toJson),
121-
})
122-
}
123-
})
114+
type json1<'a> = Js.Json.t
115+
116+
let getStaticProps: Next.GetStaticProps.t<props<Arg.t, params>, json1<params>, void> = ctx => {
117+
switch Arg.Params.ofJson(ctx.params) {
118+
| None => failwith("BUG: Unable to parse params")
119+
| Some(params: Arg.Params.t) =>
120+
Arg.getContent(params) |> Js.Promise.then_(content => {
121+
switch content {
122+
| None =>
123+
failwith(
124+
"BUG: No content found for params: " ++ params->Arg.Params.toJson->Js.Json.stringify,
125+
)
126+
| Some(content) =>
127+
let props = {Props.content: content, params: params}
128+
Js.Promise.resolve({
129+
"props": props->Props.toJson(Arg.toJson, Arg.Params.toJson),
130+
})
131+
}
132+
})
133+
}
124134
}
125135

126136
let default = (props: props<Js.Json.t, Js.Json.t>) => {
@@ -134,12 +144,12 @@ module Make = (Arg: Arg): (S with type t := Arg.t and type params = Arg.Params.t
134144
}
135145
}
136146

137-
let getStaticPaths: Next.GetStaticPaths.t<Arg.Params.t> = () => {
147+
let getStaticPaths: Next.GetStaticPaths.t<json1<Arg.Params.t>> = () => {
138148
let params = Arg.getParams()
139149
params |> Js.Promise.then_(params =>
140150
Js.Promise.resolve({
141151
Next.GetStaticPaths.paths: params->Belt.Array.map(params => {
142-
Next.GetStaticPaths.params: params,
152+
Next.GetStaticPaths.params: Arg.Params.toJson(params),
143153
}),
144154
fallback: false,
145155
})

src/Pages.resi

+8-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ module Params: {
77
type t = {lang: Lang.t}
88
include S with type t := t
99

10-
module Tutorial: {
11-
type t = {lang: Lang.t, tutorial: string}
10+
module Slug: {
11+
let name: string
12+
} =>
13+
{
14+
type t = {lang: Lang.t, slug: string}
1215
include S with type t := t
1316
}
1417
}
@@ -18,12 +21,13 @@ module type S = {
1821
type t
1922
type props<'content, 'params>
2023
type params
24+
type json1<'a> = Js.Json.t
2125

2226
@react.component
2327
let make: (~content: t, ~params: params) => React.element
2428

25-
let getStaticProps: Next.GetStaticProps.t<props<t, params>, params, void>
26-
let getStaticPaths: Next.GetStaticPaths.t<params>
29+
let getStaticProps: Next.GetStaticProps.t<props<t, params>, json1<params>, void>
30+
let getStaticPaths: Next.GetStaticPaths.t<json1<params>>
2731
let default: props<Js.Json.t, Js.Json.t> => React.element
2832
}
2933

0 commit comments

Comments
 (0)