|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { Button, FormGroup, Input, Label, Modal, ModalBody, ModalFooter, ModalHeader } from 'reactstrap'; |
| 3 | +import { ONCOGENICITY_OPTIONS } from 'app/config/constants/firebase'; |
| 4 | +import { IRootStore } from 'app/stores'; |
| 5 | +import { observer } from 'mobx-react'; |
| 6 | +import { componentInject } from '../util/typed-inject'; |
| 7 | +import { onValue, ref } from 'firebase/database'; |
| 8 | +import { getFirebaseRangesPath } from '../util/firebase/firebase-utils'; |
| 9 | +import { MutationRange, RangeList } from '../model/firebase/firebase.model'; |
| 10 | + |
| 11 | +enum AddRangeStep { |
| 12 | + POSITION = 1, |
| 13 | + CRITERIA, |
| 14 | + ALIAS, |
| 15 | +} |
| 16 | +const addRangeStepLength = Object.keys(AddRangeStep).length / 2; |
| 17 | +const mutationTypes = ['Missense', 'Insertion', 'Deletion']; |
| 18 | + |
| 19 | +export interface IAddRangeModalProps extends StoreProps { |
| 20 | + hugoSymbol: string; |
| 21 | + isGermline: boolean; |
| 22 | + onCancel: () => void; |
| 23 | + onConfirm: (alias: string, start: number, end: number, oncogencities: string[], mutationTypes: string[]) => void; |
| 24 | +} |
| 25 | + |
| 26 | +function AddRangeModal({ hugoSymbol, isGermline, onCancel, onConfirm, firebaseDb }: IAddRangeModalProps) { |
| 27 | + const [currentStep, setCurrentStep] = useState(AddRangeStep.POSITION); |
| 28 | + const [position, setPosition] = useState<[number | undefined, number | undefined]>([undefined, undefined]); |
| 29 | + const [selectedOncogenicity, setSelectedOncogenicity] = useState<string[]>([]); |
| 30 | + const [selectedMutationTypes, setSelectedMutationTypes] = useState<string[]>([]); |
| 31 | + const [alias, setAlias] = useState(''); |
| 32 | + const [existingAliases, setExistingAliases] = useState<string[]>([]); |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + if (!firebaseDb) { |
| 36 | + return; |
| 37 | + } |
| 38 | + const callback = onValue(ref(firebaseDb, getFirebaseRangesPath(isGermline, hugoSymbol)), snapshot => { |
| 39 | + const ranges: RangeList | undefined = snapshot.val(); |
| 40 | + if (ranges) { |
| 41 | + setExistingAliases(Object.values(ranges).map(range => range.alias)); |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + return () => { |
| 46 | + callback(); |
| 47 | + }; |
| 48 | + }, [firebaseDb]); |
| 49 | + |
| 50 | + let errorMessage: string | undefined = undefined; |
| 51 | + switch (currentStep) { |
| 52 | + case AddRangeStep.POSITION: |
| 53 | + errorMessage = validateRange(position[0], position[1]); |
| 54 | + break; |
| 55 | + case AddRangeStep.CRITERIA: |
| 56 | + break; |
| 57 | + case AddRangeStep.ALIAS: |
| 58 | + errorMessage = validateAlias(alias, existingAliases); |
| 59 | + break; |
| 60 | + default: |
| 61 | + } |
| 62 | + |
| 63 | + function nextStep() { |
| 64 | + setCurrentStep(step => step.valueOf() + 1); |
| 65 | + } |
| 66 | + |
| 67 | + function prevStep() { |
| 68 | + setCurrentStep(step => step.valueOf() - 1); |
| 69 | + } |
| 70 | + |
| 71 | + function toggleOncogenicity(value: string) { |
| 72 | + setSelectedOncogenicity(prev => (prev.includes(value) ? prev.filter(v => v !== value) : [...prev, value])); |
| 73 | + } |
| 74 | + |
| 75 | + function toggleMutationType(value: string) { |
| 76 | + setSelectedMutationTypes(prev => (prev.includes(value) ? prev.filter(v => v !== value) : [...prev, value])); |
| 77 | + } |
| 78 | + |
| 79 | + return ( |
| 80 | + <Modal isOpen> |
| 81 | + <ModalHeader toggle={() => onCancel()}>{`Add Range (${currentStep} / ${addRangeStepLength})`}</ModalHeader> |
| 82 | + <ModalBody> |
| 83 | + <Label>Position</Label> |
| 84 | + <div className="d-flex align-items-center gap-2 mb-2"> |
| 85 | + <Input |
| 86 | + type="number" |
| 87 | + disabled={currentStep !== AddRangeStep.POSITION} |
| 88 | + placeholder="Start" |
| 89 | + onChange={event => setPosition(pos => [event.target.value ? Number(event.target.value) : undefined, pos[1]])} |
| 90 | + /> |
| 91 | + <span>—</span> |
| 92 | + <Input |
| 93 | + type="number" |
| 94 | + disabled={currentStep !== AddRangeStep.POSITION} |
| 95 | + placeholder="End" |
| 96 | + onChange={event => setPosition(pos => [pos[0], event.target.value ? Number(event.target.value) : undefined])} |
| 97 | + /> |
| 98 | + </div> |
| 99 | + {currentStep.valueOf() >= AddRangeStep.CRITERIA.valueOf() && ( |
| 100 | + <> |
| 101 | + <Label>Oncogenicity</Label> |
| 102 | + <div className="mb-2"> |
| 103 | + {ONCOGENICITY_OPTIONS.map(option => ( |
| 104 | + <FormGroup inline check key={option}> |
| 105 | + <Input |
| 106 | + disabled={currentStep !== AddRangeStep.CRITERIA} |
| 107 | + type="checkbox" |
| 108 | + id={`oncogenicity-${option}`} |
| 109 | + checked={selectedOncogenicity.includes(option)} |
| 110 | + onChange={() => toggleOncogenicity(option)} |
| 111 | + /> |
| 112 | + <Label check for={`oncogenicity-${option}`}> |
| 113 | + {option} |
| 114 | + </Label> |
| 115 | + </FormGroup> |
| 116 | + ))} |
| 117 | + </div> |
| 118 | + <Label>Mutation Type</Label> |
| 119 | + <div className="mb-2"> |
| 120 | + {mutationTypes.map(option => ( |
| 121 | + <FormGroup inline check key={option}> |
| 122 | + <Input |
| 123 | + disabled={currentStep !== AddRangeStep.CRITERIA} |
| 124 | + type="checkbox" |
| 125 | + id={`mutation-type-${option}`} |
| 126 | + checked={selectedMutationTypes.includes(option)} |
| 127 | + onChange={() => toggleMutationType(option)} |
| 128 | + /> |
| 129 | + <Label check for={`mutation-type-${option}`}> |
| 130 | + {option} |
| 131 | + </Label> |
| 132 | + </FormGroup> |
| 133 | + ))} |
| 134 | + </div> |
| 135 | + </> |
| 136 | + )} |
| 137 | + {currentStep.valueOf() >= AddRangeStep.ALIAS.valueOf() && ( |
| 138 | + <> |
| 139 | + <Label>Alias</Label> |
| 140 | + <Input value={alias} placeholder="Enter an alias for the range" onChange={event => setAlias(event.target.value)} /> |
| 141 | + </> |
| 142 | + )} |
| 143 | + </ModalBody> |
| 144 | + <ModalFooter className="d-flex justify-content-between"> |
| 145 | + {currentStep.valueOf() === 1 ? ( |
| 146 | + <Button outline color="danger" onClick={onCancel}> |
| 147 | + Cancel |
| 148 | + </Button> |
| 149 | + ) : ( |
| 150 | + <Button outline color="danger" onClick={prevStep}> |
| 151 | + Previous |
| 152 | + </Button> |
| 153 | + )} |
| 154 | + <span className="text-danger">{errorMessage}</span> |
| 155 | + {currentStep.valueOf() < addRangeStepLength ? ( |
| 156 | + <Button disabled={!!errorMessage} outline color="primary" onClick={nextStep}> |
| 157 | + Next |
| 158 | + </Button> |
| 159 | + ) : ( |
| 160 | + <Button |
| 161 | + disabled={!!errorMessage} |
| 162 | + color="primary" |
| 163 | + onClick={() => { |
| 164 | + onConfirm(alias, position[0]!, position[1]!, selectedOncogenicity, selectedMutationTypes); |
| 165 | + }} |
| 166 | + > |
| 167 | + Confirm |
| 168 | + </Button> |
| 169 | + )} |
| 170 | + </ModalFooter> |
| 171 | + </Modal> |
| 172 | + ); |
| 173 | +} |
| 174 | + |
| 175 | +const mapStoreToProps = ({ firebaseAppStore }: IRootStore) => ({ |
| 176 | + firebaseDb: firebaseAppStore.firebaseDb, |
| 177 | +}); |
| 178 | + |
| 179 | +type StoreProps = Partial<ReturnType<typeof mapStoreToProps>>; |
| 180 | + |
| 181 | +export default componentInject(mapStoreToProps)(observer(AddRangeModal)); |
| 182 | + |
| 183 | +function validateRange(start: number | undefined, end: number | undefined): string | undefined { |
| 184 | + if (start === undefined || end === undefined) { |
| 185 | + return 'Start and end must both be specified'; |
| 186 | + } |
| 187 | + if (start >= end) { |
| 188 | + return 'Start must be less than end.'; |
| 189 | + } |
| 190 | + return undefined; |
| 191 | + // TODO: transcript validation |
| 192 | +} |
| 193 | + |
| 194 | +function validateAlias(alias: string, existingAliases: string[]) { |
| 195 | + alias = alias.trim(); |
| 196 | + if (!alias) { |
| 197 | + return 'Alias must be specified.'; |
| 198 | + } |
| 199 | + if (existingAliases.some(existing => alias.toLowerCase() === existing.toLowerCase())) { |
| 200 | + return 'Alias already exists.'; |
| 201 | + } |
| 202 | + return undefined; |
| 203 | +} |
0 commit comments