Skip to content

Commit

Permalink
Extract about-us string
Browse files Browse the repository at this point in the history
Refs #1817
  • Loading branch information
thewilkybarkid committed Nov 12, 2024
1 parent ff71f33 commit 5cc5081
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 31 deletions.
5 changes: 5 additions & 0 deletions locales/en-US/about-us.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"title": {
"message": "About us"
}
}
20 changes: 13 additions & 7 deletions src/about-us.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@ import { pipe } from 'fp-ts/lib/function.js'
import { getPage } from './ghost.js'
import { type Html, fixHeadingLevels, html, plainText } from './html.js'
import { havingProblemsPage } from './http-error.js'
import { type SupportedLocale, translate } from './locales/index.js'
import { PageResponse } from './response.js'
import { aboutUsMatch } from './routes.js'

export const aboutUs = pipe(
getPage('6154aa157741400e8722bb14'),
RTE.matchW(() => havingProblemsPage, createPage),
)
export const aboutUs = (locale: SupportedLocale) =>
pipe(
RTE.Do,
RTE.apS('content', getPage('6154aa157741400e8722bb14')),
RTE.let('locale', () => locale),
RTE.matchW(() => havingProblemsPage, createPage),
)

function createPage({ content, locale }: { content: Html; locale: SupportedLocale }) {
const t = translate(locale)

function createPage(content: Html) {
return PageResponse({
title: plainText`About us`,
title: plainText(t('about-us', 'title')()),
main: html`
<h1>About us</h1>
<h1>${t('about-us', 'title')()}</h1>
${fixHeadingLevels(1, content)}
`,
Expand Down
8 changes: 7 additions & 1 deletion src/app-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,13 @@ const router: P.Parser<RM.ReaderMiddleware<RouterEnv, StatusOpen, ResponseEnded,
pipe(
RM.of({}),
RM.apS('user', maybeGetUser),
RM.apSW('response', RM.fromReaderTask(aboutUs)),
RM.apSW(
'response',
pipe(
RM.asks((env: RouterEnv) => env.locale),
RM.chainReaderTaskKW(aboutUs),
),
),
RM.ichainW(handleResponse),
),
),
Expand Down
49 changes: 26 additions & 23 deletions test/about-us.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,35 @@ import * as fc from './fc.js'
import { shouldNotBeCalled } from './should-not-be-called.js'

describe('aboutUs', () => {
test.prop([fc.string({ unit: fc.alphanumeric(), minLength: 1 })])('when the page can be loaded', async key => {
const fetch = fetchMock.sandbox().getOnce(
{
url: 'https://content.prereview.org/ghost/api/content/pages/6154aa157741400e8722bb14',
query: { key },
},
{ body: { pages: [{ html: '<p>Foo<script>bar</script></p>' }] } },
)
test.prop([fc.supportedLocale(), fc.string({ unit: fc.alphanumeric(), minLength: 1 })])(
'when the page can be loaded',
async (locale, key) => {
const fetch = fetchMock.sandbox().getOnce(
{
url: 'https://content.prereview.org/ghost/api/content/pages/6154aa157741400e8722bb14',
query: { key },
},
{ body: { pages: [{ html: '<p>Foo<script>bar</script></p>' }] } },
)

const actual = await _.aboutUs({ fetch, ghostApi: { key }, sleep: shouldNotBeCalled })()
const actual = await _.aboutUs(locale)({ fetch, ghostApi: { key }, sleep: shouldNotBeCalled })()

expect(actual).toStrictEqual({
_tag: 'PageResponse',
canonical: format(aboutUsMatch.formatter, {}),
current: 'about-us',
status: Status.OK,
title: expect.anything(),
main: expect.anything(),
skipToLabel: 'main',
js: [],
})
})
expect(actual).toStrictEqual({
_tag: 'PageResponse',
canonical: format(aboutUsMatch.formatter, {}),
current: 'about-us',
status: Status.OK,
title: expect.anything(),
main: expect.anything(),
skipToLabel: 'main',
js: [],
})
},
)

test.prop([fc.string({ unit: fc.alphanumeric(), minLength: 1 }), fc.fetchResponse()])(
test.prop([fc.supportedLocale(), fc.string({ unit: fc.alphanumeric(), minLength: 1 }), fc.fetchResponse()])(
'when the page cannot be loaded',
async (key, response) => {
async (locale, key, response) => {
const fetch = fetchMock.sandbox().getOnce(
{
url: 'begin:https://content.prereview.org/ghost/api/content/pages/6154aa157741400e8722bb14?',
Expand All @@ -43,7 +46,7 @@ describe('aboutUs', () => {
response,
)

const actual = await _.aboutUs({ fetch, ghostApi: { key }, sleep: shouldNotBeCalled })()
const actual = await _.aboutUs(locale)({ fetch, ghostApi: { key }, sleep: shouldNotBeCalled })()

expect(actual).toStrictEqual({
_tag: 'PageResponse',
Expand Down

0 comments on commit 5cc5081

Please sign in to comment.