@jsb188/auth-web 1.1.10 → 1.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,162 +1,114 @@
1
1
  import { SUPPORTED_LANGUAGES } from '@jsb188/app/constants/app.ts';
2
2
  import i18n from '@jsb188/app/i18n/index.ts';
3
- import type { AccountData } from '@jsb188/app/types/auth.d.ts';
3
+ import type { AccountData, AccountSettings } from '@jsb188/app/types/auth.d.ts';
4
4
  import { formatPhoneNumber } from '@jsb188/app/utils/string.ts';
5
+ import { defineForm } from '@jsb188/form/libs/defineForm';
5
6
  import type { OpenModalPopUpFn } from '@jsb188/react/states';
7
+ import { z } from 'zod';
6
8
 
7
- /**
8
- * Length values for validation
9
- */
10
-
11
- const MIN_LEN_VALUES = {
12
- password: 8,
13
- firstName: 32,
14
- lastName: 32,
15
- description: 750,
16
- email: 80,
17
- phone: 15,
18
- };
19
-
20
- /**
21
- * Schema; Edit account
22
- */
23
-
9
+ /* Define editable account values and the existing verification action controls. */
24
10
  export function makeEditAccountSchema(
25
- formId: string,
26
- focusedName: string,
27
- formValues: any,
28
- account: AccountData | null,
29
- openModalPopUp: OpenModalPopUpFn
11
+ account: AccountData | null,
12
+ settings: AccountSettings | null,
13
+ openModalPopUp: OpenModalPopUpFn,
30
14
  ) {
31
-
32
- const poProps = {
33
- // scrollAreaDOMId,
34
- zClassName: 'z9',
35
- position: 'bottom_left',
36
- offsetX: 0,
37
- offsetY: 7,
38
- };
39
-
40
- return {
41
- listData: [{
42
- // __type: 'subtitle',
43
- // item: {
44
- // text: i18n.t('account.profile'),
45
- // },
46
- // }, {
47
- __type: 'group',
48
- item: {
49
- items: [{
50
- __type: 'input',
51
- item: {
52
- name: 'profile.firstName',
53
- maxLength: MIN_LEN_VALUES.firstName,
54
- label: i18n.t('account.fName'),
55
- placeholder: account?.profile?.firstName,
56
- autoComplete: 'off',
57
- },
58
- }, {
59
- __type: 'input',
60
- item: {
61
- name: 'profile.lastName',
62
- maxLength: MIN_LEN_VALUES.lastName,
63
- label: i18n.t('account.lName'),
64
- placeholder: account?.profile?.lastName,
65
- autoComplete: 'off',
66
- },
67
- }],
68
- }
69
- }, {
70
- __type: 'input_click',
71
- forceClickId: 'input_click_settings.language',
72
- label: i18n.t('form.activity'),
73
- item: {
74
- label: i18n.t('account.ai_language'),
75
- locked: true,
76
- focused: focusedName === (formId + '_settings.language'),
77
- name: 'settings.language',
78
- placeholder: i18n.t(`form.activity_ph`),
79
- getter: (value: string) => i18n.t(`form.lang.${value || 'en'}`),
80
-
81
- popOverProps: {
82
- id: formId + '_settings.language',
83
- ...poProps,
84
- iface: {
85
- name: 'PO_LIST',
86
- variables: {
87
- designClassName: 'w_400',
88
- className: 'max_h_40vh',
89
- options: SUPPORTED_LANGUAGES.map(lang => ({
90
- __type: 'LIST_ITEM' as const,
91
- text: i18n.t(`form.lang.${lang}`),
92
- // textClassName: 'ft_xs',
93
- // rightIconClassName: 'ft_xs mb_2',
94
- name: 'settings.language',
95
- value: lang,
96
- selected: (formValues?.settings?.language || 'en') === lang,
97
- }))
98
- }
99
- }
100
- }
101
- },
102
- }, {
103
- __type: 'input_w_button',
104
- item: {
105
- name: 'change_phoneNumber',
106
- maxLength: MIN_LEN_VALUES.phone,
107
- setter: (data: any) => formatPhoneNumber(data?.change_phoneNumber || data?.phone?.number || ''),
108
- label: i18n.t('form.phone_number'),
109
- autoComplete: 'off',
110
- disabled: true,
111
- buttonText: i18n.t('form.change'),
112
- onClickButton: () => openModalPopUp({
113
- preset: 'CHANGE_PHONE',
114
- name: 'edit_account_phone'
115
- }),
116
- },
117
- }, {
118
- __type: 'input_w_button',
119
- item: {
120
- name: 'change_emailAddress',
121
- maxLength: MIN_LEN_VALUES.email,
122
- setter: (data: any) => data?.change_emailAddress || data?.email?.address,
123
- label: i18n.t('account.email'),
124
- autoComplete: 'off',
125
- disabled: true,
126
- buttonText: i18n.t('form.change'),
127
- onClickButton: () => openModalPopUp({
128
- preset: 'CHANGE_EMAIL',
129
- name: 'edit_account_email'
130
- })
131
- },
132
- }, {
133
- __type: 'input_w_button',
134
- item: {
135
- name: 'change_password',
136
- minLength: MIN_LEN_VALUES.password,
137
- label: i18n.t('account.password'),
138
- setter: () => '....',
139
- autoComplete: 'off',
140
- type: 'password',
141
- disabled: true,
142
- buttonText: i18n.t('form.change'),
143
- onClickButton: () => openModalPopUp({
144
- preset: 'CHANGE_PASSWORD',
145
- name: 'edit_account_password'
146
- }),
147
- },
148
- // __type: 'textarea',
149
- // item: {
150
- // name: 'description',
151
- // maxLength: MIN_LEN_VALUES.description,
152
- // setter: (data: any) => data?.profile?.description,
153
- // label: i18n.t('account.about'),
154
- // placeholder: i18n.t('account.about_ph'),
155
- // autoComplete: 'off',
156
- // },
157
- }],
158
- rules: [
159
- // RULES.password,
160
- ],
161
- };
15
+ return defineForm({
16
+ schema: z.object({
17
+ profile: z.object({ firstName: z.string(), lastName: z.string() }),
18
+ settings: z.object({ language: z.string() }),
19
+ change_phoneNumber: z.string(),
20
+ change_emailAddress: z.string(),
21
+ change_password: z.string(),
22
+ }),
23
+ defaults: {
24
+ profile: { firstName: account?.profile?.firstName || '', lastName: account?.profile?.lastName || '' },
25
+ settings: { language: settings?.language || 'en' },
26
+ change_phoneNumber: '',
27
+ change_emailAddress: '',
28
+ change_password: '',
29
+ },
30
+ fields: {
31
+ 'profile.firstName': {
32
+ kind: 'input',
33
+ maxLength: 32,
34
+ label: i18n.t('account.fName'),
35
+ placeholder: account?.profile?.firstName,
36
+ autoComplete: 'off',
37
+ },
38
+ 'profile.lastName': {
39
+ kind: 'input',
40
+ maxLength: 32,
41
+ label: i18n.t('account.lName'),
42
+ placeholder: account?.profile?.lastName,
43
+ autoComplete: 'off',
44
+ },
45
+ 'settings.language': {
46
+ kind: 'click',
47
+ label: i18n.t('account.ai_language'),
48
+ placeholder: i18n.t('c.activity_ph'),
49
+ getter: (value: string) => i18n.t(`c.lang.${value || 'en'}`),
50
+ resolve: ({ values }) => ({
51
+ popOverProps: {
52
+ id: 'edit_account_settings.language',
53
+ zClassName: 'z9',
54
+ position: 'bottom_left',
55
+ offsetX: 0,
56
+ offsetY: 7,
57
+ iface: {
58
+ name: 'PO_LIST',
59
+ variables: {
60
+ designClassName: 'w_400',
61
+ className: 'max_h_40vh',
62
+ options: SUPPORTED_LANGUAGES.map((language) => ({
63
+ __type: 'LIST_ITEM' as const,
64
+ text: i18n.t(`c.lang.${language}`),
65
+ name: 'settings.language',
66
+ value: language,
67
+ selected: (values.settings.language || 'en') === language,
68
+ })),
69
+ },
70
+ },
71
+ },
72
+ }),
73
+ },
74
+ change_phoneNumber: {
75
+ kind: 'action',
76
+ maxLength: 15,
77
+ getter: () => formatPhoneNumber(account?.phone?.number || '') || '',
78
+ label: i18n.t('c.phone_number'),
79
+ autoComplete: 'off',
80
+ disabled: true,
81
+ buttonText: i18n.t('c.change'),
82
+ onClickButton: () => openModalPopUp({ preset: 'CHANGE_PHONE', name: 'edit_account_phone' }),
83
+ },
84
+ change_emailAddress: {
85
+ kind: 'action',
86
+ maxLength: 80,
87
+ getter: () => account?.email?.address || '',
88
+ label: i18n.t('account.email'),
89
+ autoComplete: 'off',
90
+ disabled: true,
91
+ buttonText: i18n.t('c.change'),
92
+ onClickButton: () => openModalPopUp({ preset: 'CHANGE_EMAIL', name: 'edit_account_email' }),
93
+ },
94
+ change_password: {
95
+ kind: 'action',
96
+ minLength: 8,
97
+ getter: () => '....',
98
+ label: i18n.t('account.password'),
99
+ autoComplete: 'off',
100
+ type: 'password',
101
+ disabled: true,
102
+ buttonText: i18n.t('c.change'),
103
+ onClickButton: () => openModalPopUp({ preset: 'CHANGE_PASSWORD', name: 'edit_account_password' }),
104
+ },
105
+ },
106
+ layout: [
107
+ { kind: 'row', fields: ['profile.firstName', 'profile.lastName'] },
108
+ 'settings.language',
109
+ 'change_phoneNumber',
110
+ 'change_emailAddress',
111
+ 'change_password',
112
+ ],
113
+ });
162
114
  }
