|
| 1 | +import { Accessor, For, Resource, Setter, createSignal } from "solid-js"; |
| 2 | + |
| 3 | +import { titleCase } from "@/utils/title_case"; |
| 4 | + |
| 5 | +import type { CheckConfig, EligibilityCheck } from "@/types"; |
| 6 | + |
| 7 | +export type EligibilityCheckListMode = "user-defined" | "public"; |
| 8 | +interface CheckModeConfig { |
| 9 | + mode: EligibilityCheckListMode; |
| 10 | + title: string; |
| 11 | + description: string; |
| 12 | + buttonTitle: string; |
| 13 | +} |
| 14 | +const PublicCheckConfig: CheckModeConfig = { |
| 15 | + mode: "public", |
| 16 | + title: "Public eligibility checks", |
| 17 | + description: "Browse and select pre-built checks to add to your benefit.", |
| 18 | + buttonTitle: "Public checks", |
| 19 | +}; |
| 20 | +const UserDefinedCheckConfig: CheckModeConfig = { |
| 21 | + mode: "user-defined", |
| 22 | + title: "User defined eligibility checks", |
| 23 | + description: |
| 24 | + "Browse and select your own custom checks to add to your benefit.", |
| 25 | + buttonTitle: "Your checks", |
| 26 | +}; |
| 27 | + |
| 28 | +/* |
| 29 | + Renders a list of eligibility checks with checkboxes to select/deselect them for the current benefit. |
| 30 | + Uses CheckModeConfig object to display a different title/description based on the current mode. |
| 31 | + Props: |
| 32 | + - mode: current mode ("user-defined" or "public") |
| 33 | + - setMode: function to change the mode |
| 34 | + - publicChecks: resource containing the list of public eligibility checks |
| 35 | + - userDefinedChecks: resource containing the list of user-defined eligibility checks |
| 36 | +*/ |
| 37 | +const EligibilityCheckListView = ({ |
| 38 | + addCheck, |
| 39 | + mode, |
| 40 | + setMode, |
| 41 | + publicChecks, |
| 42 | + userDefinedChecks, |
| 43 | +}: { |
| 44 | + addCheck: (newCheck: CheckConfig) => void; |
| 45 | + mode: Accessor<EligibilityCheckListMode>; |
| 46 | + setMode: Setter<EligibilityCheckListMode>; |
| 47 | + publicChecks: Resource<EligibilityCheck[]>; |
| 48 | + userDefinedChecks: Resource<EligibilityCheck[]>; |
| 49 | +}) => { |
| 50 | + const activeCheckConfig: Accessor<CheckModeConfig> = () => |
| 51 | + mode() === "public" ? PublicCheckConfig : UserDefinedCheckConfig; |
| 52 | + const activeChecks: Accessor<Resource<EligibilityCheck[]>> = () => |
| 53 | + mode() === "public" ? publicChecks : userDefinedChecks; |
| 54 | + |
| 55 | + const onAddEligibilityCheck = (check: EligibilityCheck) => { |
| 56 | + const checkConfig: CheckConfig = { |
| 57 | + checkId: check.id, |
| 58 | + checkName: check.name, |
| 59 | + checkVersion: check.version, |
| 60 | + checkModule: check.module, |
| 61 | + checkDescription: check.description, |
| 62 | + evaluationUrl: check.evaluationUrl, |
| 63 | + parameters: {}, |
| 64 | + parameterDefinitions: check.parameterDefinitions, |
| 65 | + inputDefinition: check.inputDefinition, |
| 66 | + }; |
| 67 | + addCheck(checkConfig); |
| 68 | + }; |
| 69 | + |
| 70 | + const flipMode = () => { |
| 71 | + console.log("click") |
| 72 | + console.log(mode()) |
| 73 | + setMode(mode() === "public" ? "user-defined" : "public"); |
| 74 | + } |
| 75 | + |
| 76 | + return ( |
| 77 | + <> |
| 78 | + <div class="p-4"> |
| 79 | + <div class="flex items-center mb-2"> |
| 80 | + <div class="text-2xl font-bold">{activeCheckConfig().title}</div> |
| 81 | + <div class="grid w-full grid-cols-2 items-center justify-center rounded-md bg-muted bg-gray-100 p-1 text-gray-500 mb-2 w-xs"> |
| 82 | + <button |
| 83 | + onClick={() => flipMode()} |
| 84 | + class={`inline-flex items-center justify-center whitespace-nowrap rounded-sm px-3 py-1.5 text-sm font-medium transition-all focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 rounded ${ |
| 85 | + mode() === "public" |
| 86 | + ? "bg-white text-gray-950 shadow-sm" |
| 87 | + : "hover:bg-gray-200" |
| 88 | + }`} |
| 89 | + > |
| 90 | + Public Checks |
| 91 | + </button> |
| 92 | + <button |
| 93 | + onClick={() => flipMode()} |
| 94 | + class={`inline-flex items-center justify-center whitespace-nowrap rounded-sm px-3 py-1.5 text-sm font-medium transition-all focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 rounded ${ |
| 95 | + mode() === "user-defined" |
| 96 | + ? "bg-white text-gray-950 shadow-sm" |
| 97 | + : "hover:bg-gray-200" |
| 98 | + }`} |
| 99 | + > |
| 100 | + Custom Checks |
| 101 | + </button> |
| 102 | + </div> |
| 103 | + |
| 104 | + </div> |
| 105 | + <div>{activeCheckConfig().description}</div> |
| 106 | + </div> |
| 107 | + <table class="table-auto w-full mt-4 border-collapse"> |
| 108 | + <thead> |
| 109 | + <tr> |
| 110 | + <th class="eligibility-check-table-header">Add</th> |
| 111 | + <th class="eligibility-check-table-header">Check Name</th> |
| 112 | + <th class="eligibility-check-table-header">Description</th> |
| 113 | + <th class="eligibility-check-table-header">Version</th> |
| 114 | + </tr> |
| 115 | + </thead> |
| 116 | + <tbody> |
| 117 | + {activeChecks().loading && ( |
| 118 | + <tr> |
| 119 | + <td colSpan={3} class="p-4 font-bold text-center"> |
| 120 | + Loading checks... |
| 121 | + </td> |
| 122 | + </tr> |
| 123 | + )} |
| 124 | + {activeChecks()() && activeChecks()().length === 0 && ( |
| 125 | + <tr> |
| 126 | + <td colSpan={3} class="p-4 font-bold text-center text-gray-600"> |
| 127 | + No checks available. |
| 128 | + </td> |
| 129 | + </tr> |
| 130 | + )} |
| 131 | + <For each={activeChecks()()}> |
| 132 | + {(check) => ( |
| 133 | + <EligibilityCheckRow |
| 134 | + check={check} |
| 135 | + onAdd={() => onAddEligibilityCheck(check)} |
| 136 | + /> |
| 137 | + )} |
| 138 | + </For> |
| 139 | + </tbody> |
| 140 | + </table> |
| 141 | + </> |
| 142 | + ); |
| 143 | +}; |
| 144 | + |
| 145 | +const EligibilityCheckRow = ({ |
| 146 | + check, |
| 147 | + onAdd, |
| 148 | +}: { |
| 149 | + check: EligibilityCheck; |
| 150 | + onAdd: (check: EligibilityCheck) => void; |
| 151 | +}) => { |
| 152 | + return ( |
| 153 | + <tr> |
| 154 | + <td class="eligibility-check-table-cell border-top"> |
| 155 | + <div class="btn-default btn-blue" onClick={() => onAdd(check)}> |
| 156 | + Add |
| 157 | + </div> |
| 158 | + </td> |
| 159 | + <td class="eligibility-check-table-cell border-top"> |
| 160 | + {titleCase(check.name)} |
| 161 | + </td> |
| 162 | + <td class="eligibility-check-table-cell border-top"> |
| 163 | + {check.description} |
| 164 | + </td> |
| 165 | + <td class="eligibility-check-table-cell border-top">{check.version}</td> |
| 166 | + </tr> |
| 167 | + ); |
| 168 | +}; |
| 169 | +export default EligibilityCheckListView; |
0 commit comments