Skip to content

Commit e7b4107

Browse files
committed
Improve oa input robustness
1 parent a015edf commit e7b4107

File tree

1 file changed

+40
-18
lines changed
  • src/Client/MainComponents/Metadata

1 file changed

+40
-18
lines changed

src/Client/MainComponents/Metadata/Forms.fs

+40-18
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ module Helper =
5252
?Roles=this.Roles
5353
)
5454

55+
type OntologyAnnotationMutable(?name,?tsr,?tan) =
56+
member val Name : string option = name with get, set
57+
member val TSR : string option = tsr with get, set
58+
member val TAN : string option = tan with get, set
59+
60+
static member fromOntologyAnnotation(oa: OntologyAnnotation) =
61+
let name = if oa.NameText = "" then None else Some oa.NameText
62+
OntologyAnnotationMutable(?name=name, ?tsr=oa.TermSourceREF, ?tan=oa.TermAccessionNumber)
63+
64+
member this.ToOntologyAnnotation() =
65+
OntologyAnnotation.fromString(?termName=this.Name,?tsr=this.TSR,?tan=this.TAN)
66+
5567
let addButton (clickEvent: MouseEvent -> unit) =
5668
Html.div [
5769
prop.classes ["is-flex"; "is-justify-content-center"]
@@ -110,31 +122,37 @@ type FormComponents =
110122
[<ReactComponent>]
111123
static member OntologyAnnotationInput (oa: OntologyAnnotation, label: string, setter: OntologyAnnotation -> unit, ?showTextLabels: bool, ?removebutton: MouseEvent -> unit) =
112124
let showTextLabels = defaultArg showTextLabels true
125+
let oa = React.useRef(Helper.OntologyAnnotationMutable.fromOntologyAnnotation oa)
113126
Bulma.field.div [
114127
if label <> "" then Bulma.label label
115128
Html.div [
116129
prop.classes ["form-container"]
117130
prop.children [
118131
FormComponents.TextInput(
119-
oa.NameText,
132+
Option.defaultValue "" oa.current.Name,
120133
(if showTextLabels then $"Term Name" else ""),
121-
(fun s -> { oa with Name = AnnotationValue.Text s |> Some } |> setter),
134+
(fun s ->
135+
let s = if s = "" then None else Some s
136+
oa.current.Name <- s
137+
oa.current.ToOntologyAnnotation() |> setter),
122138
fullwidth = true
123139
)
124140
FormComponents.TextInput(
125-
oa.TermSourceREFString,
141+
Option.defaultValue "" oa.current.TSR,
126142
(if showTextLabels then $"TSR" else ""),
127143
(fun s ->
128-
let s2 = s |> fun s -> if s = "" then None else Some s
129-
{ oa with TermSourceREF = s2 } |> setter),
144+
let s = if s = "" then None else Some s
145+
oa.current.TSR <- s
146+
oa.current.ToOntologyAnnotation() |> setter),
130147
fullwidth = true
131148
)
132149
FormComponents.TextInput(
133-
oa.TermAccessionShort,
150+
Option.defaultValue "" oa.current.TAN,
134151
(if showTextLabels then $"TAN" else ""),
135152
(fun s ->
136-
let s2 = s |> fun s -> if s = "" then None else Some s
137-
{ oa with TermAccessionNumber = s2 } |> setter),
153+
let s = if s = "" then None else Some s
154+
oa.current.TAN <- s
155+
oa.current.ToOntologyAnnotation() |> setter),
138156
fullwidth = true
139157
)
140158
if removebutton.IsSome then
@@ -149,13 +167,14 @@ type FormComponents =
149167
]
150168

151169
[<ReactComponent>]
152-
static member PersonInput(person: Person, setter: Person -> unit, ?deletebutton: MouseEvent -> unit) =
170+
static member PersonInput(person': Person, setter: Person -> unit, ?deletebutton: MouseEvent -> unit) =
153171
let isExtended, setIsExtended = React.useState(false)
154-
let fn = Option.defaultValue "" person.FirstName
155-
let ln = Option.defaultValue "" person.LastName
156-
let mi = Option.defaultValue "" person.MidInitials
157172
// Must use `React.useRef` do this. Otherwise simultanios updates will overwrite each other
158-
let person = React.useRef(Helper.PersonMutable.fromPerson person)
173+
let person = React.useRef(Helper.PersonMutable.fromPerson person')
174+
React.useEffect((fun _ -> person.current <- Helper.PersonMutable.fromPerson person'), [|box person|])
175+
let fn = Option.defaultValue "" person.current.FirstName
176+
let ln = Option.defaultValue "" person.current.LastName
177+
let mi = Option.defaultValue "" person.current.MidInitials
159178
let nameStr =
160179
let x = $"{fn} {mi} {ln}".Trim()
161180
if x = "" then "<name>" else x
@@ -263,17 +282,20 @@ type FormComponents =
263282
setter {person.current.ToPerson() with Roles = Some nextRoles}
264283
),
265284
showTextLabels = false,
266-
removebutton=(fun e ->
267-
let nextRoles = person.current.Roles.Value |> Array.removeAt i
268-
setter {person.current.ToPerson() with Roles = if nextRoles.Length = 0 then None else Some nextRoles}
285+
removebutton=(fun _ ->
286+
person.current.Roles <- (
287+
let a = Array.removeAt i person.current.Roles.Value
288+
if a = Array.empty then None else Some a
289+
)
290+
person.current.ToPerson() |> setter
269291
))
270292
]
271293
)
272294
]
273295
Helper.addButton (fun _ ->
274296
let roles = Option.defaultValue [||] person.current.Roles
275-
let newRoles = Array.append roles [|OntologyAnnotation.empty|]
276-
setter {person.current.ToPerson() with Roles = Some newRoles}
297+
person.current.Roles <- Array.append roles [|OntologyAnnotation.empty|] |> Some
298+
person.current.ToPerson() |> setter
277299
)
278300
]
279301
if deletebutton.IsSome then

0 commit comments

Comments
 (0)