Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mobile] Fix gallery upload sync #19941

Merged
merged 13 commits into from
Feb 3, 2020
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* External dependencies
*/
import { useRef } from 'react';
mkevins marked this conversation as resolved.
Show resolved Hide resolved
import { View, Text, TouchableWithoutFeedback } from 'react-native';
import { uniqWith } from 'lodash';

Expand All @@ -21,6 +22,11 @@ import { withPreferredColorScheme } from '@wordpress/compose';
*/
import styles from './styles.scss';

// remove duplicates after gallery append
const dedupMedia = ( media ) => uniqWith( media, ( media1, media2 ) => {
return media1.id === media2.id || media1.url === media2.url;
} );

function MediaPlaceholder( props ) {
const {
addToGallery,
Expand All @@ -35,10 +41,14 @@ function MediaPlaceholder( props ) {
value = [],
} = props;

const setMedia = multiple && addToGallery ?
( selected ) => onSelect( uniqWith( [ ...value, ...selected ], ( media1, media2 ) => {
return media1.id === media2.id || media1.url === media2.url;
} ) ) :
// use ref to keep media array current for callbacks during rerenders
const mediaRef = useRef( value );
mediaRef.current = value;

// append and deduplicate media array for gallery use case
const setMedia = multiple && addToGallery ? ( selected ) => {
return onSelect( dedupMedia( [ ...mediaRef.current, ...selected ] ) );
} :
onSelect;

const isOneType = allowedTypes.length === 1;
Expand Down
10 changes: 5 additions & 5 deletions packages/block-library/src/gallery/gallery-image.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,15 @@ class GalleryImage extends Component {
}

finishMediaUploadWithSuccess( payload ) {
this.props.setAttributes( {
id: payload.mediaServerId,
url: payload.mediaUrl,
} );

this.setState( {
isUploadInProgress: false,
didUploadFail: false,
} );

this.props.setAttributes( {
id: payload.mediaServerId,
url: payload.mediaUrl,
} );
mkevins marked this conversation as resolved.
Show resolved Hide resolved
}

finishMediaUploadWithFailure() {
Expand Down
6 changes: 4 additions & 2 deletions packages/block-library/src/gallery/gallery.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { isEmpty } from 'lodash';
/**
* Internal dependencies
*/
import { mediaUploadSync } from 'react-native-gutenberg-bridge';
import GalleryImage from './gallery-image';
import { defaultColumnsNumber } from './shared';
import styles from './gallery-styles.scss';
Expand All @@ -17,7 +18,7 @@ import Tiles from './tiles';
*/
import { __, sprintf } from '@wordpress/i18n';
import { BlockCaption } from '@wordpress/block-editor';
import { useState } from '@wordpress/element';
import { useState, useEffect } from '@wordpress/element';

const TILE_SPACING = 15;

Expand All @@ -27,6 +28,7 @@ const MAX_DISPLAYED_COLUMNS_NARROW = 2;

export const Gallery = ( props ) => {
const [ isCaptionSelected, setIsCaptionSelected ] = useState( false );
useEffect( mediaUploadSync, [] );

const {
clientId,
Expand Down Expand Up @@ -90,7 +92,7 @@ export const Gallery = ( props ) => {
key={ img.id || img.url }
url={ img.url }
alt={ img.alt }
id={ img.id }
id={ parseInt( img.id ) } // make id an integer explicitly
mkevins marked this conversation as resolved.
Show resolved Hide resolved
isCropped={ imageCrop }
isFirstItem={ index === 0 }
isLastItem={ ( index + 1 ) === images.length }
Expand Down