muba-posting 5.0.5 → 5.0.7

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.
Files changed (3) hide show
  1. package/Posting.js +6 -1
  2. package/Publish4.js +76 -51
  3. package/package.json +1 -1
package/Posting.js CHANGED
@@ -36,6 +36,7 @@ const MAX_PICTURES = { method: 'GET', url: '/controller/posting/max-pictures' };
36
36
  const COUNTRY_PHONE = { method: 'GET', url: '/controller/countries/{countryCode}/phones' };
37
37
  const LOAD_PENDING_CREATED = { method: 'GET', url: '/controller/ads/pending-created/posting' };
38
38
  const PAYMENT_ONLINE_ACTIVE = { method: 'GET', url: '/controller/payments-online/{countryCode}/active' };
39
+ const UPLOADED_FILES_KEY = 'uploadedFiles';
39
40
 
40
41
  export default class Posting extends React.Component {
41
42
  constructor(props) {
@@ -307,8 +308,12 @@ export default class Posting extends React.Component {
307
308
  }
308
309
  const response = await this.props.request(this, CREATE_AD, ad);
309
310
  if (response.httpStatus === 200) {
311
+ let images = this.state.postingAd.images;
312
+ if (response[UPLOADED_FILES_KEY]) {
313
+ images = response[UPLOADED_FILES_KEY];
314
+ }
310
315
  await this.setState({
311
- postingAd: { ...this.state.postingAd, id: response.id }
316
+ postingAd: { ...this.state.postingAd, images: images, id: response.id }
312
317
  })
313
318
  }
314
319
  }
package/Publish4.js CHANGED
@@ -61,18 +61,20 @@ export default class Publish4 extends React.Component {
61
61
  return true;
62
62
  }
63
63
 
64
- async loadUri(imgUri) {
65
- if (imgUri) {
66
- let fileType = imgUri.substring(imgUri.lastIndexOf('.') + 1).toLowerCase();
64
+ async loadUri(photo) {
65
+ if (photo) {
66
+ let fileType = photo.uri.substring(photo.uri.lastIndexOf('.') + 1).toLowerCase();
67
67
 
68
68
  const image = {
69
- uri: imgUri,
69
+ uri: photo.uri,
70
70
  name: 'photo_' + this.state.images.length + '.' + fileType,
71
71
  type: 'image/' + fileType,
72
72
  mainPicture: this.state.images.length === 0,
73
73
  referenceType: 'AD',
74
74
  referenceId: this.props.postingAd.id,
75
- extraInfo: this.state.images.length
75
+ extraInfo: this.state.images.length,
76
+ width: photo.width,
77
+ height: photo.height
76
78
  }
77
79
 
78
80
  this.loadImage(image);
@@ -97,6 +99,10 @@ export default class Publish4 extends React.Component {
97
99
  });
98
100
  }
99
101
 
102
+ async imageExists(imageUrl) {
103
+ return fetch(imageUrl).then(response => response.status === 200);
104
+ }
105
+
100
106
  async deleteImage(itemId, index) {
101
107
  if (itemId) {
102
108
  if (!this.props.postingAd.deletedFiles) {
@@ -106,11 +112,14 @@ export default class Publish4 extends React.Component {
106
112
  }
107
113
 
108
114
  let imagesArray = [...this.state.images];
115
+ let imageSize = [...this.state.imageSize];
109
116
  imagesArray.splice(index, 1)
117
+ imageSize.splice(index, 1)
110
118
 
111
119
  await this.setState({
112
120
  allPhotosSelected: false,
113
- images: imagesArray
121
+ images: imagesArray,
122
+ imageSize: imageSize
114
123
  });
115
124
 
116
125
  if (this.state.images.length >= 1) {
@@ -179,58 +188,74 @@ export default class Publish4 extends React.Component {
179
188
  }, 500);
180
189
  }
181
190
 
182
- calculateImageSize = (image) => {
191
+ calculateImageSize = async (image) => {
192
+ if (image.width && image.height) {
193
+ this.setImageSize(image.width, image.height);
194
+ } else if (!image.uri.startsWith('https://') || await this.imageExists(image.uri)) {
195
+ Image.getSize(image.uri, (width, height) => {
196
+ this.setImageSize(width, height);
197
+ });
198
+ } else {
199
+ setTimeout(() => this.calculateImageSize(image), 500);
200
+ }
201
+ }
202
+
203
+ setImageSize(width, height) {
183
204
  let imageSize = this.state.imageSize;
184
- Image.getSize(image.uri, (width, height) => {
185
- if (this.state.containerWidth !== 0 && this.state.containerHeight !== 0) {
186
- const containerRatio = this.state.containerWidth / this.state.containerHeight;
187
- const imageRatio = width / height;
188
-
189
- let finalWidth;
190
- let finalHeight;
191
- if (imageRatio > containerRatio) {
192
- if (width > this.state.containerWidth) {
193
- finalWidth = this.state.containerWidth;
194
- } else {
195
- finalWidth = width;
196
- }
197
- finalHeight = height * (finalWidth / width);
198
- } else if (imageRatio < containerRatio) {
199
- if (height > this.state.containerHeight) {
200
- finalHeight = this.state.containerHeight;
201
- } else {
202
- finalHeight = height;
203
- }
204
- finalWidth = width * (finalHeight / height);
205
+ if (this.state.containerWidth !== 0 && this.state.containerHeight !== 0) {
206
+ const containerRatio = this.state.containerWidth / this.state.containerHeight;
207
+ const imageRatio = width / height;
208
+
209
+ let finalWidth;
210
+ let finalHeight;
211
+ if (imageRatio > containerRatio) {
212
+ if (width > this.state.containerWidth) {
213
+ finalWidth = this.state.containerWidth;
214
+ } else {
215
+ finalWidth = width;
216
+ }
217
+ finalHeight = height * (finalWidth / width);
218
+ } else if (imageRatio < containerRatio) {
219
+ if (height > this.state.containerHeight) {
220
+ finalHeight = this.state.containerHeight;
205
221
  } else {
206
- if (width > this.state.containerWidth) {
207
- finalWidth = this.state.containerWidth;
208
- finalHeight = this.state.containerHeight;
209
- } else {
210
- finalWidth = width;
211
- finalHeight = height;
212
- }
222
+ finalHeight = height;
213
223
  }
214
- imageSize.push({
215
- width: finalWidth,
216
- height: finalHeight,
217
- marginVertical: (this.state.containerHeight - finalHeight) / 2,
218
- marginHorizontal: (this.state.containerWidth - finalWidth) / 2
219
- });
224
+ finalWidth = width * (finalHeight / height);
220
225
  } else {
221
- imageSize.push({
222
- width: '100%',
223
- height: '100%',
224
- marginVertical: 0,
225
- marginHorizontal: 0
226
- })
226
+ if (width > this.state.containerWidth) {
227
+ finalWidth = this.state.containerWidth;
228
+ finalHeight = this.state.containerHeight;
229
+ } else {
230
+ finalWidth = width;
231
+ finalHeight = height;
232
+ }
227
233
  }
228
- this.setState({
229
- imageSize: imageSize
234
+ imageSize.push({
235
+ width: finalWidth,
236
+ height: finalHeight,
237
+ marginVertical: (this.state.containerHeight - finalHeight) / 2,
238
+ marginHorizontal: (this.state.containerWidth - finalWidth) / 2
230
239
  });
240
+ } else {
241
+ imageSize.push({
242
+ width: '100%',
243
+ height: '100%',
244
+ marginVertical: 0,
245
+ marginHorizontal: 0
246
+ })
247
+ }
248
+ this.setState({
249
+ imageSize: imageSize
231
250
  });
232
251
  }
233
252
 
253
+ setContainerWidth = (event) => {
254
+ if (this.state.containerWidth === 0) {
255
+ this.setState({ containerWidth: event.nativeEvent.layout.width, containerHeight: event.nativeEvent.layout.height })
256
+ }
257
+ }
258
+
234
259
  render() {
235
260
  return (
236
261
  <View style={{ minHeight: this.state.minHeight }}>
@@ -256,7 +281,7 @@ export default class Publish4 extends React.Component {
256
281
  {this.state.allPhotosSelected ?
257
282
  null
258
283
  :
259
- <TouchableOpacity style={[commonStyles.photoBox, this.state.showErrorNoSelectedImages ? commonStyles.fieldError : null]} onPress={() => this.props.openPictureSelector()}>
284
+ <TouchableOpacity onLayout={this.setContainerWidth} style={[commonStyles.photoBox, this.state.showErrorNoSelectedImages ? commonStyles.fieldError : null]} onPress={() => this.props.openPictureSelector()}>
260
285
  <TouchableOpacity style={[commonStyles.photoBoxAddIcon]} onPress={() => this.props.openPictureSelector()}>
261
286
  <TouchableHighlight underlayColor="#104a7a" onPress={() => this.props.openPictureSelector()}>
262
287
  <FontAwesomeIcon style={commonStyles.photoAddIcon} name="plus-circled" />
@@ -269,7 +294,7 @@ export default class Publish4 extends React.Component {
269
294
  }
270
295
  {this.state.images.map((item, index) => {
271
296
  return (
272
- <View style={[commonStyles.photoBox]} onLayout={(event) => this.setState({ containerWidth: event.nativeEvent.layout.width, containerHeight: event.nativeEvent.layout.height })} key={index}>
297
+ <View style={[commonStyles.photoBox]} onLayout={this.setContainerWidth} key={index}>
273
298
  <Image style={{ width: this.state.imageSize[index]?.width, height: this.state.imageSize[index]?.height, marginVertical: this.state.imageSize[index]?.marginVertical, marginHorizontal: this.state.imageSize[index]?.marginHorizontal }}
274
299
  resizeMode='center' source={{ uri: item.uri }} borderRadius={10} onError={() => this.rerenderImage(index)} />
275
300
  <ImageBackground style={[commonStyles.photoControls, commonStyles.row]} source={require('./assets/images/bg-shadow.png')} resizeMode='center' borderRadius={1.5}>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "muba-posting",
3
- "version": "5.0.5",
3
+ "version": "5.0.7",
4
4
  "description": "Posting",
5
5
  "main": "Posting.js",
6
6
  "scripts": {