Skip to content

Commit 67b023d

Browse files
committed
use TE and pass dependencies to form so that async stuff can be called
1 parent 39cd97b commit 67b023d

27 files changed

+92
-48
lines changed

src/commands/area/add-owner-form.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {flow, pipe} from 'fp-ts/lib/function';
22
import * as RA from 'fp-ts/ReadonlyArray';
33
import * as E from 'fp-ts/Either';
4+
import * as TE from 'fp-ts/TaskEither';
45
import * as O from 'fp-ts/Option';
56
import {EmailAddress} from '../../types';
67
import * as t from 'io-ts';
@@ -203,14 +204,15 @@ const getAreaName = (db: SharedReadModel['db'], areaId: string) =>
203204

204205
const constructForm: Form<ViewModel>['constructForm'] =
205206
input =>
206-
({readModel}): E.Either<FailureWithStatus, ViewModel> =>
207+
({readModel}) =>
207208
pipe(
208209
E.Do,
209210
E.bind('areaId', () => getAreaId(input)),
210211
E.bind('areaName', ({areaId}) => getAreaName(readModel.db, areaId)),
211212
E.bind('areaOwners', ({areaId}) =>
212213
E.right(getExistingAndPotentialOwners(readModel.db, areaId))
213-
)
214+
),
215+
TE.fromEither,
214216
);
215217

216218
export const addOwnerForm: Form<ViewModel> = {

src/commands/area/create-form.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as E from 'fp-ts/Either';
1+
import * as TE from 'fp-ts/TaskEither';
22
import {Form} from '../../types/form';
33
import {pipe} from 'fp-ts/lib/function';
44
import {html, safe, toLoggedInContent} from '../../types/html';
@@ -23,5 +23,5 @@ const renderForm = () =>
2323

2424
export const createForm: Form<ViewModel> = {
2525
renderForm,
26-
constructForm: () => () => E.right({}),
26+
constructForm: () => () => TE.right({}),
2727
};

src/commands/area/remove-area-form.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as E from 'fp-ts/Either';
2+
import * as TE from 'fp-ts/TaskEither';
23
import * as t from 'io-ts';
34
import {Form} from '../../types/form';
45
import {flow, pipe} from 'fp-ts/lib/function';
@@ -55,6 +56,7 @@ export const removeAreaForm: Form<ViewModel> = {
5556
pipe(
5657
E.Do,
5758
E.bind('areaId', () => getAreaId(input)),
58-
E.bind('areaName', ({areaId}) => getAreaName(readModel.db, areaId))
59+
E.bind('areaName', ({areaId}) => getAreaName(readModel.db, areaId)),
60+
TE.fromEither,
5961
),
6062
};

src/commands/area/remove-owner-form.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as tt from 'io-ts-types';
22
import * as t from 'io-ts';
33
import * as E from 'fp-ts/Either';
4+
import * as TE from 'fp-ts/TaskEither';
45
import {EmailAddress} from '../../types';
56
import {Form} from '../../types/form';
67
import {flow, pipe} from 'fp-ts/lib/function';
@@ -123,6 +124,7 @@ export const removeOwnerForm: Form<ViewModel> = {
123124
E.bind('areaName', ({areaId}) => getAreaName(readModel.db, areaId)),
124125
E.bind('owner', ({memberNumber}) =>
125126
getOwner(readModel.db, memberNumber)
126-
)
127+
),
128+
TE.fromEither,
127129
),
128130
};

src/commands/area/set-mailing-list-form.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {flow, pipe} from 'fp-ts/lib/function';
22
import * as E from 'fp-ts/Either';
3+
import * as TE from 'fp-ts/TaskEither';
34
import * as t from 'io-ts';
45
import {StatusCodes} from 'http-status-codes';
56
import {formatValidationErrors} from 'io-ts-reporters';
@@ -88,7 +89,7 @@ const getAreaInfo = (db: SharedReadModel['db'], areaId: string) =>
8889

8990
const constructForm: Form<ViewModel>['constructForm'] =
9091
input =>
91-
({readModel}): E.Either<FailureWithStatus, ViewModel> =>
92+
({readModel}) =>
9293
pipe(
9394
E.Do,
9495
E.bind('areaId', () => getAreaId(input)),
@@ -97,7 +98,8 @@ const constructForm: Form<ViewModel>['constructForm'] =
9798
areaId,
9899
areaName: areaInfo.areaName,
99100
currentEmail: areaInfo.currentEmail,
100-
}))
101+
})),
102+
TE.fromEither,
101103
);
102104

