| 1 | package fichas |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "strings" |
| 6 | |
| 7 | "github.com/go-chi/chi/v5" |
| 8 | ) |
| 9 | |
| 10 | // The type designer's pages. Every one is a form that POSTs and works with |
| 11 | // scripts off; a FormError comes back to the same form with what was typed |
| 12 | // and a message beside the field that needs it (422), never a 500. |
| 13 | |
| 14 | // kindWords names a kind for a person. |
| 15 | var kindWords = map[string]string{ |
| 16 | KindText: "Text", KindLongText: "Long text", KindNumber: "Number", |
| 17 | KindDate: "Date", KindEnum: "Choice", KindRef: "Link", KindURL: "Web address", |
| 18 | KindEmail: "Email", KindPhone: "Phone", |
| 19 | } |
| 20 | |
| 21 | // FieldRow is one line of a type's fields page. |
| 22 | type FieldRow struct { |
| 23 | Key, Label, Kind string |
| 24 | Findable, Title bool |
| 25 | Options []string |
| 26 | } |
| 27 | |
| 28 | func (a *app) fieldsPage(w http.ResponseWriter, r *http.Request) { |
| 29 | ti, err := a.currentType(r) |
| 30 | if err != nil { |
| 31 | a.notFoundOrFail(w, r, err) |
| 32 | return |
| 33 | } |
| 34 | fields, err := a.ix.Fields(ti.Key) |
| 35 | if err != nil { |
| 36 | a.fail(w, r, err) |
| 37 | return |
| 38 | } |
| 39 | var rows []FieldRow |
| 40 | for i, f := range fields { |
| 41 | kind := kindWords[f.Def.Kind] |
| 42 | if isLong(f.Def) { |
| 43 | kind = kindWords[KindLongText] |
| 44 | } |
| 45 | rows = append(rows, FieldRow{Key: f.Def.Key, Label: f.Label, Kind: kind, |
| 46 | Findable: f.Def.Tier == TierPrint, Title: f.Def.Key == FieldName || (i == 0 && !hasName(fields)), |
| 47 | Options: f.Options}) |
| 48 | } |
| 49 | a.page(w, r, "fields", map[string]any{"TypeKey": ti.Key, "Type": ti, "Rows": rows}) |
| 50 | } |
| 51 | |
| 52 | func hasName(fields []FieldView) bool { |
| 53 | for _, f := range fields { |
| 54 | if f.Def.Key == FieldName { |
| 55 | return true |
| 56 | } |
| 57 | } |
| 58 | return false |
| 59 | } |
| 60 | |
| 61 | func (a *app) newField(w http.ResponseWriter, r *http.Request) { |
| 62 | ti, err := a.currentType(r) |
| 63 | if err != nil { |
| 64 | a.notFoundOrFail(w, r, err) |
| 65 | return |
| 66 | } |
| 67 | a.page(w, r, "field_new", map[string]any{"TypeKey": ti.Key, "Type": ti, |
| 68 | "Form": map[string]string{"kind": KindText, "tier": "auto"}}) |
| 69 | } |
| 70 | |
| 71 | func (a *app) createField(w http.ResponseWriter, r *http.Request) { |
| 72 | actor, err := a.actor(r) |
| 73 | if err != nil { |
| 74 | a.fail(w, r, err) |
| 75 | return |
| 76 | } |
| 77 | ti, err := a.currentType(r) |
| 78 | if err != nil { |
| 79 | a.notFoundOrFail(w, r, err) |
| 80 | return |
| 81 | } |
| 82 | _ = r.ParseForm() |
| 83 | spec := FieldSpec{ |
| 84 | Label: r.PostForm.Get("label"), |
| 85 | Kind: r.PostForm.Get("kind"), |
| 86 | Tier: Tier(r.PostForm.Get("tier")), |
| 87 | Options: strings.Split(r.PostForm.Get("options"), "\n"), |
| 88 | } |
| 89 | // "auto" (the default, and what a form without the Advanced choice |
| 90 | // sends) is the kind's own tier. |
| 91 | formTier := r.PostForm.Get("tier") |
| 92 | if formTier == "" || formTier == "auto" { |
| 93 | spec.Tier, formTier = DefaultTier(spec.Kind), "auto" |
| 94 | } |
| 95 | _, err = AddField(r.Context(), a.db.G, a.sealer, ti.Key, actor, spec) |
| 96 | if fe, ok := IsFormError(err); ok { |
| 97 | a.pageStatus(w, r, http.StatusUnprocessableEntity, "field_new", map[string]any{ |
| 98 | "TypeKey": ti.Key, "Type": ti, "Errors": fe, |
| 99 | "Form": map[string]string{"label": spec.Label, "kind": spec.Kind, "tier": formTier, "options": r.PostForm.Get("options")}}) |
| 100 | return |
| 101 | } |
| 102 | if err != nil { |
| 103 | a.fail(w, r, err) |
| 104 | return |
| 105 | } |
| 106 | http.Redirect(w, r, typeHref(ti.Key)+"/fields", http.StatusSeeOther) |
| 107 | } |
| 108 | |
| 109 | // fieldForEdit finds a field and the type it belongs to, from the index. |
| 110 | func (a *app) fieldForEdit(r *http.Request) (TypeInfo, FieldView, error) { |
| 111 | if err := a.ix.Sync(r.Context()); err != nil { |
| 112 | return TypeInfo{}, FieldView{}, err |
| 113 | } |
| 114 | key := chi.URLParam(r, "key") |
| 115 | for _, t := range a.ix.Types() { |
| 116 | fields, err := a.ix.Fields(t.Key) |
| 117 | if err != nil { |
| 118 | return TypeInfo{}, FieldView{}, err |
| 119 | } |
| 120 | for _, f := range fields { |
| 121 | if f.Def.Key == key { |
| 122 | return t, f, nil |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | return TypeInfo{}, FieldView{}, errNotFound |
| 127 | } |
| 128 | |
| 129 | func (a *app) editField(w http.ResponseWriter, r *http.Request) { |
| 130 | ti, f, err := a.fieldForEdit(r) |
| 131 | if err != nil { |
| 132 | a.notFoundOrFail(w, r, err) |
| 133 | return |
| 134 | } |
| 135 | a.page(w, r, "field_edit", map[string]any{"TypeKey": ti.Key, "Type": ti, "Field": f, |
| 136 | "KindWords": kindWords[f.Def.Kind], "Label": f.Label, "Options": strings.Join(f.Options, "\n")}) |
| 137 | } |
| 138 | |
| 139 | func (a *app) updateField(w http.ResponseWriter, r *http.Request) { |
| 140 | actor, err := a.actor(r) |
| 141 | if err != nil { |
| 142 | a.fail(w, r, err) |
| 143 | return |
| 144 | } |
| 145 | ti, f, err := a.fieldForEdit(r) |
| 146 | if err != nil { |
| 147 | a.notFoundOrFail(w, r, err) |
| 148 | return |
| 149 | } |
| 150 | _ = r.ParseForm() |
| 151 | label, options := r.PostForm.Get("label"), r.PostForm.Get("options") |
| 152 | err = UpdateField(r.Context(), a.db.G, a.sealer, f.Def.Key, actor, label, strings.Split(options, "\n")) |
| 153 | if fe, ok := IsFormError(err); ok { |
| 154 | a.pageStatus(w, r, http.StatusUnprocessableEntity, "field_edit", map[string]any{"TypeKey": ti.Key, "Type": ti, |
| 155 | "Field": f, "KindWords": kindWords[f.Def.Kind], "Label": label, "Options": options, "Errors": fe}) |
| 156 | return |
| 157 | } |
| 158 | if err != nil { |
| 159 | a.fail(w, r, err) |
| 160 | return |
| 161 | } |
| 162 | http.Redirect(w, r, typeHref(ti.Key)+"/fields", http.StatusSeeOther) |
| 163 | } |
| 164 | |
| 165 | func (a *app) newType(w http.ResponseWriter, r *http.Request) { |
| 166 | a.page(w, r, "type_new", map[string]any{"Form": map[string]string{}}) |
| 167 | } |
| 168 | |
| 169 | func (a *app) createType(w http.ResponseWriter, r *http.Request) { |
| 170 | actor, err := a.actor(r) |
| 171 | if err != nil { |
| 172 | a.fail(w, r, err) |
| 173 | return |
| 174 | } |
| 175 | _ = r.ParseForm() |
| 176 | singular, plural := r.PostForm.Get("label"), r.PostForm.Get("plural") |
| 177 | ct, err := CreateType(r.Context(), a.db.G, a.sealer, actor, singular, plural) |
| 178 | if fe, ok := IsFormError(err); ok { |
| 179 | a.pageStatus(w, r, http.StatusUnprocessableEntity, "type_new", map[string]any{ |
| 180 | "Errors": fe, "Form": map[string]string{"label": singular, "plural": plural}}) |
| 181 | return |
| 182 | } |
| 183 | if err != nil { |
| 184 | a.fail(w, r, err) |
| 185 | return |
| 186 | } |
| 187 | // Straight to its fields: a new type has only a name, and adding what |
| 188 | // it needs is the next thing anyone does. |
| 189 | http.Redirect(w, r, typeHref(ct.Key)+"/fields", http.StatusSeeOther) |
| 190 | } |
| 191 | |
| 192 | // FaceOption is one switch on the card-layout page. |
| 193 | type FaceOption struct { |
| 194 | Key, Label string |
| 195 | On, Locked bool |
| 196 | } |
| 197 | |
| 198 | func (a *app) layoutPage(w http.ResponseWriter, r *http.Request) { |
| 199 | ti, err := a.currentType(r) |
| 200 | if err != nil { |
| 201 | a.notFoundOrFail(w, r, err) |
| 202 | return |
| 203 | } |
| 204 | a.renderLayout(w, r, ti, a.ix.Face(ti.Key), nil) |
| 205 | } |
| 206 | |
| 207 | func (a *app) renderLayout(w http.ResponseWriter, r *http.Request, ti TypeInfo, face []string, fe FormError) { |
| 208 | fields, err := a.ix.Fields(ti.Key) |
| 209 | if err != nil { |
| 210 | a.fail(w, r, err) |
| 211 | return |
| 212 | } |
| 213 | on := map[string]bool{} |
| 214 | for _, k := range face { |
| 215 | on[k] = true |
| 216 | } |
| 217 | var opts []FaceOption |
| 218 | for i, f := range fields { |
| 219 | title := f.Def.Key == FieldName || (i == 0 && !hasName(fields)) |
| 220 | opts = append(opts, FaceOption{Key: f.Def.Key, Label: f.Label, On: title || on[f.Def.Key], Locked: title}) |
| 221 | } |
| 222 | opts = append(opts, FaceOption{Key: FaceLatestCall, Label: "Latest call", On: on[FaceLatestCall]}) |
| 223 | |
| 224 | // A preview card: the most recently touched one, drawn with this face. |
| 225 | var preview *CardView |
| 226 | if res, err := a.ix.Query(ti.Key, Query{PerPage: 1}); err == nil && len(res.Cards) == 1 { |
| 227 | preview = &res.Cards[0] |
| 228 | } |
| 229 | status := http.StatusOK |
| 230 | if fe != nil { |
| 231 | status = http.StatusUnprocessableEntity |
| 232 | } |
| 233 | a.pageStatus(w, r, status, "card_layout", map[string]any{"TypeKey": ti.Key, "Type": ti, |
| 234 | "Options": opts, "Face": face, "Preview": preview, "Errors": fe}) |
| 235 | } |
| 236 | |
| 237 | func (a *app) saveLayout(w http.ResponseWriter, r *http.Request) { |
| 238 | actor, err := a.actor(r) |
| 239 | if err != nil { |
| 240 | a.fail(w, r, err) |
| 241 | return |
| 242 | } |
| 243 | ti, err := a.currentType(r) |
| 244 | if err != nil { |
| 245 | a.notFoundOrFail(w, r, err) |
| 246 | return |
| 247 | } |
| 248 | _ = r.ParseForm() |
| 249 | keys := r.PostForm["face"] |
| 250 | err = SetFace(r.Context(), a.db.G, a.sealer, ti.Key, actor, keys) |
| 251 | if fe, ok := IsFormError(err); ok { |
| 252 | a.renderLayout(w, r, ti, keys, fe) |
| 253 | return |
| 254 | } |
| 255 | if err != nil { |
| 256 | a.fail(w, r, err) |
| 257 | return |
| 258 | } |
| 259 | http.Redirect(w, r, typeHref(ti.Key)+"?view=cards", http.StatusSeeOther) |
| 260 | } |
| 261 | |