Select the types of activity you want to include in your feed.
Make changes to the inertia package to support session data. Change
validation package to make interacting with the inertia package easier.
Add implementation of session store for inertia to the server.
···2233import (
44 "context"
55+ "fmt"
66+ "log"
57 "net/http"
68 "slices"
79)
···121123// use the SetProp function to set props. The value will be stored in the request context and will
122124// be retrieved when rendering the page.
123125func SetProp(r *http.Request, key string, value Prop) {
124124- props, ok := r.Context().Value(ContextKey).(Props)
126126+ ctx, ok := r.Context().Value(ContextKey).(Context)
127127+128128+ fmt.Println(ctx.props)
125129126130 if !ok {
127127- props = make(Props)
131131+ log.Panicln("context not found")
128132 }
129133130130- props[key] = value
134134+ ctx.props[key] = value
131135132132- ctx := context.WithValue(r.Context(), ContextKey, props)
136136+ r.WithContext(context.WithValue(r.Context(), ContextKey, ctx))
137137+}
133138134134- *r = *r.WithContext(ctx)
139139+func (in *Inertia) SetErrors(w http.ResponseWriter, r *http.Request, errors map[string][]string) {
140140+ data, err := in.Store.Get(r, SessionKey)
141141+ if err != nil {
142142+ log.Println(err)
143143+ }
144144+ data.Errors = errors
145145+ fmt.Println(data)
146146+ if err := in.Store.Set(w, r, SessionKey, data); err != nil {
147147+ log.Println(err)
148148+ }
149149+ SetProp(r, "errors", Default(errors))
135150}
+1-2
pkg/inertia/response.go
···1515 errors = make(map[string][]string)
1616 )
17171818+ // here
1819 ctx, ok := r.Context().Value(ContextKey).(Context)
1920 if !ok {
2021 log.Fatalln("Failed to get inertia context, did you forget to use inertia middleware?")
···4243 }
4344 properties[key] = value
4445 }
4545-4646- properties["errors"] = errors
47464847 page := Page{
4948 Component: view,
+16-14
pkg/validation/validation.go
···11package validation
2233import (
44- "encoding/json"
54 "fmt"
65)
7687type Options struct {
99- FlatMap bool
88+ NestedMap bool
109}
11101211type Option func(*Options)
13121313+type Errors map[string][]string
1414+1415type Validator struct {
1515- errors map[string][]string
1616+ errors Errors
1617 options Options
1718}
18191919-func WithFlatMap() Option {
2020+func WithNestedMap() Option {
2021 return func(o *Options) {
2121- o.FlatMap = true
2222+ o.NestedMap = true
2223 }
2324}
24252526func New(opts ...Option) *Validator {
2627 options := Options{
2727- FlatMap: false,
2828+ NestedMap: false,
2829 }
29303031 for _, opt := range opts {
···3738 }
3839}
39404141+// Add validations for a key. For every validation that fails, the message is added to the errors
4242+// map of the `Validator`. You can choose to use validation messages from this package. Nested
4343+// validations like a city of an address can be added by using a dot notation, like so
4444+// `address.city`.
4045func (v *Validator) Validate(key string, validations map[string]bool) {
4146 for message, isValid := range validations {
4247 if !isValid {
4343- v.errors[message] = append(v.errors[message], key)
4848+ v.errors[key] = append(v.errors[key], message)
4449 }
4550 }
4651}
47524848-func (e Validator) Error() string {
4949- return fmt.Sprintf("%v", e.errors)
5353+func (e Errors) Error() string {
5454+ return fmt.Sprintf("%v", e)
5055}
51565757+// Run returns an error if there are any validation errors for one of the keys.
5258func (e Validator) Run() error {
5359 if len(e.errors) > 0 {
5454- return e
6060+ return e.errors
5561 }
56625763 return nil
5864}
5959-6060-func (v Validator) MarshalJSON() ([]byte, error) {
6161- return json.Marshal(v.errors)
6262-}