Skip to content

Commit

Permalink
Update all forms to only call page template in single place #76
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Haarhoff committed Nov 7, 2024
1 parent 1da1155 commit 94176f1
Show file tree
Hide file tree
Showing 19 changed files with 117 additions and 160 deletions.
19 changes: 11 additions & 8 deletions src/commands/area/add-owner-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {flow, pipe} from 'fp-ts/lib/function';
import * as RA from 'fp-ts/ReadonlyArray';
import * as E from 'fp-ts/Either';
import * as O from 'fp-ts/Option';
import {EmailAddress, User} from '../../types';
import {EmailAddress} from '../../types';
import * as t from 'io-ts';
import {StatusCodes} from 'http-status-codes';
import {formatValidationErrors} from 'io-ts-reporters';
Expand All @@ -11,8 +11,13 @@ import {
failureWithStatus,
} from '../../types/failure-with-status';
import {Form} from '../../types/form';
import {html, joinHtml, safe, sanitizeString} from '../../types/html';
import {pageTemplate} from '../../templates';
import {
html,
joinHtml,
safe,
sanitizeString,
toLoggedInContent,
} from '../../types/html';
import {renderMemberNumber} from '../../templates/member-number';
import {SharedReadModel} from '../../read-models/shared-state';
import {
Expand All @@ -31,7 +36,6 @@ type Member = {
};

type ViewModel = {
user: User;
areaId: string;
areaOwners: {
existing: ReadonlyArray<Member>;
Expand Down Expand Up @@ -112,7 +116,7 @@ const renderBody = (viewModel: ViewModel) => html`
`;

const renderForm = (viewModel: ViewModel) =>
pipe(viewModel, renderBody, pageTemplate(safe('Add Owner'), viewModel.user));
pipe(viewModel, renderBody, toLoggedInContent(safe('Add Owner')));

const paramsCodec = t.strict({
area: t.string,
Expand Down Expand Up @@ -199,10 +203,9 @@ const getAreaName = (db: SharedReadModel['db'], areaId: string) =>

const constructForm: Form<ViewModel>['constructForm'] =
input =>
({user, readModel}): E.Either<FailureWithStatus, ViewModel> =>
({readModel}): E.Either<FailureWithStatus, ViewModel> =>
pipe(
{user},
E.right,
E.Do,
E.bind('areaId', () => getAreaId(input)),
E.bind('areaName', ({areaId}) => getAreaName(readModel.db, areaId)),
E.bind('areaOwners', ({areaId}) =>
Expand Down
20 changes: 6 additions & 14 deletions src/commands/area/create-form.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import * as E from 'fp-ts/Either';
import {pageTemplate} from '../../templates';
import {User} from '../../types';
import {Form} from '../../types/form';
import {pipe} from 'fp-ts/lib/function';
import {html, safe} from '../../types/html';
import {html, safe, toLoggedInContent} from '../../types/html';
import {v4} from 'uuid';
import {UUID} from 'io-ts-types';

type ViewModel = {
user: User;
};
type ViewModel = unknown;

const renderForm = (viewModel: ViewModel) =>
const renderForm = () =>
pipe(
viewModel,
() => html`
html`
<h1>Create an area</h1>
<form action="#" method="post">
<label for="name">What is this area called</label>
Expand All @@ -23,13 +18,10 @@ const renderForm = (viewModel: ViewModel) =>
<button type="submit">Confirm and send</button>
</form>
`,
pageTemplate(safe('Create Area'), viewModel.user)
toLoggedInContent(safe('Create Area'))
);

export const createForm: Form<ViewModel> = {
renderForm,
constructForm:
() =>
({user}) =>
E.right({user}),
constructForm: () => () => E.right({}),
};
15 changes: 5 additions & 10 deletions src/commands/area/remove-area-form.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
import * as E from 'fp-ts/Either';
import * as t from 'io-ts';
import {pageTemplate} from '../../templates';
import {User} from '../../types';
import {Form} from '../../types/form';
import {flow, pipe} from 'fp-ts/lib/function';
import {html, safe, sanitizeString} from '../../types/html';
import {html, safe, sanitizeString, toLoggedInContent} from '../../types/html';
import {StatusCodes} from 'http-status-codes';
import {failureWithStatus} from '../../types/failure-with-status';
import {formatValidationErrors} from 'io-ts-reporters';
import {getAreaName} from './get-area-name';

type ViewModel = {
user: User;
areaId: string;
areaName: string;
};

const renderForm = (viewModel: ViewModel) =>
pipe(
viewModel,
() => html`
html`
<div class="stack-large">
<h1>Remove '${sanitizeString(viewModel.areaName)}'?</h1>
<form action="#" method="post">
Expand All @@ -28,7 +24,7 @@ const renderForm = (viewModel: ViewModel) =>
</form>
</div>
`,
pageTemplate(safe('Remove Area'), viewModel.user)
toLoggedInContent(safe('Remove Area'))
);

const paramsCodec = t.strict({
Expand All @@ -55,10 +51,9 @@ export const removeAreaForm: Form<ViewModel> = {
renderForm,
constructForm:
input =>
({user, readModel}) =>
({readModel}) =>
pipe(
{user},
E.right,
E.Do,
E.bind('areaId', () => getAreaId(input)),
E.bind('areaName', ({areaId}) => getAreaName(readModel.db, areaId))
),
Expand Down
11 changes: 4 additions & 7 deletions src/commands/area/remove-owner-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
import * as tt from 'io-ts-types';
import * as t from 'io-ts';
import * as E from 'fp-ts/Either';
import {pageTemplate} from '../../templates';
import {EmailAddress, User} from '../../types';
import {EmailAddress} from '../../types';
import {Form} from '../../types/form';
import {flow, pipe} from 'fp-ts/lib/function';
import {html, safe, sanitizeString} from '../../types/html';
import {html, safe, sanitizeString, toLoggedInContent} from '../../types/html';
import {StatusCodes} from 'http-status-codes';
import {failureWithStatus} from '../../types/failure-with-status';
import {formatValidationErrors} from 'io-ts-reporters';
Expand All @@ -17,7 +16,6 @@ import {SharedReadModel} from '../../read-models/shared-state';
import * as O from 'fp-ts/Option';

type ViewModel = {
user: User;
areaId: string;
areaName: string;
owner: {
Expand Down Expand Up @@ -73,7 +71,7 @@ const renderForm = (viewModel: ViewModel) =>
</form>
</div>
`,
pageTemplate(safe('Remove Owner'), viewModel.user)
toLoggedInContent(safe('Remove Owner'))
);

const getOwner = (db: SharedReadModel['db'], memberNumber: number) =>
Expand Down Expand Up @@ -119,11 +117,10 @@ export const removeOwnerForm: Form<ViewModel> = {
renderForm,
constructForm:
input =>
({user, readModel}) =>
({readModel}) =>
pipe(
input,
decodeParams,
E.bind('user', () => E.right(user)),
E.bind('areaName', ({areaId}) => getAreaName(readModel.db, areaId)),
E.bind('owner', ({memberNumber}) =>
getOwner(readModel.db, memberNumber)
Expand Down
13 changes: 5 additions & 8 deletions src/commands/equipment/add-form.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {pipe} from 'fp-ts/lib/function';
import * as t from 'io-ts';
import * as E from 'fp-ts/Either';
import {pageTemplate} from '../../templates';
import {html, safe, sanitizeString} from '../../types/html';
import {DomainEvent, User} from '../../types';
import {html, safe, sanitizeString, toLoggedInContent} from '../../types/html';
import {DomainEvent} from '../../types';
import {v4} from 'uuid';
import {Form} from '../../types/form';
import {formatValidationErrors} from 'io-ts-reporters';
Expand All @@ -13,7 +12,6 @@ import {readModels} from '../../read-models';
import {UUID} from 'io-ts-types';

type ViewModel = {
user: User;
areaId: UUID;
areaName: string;
};
Expand All @@ -30,7 +28,7 @@ const renderForm = (viewModel: ViewModel) =>
<button type="submit">Confirm and send</button>
</form>
`,
pageTemplate(safe('Create Equipment'), viewModel.user)
toLoggedInContent(safe('Create Equipment'))
);

const getAreaId = (input: unknown) =>
Expand All @@ -54,12 +52,11 @@ const getAreaName = (events: ReadonlyArray<DomainEvent>, areaId: UUID) =>

const constructForm: Form<ViewModel>['constructForm'] =
input =>
({user, events}) =>
({events}) =>
pipe(
E.Do,
E.bind('areaId', () => getAreaId(input)),
E.bind('areaName', ({areaId}) => getAreaName(events, areaId)),
E.bind('user', () => E.right(user))
E.bind('areaName', ({areaId}) => getAreaName(events, areaId))
);

export const addForm: Form<ViewModel> = {
Expand Down
12 changes: 4 additions & 8 deletions src/commands/equipment/register-training-sheet-form.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import {pipe} from 'fp-ts/lib/function';
import * as E from 'fp-ts/Either';
import {html, safe, sanitizeString} from '../../types/html';
import {User} from '../../types';
import {html, safe, sanitizeString, toLoggedInContent} from '../../types/html';
import {Form} from '../../types/form';
import {pageTemplate} from '../../templates';
import {getEquipmentName} from './get-equipment-name';
import {getEquipmentIdFromForm} from './get-equipment-id-from-form';
import {UUID} from 'io-ts-types';

type ViewModel = {
user: User;
equipmentId: UUID;
equipmentName: string;
};
Expand All @@ -31,15 +28,14 @@ const renderForm = (viewModel: ViewModel) =>
<button type="submit">Confirm and send</button>
</form>
`,
pageTemplate(safe('Register training sheet'), viewModel.user)
toLoggedInContent(safe('Register training sheet'))
);

const constructForm: Form<ViewModel>['constructForm'] =
input =>
({events, user}) =>
({events}) =>
pipe(
{user},
E.right,
E.Do,
E.bind('equipmentId', () => getEquipmentIdFromForm(input)),
E.bind('equipmentName', ({equipmentId}) =>
getEquipmentName(events, equipmentId)
Expand Down
20 changes: 5 additions & 15 deletions src/commands/member-numbers/link-number-to-email-form.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import {pipe} from 'fp-ts/lib/function';
import * as E from 'fp-ts/Either';
import {pageTemplate} from '../../templates';
import {html, safe} from '../../types/html';
import {User} from '../../types';
import {html, safe, toLoggedInContent} from '../../types/html';
import {Form} from '../../types/form';

type ViewModel = {
user: User;
};
type ViewModel = unknown;

const renderForm = (viewModel: ViewModel) =>
const renderForm = () =>
pipe(
html`
<h1>Link a member number to an e-mail address</h1>
Expand All @@ -24,16 +20,10 @@ const renderForm = (viewModel: ViewModel) =>
<button type="submit">Confirm and send</button>
</form>
`,
pageTemplate(
safe('Link a member number to an e-mail address'),
viewModel.user
)
toLoggedInContent(safe('Link a member number to an e-mail address'))
);

const constructForm: Form<ViewModel>['constructForm'] =
() =>
({user}) =>
E.right({user});
const constructForm: Form<ViewModel>['constructForm'] = () => () => E.right({});

export const linkNumberToEmailForm: Form<ViewModel> = {
renderForm,
Expand Down
5 changes: 2 additions & 3 deletions src/commands/members/edit-name-form.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {flow, pipe} from 'fp-ts/lib/function';
import * as E from 'fp-ts/Either';
import {pageTemplate} from '../../templates';
import {html, safe} from '../../types/html';
import {html, safe, toLoggedInContent} from '../../types/html';
import {User} from '../../types';
import {Form} from '../../types/form';
import * as t from 'io-ts';
Expand Down Expand Up @@ -30,7 +29,7 @@ const renderForm = (viewModel: ViewModel) =>
<button type="submit">Confirm</button>
</form>
`,
pageTemplate(safe('Edit name'), viewModel.user)
toLoggedInContent(safe('Edit name'))
);

const paramsCodec = t.strict({
Expand Down
5 changes: 2 additions & 3 deletions src/commands/members/edit-pronouns-form.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {flow, pipe} from 'fp-ts/lib/function';
import * as E from 'fp-ts/Either';
import {pageTemplate} from '../../templates';
import {html, safe} from '../../types/html';
import {html, safe, toLoggedInContent} from '../../types/html';
import {User} from '../../types';
import {Form} from '../../types/form';
import * as t from 'io-ts';
Expand Down Expand Up @@ -30,7 +29,7 @@ const renderForm = (viewModel: ViewModel) =>
<button type="submit">Confirm</button>
</form>
`,
pageTemplate(safe('Edit pronouns'), viewModel.user)
toLoggedInContent(safe('Edit pronouns'))
);

const paramsCodec = t.strict({
Expand Down
5 changes: 2 additions & 3 deletions src/commands/members/sign-owner-agreement-form.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {pipe} from 'fp-ts/lib/function';
import * as E from 'fp-ts/Either';
import {pageTemplate} from '../../templates';
import {html, safe} from '../../types/html';
import {html, safe, toLoggedInContent} from '../../types/html';
import {Form} from '../../types/form';
import {User} from '../../types';
import {ownerAgreement} from './owner-agreement';
Expand All @@ -27,7 +26,7 @@ const renderForm = (viewModel: ViewModel) =>
<button type="submit">Sign Agreement</button>
</form>
`,
pageTemplate(safe('Sign Owner Agreement'), viewModel.user)
toLoggedInContent(safe('Sign Owner Agreement'))
);

const constructForm: Form<ViewModel>['constructForm'] =
Expand Down
11 changes: 2 additions & 9 deletions src/commands/super-user/declare-form.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import {pipe} from 'fp-ts/lib/function';
import * as E from 'fp-ts/Either';
import {pageTemplate} from '../../templates';
import {html, safe} from '../../types/html';
import {User} from '../../types';
import {html, safe, toLoggedInContent} from '../../types/html';
import {Form} from '../../types/form';
import {memberInput} from '../../templates/member-input';
import {readModels} from '../../read-models';
import {Member} from '../../read-models/members';

type ViewModel = {
user: User;
members: ReadonlyArray<Member>;
};

Expand All @@ -25,11 +22,7 @@ const render = (viewModel: ViewModel) => html`
`;

const renderForm = (viewModel: ViewModel) =>
pipe(
viewModel,
render,
pageTemplate(safe('Declare super user'), viewModel.user)
);
pipe(viewModel, render, toLoggedInContent(safe('Declare super user')));

const constructForm: Form<ViewModel>['constructForm'] =
() =>
Expand Down
Loading

0 comments on commit 94176f1

Please sign in to comment.