From 8edb0a745d155a09b50ef1e64c554ba50dcc8a82 Mon Sep 17 00:00:00 2001 From: Sadaqat Ali Date: Wed, 17 Aug 2022 09:42:22 +0500 Subject: [PATCH] remove default beneficiary account while saving beneficiaries and draft and add it back when loading draft --- ios/Podfile.lock | 2 +- .../beneficiarySelectionContent.tsx | 13 ++++++++----- src/screens/editor/container/editorContainer.tsx | 12 ++++++++---- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/ios/Podfile.lock b/ios/Podfile.lock index 754f09c9e..9161cfe47 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -827,4 +827,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 0282022703ad578ab2d9afbf3147ba3b373b4311 -COCOAPODS: 1.11.3 +COCOAPODS: 1.11.2 diff --git a/src/components/beneficiarySelectionContent/beneficiarySelectionContent.tsx b/src/components/beneficiarySelectionContent/beneficiarySelectionContent.tsx index f0144771d..ddcbbd503 100644 --- a/src/components/beneficiarySelectionContent/beneficiarySelectionContent.tsx +++ b/src/components/beneficiarySelectionContent/beneficiarySelectionContent.tsx @@ -44,6 +44,7 @@ const BeneficiarySelectionContent = ({ const beneficiariesMap = useAppSelector((state) => state.editor.beneficiariesMap); const username = useAppSelector((state) => state.account.currentAccount.name); + const DEFAULT_BENEFICIARY = { account: username, weight: 10000 }; const [beneficiaries, setBeneficiaries] = useState([ { account: username, weight: 10000, autoPowerUp: false }, @@ -94,8 +95,9 @@ const BeneficiarySelectionContent = ({ const readTempBeneficiaries = async () => { if (beneficiariesMap) { - const tempBeneficiaries = beneficiariesMap[draftId || TEMP_BENEFICIARIES_ID]; - + const savedBeneficiareis = beneficiariesMap[draftId || TEMP_BENEFICIARIES_ID]; + const tempBeneficiaries = savedBeneficiareis && savedBeneficiareis.length ? [DEFAULT_BENEFICIARY, ...beneficiariesMap[draftId || TEMP_BENEFICIARIES_ID]] : [DEFAULT_BENEFICIARY]; + if (isArray(tempBeneficiaries) && tempBeneficiaries.length > 0) { //weight correction algorithm. let othersWeight = 0; @@ -105,17 +107,17 @@ const BeneficiarySelectionContent = ({ } }); tempBeneficiaries[0].weight = 10000 - othersWeight; - setBeneficiaries(tempBeneficiaries); } } }; const _saveBeneficiaries = (value: Beneficiary[]) => { + const filteredBeneficiaries = value.filter((item) => item.account !== username); //remove default beneficiary from array while saving if (handleSaveBeneficiary) { - handleSaveBeneficiary(value); + handleSaveBeneficiary(filteredBeneficiaries); } else { - dispatch(setBeneficiariesAction(draftId || TEMP_BENEFICIARIES_ID, value)); + dispatch(setBeneficiariesAction(draftId || TEMP_BENEFICIARIES_ID, filteredBeneficiaries)); } }; @@ -359,6 +361,7 @@ const BeneficiarySelectionContent = ({ ); }; +console.log('draftId : ',draftId ); return ( diff --git a/src/screens/editor/container/editorContainer.tsx b/src/screens/editor/container/editorContainer.tsx index 9abbe8dc1..42b2e0c8a 100644 --- a/src/screens/editor/container/editorContainer.tsx +++ b/src/screens/editor/container/editorContainer.tsx @@ -5,6 +5,7 @@ import { Alert } from 'react-native'; import ImagePicker from 'react-native-image-crop-picker'; import get from 'lodash/get'; import AsyncStorage from '@react-native-community/async-storage'; +import { isArray } from 'lodash'; // Services and Actions import { Buffer } from 'buffer'; @@ -254,7 +255,7 @@ class EditorContainer extends Component { // load meta from local/param drfat into state _loadMeta = (draft: any) => { - const { dispatch } = this.props; + const { dispatch, currentAccount } = this.props; // if meta exist on draft, get the index of 1st image in meta from images urls in body const body = draft.body; if (draft.meta && draft.meta.image) { @@ -279,7 +280,10 @@ class EditorContainer extends Component { } if (draft._id && draft.meta && draft.meta.beneficiaries) { - dispatch(setBeneficiaries(draft._id || TEMP_BENEFICIARIES_ID, draft.meta.beneficiaries)); + if(isArray(draft.meta.beneficiaries)){ + const filteredBeneficiaries = draft.meta.beneficiaries.filter((item) => item.account !== currentAccount.username); //remove default beneficiary from array while saving + dispatch(setBeneficiaries(draft._id || TEMP_BENEFICIARIES_ID, filteredBeneficiaries)); + } } } _requestKeyboardFocus = () => { @@ -625,8 +629,8 @@ class EditorContainer extends Component { draftId: response._id, }); } - - dispatch(setBeneficiaries(response._id, beneficiaries)); + const filteredBeneficiaries = beneficiaries.filter((item) => item.account !== currentAccount.username); //remove default beneficiary from array while saving + dispatch(setBeneficiaries(response._id, filteredBeneficiaries)); dispatch(removeBeneficiaries(TEMP_BENEFICIARIES_ID)); //clear local copy if darft save is successful