Skip to content

Commit

Permalink
Don't show an empty list of authors
Browse files Browse the repository at this point in the history
If the user somehow returns to the list of authors, this change prompts them to add an author rather than showing an empty list.

Refs #388, 74e2f62
  • Loading branch information
thewilkybarkid committed Oct 24, 2022
1 parent f0e81a1 commit b7bf547
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/write-review/write-review-add-authors.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { format } from 'fp-ts-routing'
import * as E from 'fp-ts/Either'
import * as O from 'fp-ts/Option'
import { Reader } from 'fp-ts/Reader'
import { flow, pipe } from 'fp-ts/function'
import { Status, StatusOpen } from 'hyper-ts'
import * as M from 'hyper-ts/lib/Middleware'
import * as RM from 'hyper-ts/lib/ReaderMiddleware'
import * as D from 'io-ts/Decoder'
import { get } from 'spectacles-ts'
import { match } from 'ts-pattern'
import { P, match } from 'ts-pattern'
import { canAddAuthors } from '../feature-flags'
import { MissingE, hasAnError, missingE } from '../form'
import { html, plainText, rawHtml, sendHtml } from '../html'
Expand Down Expand Up @@ -42,6 +44,10 @@ export const writeReviewAddAuthors = flow(
RM.apSW('method', RM.fromMiddleware(getMethod)),
RM.ichainW(state =>
match(state)
.with(
{ canAddAuthors: true, form: { moreAuthors: 'yes', otherAuthors: P.optional([]) } },
fromMiddlewareK(() => seeOther(format(writeReviewAddAuthorMatch.formatter, { doi: preprint.doi }))),
)
.with({ canAddAuthors: true, form: { moreAuthors: 'yes' }, method: 'POST' }, handleAddAuthorsForm)
.with({ canAddAuthors: true, form: { moreAuthors: 'yes' } }, showAddAuthorsForm)
.with({ form: { moreAuthors: 'yes' }, method: 'POST' }, handleCannotAddAuthorsForm)
Expand Down Expand Up @@ -268,6 +274,14 @@ function cannotAddAuthorsForm(preprint: Preprint) {
})
}

// https://github.com/DenisFrezzato/hyper-ts/pull/83
const fromMiddlewareK =
<R, A extends ReadonlyArray<unknown>, B, I, O, E>(
f: (...a: A) => M.Middleware<I, O, E, B>,
): ((...a: A) => RM.ReaderMiddleware<R, I, O, E, B>) =>
(...a) =>
RM.fromMiddleware(f(...a))

// https://github.com/DenisFrezzato/hyper-ts/pull/85
function fromReaderK<R, A extends ReadonlyArray<unknown>, B, I = StatusOpen, E = never>(
f: (...a: A) => Reader<R, B>,
Expand Down
66 changes: 65 additions & 1 deletion test/write-review/write-review-add-authors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('writeReviewAddAuthors', () => {
competingInterestsDetails: fc.lorem(),
conduct: fc.constant('yes'),
moreAuthors: fc.constant('yes'),
otherAuthors: fc.array(
otherAuthors: fc.nonEmptyArray(
fc.record({ name: fc.nonEmptyString(), orcid: fc.orcid() }, { requiredKeys: ['name'] }),
),
persona: fc.constantFrom('public', 'pseudonym'),
Expand Down Expand Up @@ -214,6 +214,70 @@ describe('writeReviewAddAuthors', () => {
expect(getPreprintTitle).toHaveBeenCalledWith(preprintDoi)
},
)

fc.test(
'when there are no other authors yet',
[
fc.preprintDoi(),
fc.record({ title: fc.html(), language: fc.languageCode() }),
fc.tuple(fc.uuid(), fc.string()).chain(([sessionId, secret]) =>
fc.tuple(
fc.connection({
headers: fc.constant({ Cookie: `session=${cookieSignature.sign(sessionId, secret)}` }),
}),
fc.constant(sessionId),
fc.constant(secret),
),
),
fc.user(),
fc.record(
{
competingInterests: fc.constantFrom('yes', 'no'),
competingInterestsDetails: fc.lorem(),
conduct: fc.constant('yes'),
moreAuthors: fc.constant('yes'),
otherAuthors: fc.constant([]),
persona: fc.constantFrom('public', 'pseudonym'),
review: fc.nonEmptyString(),
},
{ requiredKeys: ['moreAuthors'] },
),
],
async (preprintDoi, preprintTitle, [connection, sessionId, secret], user, newReview) => {
const sessionStore = new Keyv()
await sessionStore.set(sessionId, UserC.encode(user))
const formStore = new Keyv()
await formStore.set(`${user.orcid}_${preprintDoi}`, newReview)
const getPreprintTitle: jest.MockedFunction<_.GetPreprintTitleEnv['getPreprintTitle']> = jest.fn(_ =>
TE.right(preprintTitle),
)
const actual = await runMiddleware(
_.writeReviewAddAuthors(preprintDoi)({
canAddAuthors: () => true,
formStore,
getPreprintTitle,
secret,
sessionStore,
}),
connection,
)()

expect(actual).toStrictEqual(
E.right([
{ type: 'setStatus', status: Status.SeeOther },
{
type: 'setHeader',
name: 'Location',
value: `/preprints/doi-${encodeURIComponent(
preprintDoi.toLowerCase().replaceAll('-', '+').replaceAll('/', '-'),
)}/write-a-prereview/add-author`,
},
{ type: 'endResponse' },
]),
)
expect(getPreprintTitle).toHaveBeenCalledWith(preprintDoi)
},
)
})

describe('when authors cannot be added', () => {
Expand Down

0 comments on commit b7bf547

Please sign in to comment.