···11-package inertia
22-33-import (
44- "context"
55- "fmt"
66- "log"
77- "net/http"
88- "slices"
99-)
1010-1111-type Props map[string]Prop
1212-1313-type Prop struct {
1414- Value func() (any, error)
1515- Lazy bool
1616- Optional bool
1717- Always bool
1818- Deferred bool
1919- DeferredKey string
2020- Default bool
2121- isError bool
2222- Scroll ScrollMetadata
2323-}
2424-2525-type ScrollMetadata struct {
2626- Name string
2727- PreviousPage int
2828- NextPage int
2929- CurrentPage int
3030-}
3131-3232-func (p *Prop) ShouldDefer() bool {
3333- return true
3434-}
3535-3636-func (p *Prop) IsReturned(ctx Context, key string, only []string, except []string) bool {
3737- isFirstLoad := !ctx.isPartialRequest
3838-3939- if isFirstLoad && p.Deferred {
4040- return false
4141- }
4242-4343- if slices.Contains(except, key) {
4444- return false
4545- }
4646-4747- if slices.Contains(only, key) {
4848- return true
4949- }
5050-5151- if p.Always || p.Default {
5252- return true
5353- }
5454-5555- if isFirstLoad && p.Lazy {
5656- return true
5757- }
5858-5959- return false
6060-}
6161-6262-const ContextKey = "INERTIA_KEY"
6363-6464-type PropOption func(*Prop)
6565-6666-// The value is always evaluated during a request, but can be left out of the response when
6767-// the request is a partial request. In other Inertia adapters this would be the same as just
6868-// passing the value.
6969-func Default(value any, opts ...PropOption) Prop {
7070- return Prop{Value: func() (any, error) { return value, nil }, Default: true}
7171-}
7272-7373-// The value is evaluated and returned for the first request to a page. For all partial request
7474-// to that same page, the value is not evaluated and returned unless the property is specified
7575-// in the `X-Inertia-Partial-Data` header. In other Inertia adapters this would be the same as
7676-// passing a closure.
7777-func Lazy(valuefn func() (any, error), opts ...PropOption) Prop {
7878- return Prop{Value: valuefn, Lazy: true}
7979-}
8080-8181-// The value is only evaluated during a request when the request is a partial request. The value
8282-// is never returned during the first request from a page.
8383-func Optional(valuefn func() (any, error), opts ...PropOption) Prop {
8484- return Prop{Value: valuefn, Optional: true}
8585-}
8686-8787-// The value is always returned during a partial request, and the first request from a page.
8888-func Always(value any) Prop {
8989- return Prop{Value: func() (any, error) { return value, nil }, Always: true}
9090-}
9191-9292-func Scroll(value any, current, previous, next int, opts ...PropOption) Prop {
9393- prop := Prop{
9494- Value: func() (any, error) { return value, nil },
9595- Default: true,
9696- Scroll: ScrollMetadata{
9797- CurrentPage: current,
9898- NextPage: next,
9999- PreviousPage: previous,
100100- }}
101101-102102- for _, opt := range opts {
103103- opt(&prop)
104104- }
105105-106106- return prop
107107-}
108108-109109-func WithDeferredKey(key string) PropOption {
110110- return func(p *Prop) {
111111- p.DeferredKey = key
112112- }
113113-}
114114-115115-func WithPageName(name string) PropOption {
116116- return func(p *Prop) {
117117- p.Scroll.Name = name
118118- }
119119-}
120120-121121-// Besides setting the props in the handler when rendering the page, you can also set props in the
122122-// middleware. This is useful when you want to set props that are available to all pages. One can
123123-// use the SetProp function to set props. The value will be stored in the request context and will
124124-// be retrieved when rendering the page.
125125-func SetProp(r *http.Request, key string, value Prop) {
126126- ctx, ok := r.Context().Value(ContextKey).(Context)
127127-128128- fmt.Println(ctx.props)
129129-130130- if !ok {
131131- log.Panicln("context not found")
132132- }
133133-134134- ctx.props[key] = value
135135-136136- r.WithContext(context.WithValue(r.Context(), ContextKey, ctx))
137137-}
138138-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))
150150-}