|
| 1 | +import {pipe} from 'fp-ts/lib/function'; |
| 2 | +import * as E from 'fp-ts/Either'; |
| 3 | +import {html, safe, sanitizeString, toLoggedInContent} from '../../types/html'; |
| 4 | +import {Form} from '../../types/form'; |
| 5 | +import {getEquipmentIdFromForm} from './get-equipment-id-from-form'; |
| 6 | +import {UUID} from 'io-ts-types'; |
| 7 | +import {failureWithStatus} from '../../types/failure-with-status'; |
| 8 | +import {StatusCodes} from 'http-status-codes'; |
| 9 | +import {currentTrainingSheetButton} from '../../queries/shared-render/current-training-sheet-button'; |
| 10 | + |
| 11 | +type ViewModel = { |
| 12 | + equipmentId: UUID; |
| 13 | + equipmentName: string; |
| 14 | + currentTrainingSheetId: string; |
| 15 | +}; |
| 16 | + |
| 17 | +const renderForm = (viewModel: ViewModel) => |
| 18 | + pipe( |
| 19 | + html` |
| 20 | + <h1> |
| 21 | + Are you sure you wish to remove the current training sheet for |
| 22 | + ${sanitizeString(viewModel.equipmentName)}? |
| 23 | + </h1> |
| 24 | + ${currentTrainingSheetButton(viewModel.currentTrainingSheetId)} |
| 25 | + <form action="/equipment/remove-training-sheet" method="del"> |
| 26 | + <input |
| 27 | + type="hidden" |
| 28 | + name="equipmentId" |
| 29 | + value="${viewModel.equipmentId}" |
| 30 | + /> |
| 31 | + <button type="submit">Confirm</button> |
| 32 | + </form> |
| 33 | + `, |
| 34 | + toLoggedInContent(safe('Remove training sheet')) |
| 35 | + ); |
| 36 | + |
| 37 | +const constructForm: Form<ViewModel>['constructForm'] = |
| 38 | + input => |
| 39 | + ({readModel}) => |
| 40 | + pipe( |
| 41 | + E.Do, |
| 42 | + E.bind('equipmentId', () => getEquipmentIdFromForm(input)), |
| 43 | + E.bind('equipment', ({equipmentId}) => |
| 44 | + pipe( |
| 45 | + equipmentId, |
| 46 | + readModel.equipment.get, |
| 47 | + E.fromOption(() => |
| 48 | + failureWithStatus('No such equipment', StatusCodes.NOT_FOUND)() |
| 49 | + ) |
| 50 | + ) |
| 51 | + ), |
| 52 | + E.bind('equipmentName', ({equipment}) => E.right(equipment.name)), |
| 53 | + E.bind('currentTrainingSheetId', ({equipment}) => |
| 54 | + pipe( |
| 55 | + equipment.trainingSheetId, |
| 56 | + E.fromOption(() => |
| 57 | + failureWithStatus( |
| 58 | + 'No training sheet currently registered', |
| 59 | + StatusCodes.NOT_FOUND |
| 60 | + )() |
| 61 | + ) |
| 62 | + ) |
| 63 | + ) |
| 64 | + ); |
| 65 | + |
| 66 | +export const removeTrainingSheetForm: Form<ViewModel> = { |
| 67 | + renderForm, |
| 68 | + constructForm, |
| 69 | +}; |
0 commit comments