@@ -1,165 +1,133 @@
1
1
  import { SUPPORTED_COUNTRIES } from '@jsb188/app/constants/app.ts';
2
2
  import i18n from '@jsb188/app/i18n/index.ts';
3
+ import type { AddressObj } from '@jsb188/app/types/other.d.ts';
4
+ import { defineForm } from '@jsb188/form/libs/defineForm';
5
+ import { z } from 'zod';
3
6
 
4
- /**
5
- * Length values for address field validation.
6
- */
7
+ /* Validate an optional address, requiring its core fields once an address is entered. */
8
+ export function makeAddressValueSchema() {
9
+ return z
10
+ .object({
11
+ line1: z.string(),
12
+ line2: z.string(),
13
+ city: z.string(),
14
+ state: z.string(),
15
+ postalCode: z.string(),
16
+ country: z.string(),
17
+ })
18
+ .superRefine((address, context) => {
19
+ const hasAddress = [address.line1, address.line2, address.city, address.state, address.postalCode].some((value) => value.trim());
20
+ if (!hasAddress) {
21
+ return;
22
+ }
7
23
 
8
- const MIN_LEN_VALUES = {
9
- city: 100,
10
- state: 32,
11
- };
12
-
13
- /**
14
- * Build a required-address-field validator.
15
- */
16
-
17
- function makeAddressRequiredRule(fieldName: string, labelKey: string) {
18
- return {
19
- for: `address.${fieldName}`,
20
- rule: (_value: unknown, formValues: any) => {
21
- const address = formValues?.address || {};
22
- const hasAddressValueExceptCountry = ['line1', 'line2', 'city', 'state', 'postalCode'].some((key) => {
23
- return String(address?.[key] || '').trim().length > 0;
24
- });
25
-
26
- if (!hasAddressValueExceptCountry) {
27
- return true;
28
- }
29
-
30
- return String(address?.[fieldName] || '').trim().length > 0;
31
- },
32
- error: () => ({
33
- message: i18n.t('error.msg_value_required', { value: i18n.t(labelKey) }),
34
- }),
35
- };
24
+ for (const field of ['line1', 'postalCode', 'city', 'state', 'country'] as const) {
25
+ if (!address[field].trim()) {
26
+ context.addIssue({
27
+ code: z.ZodIssueCode.custom,
28
+ path: [field],
29
+ message: i18n.t('error.msg_value_required', { value: i18n.t(`c.${field}`) }),
30
+ });
31
+ }
32
+ }
33
+ });
36
34
  }
37
35
 
38
- /**
39
- * Create reusable address schema section and rules.
40
- */
41
-
42
- export function makeAddressSchema(
43
- formId: string,
44
- focusedName: string,
45
- addressFormValues?: Partial<{
46
- line1: string | null;
47
- line2: string | null;
48
- city: string | null;
49
- state: string | null;
50
- postalCode: string | null;
51
- country: string | null;
52
- }> | null,
53
- popOverDesignClassName?: string,
54
- ) {
55
- const currentAddress = addressFormValues || {};
56
- const selectedCountryCode = currentAddress?.country || '';
57
- const priorityCountryOptions = ['US', 'MX', 'CA'].map((countryCode) => ({
58
- __type: 'LIST_ITEM' as const,
59
- text: i18n.t(`country.from_code.${countryCode}`),
60
- name: 'address.country',
61
- value: countryCode,
62
- selected: selectedCountryCode === countryCode,
63
- }));
64
- const supportedCountryOptions = SUPPORTED_COUNTRIES.map((countryCode) => ({
65
- __type: 'LIST_ITEM' as const,
66
- text: i18n.t(`country.from_code.${countryCode}`),
67
- name: 'address.country',
68
- value: countryCode,
69
- selected: selectedCountryCode === countryCode,
70
- }));
71
- const countryOptions = [
72
- ...priorityCountryOptions,
73
- {
74
- __type: 'BREAK' as const,
75
- },
76
- ...supportedCountryOptions,
77
- ];
78
- const popOverProps = {
79
- zClassName: 'z9',
80
- position: 'bottom_left' as const,
81
- offsetX: 0,
82
- offsetY: 7,
83
- };
36
+ /* Map stored, nullable address data to the strings used by editable controls. */
37
+ export function addressFormValues(address?: Partial<AddressObj> | null) {
38
+ return {
39
+ line1: address?.line1 || '',
40
+ line2: address?.line2 || '',
41
+ postalCode: address?.postalCode || '',
42
+ city: address?.city || '',
43
+ state: address?.state || '',
44
+ country: address?.country || '',
45
+ };
46
+ }
84
47
 
85
- return {
86
- listData: [{
87
- __type: 'input' as const,
88
- item: {
89
- name: 'address.line1',
90
- label: i18n.t('form.line1'),
91
- placeholder: i18n.t('form.line1_ph'),
92
- },
93
- }, {
94
- __type: 'input' as const,
95
- item: {
96
- name: 'address.line2',
97
- label: i18n.t('form.line2_optional'),
98
- placeholder: i18n.t('form.line2_ph'),
99
- },
100
- }, {
101
- __type: 'input' as const,
102
- item: {
103
- name: 'address.postalCode',
104
- label: i18n.t('form.postalCode'),
105
- placeholder: currentAddress?.postalCode || i18n.t('form.postalCode_ph'),
106
- },
107
- }, {
108
- __type: 'group' as const,
109
- item: {
110
- items: [{
111
- __type: 'input' as const,
112
- item: {
113
- name: 'address.city',
114
- maxLength: MIN_LEN_VALUES.city,
115
- label: i18n.t('form.city'),
116
- placeholder: currentAddress?.city || i18n.t('form.city_ph'),
117
- },
118
- }, {
119
- __type: 'input' as const,
120
- item: {
121
- name: 'address.state',
122
- maxLength: MIN_LEN_VALUES.state,
123
- label: i18n.t('form.state'),
124
- placeholder: currentAddress?.state || '',
125
- },
126
- }],
127
- },
128
- }, {
129
- __type: 'input_click' as const,
130
- forceClickId: 'input_click_address.country',
131
- label: i18n.t('form.country'),
132
- item: {
133
- label: i18n.t('form.country'),
134
- locked: true,
135
- focused: focusedName === (formId + '_address.country'),
136
- name: 'address.country',
137
- getter: (value: string) => value ? i18n.t(`country.from_code.${value}`) : '',
138
- placeholder: selectedCountryCode || i18n.t('form.country_ph'),
139
- popOverProps: {
140
- ...popOverProps,
141
- id: formId + '_address.country',
142
- doNotFixToBottom: true,
143
- iface: {
144
- name: 'PO_LIST',
145
- variables: {
146
- designClassName: popOverDesignClassName ?? 'w_400',
147
- className: 'max_h_50vh',
148
- initialState: {
149
- 'address.country': selectedCountryCode,
150
- },
151
- options: countryOptions,
152
- },
153
- },
154
- },
155
- },
156
- }],
157
- rules: [
158
- makeAddressRequiredRule('line1', 'form.line1'),
159
- makeAddressRequiredRule('postalCode', 'form.postalCode'),
160
- makeAddressRequiredRule('city', 'form.city'),
161
- makeAddressRequiredRule('state', 'form.state'),
162
- makeAddressRequiredRule('country', 'form.country'),
163
- ],
164
- };
48
+ /* Define the reusable address fields with the existing grouped country popover. */
49
+ export function makeAddressSchema(address?: Partial<AddressObj> | null, popOverDesignClassName = 'w_400') {
50
+ return defineForm({
51
+ schema: z.object({ address: makeAddressValueSchema() }),
52
+ defaults: { address: addressFormValues(address) },
53
+ fields: {
54
+ 'address.line1': {
55
+ kind: 'input',
56
+ label: i18n.t('c.line1'),
57
+ placeholder: i18n.t('c.line1_ph'),
58
+ },
59
+ 'address.line2': {
60
+ kind: 'input',
61
+ label: i18n.t('c.line2_optional'),
62
+ placeholder: i18n.t('c.line2_ph'),
63
+ },
64
+ 'address.postalCode': {
65
+ kind: 'input',
66
+ label: i18n.t('c.postalCode'),
67
+ placeholder: address?.postalCode || i18n.t('c.postalCode_ph'),
68
+ },
69
+ 'address.city': {
70
+ kind: 'input',
71
+ maxLength: 100,
72
+ label: i18n.t('c.city'),
73
+ placeholder: address?.city || i18n.t('c.city_ph'),
74
+ },
75
+ 'address.state': {
76
+ kind: 'input',
77
+ maxLength: 32,
78
+ label: i18n.t('c.state'),
79
+ placeholder: address?.state || '',
80
+ },
81
+ 'address.country': {
82
+ kind: 'click',
83
+ label: i18n.t('c.country'),
84
+ getter: (value: string) => (value ? i18n.t(`country.from_code.${value}`) : ''),
85
+ resolve: ({ values }) => ({
86
+ placeholder: values.address.country || i18n.t('c.country_ph'),
87
+ popOverProps: {
88
+ id: 'address.country',
89
+ zClassName: 'z9',
90
+ position: 'bottom_left',
91
+ offsetX: 0,
92
+ offsetY: 7,
93
+ doNotFixToBottom: true,
94
+ iface: {
95
+ name: 'PO_LIST',
96
+ variables: {
97
+ designClassName: popOverDesignClassName,
98
+ className: 'max_h_50vh',
99
+ initialState: { 'address.country': values.address.country },
100
+ options: [
101
+ ...['US', 'MX', 'CA'].map((country) => ({
102
+ id: `priority_country_${country}`,
103
+ __type: 'LIST_ITEM' as const,
104
+ text: i18n.t(`country.from_code.${country}`),
105
+ name: 'address.country',
106
+ value: country,
107
+ selected: values.address.country === country,
108
+ })),
109
+ { __type: 'BREAK' as const },
110
+ ...SUPPORTED_COUNTRIES.map((country) => ({
111
+ id: `country_${country}`,
112
+ __type: 'LIST_ITEM' as const,
113
+ text: i18n.t(`country.from_code.${country}`),
114
+ name: 'address.country',
115
+ value: country,
116
+ selected: values.address.country === country,
117
+ })),
118
+ ],
119
+ },
120
+ },
121
+ },
122
+ }),
123
+ },
124
+ },
125
+ layout: [
126
+ 'address.line1',
127
+ 'address.line2',
128
+ 'address.postalCode',
129
+ { kind: 'row', fields: ['address.city', 'address.state'] },
130
+ 'address.country',
131
+ ],
132
+ });
165
133
  }