Skip to content

Commit

Permalink
Reduce number of places using pageTemplate as preparation to evolving…
Browse files Browse the repository at this point in the history
… its inputs #86
  • Loading branch information
Daniel Haarhoff committed Nov 24, 2024
1 parent 3132def commit 295f7c5
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 172 deletions.
4 changes: 2 additions & 2 deletions src/queries/all-equipment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {pipe} from 'fp-ts/lib/function';
import * as TE from 'fp-ts/TaskEither';
import {render} from './render';
import {Query} from '../query';
import {HttpResponse} from '../../types';
import {eq} from 'drizzle-orm';
import {SharedReadModel} from '../../read-models/shared-state';
import {
Expand All @@ -13,6 +12,7 @@ import {
} from '../../read-models/shared-state/state';
import {ViewModel} from './view-model';
import * as RA from 'fp-ts/ReadonlyArray';
import {safe, toLoggedInContent} from '../../types/html';

const getAreas = (db: SharedReadModel['db']): ViewModel['areas'] => {
return pipe(
Expand Down Expand Up @@ -47,5 +47,5 @@ export const allEquipment: Query = deps => user =>
TE.of,
TE.let('areas', () => getAreas(deps.sharedReadModel.db)),
TE.map(render),
TE.map(HttpResponse.CompleteHtmlPage)
TE.map(toLoggedInContent(safe('Equipment of Makespace')))
);
17 changes: 6 additions & 11 deletions src/queries/all-equipment/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
} from '../../types/html';
import * as RA from 'fp-ts/ReadonlyArray';
import {ViewModel} from './view-model';
import {pageTemplate} from '../../templates';

const renderEquipment = (equipment: ViewModel['areas'][number]['equipment']) =>
pipe(
Expand Down Expand Up @@ -39,13 +38,9 @@ const renderArea = (area: ViewModel['areas'][number]) => html`
const renderAreas = (areas: ViewModel['areas']) =>
pipe(areas, RA.map(renderArea), joinHtml);

export const render = (viewModel: ViewModel) =>
pipe(
html`
<div class="stack-large">
<h1>Equipment of Makespace</h1>
<div class="stack-large">${renderAreas(viewModel.areas)}</div>
</div>
`,
pageTemplate(safe('Equipment'), viewModel.user)
);
export const render = (viewModel: ViewModel) => html`
<div class="stack-large">
<h1>Equipment of Makespace</h1>
<div class="stack-large">${renderAreas(viewModel.areas)}</div>
</div>
`;
4 changes: 2 additions & 2 deletions src/queries/areas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {pipe} from 'fp-ts/lib/function';
import * as TE from 'fp-ts/TaskEither';
import {render} from './render';
import {Query} from '../query';
import {HttpResponse} from '../../types';
import {SharedReadModel} from '../../read-models/shared-state';
import {ViewModel} from './view-model';
import {
Expand All @@ -13,6 +12,7 @@ import {
} from '../../read-models/shared-state/state';
import * as RA from 'fp-ts/ReadonlyArray';
import {eq} from 'drizzle-orm';
import {safe, toLoggedInContent} from '../../types/html';

const getAreas = (db: SharedReadModel['db']): ViewModel['areas'] => {
return pipe(
Expand Down Expand Up @@ -48,5 +48,5 @@ export const areas: Query = deps => user =>
TE.let('user', () => user),
TE.let('areas', () => getAreas(deps.sharedReadModel.db)),
TE.map(render),
TE.map(HttpResponse.CompleteHtmlPage)
TE.map(toLoggedInContent(safe('Manage Areas and Owners')))
);
19 changes: 7 additions & 12 deletions src/queries/areas/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
} from '../../types/html';
import * as RA from 'fp-ts/ReadonlyArray';
import {ViewModel} from './view-model';
import {pageTemplate} from '../../templates';
import {renderMemberNumber} from '../../templates/member-number';
import * as O from 'fp-ts/Option';
import {displayDate} from '../../templates/display-date';
Expand Down Expand Up @@ -119,14 +118,10 @@ const addAreaCallToAction = html`
<a class="button" href="/areas/create">Add area of responsibility</a>
`;

export const render = (viewModel: ViewModel) =>
pipe(
html`
<div class="stack-large">
<h1>Manage Areas and Owners</h1>
<div>${addAreaCallToAction}</div>
<section class="stack-large">${renderAreas(viewModel.areas)}</section>
</div>
`,
pageTemplate(safe('Manage Areas and Owners'), viewModel.user)
);
export const render = (viewModel: ViewModel) => html`
<div class="stack-large">
<h1>Manage Areas and Owners</h1>
<div>${addAreaCallToAction}</div>
<section class="stack-large">${renderAreas(viewModel.areas)}</section>
</div>
`;
4 changes: 1 addition & 3 deletions src/queries/equipment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {render} from './render';
import * as E from 'fp-ts/Either';
import {formatValidationErrors} from 'io-ts-reporters';
import {Query} from '../query';
import {HttpResponse} from '../../types';
import {UUID} from 'io-ts-types';

const invalidParams = flow(
Expand All @@ -24,6 +23,5 @@ export const equipment: Query = deps => (user, params) =>
E.map(params => params.equipment),
TE.fromEither,
TE.chain(constructViewModel(deps, user)),
TE.map(render),
TE.map(HttpResponse.CompleteHtmlPage)
TE.map(render)
);
4 changes: 2 additions & 2 deletions src/queries/equipment/render.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {pipe} from 'fp-ts/lib/function';
import {pageTemplate} from '../../templates';
import {displayDate} from '../../templates/display-date';
import {renderMemberNumber} from '../../templates/member-number';
import {
Expand All @@ -8,6 +7,7 @@ import {
joinHtml,
sanitizeOption,
sanitizeString,
toLoggedInContent,
} from '../../types/html';
import {ViewModel} from './view-model';
import * as O from 'fp-ts/Option';
Expand Down Expand Up @@ -313,5 +313,5 @@ export const render = (viewModel: ViewModel) =>
${isTrainerOrOwner(viewModel) ? trainingQuizResults(viewModel) : html``}
</div>
`,
pageTemplate(sanitizeString(viewModel.equipment.name), viewModel.user)
toLoggedInContent(sanitizeString(viewModel.equipment.name))
);
4 changes: 2 additions & 2 deletions src/queries/failed-imports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import * as TE from 'fp-ts/TaskEither';
import {render} from './render';
import {constructViewModel} from './construct-view-model';
import {Query} from '../query';
import {HttpResponse} from '../../types';
import {safe, toLoggedInContent} from '../../types/html';

export const failedImports: Query = deps => user =>
pipe(
user,
constructViewModel(deps),
TE.map(render),
TE.map(HttpResponse.CompleteHtmlPage)
TE.map(toLoggedInContent(safe('Failed Member Imports')))
);
23 changes: 9 additions & 14 deletions src/queries/failed-imports/render.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {pipe} from 'fp-ts/lib/function';
import {html, joinHtml, safe, sanitizeString} from '../../types/html';
import {html, joinHtml, sanitizeString} from '../../types/html';
import {ViewModel} from './view-model';
import * as RA from 'fp-ts/ReadonlyArray';
import {pageTemplate} from '../../templates';

const renderFailedLinkings = (failedImports: ViewModel['failedImports']) =>
pipe(
Expand All @@ -20,15 +19,11 @@ const renderFailedLinkings = (failedImports: ViewModel['failedImports']) =>
</ul>`
);

export const render = (viewModel: ViewModel) =>
pipe(
html`
<h1>Failed member imports</h1>
<p>
During import from the legacy database the following members could not
be imported because the email address is already used by another member.
</p>
${renderFailedLinkings(viewModel.failedImports)}
`,
pageTemplate(safe('Failed member imports'), viewModel.user)
);
export const render = (viewModel: ViewModel) => html`
<h1>Failed member imports</h1>
<p>
During import from the legacy database the following members could not be
imported because the email address is already used by another member.
</p>
${renderFailedLinkings(viewModel.failedImports)}
`;
4 changes: 2 additions & 2 deletions src/queries/log/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import * as TE from 'fp-ts/TaskEither';
import {render} from './render';
import {constructViewModel} from './construct-view-model';
import {Query} from '../query';
import {HttpResponse} from '../../types';
import {safe, toLoggedInContent} from '../../types/html';

export const log: Query = deps => user =>
pipe(
user,
constructViewModel(deps),
TE.map(render),
TE.map(HttpResponse.CompleteHtmlPage)
TE.map(toLoggedInContent(safe('Event Log')))
);
17 changes: 6 additions & 11 deletions src/queries/log/render.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {pipe} from 'fp-ts/lib/function';
import * as RA from 'fp-ts/ReadonlyArray';
import {html, joinHtml, safe, sanitizeString} from '../../types/html';
import {html, joinHtml, sanitizeString} from '../../types/html';
import {ViewModel} from './view-model';
import {DomainEvent} from '../../types';
import {inspect} from 'node:util';
import {displayDate} from '../../templates/display-date';
import {pageTemplate} from '../../templates';
import {DateTime} from 'luxon';
import {renderActor} from '../../types/actor';

Expand Down Expand Up @@ -41,12 +40,8 @@ const renderLog = (log: ViewModel['events']) =>
`
);

export const render = (viewModel: ViewModel) =>
pipe(
html`
<h1>Event log</h1>
<p>Most recent at top</p>
${renderLog(viewModel.events)}
`,
pageTemplate(safe('Event Log'), viewModel.user)
);
export const render = (viewModel: ViewModel) => html`
<h1>Event log</h1>
<p>Most recent at top</p>
${renderLog(viewModel.events)}
`;
4 changes: 2 additions & 2 deletions src/queries/member/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {failureWithStatus} from '../../types/failure-with-status';
import {StatusCodes} from 'http-status-codes';
import {formatValidationErrors} from 'io-ts-reporters';
import * as tt from 'io-ts-types';
import {HttpResponse} from '../../types';
import {safe, toLoggedInContent} from '../../types/html';

const invalidParams = flow(
formatValidationErrors,
Expand All @@ -25,6 +25,6 @@ export const member: Query = deps => (user, params) =>
E.map(constructViewModel(deps, user)),
E.flatten,
E.map(viewModel => render(viewModel)),
E.map(HttpResponse.CompleteHtmlPage),
E.map(toLoggedInContent(safe('Member'))),
TE.fromEither
);
Loading

0 comments on commit 295f7c5

Please sign in to comment.