| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | |
| 8 | "amadan.net/oficina/mapa/internal/mapa/locale" |
| 9 | ) |
| 10 | |
| 11 | // Settings' date and number format (level-up build note 85): the |
| 12 | // browser's by default, or one of the locales Mapa offers, the person's |
| 13 | // own, kept sealed on their member row and set with an event. |
| 14 | |
| 15 | // formatProblem carries a refused format's sentence to the settings |
| 16 | // page drawn again. |
| 17 | type formatProblem struct{} |
| 18 | |
| 19 | // formatField is the format's select. |
| 20 | type formatField struct { |
| 21 | Options []formatOption |
| 22 | Hint string |
| 23 | Error string |
| 24 | } |
| 25 | |
| 26 | // formatOption is field-select's option. |
| 27 | type formatOption struct { |
| 28 | Value, Label string |
| 29 | Selected bool |
| 30 | } |
| 31 | |
| 32 | // formatFor is the field: the browser's first, then each offered |
| 33 | // locale, named in English with today's date written its way. |
| 34 | func formatFor(viewer Viewer, browser *locale.Locale, |
| 35 | today string) formatField { |
| 36 | // A stored code Mapa no longer offers is no choice: the browser's |
| 37 | // is what applies (ownLocale), so that is what the field says. |
| 38 | own := viewer.OwnLocale |
| 39 | if locale.Get(own) == nil { |
| 40 | own = "" |
| 41 | } |
| 42 | field := formatField{Hint: "until you choose, your browser's"} |
| 43 | if own != "" { |
| 44 | field.Hint = "your own setting" |
| 45 | } |
| 46 | field.Options = []formatOption{{Value: "", |
| 47 | Label: "Your browser's: " + locale.Name(browser.Code()), |
| 48 | Selected: own == ""}} |
| 49 | for _, code := range locale.Offered() { |
| 50 | field.Options = append(field.Options, formatOption{Value: code, |
| 51 | Label: locale.Name(code) + ": " + |
| 52 | locale.Get(code).Long(today), |
| 53 | Selected: code == own}) |
| 54 | } |
| 55 | return field |
| 56 | } |
| 57 | |
| 58 | // formatNotice is the bar after a save. |
| 59 | func formatNotice(viewer Viewer) string { |
| 60 | if locale.Get(viewer.OwnLocale) == nil { |
| 61 | return "Saved. Dates, times and numbers follow your browser again." |
| 62 | } |
| 63 | return "Saved. Dates, times and numbers are now written as in " + |
| 64 | locale.Name(viewer.OwnLocale) + "." |
| 65 | } |
| 66 | |
| 67 | // saveLocale is POST /settings/locale. |
| 68 | func (app *app) saveLocale(writer http.ResponseWriter, |
| 69 | request *http.Request) { |
| 70 | address, _ := app.identity(request) |
| 71 | err := app.store.SetLocale(request.Context(), address, |
| 72 | request.PostFormValue("locale")) |
| 73 | if errors.Is(err, ErrBadLocale) { |
| 74 | app.showSettings(writer, request.WithContext(context.WithValue( |
| 75 | request.Context(), formatProblem{}, |
| 76 | "Choose one of the formats listed.")), |
| 77 | http.StatusUnprocessableEntity, "", "") |
| 78 | return |
| 79 | } |
| 80 | if err != nil { |
| 81 | app.fail(writer, request, err) |
| 82 | return |
| 83 | } |
| 84 | seeOther(writer, request, "/settings?done=locale") |
| 85 | } |
| 86 | |