|
| 1 | +type formDisplay = |
| 2 | + | Horizontal |
| 3 | + | Inline |
| 4 | + | Stacked |
| 5 | + |
| 6 | +type savingStatus = |
| 7 | + | Idle |
| 8 | + | Saving |
| 9 | + | Error |
| 10 | + |
| 11 | +type formData = { |
| 12 | + formName: string, |
| 13 | + formType: formDisplay, |
| 14 | +} |
| 15 | + |
| 16 | +type state = { |
| 17 | + author: string, |
| 18 | + text: string, |
| 19 | + form: formDisplay, |
| 20 | + savingStatus: savingStatus, |
| 21 | +} |
| 22 | + |
| 23 | +type action = |
| 24 | + | SetAuthor(string) |
| 25 | + | SetText(string) |
| 26 | + | SetFormType(formDisplay) |
| 27 | + | SetSavingError |
| 28 | + | ClearSavingError |
| 29 | + | SetStoreStatusSaving |
| 30 | + |
| 31 | +let reducer = (state: state, action: action): state => { |
| 32 | + switch action { |
| 33 | + | SetAuthor(author) => {...state, author} |
| 34 | + | SetText(text) => {...state, text} |
| 35 | + | SetFormType(form) => {...state, form} |
| 36 | + | SetSavingError => {...state, savingStatus: Error} |
| 37 | + | ClearSavingError => {...state, savingStatus: Idle} |
| 38 | + | SetStoreStatusSaving => {...state, savingStatus: Saving} |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +@react.component |
| 43 | +let make = (~fetchData) => { |
| 44 | + let (state, dispatch) = React.useReducer( |
| 45 | + reducer, |
| 46 | + { |
| 47 | + author: "", |
| 48 | + text: "", |
| 49 | + form: Horizontal, |
| 50 | + savingStatus: Idle, |
| 51 | + }, |
| 52 | + ) |
| 53 | + |
| 54 | + let disabled = React.useMemo1(() => { |
| 55 | + switch state.savingStatus { |
| 56 | + | Saving => true |
| 57 | + | Idle |
| 58 | + | Error => false |
| 59 | + } |
| 60 | + }, [state.savingStatus]) |
| 61 | + |
| 62 | + let storeComment = (newComment: Actions.Create.t) => { |
| 63 | + SetStoreStatusSaving->dispatch |
| 64 | + let saveAndFetchComments = async () => { |
| 65 | + try { |
| 66 | + let _ = await Actions.Create.storeComment(newComment) |
| 67 | + ClearSavingError->dispatch |
| 68 | + |
| 69 | + await fetchData() |
| 70 | + } catch { |
| 71 | + | _ => SetSavingError->dispatch |
| 72 | + } |
| 73 | + } |
| 74 | + saveAndFetchComments()->ignore |
| 75 | + } |
| 76 | + |
| 77 | + let handleAuthorChange = event => { |
| 78 | + let value = ReactEvent.Form.currentTarget(event)["value"] |
| 79 | + SetAuthor(value)->dispatch |
| 80 | + } |
| 81 | + |
| 82 | + let handleTextChange = event => { |
| 83 | + let value = ReactEvent.Form.currentTarget(event)["value"] |
| 84 | + SetText(value)->dispatch |
| 85 | + } |
| 86 | + |
| 87 | + let handleSubmit = event => { |
| 88 | + ReactEvent.Form.preventDefault(event) |
| 89 | + storeComment({author: state.author, text: state.text}) |
| 90 | + } |
| 91 | + |
| 92 | + let forms: array<formData> = [ |
| 93 | + {formName: "Horizontal Form", formType: Horizontal}, |
| 94 | + {formName: "Inline Form", formType: Inline}, |
| 95 | + {formName: "Stacked Form", formType: Stacked}, |
| 96 | + ] |
| 97 | + |
| 98 | + <div> |
| 99 | + <div className="flex gap-1 not-prose"> |
| 100 | + {forms |
| 101 | + ->Array.map(form => |
| 102 | + <button |
| 103 | + key={`form_${form.formName}`} |
| 104 | + className={`px-6 py-2 font-semibold border-0 rounded ${state.form == form.formType |
| 105 | + ? "text-sky-50 bg-sky-600" |
| 106 | + : "text-sky-600 hover:bg-gray-100"}`} |
| 107 | + onClick={event => SetFormType(form.formType)->dispatch}> |
| 108 | + {form.formName->React.string} |
| 109 | + </button> |
| 110 | + ) |
| 111 | + ->React.array} |
| 112 | + </div> |
| 113 | + <hr /> |
| 114 | + {switch state.form { |
| 115 | + | Horizontal => |
| 116 | + <HorizontalForm |
| 117 | + author={state.author} |
| 118 | + handleAuthorChange |
| 119 | + text={state.text} |
| 120 | + handleTextChange |
| 121 | + handleSubmit |
| 122 | + disabled |
| 123 | + /> |
| 124 | + | Stacked => |
| 125 | + <StackedFrom |
| 126 | + author={state.author} |
| 127 | + handleAuthorChange |
| 128 | + text={state.text} |
| 129 | + handleTextChange |
| 130 | + handleSubmit |
| 131 | + disabled |
| 132 | + /> |
| 133 | + | Inline => |
| 134 | + <InlineForm |
| 135 | + author={state.author} |
| 136 | + handleAuthorChange |
| 137 | + text={state.text} |
| 138 | + handleTextChange |
| 139 | + handleSubmit |
| 140 | + disabled |
| 141 | + /> |
| 142 | + }} |
| 143 | + {switch state.savingStatus { |
| 144 | + | Error => <AlertError errorMsg="Can't save the comment!" /> |
| 145 | + | Idle |
| 146 | + | Saving => React.null |
| 147 | + }} |
| 148 | + </div> |
| 149 | +} |
0 commit comments