-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPhotos.tsx
137 lines (115 loc) · 4.45 KB
/
Photos.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import * as React from 'react';
import DMInit from 'digimaker-ui/DMInit'
import {FetchWithAuth} from 'digimaker-ui/util';
import util from 'digimaker-ui/util';
import Moment from 'react-moment';
import Loading from './Loading';
import FileUpload from 'digimaker-ui/FileUpload';
export default class Photos extends React.Component<{}, {data:any, showMine:boolean, showAdding: boolean, uploadedPath: string, uploadedName:string}> {
constructor(props:any){
super(props);
this.state={data:'', showMine: false, showAdding: false, uploadedPath:'',uploadedName:''};
}
componentDidMount(){
this.fetchPhotos();
}
componentDidUpdate( prevProps, prevState, snapshot ){
if( prevState.showMine != this.state.showMine)
{
this.fetchPhotos();
}
}
//fetch photo data
fetchPhotos(){
let showMine = '';
if( this.state.showMine ){
showMine='&author=self';
}
FetchWithAuth(process.env.REACT_APP_REMOTE_URL + '/content/list/image?parent='+process.env.REACT_APP_PHOTO_ROOT+'&level=1&sortby=published%20desc&limit=0&offset=0'+showMine)
.then((data) => {
this.setState({data: data.data});
}).catch(err=>{
this.setState(()=>{throw err});
})
}
//after image is uploaded
uploaded(data){
let name = this.state.uploadedName;
if( !name ){
name = data.name;
name = name.split('.').slice(0, -1).join('.')
}
this.setState({uploadedPath: data.nameUploaded, uploadedName: name})
}
//when inputing name
inputName(e){
this.setState({uploadedName: e.target.value})
}
//submit adding photo
submit(){
let dataObject = {'name': this.state.uploadedName, 'image': this.state.uploadedPath};
FetchWithAuth(process.env.REACT_APP_REMOTE_URL + '/content/new/' + process.env.REACT_APP_PHOTO_ROOT + '/image', {
method: 'POST',
body: JSON.stringify(dataObject),
}).then((data) => {
if (data.error===false) {
this.setState({uploadedPath: '', uploadedName:'', showAdding: false});
this.fetchPhotos();
} else {
console.log(data)
}
});
}
//show/hide adding panel
showAdding(show:boolean){
this.setState({showAdding: show});
}
render () {
if( !this.state.data ){
return <Loading />;
}
return (
<div>
<h2>Photos</h2>
<div className="block right">
<label>
<input type="checkbox" checked={this.state.showMine} onChange={()=>this.setState({showMine: !this.state.showMine})} /> Show mine only
</label>
<input className="btn btn-sm btn-primary" type="button" value="Add photo" onClick={()=>this.showAdding(true)} />
</div>
{this.state.showAdding&&
<div className="panel-add">
<h3>Add photo</h3>
<div className="block">
<label>Upload image: </label>
<FileUpload service="content" format="image/*" onSuccess={(data)=>{this.uploaded(data)}} />
{this.state.uploadedPath&&<img src={process.env.REACT_APP_ASSET_URL+"/"+this.state.uploadedPath} />}
</div>
<div className="block">
<label>Name: </label>
<input className="form-control" type="text" onChange={(e)=>this.inputName(e)} value={this.state.uploadedName} />
</div>
<div className="block">
<input className="btn btn-sm btn-primary" type="button" value="Submit" onClick={()=>this.submit()} />
<input className="btn btn-sm btn-secondary" type="button" value="Cancel" onClick={()=>this.showAdding(false)} />
</div>
</div>}
<div className="gallery">
{this.state.data.list.map( (item) =>
<div className="gallery-item">
<div className="gallery-item-header">{item.name}</div>
<div>
<a target="_blank" href={process.env.REACT_APP_ASSET_URL+"/"+item.image} >
<img src={process.env.REACT_APP_ASSET_URL+"/images/thumbnail/"+item.image} />
</a>
</div>
<div className="gallery-item-author">
<span className="author">{item.author_name}</span> on <Moment format="YYYY-MM-DD HH:mm" unix>{item.modified}</Moment>
</div>
</div> )}
</div>
</div>
);
}
}