103105
export const setMailingListForm: Form<ViewModel> = {

src/commands/equipment/add-form.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {pipe} from 'fp-ts/lib/function';
22
import * as t from 'io-ts';
33
import * as E from 'fp-ts/Either';
4+
import * as TE from 'fp-ts/TaskEither';
45
import {html, safe, sanitizeString, toLoggedInContent} from '../../types/html';
56
import {v4} from 'uuid';
67
import {Form} from '../../types/form';
@@ -55,7 +56,8 @@ const constructForm: Form<ViewModel>['constructForm'] =
5556
pipe(
5657
E.Do,
5758
E.bind('areaId', () => getAreaId(input)),
58-
E.bind('areaName', ({areaId}) => getAreaName(readModel, areaId))
59+
E.bind('areaName', ({areaId}) => getAreaName(readModel, areaId)),
60+
TE.fromEither,
5961
);
6062

6163
export const addForm: Form<ViewModel> = {

src/commands/equipment/register-training-sheet-form.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {pipe} from 'fp-ts/lib/function';
22
import * as E from 'fp-ts/Either';
3+
import * as TE from 'fp-ts/TaskEither';
34
import {html, safe, sanitizeString, toLoggedInContent} from '../../types/html';
45
import {Form} from '../../types/form';
56
import {getEquipmentName} from './get-equipment-name';
@@ -39,7 +40,8 @@ const constructForm: Form<ViewModel>['constructForm'] =
3940
E.bind('equipmentId', () => getEquipmentIdFromForm(input)),
4041
E.bind('equipmentName', ({equipmentId}) =>
4142
getEquipmentName(readModel, equipmentId)
42-
)
43+
),
44+
TE.fromEither,
4345
);
4446

4547
export const registerTrainingSheetForm: Form<ViewModel> = {

src/commands/equipment/remove-training-sheet-form.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {pipe} from 'fp-ts/lib/function';
22
import * as E from 'fp-ts/Either';
3+
import * as TE from 'fp-ts/TaskEither';
34
import {html, safe, sanitizeString, toLoggedInContent} from '../../types/html';
45
import {Form} from '../../types/form';
56
import {getEquipmentIdFromForm} from './get-equipment-id-from-form';
@@ -63,7 +64,8 @@ const constructForm: Form<ViewModel>['constructForm'] =
6364
)()
6465
)
6566
)
66-
)
67+
),
68+
TE.fromEither,
6769
);
6870

6971
export const removeTrainingSheetForm: Form<ViewModel> = {

src/commands/exclusion-events/exclude-event-form.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {flow, pipe} from 'fp-ts/lib/function';
22
import * as E from 'fp-ts/Either';
3+
import * as TE from 'fp-ts/TaskEither';
34
import {html, safe, toLoggedInContent} from '../../types/html';
45
import {Form} from '../../types/form';
56
import {StoredDomainEvent} from '../../types';
@@ -13,7 +14,6 @@ import * as O from 'fp-ts/Option';
1314

1415
type ViewModel = {
1516
event: O.Option<StoredDomainEvent>,
16-
event_id: tt.UUID,
1717
};
1818

1919
const renderForm = (viewModel: ViewModel) =>
@@ -25,14 +25,14 @@ const renderForm = (viewModel: ViewModel) =>
2525
</p>
2626
${renderEvent(viewModel.event.value)}
2727
<form action="/events/exclude-event" method="post">
28-
<input type="hidden" name="event_id" value="${viewModel.event_id}"/>
28+
<input type="hidden" name="event_id" value="${viewModel.event.value.event_id}"/>
2929
<button type="submit">Confirm</button>
3030
</form>
3131
`,
3232
toLoggedInContent(safe('Exclude event'))
3333
) : pipe(
3434
html`
35-
<h1>Unknown event: ${viewModel.event_id}</h1>
35+
<h1>Unknown event</h1>
3636
`,
3737
toLoggedInContent(safe('Exclude event'))
3838
);
@@ -41,7 +41,7 @@ const paramsCodec = t.strict({
4141
event_id: tt.UUID
4242
});
4343

44-
const constructForm: Form<ViewModel>['constructForm'] = input => ({events}) => pipe(
44+
const constructForm: Form<ViewModel>['constructForm'] = input => ({deps}) => pipe(
4545
input,
4646
paramsCodec.decode,
4747
E.mapLeft(
@@ -54,12 +54,13 @@ const constructForm: Form<ViewModel>['constructForm'] = input => ({events}) => p
5454
)
5555
),
5656
E.map(params => params.event_id),
57-
E.map(event_id => ({
58-
event_id,
59-
event: O.fromNullable(
60-
events.findLast(event => event.event_id === event_id)
61-
),
62-
}))
57+
TE.fromEither,
58+
TE.chain(deps.getEventById),
59+
TE.map(
60+
event => ({
61+
event,
62+
})
63+
),
6364
);
6465

6566
export const excludeEventForm: Form<ViewModel> = {

src/commands/member-numbers/link-number-to-email-form.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {pipe} from 'fp-ts/lib/function';
2-
import * as E from 'fp-ts/Either';
2+
import * as TE from 'fp-ts/TaskEither';
33
import {html, safe, toLoggedInContent} from '../../types/html';
44
import {Form} from '../../types/form';
55

@@ -30,7 +30,7 @@ const renderForm = () =>
3030
toLoggedInContent(safe('Link a member number to an e-mail address'))
3131
);
3232

33-
const constructForm: Form<ViewModel>['constructForm'] = () => () => E.right({});
33+
const constructForm: Form<ViewModel>['constructForm'] = () => () => TE.right({});
3434

3535
export const linkNumberToEmailForm: Form<ViewModel> = {
3636
renderForm,

0 commit comments

Comments
 (0)