11import { useCallback , useEffect , useState } from "react" ;
2+
23import {
34 Avatar ,
45 Button ,
@@ -16,15 +17,17 @@ import {
1617import ImgCrop from "antd-img-crop" ;
1718import moment from "moment" ;
1819import {
20+ ConsoleSqlOutlined ,
1921 MinusCircleOutlined ,
2022 PlusOutlined ,
2123 UploadOutlined ,
2224} from "@ant-design/icons" ;
23- import { getBase64 } from "~/lib/images" ;
25+ import { getAvatarSrc , getBase64 } from "~/lib/images" ;
2426import { useAuth } from "@coderdojobraga/ui" ;
2527import { notifyError , notifyInfo } from "~/components/Notification" ;
2628import { getIcon } from "~/lib/utils" ;
2729import {
30+ API_URL ,
2831 EUser ,
2932 addMentorSkills ,
3033 addNinjaSkills ,
@@ -63,7 +66,10 @@ function Settings() {
6366 const { user, edit_user, isLoading } = useAuth ( ) ;
6467 const [ formPersonal ] = Form . useForm ( ) ;
6568 const [ formPassword ] = Form . useForm ( ) ;
66- const [ avatar , setAvatar ] = useState < void | string > ( ) ;
69+ const [ avatar , setAvatar ] = useState < null | File | string > ( ) ;
70+ const [ avatarPreview , setAvatarPreview ] = useState <
71+ null | string | undefined
72+ > ( ) ;
6773
6874 const [ userSkills , setUserSkills ] = useState < any [ ] > ( [ ] ) ;
6975 const [ skills , setSkills ] = useState < any [ ] > ( [ ] ) ;
@@ -79,6 +85,20 @@ function Settings() {
7985 "Slack" ,
8086 ] ) ;
8187
88+ // const avatarSrc = getAvatarSrc(avatarPreview, user?.photo, API_URL);
89+ // if (avatarSrc !== avatarPreview) {
90+ // setAvatarPreview(avatarSrc);
91+ // }
92+
93+ if (
94+ ! avatarPreview &&
95+ typeof avatar === "string" &&
96+ avatar . startsWith ( "/uploads/" )
97+ ) {
98+ console . log ( "avatar" , API_URL + avatar ) ;
99+ setAvatarPreview ( API_URL + avatar ) ;
100+ }
101+
82102 const onSubmit = ( values : any ) => {
83103 if ( avatar ) {
84104 values [ "user[photo]" ] = avatar ;
@@ -182,12 +202,6 @@ function Settings() {
182202 }
183203 } ;
184204
185- const base64 = ( file : any ) => {
186- getBase64 ( file , ( result : string ) => {
187- setAvatar ( result ) ;
188- } ) ;
189- } ;
190-
191205 const changeSkills = ( ) => {
192206 const deleted = userSkills
193207 . map ( ( skill : any ) => skill . id )
@@ -227,9 +241,9 @@ function Settings() {
227241 useEffect ( ( ) => {
228242 if ( user ?. photo ) {
229243 setAvatar ( user ?. photo ) ;
230- } ;
231- getUserSkills ( ) ;
232- getAllSkills ( ) ;
244+ }
245+ getUserSkills ( ) ;
246+ getAllSkills ( ) ;
233247 } , [ user , getUserSkills ] ) ;
234248
235249 const breakpoints = {
@@ -272,14 +286,17 @@ function Settings() {
272286 < Form form = { formPersonal } onFinish = { onSubmit } layout = "vertical" >
273287 < Section title = "Foto de Perfil" />
274288 < Space >
275- < Avatar size = { 100 } src = { avatar } />
289+ < Avatar size = { 100 } src = { avatarPreview } />
276290 < Form . Item name = "user[photo]" >
277291 < ImgCrop >
278292 < Upload
279293 accept = "image/*"
280294 maxCount = { 1 }
281- beforeUpload = { ( file ) => {
282- base64 ( file ) ;
295+ beforeUpload = { ( file : File ) => {
296+ setAvatar ( file ) ;
297+ getBase64 ( file , ( result : string ) => {
298+ setAvatarPreview ( result ) ;
299+ } ) ;
283300 return false ;
284301 } }
285302 onRemove = { ( ) => setAvatar ( user ?. photo ) }
0 commit comments