-
Notifications
You must be signed in to change notification settings - Fork 2
/
choose_address.go
78 lines (67 loc) · 2.29 KB
/
choose_address.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
package donor
import (
"context"
"errors"
"github.com/ministryofjustice/opg-modernising-lpa/internal/form"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/place"
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
)
func newChooseAddressData(appData page.AppData, actorLabel, fullName, ID string, canSkip bool) *chooseAddressData {
return &chooseAddressData{
App: appData,
ActorLabel: actorLabel,
FullName: fullName,
ID: ID,
CanSkip: canSkip,
Form: &form.AddressForm{},
TitleKeys: titleKeys{
Manual: "personsAddress",
Postcode: "whatIsPersonsPostcode",
PostcodeSelectAndPostcodeLookup: "selectAnAddressForPerson",
ReuseAndReuseSelect: "selectAnAddressForPerson",
ReuseOrNew: "addPersonsAddress",
},
}
}
type chooseAddressData struct {
App page.AppData
Errors validation.List
ActorLabel string
FullName string
ID string
CanSkip bool
Addresses []place.Address
Form *form.AddressForm
TitleKeys titleKeys
}
type titleKeys struct {
Manual string
PostcodeSelectAndPostcodeLookup string
Postcode string
ReuseAndReuseSelect string
ReuseOrNew string
}
func (d *chooseAddressData) overrideTitleKeys(newTitleKeys titleKeys) {
d.TitleKeys = newTitleKeys
}
func lookupAddress(ctx context.Context, logger Logger, addressClient AddressClient, data *chooseAddressData, your bool) {
addresses, err := addressClient.LookupPostcode(ctx, data.Form.LookupPostcode)
if err != nil {
logger.Print(err)
if errors.As(err, &place.BadRequestError{}) {
data.Errors.Add("lookup-postcode", validation.EnterError{Label: "invalidPostcode"})
} else {
data.Errors.Add("lookup-postcode", validation.CustomError{Label: "couldNotLookupPostcode"})
}
data.Form.Action = "postcode"
} else if len(addresses) == 0 {
if your {
data.Errors.Add("lookup-postcode", validation.CustomError{Label: "noYourAddressesFound"})
} else {
data.Errors.Add("lookup-postcode", validation.CustomError{Label: "noAddressesFound"})
}
data.Form.Action = "postcode"
}
data.Addresses = addresses
}