refined external sharing flow

This commit is contained in:
Nouman Tahir 2021-04-25 01:35:19 +05:00
parent f78f1599e7
commit a245dec56a
5 changed files with 49 additions and 81 deletions

View File

@ -68,6 +68,7 @@ const MarkdownEditorView = ({
getCommunity, getCommunity,
currentAccount, currentAccount,
autoFocusText, autoFocusText,
sharedSnippetText,
}) => { }) => {
const [text, setText] = useState(draftBody || ''); const [text, setText] = useState(draftBody || '');
const [selection, setSelection] = useState({ start: 0, end: 0 }); const [selection, setSelection] = useState({ start: 0, end: 0 });
@ -97,6 +98,12 @@ const MarkdownEditorView = ({
} }
}, [draftBody]); }, [draftBody]);
useEffect(() => {
if (sharedSnippetText) {
_handleOnSnippetReceived(sharedSnippetText);
}
}, [sharedSnippetText]);
useEffect(() => { useEffect(() => {
if (editable === null) { if (editable === null) {
// workaround for android context menu issue // workaround for android context menu issue
@ -208,7 +215,7 @@ const MarkdownEditorView = ({
</ScrollView> </ScrollView>
); );
const _handleOnSnippetSelect = (snippetText) => { const _handleOnSnippetReceived = (snippetText) => {
applySnippet({ applySnippet({
text, text,
selection, selection,
@ -398,7 +405,10 @@ const MarkdownEditorView = ({
animationType="slide" animationType="slide"
style={styles.modalStyle} style={styles.modalStyle}
> >
<SnippetsModal username={currentAccount.username} handleOnSelect={_handleOnSnippetSelect} /> <SnippetsModal
username={currentAccount.username}
handleOnSelect={_handleOnSnippetReceived}
/>
</Modal> </Modal>
<UploadsGalleryModal <UploadsGalleryModal

View File

@ -22,7 +22,7 @@ const persistedReducer = persistReducer(persistConfig, reducer);
const middleware = [thunk]; const middleware = [thunk];
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
middleware.push(logger); // middleware.push(logger);
} }
const store = createStore(persistedReducer, applyMiddleware(...middleware)); const store = createStore(persistedReducer, applyMiddleware(...middleware));

View File

@ -158,10 +158,10 @@ class ApplicationContainer extends Component {
}); });
ReceiveSharingIntent.getReceivedFiles( ReceiveSharingIntent.getReceivedFiles(
(files) => { () => {
navigate({ navigate({
routeName: ROUTES.SCREENS.EDITOR, routeName: ROUTES.SCREENS.EDITOR,
params: { upload: files }, params: { hasSharedIntent: true },
}); });
// files returns as JSON Array example // files returns as JSON Array example
//[{ filePath: null, text: null, weblink: null, mimeType: null, contentUri: null, fileName: null, extension: null }] //[{ filePath: null, text: null, weblink: null, mimeType: null, contentUri: null, fileName: null, extension: null }]

View File

@ -8,6 +8,7 @@ import AsyncStorage from '@react-native-community/async-storage';
// Services and Actions // Services and Actions
import { Buffer } from 'buffer'; import { Buffer } from 'buffer';
import ReceiveSharingIntent from 'react-native-receive-sharing-intent';
import { import {
uploadImage, uploadImage,
addDraft, addDraft,
@ -69,6 +70,7 @@ class EditorContainer extends Component {
community: [], community: [],
rewardType: 'default', rewardType: 'default',
beneficiaries: [], beneficiaries: [],
sharedSnippetText: null,
}; };
} }
@ -81,9 +83,11 @@ class EditorContainer extends Component {
let isEdit; let isEdit;
let post; let post;
let _draft; let _draft;
let hasSharedIntent = false;
if (navigation.state && navigation.state.params) { if (navigation.state && navigation.state.params) {
const navigationParams = navigation.state.params; const navigationParams = navigation.state.params;
hasSharedIntent = navigationParams.hasSharedIntent;
if (navigationParams.draft) { if (navigationParams.draft) {
_draft = navigationParams.draft; _draft = navigationParams.draft;
@ -98,30 +102,6 @@ class EditorContainer extends Component {
community: navigationParams.community, community: navigationParams.community,
}); });
} }
if (navigationParams.upload) {
const { upload } = navigationParams;
upload.forEach((el) => {
if (el.filePath && el.fileName) {
// this.setState({ isUploading: true });
const _media = {
path: el.filePath,
mime: el.mimeType,
filename: el.fileName || `img_${Math.random()}.jpg`,
};
this._uploadImage(_media, { shouldInsert: true });
} else if (el.text) {
this.setState({
draftPost: {
title: '',
body: el.text,
tags: [],
},
});
}
});
}
if (navigationParams.post) { if (navigationParams.post) {
({ post } = navigationParams); ({ post } = navigationParams);
@ -154,11 +134,36 @@ class EditorContainer extends Component {
} }
} }
if (!isEdit && !_draft) { if (!isEdit && !_draft && !hasSharedIntent) {
this._fetchDraftsForComparison(isReply); this._fetchDraftsForComparison(isReply);
} else { } else {
this._requestKeyboardFocus(); this._requestKeyboardFocus();
} }
ReceiveSharingIntent.getReceivedFiles(
(files) => {
files.forEach((el) => {
if (el.filePath && el.fileName) {
const _media = {
path: el.filePath,
mime: el.mimeType,
filename: el.fileName || `img_${Math.random()}.jpg`,
};
this._uploadImage(_media, { shouldInsert: true });
} else if (el.text) {
this.setState({
sharedSnippetText: el.text,
});
}
});
// To clear Intents
ReceiveSharingIntent.clearReceivedFiles();
},
(error) => {
console.log('error :>> ', error);
},
);
} }
componentWillUnmount() { componentWillUnmount() {
@ -433,57 +438,6 @@ class EditorContainer extends Component {
isUploading: false, isUploading: false,
}); });
} }
// uploadImage(media, currentAccount.name, sign).then((res) => {
// if (res.data && res.data.url) {
// res.data.hash = res.data.url.split('/').pop();
// this.setState({
// uploadedImage: res.data,
// isUploading: false,
// });
// }
// })
// .catch((error) => {
// console.log(error, error.message);
// if (error.toString().includes('code 413')) {
// Alert.alert(
// intl.formatMessage({
// id: 'alert.fail',
// }),
// intl.formatMessage({
// id: 'alert.payloadTooLarge',
// }),
// );
// } else if (error.toString().includes('code 429')) {
// Alert.alert(
// intl.formatMessage({
// id: 'alert.fail',
// }),
// intl.formatMessage({
// id: 'alert.quotaExceeded',
// }),
// );
// } else if (error.toString().includes('code 400')) {
// Alert.alert(
// intl.formatMessage({
// id: 'alert.fail',
// }),
// intl.formatMessage({
// id: 'alert.invalidImage',
// }),
// );
// } else {
// Alert.alert(
// intl.formatMessage({
// id: 'alert.fail',
// }),
// error.message || error.toString(),
// );
// }
// this.setState({
// isUploading: false,
// });
// });
}; };
_handleMediaOnSelectFailure = (error) => { _handleMediaOnSelectFailure = (error) => {
@ -1100,6 +1054,7 @@ class EditorContainer extends Component {
uploadedImage, uploadedImage,
community, community,
isDraft, isDraft,
sharedSnippetText,
} = this.state; } = this.state;
const tags = navigation.state.params && navigation.state.params.tags; const tags = navigation.state.params && navigation.state.params.tags;
@ -1133,6 +1088,7 @@ class EditorContainer extends Component {
community={community} community={community}
currentAccount={currentAccount} currentAccount={currentAccount}
isDraft={isDraft} isDraft={isDraft}
sharedSnippetText={sharedSnippetText}
/> />
); );
} }

View File

@ -320,6 +320,7 @@ class EditorScreen extends Component {
handleBeneficiaries, handleBeneficiaries,
currentAccount, currentAccount,
autoFocusText, autoFocusText,
sharedSnippetText,
} = this.props; } = this.props;
const rightButtonText = intl.formatMessage({ const rightButtonText = intl.formatMessage({
id: isEdit ? 'basic_header.update' : isReply ? 'basic_header.reply' : 'basic_header.publish', id: isEdit ? 'basic_header.update' : isReply ? 'basic_header.reply' : 'basic_header.publish',
@ -396,6 +397,7 @@ class EditorScreen extends Component {
onTitleChanged={this._handleChangeTitle} onTitleChanged={this._handleChangeTitle}
getCommunity={this._getCommunity} getCommunity={this._getCommunity}
autoFocusText={autoFocusText} autoFocusText={autoFocusText}
sharedSnippetText={sharedSnippetText}
/> />
</PostForm> </PostForm>
</View> </View>