From fc06690b15f3b625dfe398b1062b91ba585026b8 Mon Sep 17 00:00:00 2001 From: Daniel Haarhoff Date: Fri, 18 Oct 2024 10:13:27 +0100 Subject: [PATCH] Extract strings from write-review/change-author-form Refs: #1817 --- locales/en-US/change-author-form.json | 32 ++++++ locales/en-US/write-review-shared.json | 3 + .../add-author-page/add-author-form.ts | 4 +- .../change-author-page/change-author-form.ts | 105 +++++++++--------- src/write-review/shared-elements.ts | 3 + 5 files changed, 90 insertions(+), 57 deletions(-) create mode 100644 locales/en-US/change-author-form.json diff --git a/locales/en-US/change-author-form.json b/locales/en-US/change-author-form.json new file mode 100644 index 000000000..b6b111b1e --- /dev/null +++ b/locales/en-US/change-author-form.json @@ -0,0 +1,32 @@ +{ + "title": { + "message": "Change {name}’s details – PREreview of “{preprintTitle}”" + }, + "enterName": { + "message": "Enter their name" + }, + "enterEmail": { + "message": "Enter their email address" + }, + "invalidEmail": { + "message": "Enter an email address in the correct format, like name@example.com" + }, + "changeAuthorDetails": { + "message": "Change {name}’s details" + }, + "name": { + "message": "Name" + }, + "ableToChoseName": { + "message": "They will be able to choose their published name." + }, + "error": { + "message": "Error" + }, + "emailAddress": { + "message": "Email address" + }, + "useOfEmail": { + "message": "We’ll only use this to contact them about this PREreview." + } +} diff --git a/locales/en-US/write-review-shared.json b/locales/en-US/write-review-shared.json index b1494785f..9785f736c 100644 --- a/locales/en-US/write-review-shared.json +++ b/locales/en-US/write-review-shared.json @@ -7,5 +7,8 @@ }, "errorSummaryTitle": { "message": "There is a problem" + }, + "saveAndContinue": { + "message": "Save and continue" } } diff --git a/src/write-review/add-author-page/add-author-form.ts b/src/write-review/add-author-page/add-author-form.ts index 7523a96e6..0f78123fb 100644 --- a/src/write-review/add-author-page/add-author-form.ts +++ b/src/write-review/add-author-page/add-author-form.ts @@ -11,7 +11,7 @@ import { StreamlinePageResponse } from '../../response.js' import { writeReviewAddAuthorMatch, writeReviewAddAuthorsMatch, writeReviewAuthorsMatch } from '../../routes.js' import type { EmailAddress } from '../../types/email-address.js' import type { NonEmptyString } from '../../types/string.js' -import { backNav, errorPrefix, errorSummary } from '../shared-elements.js' +import { backNav, errorPrefix, errorSummary, saveAndContinueButton } from '../shared-elements.js' export function addAuthorForm({ form, @@ -108,7 +108,7 @@ export function addAuthorForm({ /> - + ${saveAndContinueButton(locale)} `, canonical: format(writeReviewAddAuthorMatch.formatter, { id: preprint.id }), diff --git a/src/write-review/change-author-page/change-author-form.ts b/src/write-review/change-author-page/change-author-form.ts index d4f8f477b..6b7c86aef 100644 --- a/src/write-review/change-author-page/change-author-form.ts +++ b/src/write-review/change-author-page/change-author-form.ts @@ -1,21 +1,24 @@ +import { pipe } from 'effect' import { format } from 'fp-ts-routing' import * as E from 'fp-ts/lib/Either.js' import { Status } from 'hyper-ts' import { P, match } from 'ts-pattern' -import { type InvalidE, type MissingE, hasAnError } from '../../form.js' +import { hasAnError, type InvalidE, type MissingE } from '../../form.js' import { html, plainText, rawHtml } from '../../html.js' -import type { SupportedLocale } from '../../locales/index.js' +import { translate, type SupportedLocale } from '../../locales/index.js' import type { PreprintTitle } from '../../preprint.js' import { StreamlinePageResponse } from '../../response.js' import { writeReviewAddAuthorsMatch, writeReviewChangeAuthorMatch } from '../../routes.js' import type { EmailAddress } from '../../types/email-address.js' import type { NonEmptyString } from '../../types/string.js' +import { backNav, errorPrefix, errorSummary, saveAndContinueButton } from '../shared-elements.js' export function changeAuthorForm({ author, form, number, preprint, + locale, }: { author: { name: NonEmptyString } form: ChangeAuthorForm @@ -24,67 +27,36 @@ export function changeAuthorForm({ locale: SupportedLocale }) { const error = hasAnError(form) + const t = translate(locale) return StreamlinePageResponse({ status: error ? Status.BadRequest : Status.OK, - title: plainText`${error ? 'Error: ' : ''}Change ${author.name}’s details – PREreview of “${preprint.title}”`, - nav: html`Back`, + title: pipe( + t('change-author-form', 'title')({ name: author.name, preprintTitle: preprint.title.toString() }), + errorPrefix(locale, error), + plainText, + ), + nav: backNav(locale, format(writeReviewAddAuthorsMatch.formatter, { id: preprint.id })), main: html`
- ${error - ? html` - -

There is a problem

- -
- ` - : ''} + ${error ? pipe(form, toErrorItems(locale), errorSummary(locale)) : ''} -

