|
| 1 | +import {flow, pipe} from 'fp-ts/lib/function'; |
| 2 | +import * as E from 'fp-ts/Either'; |
| 3 | +import * as t from 'io-ts'; |
| 4 | +import {StatusCodes} from 'http-status-codes'; |
| 5 | +import {formatValidationErrors} from 'io-ts-reporters'; |
| 6 | +import { |
| 7 | + FailureWithStatus, |
| 8 | + failureWithStatus, |
| 9 | +} from '../../types/failure-with-status'; |
| 10 | +import {Form} from '../../types/form'; |
| 11 | +import { |
| 12 | + html, |
| 13 | + safe, |
| 14 | + sanitizeString, |
| 15 | + toLoggedInContent, |
| 16 | +} from '../../types/html'; |
| 17 | +import {SharedReadModel} from '../../read-models/shared-state'; |
| 18 | +import { |
| 19 | + areasTable, |
| 20 | +} from '../../read-models/shared-state/state'; |
| 21 | +import {eq} from 'drizzle-orm'; |
| 22 | + |
| 23 | +type ViewModel = { |
| 24 | + areaId: string; |
| 25 | + areaName: string; |
| 26 | + currentEmail: string; |
| 27 | +}; |
| 28 | + |
| 29 | +const renderBody = (viewModel: ViewModel) => html` |
| 30 | + <h1>Update mailing list for '${sanitizeString(viewModel.areaName)}'</h1> |
| 31 | + <form action="#" method="post"> |
| 32 | + <label for="email">Mailing list email address</label> |
| 33 | + <input |
| 34 | + type="text" |
| 35 | + name="email" |
| 36 | + id="email" |
| 37 | + value="${safe(viewModel.currentEmail)}" |
| 38 | + /> |
| 39 | + <input type="hidden" name="id" value="${safe(viewModel.areaId)}" /> |
| 40 | + <button type="submit">Update mailing list</button> |
| 41 | + </form> |
| 42 | + <p> |
| 43 | + <small>Leave empty to remove the mailing list email</small> |
| 44 | + </p> |
| 45 | +`; |
| 46 | + |
| 47 | +const renderForm = (viewModel: ViewModel) => |
| 48 | + pipe(viewModel, renderBody, toLoggedInContent(safe('Update area mailing list'))); |
| 49 | + |
| 50 | +const paramsCodec = t.strict({ |
| 51 | + area: t.string, |
| 52 | +}); |
| 53 | + |
| 54 | +const getAreaId = (input: unknown) => |
| 55 | + pipe( |
| 56 | + input, |
| 57 | + paramsCodec.decode, |
| 58 | + E.map(params => params.area), |
| 59 | + E.mapLeft( |
| 60 | + flow( |
| 61 | + formatValidationErrors, |
| 62 | + failureWithStatus( |
| 63 | + 'Parameters submitted to the form were invalid', |
| 64 | + StatusCodes.BAD_REQUEST |
| 65 | + ) |
| 66 | + ) |
| 67 | + ) |
| 68 | + ); |
| 69 | + |
| 70 | +const getAreaInfo = (db: SharedReadModel['db'], areaId: string) => |
| 71 | + pipe( |
| 72 | + db |
| 73 | + .select({areaName: areasTable.name, email: areasTable.email}) |
| 74 | + .from(areasTable) |
| 75 | + .where(eq(areasTable.id, areaId)) |
| 76 | + .get(), |
| 77 | + E.fromNullable( |
| 78 | + failureWithStatus( |
| 79 | + 'The requested area does not exist', |
| 80 | + StatusCodes.NOT_FOUND |
| 81 | + )() |
| 82 | + ), |
| 83 | + E.map(result => ({ |
| 84 | + areaName: result.areaName, |
| 85 | + currentEmail: result.email ?? '', |
| 86 | + })) |
| 87 | + ); |
| 88 | + |
| 89 | +const constructForm: Form<ViewModel>['constructForm'] = |
| 90 | + input => |
| 91 | + ({readModel}): E.Either<FailureWithStatus, ViewModel> => |
| 92 | + pipe( |
| 93 | + E.Do, |
| 94 | + E.bind('areaId', () => getAreaId(input)), |
| 95 | + E.bind('areaInfo', ({areaId}) => getAreaInfo(readModel.db, areaId)), |
| 96 | + E.map(({areaId, areaInfo}) => ({ |
| 97 | + areaId, |
| 98 | + areaName: areaInfo.areaName, |
| 99 | + currentEmail: areaInfo.currentEmail, |
| 100 | + })) |
| 101 | + ); |
| 102 | + |
| 103 | +export const setMailingListForm: Form<ViewModel> = { |
| 104 | + renderForm, |
| 105 | + constructForm, |
| 106 | +}; |
0 commit comments