-
Notifications
You must be signed in to change notification settings - Fork 2
/
dashboard.go
84 lines (69 loc) · 2.17 KB
/
dashboard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package page
import (
"context"
"net/http"
"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/lpastore"
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
)
type DashboardStore interface {
GetAll(ctx context.Context) (donor, attorney, certificateProvider []LpaAndActorTasks, err error)
SubExistsForActorType(ctx context.Context, sub string, actorType actor.Type) (bool, error)
}
type LpaAndActorTasks struct {
Lpa *lpastore.Lpa
CertificateProvider *actor.CertificateProviderProvidedDetails
Attorney *actor.AttorneyProvidedDetails
}
type dashboardForm struct {
hasExistingDonorLPAs bool
}
type dashboardData struct {
App AppData
Errors validation.List
UseTabs bool
DonorLpas []LpaAndActorTasks
CertificateProviderLpas []LpaAndActorTasks
AttorneyLpas []LpaAndActorTasks
}
func Dashboard(tmpl template.Template, donorStore DonorStore, dashboardStore DashboardStore) Handler {
return func(appData AppData, w http.ResponseWriter, r *http.Request) error {
if r.Method == http.MethodPost {
form := readDashboardForm(r)
lpa, err := donorStore.Create(r.Context())
if err != nil {
return err
}
path := Paths.YourDetails
if form.hasExistingDonorLPAs {
path = Paths.MakeANewLPA
}
return path.Redirect(w, r, appData, lpa)
}
donorLpas, attorneyLpas, certificateProviderLpas, err := dashboardStore.GetAll(r.Context())
if err != nil {
return err
}
tabCount := 1
if len(certificateProviderLpas) > 0 {
tabCount++
}
if len(attorneyLpas) > 0 {
tabCount++
}
data := &dashboardData{
App: appData,
UseTabs: tabCount > 1,
DonorLpas: donorLpas,
CertificateProviderLpas: certificateProviderLpas,
AttorneyLpas: attorneyLpas,
}
return tmpl(w, data)
}
}
func readDashboardForm(r *http.Request) *dashboardForm {
f := &dashboardForm{}
f.hasExistingDonorLPAs = r.PostFormValue("has-existing-donor-lpas") == "true"
return f
}