|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "net/http" |
| 7 | + "sync" |
| 8 | + |
| 9 | + "github.com/partial-coffee/go-partial" |
| 10 | +) |
| 11 | + |
| 12 | +// In-memory session store for demonstration |
| 13 | +var ( |
| 14 | + sessionStore = make(map[string]*partial.FlowSessionData) |
| 15 | + sessionMu sync.Mutex |
| 16 | +) |
| 17 | + |
| 18 | +func getSession(r *http.Request) *partial.FlowSessionData { |
| 19 | + // For demo, use a fixed session key |
| 20 | + sessionMu.Lock() |
| 21 | + defer sessionMu.Unlock() |
| 22 | + if sessionStore["demo"] == nil { |
| 23 | + sessionStore["demo"] = &partial.FlowSessionData{} |
| 24 | + } |
| 25 | + return sessionStore["demo"] |
| 26 | +} |
| 27 | + |
| 28 | +func main() { |
| 29 | + fsys := &partial.InMemoryFS{ |
| 30 | + Files: map[string]string{ |
| 31 | + "templates/layout.html": `<html><head><title>Flow Example</title></head><body>{{ child "content" }}</body></html>`, |
| 32 | + "templates/welcome.html": `<h1>Welcome</h1><a href='/?step=info'>Next</a>`, |
| 33 | + "templates/info.html": `<h1>Info</h1><a href='/?step=welcome'>Back</a> <a href='/?step=form'>Next</a>`, |
| 34 | + "templates/form.html": `<h1>Form</h1><form method='post'><input name='field' placeholder='Type something'><button type='submit'>Submit</button></form>{{ if .Data.Error }}<div style='color:red'>{{ .Data.Error }}</div>{{ end }}<a href='/?step=info'>Back</a>`, |
| 35 | + "templates/confirm.html": `<h1>Confirm</h1><div>Done: true</div><a href='/?step=welcome'>Restart</a>`, |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + steps := []partial.FlowStep{ |
| 40 | + { |
| 41 | + Name: "welcome", |
| 42 | + Partial: partial.New("templates/welcome.html").ID("content"), |
| 43 | + Validate: nil, |
| 44 | + }, |
| 45 | + { |
| 46 | + Name: "info", |
| 47 | + Partial: partial.New("templates/info.html").ID("content"), |
| 48 | + Validate: nil, |
| 49 | + }, |
| 50 | + { |
| 51 | + Name: "form", |
| 52 | + Partial: partial.New("templates/form.html").ID("content"), |
| 53 | + Validate: func(r *http.Request, data map[string]any) error { |
| 54 | + if r.Method == http.MethodPost { |
| 55 | + if r.FormValue("field") == "" { |
| 56 | + return fmt.Errorf("field required") |
| 57 | + } |
| 58 | + } |
| 59 | + return nil |
| 60 | + }, |
| 61 | + }, |
| 62 | + { |
| 63 | + Name: "confirm", |
| 64 | + Partial: partial.New("templates/confirm.html").ID("content"), |
| 65 | + Validate: nil, |
| 66 | + }, |
| 67 | + } |
| 68 | + flow := partial.NewPageFlow(steps) |
| 69 | + |
| 70 | + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 71 | + session := getSession(r) |
| 72 | + stepName := r.URL.Query().Get("step") |
| 73 | + if stepName == "" { |
| 74 | + stepName = steps[0].Name |
| 75 | + } |
| 76 | + idx := flow.FindStep(stepName) |
| 77 | + if idx == -1 { |
| 78 | + http.Error(w, "Step not found", http.StatusNotFound) |
| 79 | + return |
| 80 | + } |
| 81 | + flow.Current = idx |
| 82 | + curStep := flow.GetCurrentStep() |
| 83 | + |
| 84 | + var renderError string |
| 85 | + if r.Method == http.MethodPost && curStep.Validate != nil { |
| 86 | + err := curStep.Validate(r, nil) |
| 87 | + if err != nil { |
| 88 | + renderError = err.Error() |
| 89 | + } else { |
| 90 | + session.SetStepValidated(curStep.Name, true) |
| 91 | + if flow.Next() { |
| 92 | + http.Redirect(w, r, fmt.Sprintf("/?step=%s", flow.GetCurrentStep().Name), http.StatusSeeOther) |
| 93 | + return |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // Prepare the layout and content partials |
| 99 | + layout := partial.New("templates/layout.html").ID("root") |
| 100 | + layout.With(curStep.Partial.SetData(map[string]any{"Error": renderError})) |
| 101 | + |
| 102 | + service := partial.NewService(&partial.Config{}) |
| 103 | + out, err := service.NewLayout().FS(fsys).Set(layout).RenderWithRequest(r.Context(), r) |
| 104 | + if err != nil { |
| 105 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 106 | + return |
| 107 | + } |
| 108 | + _, _ = w.Write([]byte(out)) |
| 109 | + }) |
| 110 | + |
| 111 | + log.Println("Example flow running at http://localhost:8123/") |
| 112 | + log.Fatal(http.ListenAndServe(":8123", nil)) |
| 113 | +} |
0 commit comments