@jsb188/app-settings-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,211 +0,0 @@
1
- import { getErrorMessageContent } from '@jsb188/react-web/modules/Layout';
2
- import { Icon } from '@jsb188/react-web/svgs/Icon';
3
- import Markdown from '@jsb188/react-web/ui/Markdown';
4
- import { ModalContentContainer, ModalFloatingSaveButton, ModalSideNav, ModalSideNavIface } from '@jsb188/react-web/ui/ModalUI';
5
- import { TableListMockClient } from '@jsb188/react-web/ui/TableListUI';
6
- import type { ModalHandlerProps, OpenModalPopUpFn } from '@jsb188/react/states';
7
- import { useClosePopOver, useKeyDown } from '@jsb188/react/states';
8
- import { useEffect, useRef, useState, type ReactNode } from 'react';
9
-
10
- /**
11
- * Types
12
- */
13
-
14
- interface ControllerState {
15
- switchCase: string;
16
- hasChanges: boolean;
17
- saving: boolean;
18
- saveCounter: number;
19
- resetCounter: number;
20
- };
21
-
22
- export interface AppSettingsSwitchCaseProps extends ControllerState {
23
- onSave: () => void;
24
- onChangeDiff: (hasChanges?: boolean, saving?: boolean) => void;
25
- openModalPopUp: OpenModalPopUpFn;
26
- }
27
-
28
- export interface AppSettingsTableContentProps {
29
- notReady?: boolean;
30
- gridLayoutStyle: string;
31
- children?: ReactNode;
32
- error?: any;
33
- }
34
-
35
- /**
36
- * App settings table content helper with a built-in loading table state
37
- */
38
-
39
- export function AppSettingsTableContent(p: AppSettingsTableContentProps) {
40
- const { notReady, gridLayoutStyle, children, error } = p;
41
-
42
- if (notReady) {
43
- if (error) {
44
- const errObj = getErrorMessageContent(error);
45
- return <div className='h_item gap_df'>
46
- <div className='bd_2 bd_lt w_80 h_80 v_center r_df ic_lg ft_md rel pattern_texture medium_bf of'>
47
- <span className='rel'>
48
- <Icon
49
- tryColor
50
- name={errObj.titleIconName}
51
- backupName='alert-circle'
52
- />
53
- </span>
54
- </div>
55
-
56
- <div className='lh_2'>
57
- <div className='ft_medium ft_df mt_1 mb_4'>
58
- {errObj.title}
59
- </div>
60
- <Markdown
61
- as='p'
62
- preset='article'
63
- className='ft_sm cl_lt'
64
- >
65
- {errObj.message}
66
- </Markdown>
67
- </div>
68
- </div>;
69
- }
70
-
71
- return <TableListMockClient
72
- removeHorizontalPadding
73
- removeAvatarElement
74
- isSmallerRows
75
- removeBorderLine
76
- gridLayoutStyle={gridLayoutStyle}
77
- />;
78
- }
79
-
80
- return children;
81
- }
82
-
83
- /**
84
- * App settings layout
85
- */
86
-
87
- export function AppSettingsLayout(p: ModalHandlerProps & {
88
- switchCase: string;
89
- SwitchCaseComponent: React.ComponentType<AppSettingsSwitchCaseProps>;
90
- options: ModalSideNavIface[][];
91
- onClickNavItem?: (value: string) => boolean | void; // If "false" is returned, it denies the switchCase change
92
- }) {
93
- const { SwitchCaseComponent, openModalPopUp, options } = p;
94
- const [keyDown, setKeyDown] = useKeyDown();
95
- const closePopOver = useClosePopOver();
96
- const scrollAreaRef = useRef<HTMLDivElement | null>(null);
97
-
98
- // const setKeyDown = (value: any) => {
99
- // dispatchApp({
100
- // pressed: null,
101
- // alert: keyDown.alert,
102
- // modal: keyDown.modal,
103
- // ...value,
104
- // });
105
- // };
106
-
107
- const [controller, setController] = useState<ControllerState>({
108
- switchCase: p.switchCase,
109
- hasChanges: false,
110
- saving: false,
111
- saveCounter: 0,
112
- resetCounter: 0,
113
- });
114
-
115
- const onClickNavItem = (value: any) => {
116
- const handlerValue = p.onClickNavItem?.(value);
117
- if (handlerValue !== false) {
118
- setController({
119
- switchCase: value,
120
- hasChanges: false,
121
- saving: false,
122
- saveCounter: 0,
123
- resetCounter: 0,
124
- });
125
- }
126
- };
127
-
128
- const onSave = () => {
129
- setController({
130
- ...controller,
131
- saveCounter: controller.saveCounter + 1,
132
- });
133
- };
134
-
135
- const onReset = () => {
136
- setController({
137
- ...controller,
138
- resetCounter: controller.resetCounter + 1,
139
- });
140
- };
141
-
142
- const onChangeDiff = (hasChanges?: boolean, saving?: boolean) => {
143
- setController({
144
- ...controller,
145
- hasChanges: typeof hasChanges === 'boolean' ? hasChanges : controller.hasChanges,
146
- saving: typeof saving === 'boolean' ? saving : controller.saving,
147
- });
148
- };
149
-
150
- useEffect(() => {
151
- if (!keyDown.alert && keyDown.pressed === 's') {
152
- setKeyDown({
153
- pressed: null,
154
- });
155
- onSave();
156
- }
157
- }, [keyDown.pressed]);
158
-
159
- useEffect(() => {
160
- // Listen to scroll events inside scrollAreaRef
161
-
162
- const scrollArea = scrollAreaRef.current;
163
- if (!scrollArea) {
164
- return;
165
- }
166
-
167
- const onScroll = () => {
168
- // Close pop over on scroll
169
- closePopOver();
170
- };
171
-
172
- scrollArea.addEventListener('scroll', onScroll);
173
-
174
- return () => {
175
- scrollArea.removeEventListener('scroll', onScroll);
176
- };
177
- }, [closePopOver]);
178
-
179
- return (
180
- <div className='cw md h_top f rel of'>
181
- <ModalSideNav
182
- selectedValue={controller.switchCase}
183
- options={options}
184
- onClickItem={onClickNavItem}
185
- />
186
-
187
- <ModalContentContainer
188
- ref={scrollAreaRef}
189
- addFooterPadding
190
- addYOverflow
191
- >
192
- <div className='f py_md px_lg'>
193
- <SwitchCaseComponent
194
- key={`switch_case_${controller.resetCounter}`}
195
- onSave={onSave}
196
- onChangeDiff={onChangeDiff}
197
- openModalPopUp={openModalPopUp}
198
- {...controller}
199
- />
200
- </div>
201
- </ModalContentContainer>
202
-
203
- <ModalFloatingSaveButton
204
- hasChanges={controller.hasChanges}
205
- saving={controller.saving}
206
- onSave={onSave}
207
- onReset={onReset}
208
- />
209
- </div>
210
- );
211
- }
@@ -1,289 +0,0 @@
1
- import i18n from '@jsb188/app/i18n/index.ts';
2
- import { useConfirmPassword } from '@jsb188/graphql/hooks/use-auth-mtn';
3
- import { FormOptions, Input, SMSCodeInput } from '@jsb188/react-web/modules/Form';
4
- import { AlertPopUp, ModalErrorMessage } from '@jsb188/react-web/ui/ModalUI';
5
- import type { ModalPopUpComponentProps } from '@jsb188/react/states';
6
- import { useState } from 'react';
7
-
8
- /**
9
- * Types
10
- */
11
-
12
- export interface ConfirmPasswordProps extends ModalPopUpComponentProps {
13
- confirmPasswordTitle?: string;
14
- confirmPasswordMessage?: string;
15
- onPasswordVerified?: (password: string) => void;
16
- }
17
-
18
- export interface ChangeCredentialsFinishedProps extends ModalPopUpComponentProps {
19
- finishedIconName?: string;
20
- finishedTitle?: string;
21
- finishedMessage?: string;
22
- }
23
-
24
- export interface ChangeCredentialsProps extends ConfirmPasswordProps {
25
- iconName: string;
26
- skipPassword?: boolean;
27
- title: string;
28
- message: string;
29
-
30
- finishedIconName?: string;
31
- finishedTitle?: string;
32
- finishedMessage?: string;
33
-
34
- verifyIconName?: string;
35
- verifyTitle?: string;
36
- verifyMessage?: string;
37
-
38
- confirmText?: string;
39
- confirmSMSCodeText?: string;
40
- status: 'WAITING_FOR_INPUT' | 'WAITING_FOR_VERIFICATION' | 'WAITING_FOR_SMS_CODE' | 'FINISHED';
41
- onSubmit: (values: string[], confirmedPassword?: string) => void;
42
- onSubmitSMSCode?: (smsCode: string, values: string[], confirmedPassword?: string) => void;
43
-
44
- loading?: boolean;
45
- error: any;
46
- resetErrors: () => void;
47
- inputs: ({
48
- __type: 'TEXT_INPUT';
49
- type?: 'text' | 'password';
50
- label: string;
51
- placeholder?: string;
52
- } | {
53
- __type: 'OPTIONS_LIST',
54
- label: string;
55
- defaultValue?: string;
56
- options: {
57
- text: string;
58
- value: string;
59
- disabled?: boolean;
60
- }[];
61
- })[];
62
- }
63
-
64
- /**
65
- * Confirm password before changinge sensitive account settings
66
- */
67
-
68
- function ConfirmPasswordForm(p: ConfirmPasswordProps & {
69
- setPasswordVerified: (verified: boolean) => void;
70
- }) {
71
- const { setPasswordVerified, onCloseModal, confirmPasswordTitle, confirmPasswordMessage, onPasswordVerified } = p;
72
- const [password, setPassword] = useState('');
73
-
74
- const { confirmPassword, error, resetErrors, saving } = useConfirmPassword({
75
- onCompleted: (data: any) => {
76
- if (data.confirmPassword) {
77
- onPasswordVerified?.(password);
78
- setPasswordVerified(true);
79
- }
80
- },
81
- onError: () => {
82
- setPassword('');
83
- }
84
- });
85
-
86
- const onConfirmPassword = () => {
87
- confirmPassword({
88
- variables: {
89
- password,
90
- }
91
- });
92
- };
93
-
94
- const onPressEnter = (e: React.KeyboardEvent) => {
95
- if (e.key === 'Enter' && password) {
96
- onConfirmPassword();
97
- }
98
- }
99
-
100
- return <AlertPopUp
101
- loading={saving}
102
- iconName='password-lock-2'
103
- confirmText={i18n.t('form.authenticate')}
104
- cancelText={i18n.t('form.cancel')}
105
- onConfirm={onConfirmPassword}
106
- onCancel={onCloseModal}
107
- disabledConfirm={!password}
108
- title={confirmPasswordTitle ?? i18n.t('account.enter_password')}
109
- message={confirmPasswordMessage ?? i18n.t('account.enter_password_msg')}
110
- >
111
- {error && (
112
- <div className='px_md mt_md -mb_df'>
113
- <ModalErrorMessage
114
- error={error}
115
- resetErrors={resetErrors}
116
- />
117
- </div>
118
- )}
119
-
120
- <div className='mt_df mx_md'>
121
- <Input
122
- type='password'
123
- label={i18n.t('account.password')}
124
- placeholder='••••'
125
- value={password}
126
- onChange={(e) => setPassword(e.target.value)}
127
- onKeyDown={onPressEnter}
128
- autoFocus
129
- />
130
- </div>
131
- </AlertPopUp>;
132
- }
133
-
134
- /**
135
- * Change account settings that require confirmations (such as credentials)
136
- */
137
-
138
- export function ChangeCredentialsLayout(p: ChangeCredentialsProps) {
139
- const {
140
- iconName, skipPassword, title, message, confirmText, confirmSMSCodeText, loading, error, resetErrors, inputs, onSubmit, onSubmitSMSCode, status,
141
- finishedIconName, finishedTitle, finishedMessage,
142
- verifyIconName, verifyTitle, verifyMessage,
143
- ...rest
144
- } = p;
145
- const { onCloseModal } = rest;
146
- const [passwordVerified, setPasswordVerified] = useState(false);
147
- const [confirmedPassword, setConfirmedPassword] = useState('');
148
- const [smsCode, setSMSCode] = useState('');
149
- const [values, setValues] = useState(() => inputs.map((input) =>
150
- input.__type === 'OPTIONS_LIST'
151
- ? (input.defaultValue || '')
152
- : ''
153
- ));
154
-
155
- switch (status) {
156
- case 'FINISHED':
157
- return <AlertPopUp
158
- onCloseModal={onCloseModal}
159
- iconName={finishedIconName || 'circle-check'}
160
- onCancel={onCloseModal}
161
- title={finishedTitle || i18n.t('form.success_')}
162
- message={finishedMessage || i18n.t('form.your_request_is_completed_msg')}
163
- />;
164
- case 'WAITING_FOR_VERIFICATION':
165
- return <AlertPopUp
166
- onCloseModal={onCloseModal}
167
- iconName={verifyIconName || iconName}
168
- onCancel={onCloseModal}
169
- title={verifyTitle || i18n.t('form.please_verify')}
170
- message={verifyMessage || i18n.t('form.request_will_complete_once_verified_msg')}
171
- />;
172
- case 'WAITING_FOR_SMS_CODE': {
173
- const hasSMSCode = !!smsCode.trim();
174
-
175
- return <AlertPopUp
176
- iconName={verifyIconName || iconName}
177
- loading={loading}
178
- onCloseModal={onCloseModal}
179
- onCancel={onCloseModal}
180
- doNotExitOnConfirm
181
- onConfirm={() => {
182
- onSubmitSMSCode?.(smsCode.trim(), values, confirmedPassword);
183
- }}
184
- title={verifyTitle || i18n.t('form.please_verify')}
185
- message={verifyMessage || i18n.t('form.request_will_complete_once_verified_msg')}
186
- confirmText={confirmSMSCodeText || i18n.t('form.ok')}
187
- cancelText={i18n.t('form.cancel')}
188
- disabledConfirm={!hasSMSCode}
189
- >
190
- {error && (
191
- <div className='px_md mt_md -mb_df'>
192
- <ModalErrorMessage
193
- error={error}
194
- resetErrors={resetErrors}
195
- />
196
- </div>
197
- )}
198
-
199
- <div className='mt_md mx_md'>
200
- <SMSCodeInput
201
- saving={loading}
202
- error={error}
203
- onChangeCode={setSMSCode}
204
- onSubmit={(smsCode) => {
205
- onSubmitSMSCode?.(smsCode.trim(), values, confirmedPassword);
206
- }}
207
- />
208
- </div>
209
- </AlertPopUp>;
210
- }
211
- case 'WAITING_FOR_INPUT':
212
- default:
213
- }
214
-
215
- if (!passwordVerified && !skipPassword) {
216
- return <ConfirmPasswordForm
217
- {...rest}
218
- setPasswordVerified={setPasswordVerified}
219
- onPasswordVerified={setConfirmedPassword}
220
- />;
221
- }
222
-
223
- const hasTwoInputs = inputs.length >= 2;
224
- const hasValidInputs = !!(hasTwoInputs ? values[0]?.trim() && values[1]?.trim() : values[0]?.trim());
225
-
226
- const onPressEnter = (e: React.KeyboardEvent) => {
227
- if (e.key === 'Enter' && hasValidInputs) {
228
- onSubmit(values, confirmedPassword);
229
- }
230
- };
231
-
232
- return <AlertPopUp
233
- iconName={iconName}
234
- doNotExitOnConfirm
235
- loading={loading}
236
- onCloseModal={onCloseModal}
237
- onConfirm={() => onSubmit(values, confirmedPassword)}
238
- title={title}
239
- message={message}
240
- confirmText={confirmText}
241
- cancelText={i18n.t('form.cancel')}
242
- disabledConfirm={!hasValidInputs}
243
- >
244
- {error && (
245
- <div className='px_md mt_md -mb_df'>
246
- <ModalErrorMessage
247
- error={error}
248
- resetErrors={resetErrors}
249
- />
250
- </div>
251
- )}
252
-
253
- <div className='mt_df mx_md'>
254
- {inputs.map((obj, i) => {
255
- switch (obj.__type) {
256
- case 'OPTIONS_LIST':
257
- return <FormOptions
258
- key={i}
259
- name={obj.label}
260
- label={obj.label}
261
- value={values[i]}
262
- options={obj.options}
263
- onChange={(_e: React.MouseEvent, _name: string, value: string | number | null) => {
264
- const newValues = [...values];
265
- newValues[i] = value === null ? '' : String(value);
266
- setValues(newValues);
267
- }}
268
- />;
269
- case 'TEXT_INPUT':
270
- default:
271
- }
272
-
273
- return <Input
274
- key={i}
275
- type={obj.type || 'text'}
276
- {...obj}
277
- value={values[i]}
278
- onChange={(e) => {
279
- const newValues = [...values];
280
- newValues[i] = e.target.value;
281
- setValues(newValues);
282
- }}
283
- onKeyDown={onPressEnter}
284
- autoFocus={!i}
285
- />
286
- })}
287
- </div>
288
- </AlertPopUp>;
289
- };