Skip to content

Commit

Permalink
CorrectedCode
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajeett01 committed Sep 3, 2024
1 parent 1ea2ac2 commit d6f6d85
Show file tree
Hide file tree
Showing 3 changed files with 1,332 additions and 1,300 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"fetch-libraries": "bash fetch-libraries.sh"
},
"dependencies": {
"@json2csv/plainjs": "7.0.6",
"@keystonehq/bc-ur-registry": "0.4.2",
"@lightninglabs/lnc-core": "file:zeus_modules/@lightninglabs/lnc-core",
"@ngraveio/bc-ur": "1.1.12",
Expand Down
72 changes: 20 additions & 52 deletions views/Activity/ActivityToCsv.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import React, { useState } from 'react';
import {
StyleSheet,
View,
PermissionsAndroid,
Platform,
Alert,
Modal
} from 'react-native';
import { StyleSheet, View, Platform, Alert, Modal } from 'react-native';
import RNFS from 'react-native-fs';
import Button from '../../components/Button';
import TextInput from '../../components/TextInput';
Expand All @@ -18,6 +11,7 @@ import Transaction from '../../models/Transaction';

const JsonToCsv = ({ filteredActivity, isVisible, closeModal }) => {
const [customFileName, setCustomFileName] = useState('');
const [isLoading, setIsLoading] = useState(false);

const closeAndClearInput = () => {
setCustomFileName('');
Expand All @@ -37,6 +31,7 @@ const JsonToCsv = ({ filteredActivity, isVisible, closeModal }) => {
};

const filterData = (data, invoiceKeys, paymentKeys, transactionKeys) => {
setIsLoading(true);
return data.map((item) => {
let filteredItem = {};
if (item instanceof Invoice) {
Expand All @@ -63,14 +58,15 @@ const JsonToCsv = ({ filteredActivity, isVisible, closeModal }) => {
} else if (item instanceof Payment) {
paymentKeys.forEach((key) => {
switch (key) {
case 'payment_hash':
filteredItem[key] = item.paymentHash;
break;
case 'payment_addr':
filteredItem[key] = item.getDestination;
break;
case 'value':
filteredItem[key] = item.getAmount;
break;
case 'amount_msat':
filteredItem[key] = item.amount_msat;
filteredItem[key] = item.getAmount;
break;
case 'creation_date':
filteredItem[key] = item.getDisplayTime;
Expand Down Expand Up @@ -117,9 +113,9 @@ const JsonToCsv = ({ filteredActivity, isVisible, closeModal }) => {
];

const paymentKeysToInclude = [
{ label: 'Payment Address', value: 'payment_addr' },
{ label: 'Destination', value: 'payment_addr' },
{ label: 'Payment Hash', value: 'payment_hash' },
{ label: 'Value', value: 'value' },
{ label: 'Amount (msat)', value: 'amount_msat' },
{ label: 'Creation Date', value: 'creation_date' }
];

Expand Down Expand Up @@ -160,7 +156,7 @@ const JsonToCsv = ({ filteredActivity, isVisible, closeModal }) => {
(item) => item.amt_paid
);
const paymentData = filteredData.filter(
(item) => item.creation_date
(item) => item.payment_addr
);

const invoiceSection = [
Expand Down Expand Up @@ -190,27 +186,6 @@ const JsonToCsv = ({ filteredActivity, isVisible, closeModal }) => {
return '';
}
};

const requestAndroidPermissions = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
{
title: 'Storage Permission Required',
message:
'This app needs access to your storage to save CSV files',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK'
}
);
return granted === PermissionsAndroid.RESULTS.GRANTED;
} catch (err) {
console.error('Failed to request permission ', err);
return false;
}
};

const downloadCsv = async () => {
const invoicePaymentCsv = await convertJsonToCsv(
filteredActivity.filter((item) => !(item instanceof Transaction)),
Expand All @@ -224,14 +199,6 @@ const JsonToCsv = ({ filteredActivity, isVisible, closeModal }) => {
if (!invoicePaymentCsv && !transactionCsv) return;

try {
if (Platform.OS === 'android') {
const hasPermission = await requestAndroidPermissions();
if (!hasPermission) {
console.error('Storage permission denied');
return;
}
}

const dateTime = getFormattedDateTime();
const baseFileName = customFileName || `data_${dateTime}`;
const invoicePaymentFileName = `${baseFileName}_invoice_payment.csv`;
Expand Down Expand Up @@ -284,7 +251,16 @@ const JsonToCsv = ({ filteredActivity, isVisible, closeModal }) => {
onRequestClose={closeAndClearInput}
>
<View style={styles.modalOverlay}>
<View style={styles.modalView}>
<View
style={{
width: '80%',
backgroundColor: themeColor('background'),
padding: 20,
borderRadius: 10,
alignItems: 'center',
elevation: 5
}}
>
<TextInput
placeholder={localeString(
'views.ActivityToCsv.textInputPlaceholder'
Expand Down Expand Up @@ -323,14 +299,6 @@ const styles = StyleSheet.create({
justifyContent: 'center',
alignItems: 'center'
},
modalView: {
width: '80%',
backgroundColor: themeColor('background'),
padding: 20,
borderRadius: 10,
alignItems: 'center',
elevation: 5
},
buttonContainer: {
width: '100%',
marginTop: 20
Expand Down
Loading

0 comments on commit d6f6d85

Please sign in to comment.