@campxdev/campx-web-utils 0.3.9 → 0.3.10
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.
- package/export.ts +1 -0
- package/package.json +1 -1
- package/src/components/ChangePassword.tsx +169 -0
package/export.ts
CHANGED
package/package.json
CHANGED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CircularProgress,
|
|
3
|
+
IconButton,
|
|
4
|
+
InputAdornment,
|
|
5
|
+
Stack,
|
|
6
|
+
} from '@mui/material';
|
|
7
|
+
import { useState } from 'react';
|
|
8
|
+
import { useForm } from 'react-hook-form';
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
Button,
|
|
12
|
+
DialogButton,
|
|
13
|
+
FormControlWrapper,
|
|
14
|
+
TextField,
|
|
15
|
+
} from '@campxdev/react-blueprint';
|
|
16
|
+
import { StyledMenuItem } from '@campxdev/react-blueprint/src/components/Navigation/DropDownMenu/styles';
|
|
17
|
+
import { Visibility, VisibilityOff } from '@mui/icons-material';
|
|
18
|
+
import { toast } from 'react-toastify';
|
|
19
|
+
import { axios } from '../config/axios';
|
|
20
|
+
import { isDevelopment } from '../utils/export';
|
|
21
|
+
|
|
22
|
+
interface PasswordVisibilityState {
|
|
23
|
+
oldPassword: boolean;
|
|
24
|
+
newPassword: boolean;
|
|
25
|
+
confirmPassword: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface FormData {
|
|
29
|
+
oldPassword: string;
|
|
30
|
+
newPassword: string;
|
|
31
|
+
confirmPassword: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface Field {
|
|
35
|
+
label: string;
|
|
36
|
+
name: keyof PasswordVisibilityState;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface ChangePasswordProps {
|
|
40
|
+
close: () => void;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function ChangePassword({ close }: ChangePasswordProps) {
|
|
44
|
+
const [showPassword, setShowPassword] = useState<PasswordVisibilityState>({
|
|
45
|
+
oldPassword: false,
|
|
46
|
+
newPassword: false,
|
|
47
|
+
confirmPassword: false,
|
|
48
|
+
});
|
|
49
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
50
|
+
|
|
51
|
+
const { handleSubmit, control, reset } = useForm<FormData>();
|
|
52
|
+
|
|
53
|
+
const onSubmit = async (formData: any) => {
|
|
54
|
+
setIsLoading(true);
|
|
55
|
+
const { oldPassword, newPassword, confirmPassword } = formData;
|
|
56
|
+
if (newPassword === confirmPassword) {
|
|
57
|
+
try {
|
|
58
|
+
await axios.post(
|
|
59
|
+
isDevelopment
|
|
60
|
+
? 'https://api.campx.dev/auth-server/auth/change-password'
|
|
61
|
+
: 'https://api.campx.in/auth-server/auth/change-password',
|
|
62
|
+
{
|
|
63
|
+
oldPassword,
|
|
64
|
+
newPassword,
|
|
65
|
+
},
|
|
66
|
+
);
|
|
67
|
+
toast.success('Password Changed Successfully');
|
|
68
|
+
setIsLoading(false);
|
|
69
|
+
reset();
|
|
70
|
+
close();
|
|
71
|
+
} catch (error) {
|
|
72
|
+
console.log(error, 'this is error');
|
|
73
|
+
// axiosErrorToast(error);
|
|
74
|
+
setIsLoading(false);
|
|
75
|
+
}
|
|
76
|
+
} else {
|
|
77
|
+
toast.error('New Password, Confirm Password must be same');
|
|
78
|
+
setIsLoading(false);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const fields: Field[] = [
|
|
83
|
+
{ label: 'Old Password', name: 'oldPassword' },
|
|
84
|
+
{ label: 'New Password', name: 'newPassword' },
|
|
85
|
+
{ label: 'Confirm Password', name: 'confirmPassword' },
|
|
86
|
+
];
|
|
87
|
+
|
|
88
|
+
return (
|
|
89
|
+
<form onSubmit={handleSubmit(onSubmit)}>
|
|
90
|
+
<FormControlWrapper control={control}>
|
|
91
|
+
<Stack gap={2} direction="column">
|
|
92
|
+
{fields.map((item) => {
|
|
93
|
+
return (
|
|
94
|
+
<>
|
|
95
|
+
<TextField
|
|
96
|
+
label={item.label || 'hey test'}
|
|
97
|
+
name={item.name}
|
|
98
|
+
type={showPassword[item.name] ? 'text' : 'password'}
|
|
99
|
+
required
|
|
100
|
+
InputProps={{
|
|
101
|
+
endAdornment: (
|
|
102
|
+
<InputAdornment position="end">
|
|
103
|
+
<IconButton
|
|
104
|
+
size="small"
|
|
105
|
+
onClick={() =>
|
|
106
|
+
setShowPassword((prev: any) => ({
|
|
107
|
+
...prev,
|
|
108
|
+
[item.name]: !prev[item.name],
|
|
109
|
+
}))
|
|
110
|
+
}
|
|
111
|
+
edge="end"
|
|
112
|
+
>
|
|
113
|
+
{showPassword[item.name] ? (
|
|
114
|
+
<VisibilityOff />
|
|
115
|
+
) : (
|
|
116
|
+
<Visibility />
|
|
117
|
+
)}
|
|
118
|
+
</IconButton>
|
|
119
|
+
</InputAdornment>
|
|
120
|
+
),
|
|
121
|
+
}}
|
|
122
|
+
/>
|
|
123
|
+
</>
|
|
124
|
+
);
|
|
125
|
+
})}
|
|
126
|
+
|
|
127
|
+
<Stack direction={'row'} gap={2} sx={{ marginTop: '20px' }}>
|
|
128
|
+
<Button variant="outlined" onClick={close}>
|
|
129
|
+
Cancel
|
|
130
|
+
</Button>
|
|
131
|
+
<Button
|
|
132
|
+
type="submit"
|
|
133
|
+
variant="contained"
|
|
134
|
+
endIcon={
|
|
135
|
+
isLoading && (
|
|
136
|
+
<CircularProgress
|
|
137
|
+
style={{ color: 'white' }}
|
|
138
|
+
size="30px"
|
|
139
|
+
thickness={1.7}
|
|
140
|
+
/>
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
>
|
|
144
|
+
Submit
|
|
145
|
+
</Button>
|
|
146
|
+
</Stack>
|
|
147
|
+
</Stack>
|
|
148
|
+
</FormControlWrapper>
|
|
149
|
+
</form>
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export const ChangePasswordDialog: React.FC = () => (
|
|
154
|
+
<>
|
|
155
|
+
<DialogButton
|
|
156
|
+
anchor={({ open }) => (
|
|
157
|
+
<StyledMenuItem
|
|
158
|
+
onClick={() => {
|
|
159
|
+
open();
|
|
160
|
+
}}
|
|
161
|
+
>
|
|
162
|
+
Change Password
|
|
163
|
+
</StyledMenuItem>
|
|
164
|
+
)}
|
|
165
|
+
content={({ close }) => <ChangePassword close={close} />}
|
|
166
|
+
title="Change Password"
|
|
167
|
+
/>
|
|
168
|
+
</>
|
|
169
|
+
);
|