Skip to content

Commit

Permalink
Prepare to extract strings from write-review-competing-interests
Browse files Browse the repository at this point in the history
Refs: #1817
  • Loading branch information
erkannt committed Dec 6, 2024
1 parent fc20b7b commit 2964fa6
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions src/write-review/write-review-competing-interests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { get } from 'spectacles-ts'
import { P, match } from 'ts-pattern'
import { type MissingE, hasAnError, missingE } from '../form.js'
import { html, plainText, rawHtml, sendHtml } from '../html.js'
import { DefaultLocale, type SupportedLocale } from '../locales/index.js'
import { getMethod, notFound, seeOther, serviceUnavailable } from '../middleware.js'
import { templatePage } from '../page.js'
import { type PreprintTitle, getPreprintTitle } from '../preprint.js'
Expand All @@ -28,6 +29,7 @@ export const writeReviewCompetingInterests = flow(
pipe(
RM.right({ preprint }),
RM.apS('user', getUser),
RM.apS('locale', RM.of(DefaultLocale)),
RM.bindW(
'form',
RM.fromReaderTaskEitherK(({ user }) => getForm(user.orcid, preprint.id)),
Expand Down Expand Up @@ -57,29 +59,46 @@ export const writeReviewCompetingInterests = flow(
)

const showCompetingInterestsForm = flow(
RM.fromReaderK(({ form, preprint, user }: { form: Form; preprint: PreprintTitle; user: User }) =>
competingInterestsForm(
preprint,
{
competingInterests: E.right(form.competingInterests),
competingInterestsDetails: E.right(form.competingInterestsDetails),
},
user,
form.moreAuthors,
),
RM.fromReaderK(
({ form, preprint, user, locale }: { form: Form; preprint: PreprintTitle; user: User; locale: SupportedLocale }) =>
competingInterestsForm(
preprint,
{
competingInterests: E.right(form.competingInterests),
competingInterestsDetails: E.right(form.competingInterestsDetails),
},
user,
locale,
form.moreAuthors,
),
),
RM.ichainFirst(() => RM.status(Status.OK)),
RM.ichainMiddlewareK(sendHtml),
)

const showCompetingInterestsErrorForm = (preprint: PreprintTitle, user: User, moreAuthors: Form['moreAuthors']) =>
const showCompetingInterestsErrorForm = (
preprint: PreprintTitle,
user: User,
moreAuthors: Form['moreAuthors'],
locale: SupportedLocale,
) =>
flow(
RM.fromReaderK((form: CompetingInterestsForm) => competingInterestsForm(preprint, form, user, moreAuthors)),
RM.fromReaderK((form: CompetingInterestsForm) => competingInterestsForm(preprint, form, user, locale, moreAuthors)),
RM.ichainFirst(() => RM.status(Status.BadRequest)),
RM.ichainMiddlewareK(sendHtml),
)

const handleCompetingInterestsForm = ({ form, preprint, user }: { form: Form; preprint: PreprintTitle; user: User }) =>
const handleCompetingInterestsForm = ({
form,
preprint,
user,
locale,
}: {
form: Form
preprint: PreprintTitle
user: User
locale: SupportedLocale
}) =>
pipe(
RM.decodeBody(E.right),
RM.map(body =>
Expand Down Expand Up @@ -107,7 +126,7 @@ const handleCompetingInterestsForm = ({ form, preprint, user }: { form: Form; pr
RM.orElseW(error =>
match(error)
.with('form-unavailable', () => serviceUnavailable)
.with({ competingInterests: P.any }, showCompetingInterestsErrorForm(preprint, user, form.moreAuthors))
.with({ competingInterests: P.any }, showCompetingInterestsErrorForm(preprint, user, form.moreAuthors, locale))
.exhaustive(),
),
)
Expand All @@ -131,6 +150,8 @@ function competingInterestsForm(
preprint: PreprintTitle,
form: CompetingInterestsForm,
user: User,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
locale: SupportedLocale,
moreAuthors?: 'yes' | 'yes-private' | 'no',
) {
const error = hasAnError(form)
Expand Down

0 comments on commit 2964fa6

Please sign in to comment.