Change ${author.name}’s details

+

${t('change-author-form', 'changeAuthorDetails')({ name: author.name })}

-

+

-

They will be able to choose their published name.

+

${t('change-author-form', 'ableToChoseName')()}

${E.isLeft(form.name) ? html`
- Error: - ${match(form.name.left) - .with({ _tag: 'MissingE' }, () => 'Enter their name') - .exhaustive()} + ${t('change-author-form', 'error')()}: + ${match(form.name.left).with({ _tag: 'MissingE' }, t('change-author-form', 'enterName')).exhaustive()}
` : ''} @@ -104,20 +76,17 @@ export function changeAuthorForm({
-

+

-

We’ll only use this to contact them about this PREreview.

+

${t('change-author-form', 'useOfEmail')()}

${E.isLeft(form.emailAddress) ? html`
- Error: + ${t('change-author-form', 'error')()}: ${match(form.emailAddress.left) - .with({ _tag: 'MissingE' }, () => 'Enter their email address') - .with( - { _tag: 'InvalidE' }, - () => 'Enter an email address in the correct format, like name@example.com', - ) + .with({ _tag: 'MissingE' }, t('change-author-form', 'enterEmail')) + .with({ _tag: 'InvalidE' }, t('change-author-form', 'invalidEmail')) .exhaustive()}
` @@ -140,7 +109,7 @@ export function changeAuthorForm({ />
- + ${saveAndContinueButton(locale)}
`, canonical: format(writeReviewChangeAuthorMatch.formatter, { id: preprint.id, number }), @@ -153,3 +122,29 @@ export interface ChangeAuthorForm { readonly name: E.Either readonly emailAddress: E.Either } + +const toErrorItems = (locale: SupportedLocale) => (form: ChangeAuthorForm) => html` + ${E.isLeft(form.name) + ? html` +
  • + + ${match(form.name.left) + .with({ _tag: 'MissingE' }, translate(locale, 'change-author-form', 'enterName')) + .exhaustive()} + +
  • + ` + : ''} + ${E.isLeft(form.emailAddress) + ? html` +
  • + + ${match(form.emailAddress.left) + .with({ _tag: 'MissingE' }, translate(locale, 'change-author-form', 'enterEmail')) + .with({ _tag: 'InvalidE' }, translate(locale, 'change-author-form', 'invalidEmail')) + .exhaustive()} + +
  • + ` + : ''} +` diff --git a/src/write-review/shared-elements.ts b/src/write-review/shared-elements.ts index e7dfee8a3..4b5a5f6f2 100644 --- a/src/write-review/shared-elements.ts +++ b/src/write-review/shared-elements.ts @@ -16,3 +16,6 @@ export const errorSummary = (locale: SupportedLocale) => (errorItems: Html) => h ` + +export const saveAndContinueButton = (locale: SupportedLocale) => + html``