-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProfile.tsx
84 lines (71 loc) · 2.3 KB
/
Profile.tsx
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
79
80
81
82
83
84
import * as React from 'react';
import DMInit from 'digimaker-ui/DMInit'
import {FetchWithAuth} from 'digimaker-ui/util';
import util from 'digimaker-ui/util';
import RenderFields from 'digimaker-ui/RenderFields';
import Loading from './Loading';
export default class Profile extends React.Component<{}, {mode:string, validation: any, data:any}> {
constructor(props:any){
super(props);
this.state={mode:'view',validation:'',data:''};
}
componentDidMount(){
this.fetchProfile();
}
//fetch profile data
fetchProfile(){
FetchWithAuth(process.env.REACT_APP_REMOTE_URL + '/user/current/dmdemo')
.then((data) => {
this.setState({data: data.data});
}).catch(err=>{
this.setState(()=>{throw err});
})
}
//change to edit mode
edit(){
this.setState({mode: 'edit'});
}
//submit edit
submit(e){
e.preventDefault();
const form = new FormData(e.target);
const dataObject = {};
for (let key of Array.from(form.keys())) {
dataObject[key] = form.get(key);
};
let url = '/content/update/'+this.state.data.id;
FetchWithAuth(process.env.REACT_APP_REMOTE_URL + url, {
method: 'POST',
body: JSON.stringify(dataObject),
}).then((data) => {
if (data.error==false) {
this.setState({mode:'view', data:''});
this.fetchProfile();
}else {
this.setState( {validation: data.data} )
}
})
}
render () {
if( !this.state.data ){
return <Loading />;
}
return (
<div>
<h2>My profile</h2>
<form onSubmit={e=>this.submit(e)}>
<RenderFields mode={this.state.mode} type={"user"} data={this.state.data} validation={this.state.validation} />
{this.state.mode=='view'&&<div>
<br />
<input type="button" className="btn btn-primary btn-sm" onClick={()=>this.edit()} value="Edit" />
</div>}
{this.state.mode=='edit'&&<div>
<br /><br />
<input type="submit" className="btn btn-primary btn-sm" value="Save" />
<input type="button" onClick={()=>this.setState({mode: 'view'})} className="btn btn-light btn-sm" value='Cancel' />
</div>}
</form>
</div>
);
}
}