Skip to content

Commit

Permalink
Merge pull request #48687 from software-mansion-labs/Guccio163/47927-…
Browse files Browse the repository at this point in the history
…ExportCSV

#47927: Export Categories CSV
  • Loading branch information
mountiny authored Sep 9, 2024
2 parents 523b7bf + dc9cd2d commit 7e04e86
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/components/ImportSpreadsheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function ImportSpreedsheet({backTo, goTo}: ImportSpreedsheetProps) {
<DragAndDropProvider setIsDraggingOver={setIsDraggingOver}>
<View style={[styles.flex1, safeAreaPaddingBottomStyle]}>
<HeaderWithBackButton
title={translate('common.importSpreadsheet')}
title={translate('spreadsheet.importSpreadsheet')}
onBackButtonPress={() => Navigation.navigate(backTo)}
/>

Expand Down
3 changes: 2 additions & 1 deletion src/languages/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,6 @@ export default {
ignore: 'Ignore',
enabled: 'Enabled',
import: 'Import',
importSpreadsheet: 'Import spreadsheet',
offlinePrompt: "You can't take this action right now.",
outstanding: 'Outstanding',
chats: 'Chats',
Expand Down Expand Up @@ -687,6 +686,8 @@ export default {
importFailedDescription: 'Please ensure all fields are filled out correctly and try again. If the problem persists, please reach out to Concierge.',
invalidFileMessage:
'The file you uploaded is either empty or contains invalid data. Please ensure that the file is correctly formatted and contains the necessary information before uploading it again.',
importSpreadsheet: 'Import spreadsheet',
downloadCSV: 'Download CSV',
},
receipt: {
upload: 'Upload receipt',
Expand Down
3 changes: 2 additions & 1 deletion src/languages/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@ export default {
filterLogs: 'Registros de filtrado',
network: 'La red',
reportID: 'ID del informe',
importSpreadsheet: 'Importar hoja de cálculo',
chooseFile: 'Elegir archivo',
dropTitle: 'Suéltalo',
dropMessage: 'Suelta tu archivo aquí',
Expand Down Expand Up @@ -680,6 +679,8 @@ export default {
importSuccessfullTitle: 'Importar categorías',
invalidFileMessage:
'El archivo que ha cargado está vacío o contiene datos no válidos. Asegúrese de que el archivo tiene el formato correcto y contiene la información necesaria antes de volver a cargarlo.',
importSpreadsheet: 'Importar hoja de cálculo',
downloadCSV: 'Descargar CSV',
},
receipt: {
upload: 'Subir recibo',
Expand Down
6 changes: 6 additions & 0 deletions src/libs/API/parameters/ExportCategoriesSpreadsheet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type ExportCategoriesSpreadsheetParams = {
/** ID of the policy */
policyID: string;
};

export default ExportCategoriesSpreadsheetParams;
1 change: 1 addition & 0 deletions src/libs/API/parameters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ export type {default as SetPolicyCategoryReceiptsRequiredParams} from './SetPoli
export type {default as RemovePolicyCategoryReceiptsRequiredParams} from './RemovePolicyCategoryReceiptsRequiredParams';
export type {default as UpdateQuickbooksOnlineAutoCreateVendorParams} from './UpdateQuickbooksOnlineAutoCreateVendorParams';
export type {default as ImportCategoriesSpreadsheetParams} from './ImportCategoriesSpreadsheet';
export type {default as ExportCategoriesSpreadsheetParams} from './ExportCategoriesSpreadsheet';
export type {default as UpdateXeroGenericTypeParams} from './UpdateXeroGenericTypeParams';
export type {default as UpdateCardSettlementFrequencyParams} from './UpdateCardSettlementFrequencyParams';
export type {default as UpdateCardSettlementAccountParams} from './UpdateCardSettlementAccountParams';
Expand Down
6 changes: 4 additions & 2 deletions src/libs/API/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ const WRITE_COMMANDS = {
SET_WORKSPACE_CATEGORIES_ENABLED: 'SetWorkspaceCategoriesEnabled',
SET_POLICY_TAGS_ENABLED: 'SetPolicyTagsEnabled',
CREATE_WORKSPACE_CATEGORIES: 'CreateWorkspaceCategories',
IMPORT_CATEGORIES_SREADSHEET: 'ImportCategoriesSpreadsheet',
IMPORT_CATEGORIES_SPREADSHEET: 'ImportCategoriesSpreadsheet',
EXPORT_CATEGORIES_CSV: 'ExportCategoriesCSV',
RENAME_WORKSPACE_CATEGORY: 'RenameWorkspaceCategory',
CREATE_POLICY_TAG: 'CreatePolicyTag',
RENAME_POLICY_TAG: 'RenamePolicyTag',
Expand Down Expand Up @@ -501,7 +502,8 @@ type WriteCommandParameters = {
[WRITE_COMMANDS.CREATE_WORKSPACE_FROM_IOU_PAYMENT]: Parameters.CreateWorkspaceFromIOUPaymentParams;
[WRITE_COMMANDS.SET_WORKSPACE_CATEGORIES_ENABLED]: Parameters.SetWorkspaceCategoriesEnabledParams;
[WRITE_COMMANDS.CREATE_WORKSPACE_CATEGORIES]: Parameters.CreateWorkspaceCategoriesParams;
[WRITE_COMMANDS.IMPORT_CATEGORIES_SREADSHEET]: Parameters.ImportCategoriesSpreadsheetParams;
[WRITE_COMMANDS.IMPORT_CATEGORIES_SPREADSHEET]: Parameters.ImportCategoriesSpreadsheetParams;
[WRITE_COMMANDS.EXPORT_CATEGORIES_CSV]: Parameters.ExportCategoriesSpreadsheetParams;
[WRITE_COMMANDS.RENAME_WORKSPACE_CATEGORY]: Parameters.RenameWorkspaceCategoriesParams;
[WRITE_COMMANDS.SET_WORKSPACE_REQUIRES_CATEGORY]: Parameters.SetWorkspaceRequiresCategoryParams;
[WRITE_COMMANDS.DELETE_WORKSPACE_CATEGORIES]: Parameters.DeleteWorkspaceCategoriesParams;
Expand Down
19 changes: 18 additions & 1 deletion src/libs/actions/Policy/Category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ import type {
UpdatePolicyCategoryGLCodeParams,
} from '@libs/API/parameters';
import {READ_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
import * as ApiUtils from '@libs/ApiUtils';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import * as ErrorUtils from '@libs/ErrorUtils';
import fileDownload from '@libs/fileDownload';
import getIsNarrowLayout from '@libs/getIsNarrowLayout';
import {translateLocal} from '@libs/Localize';
import Log from '@libs/Log';
import enhanceParameters from '@libs/Network/enhanceParameters';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import {navigateWhenEnableFeature, removePendingFieldsFromCustomUnit} from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
Expand Down Expand Up @@ -512,7 +515,7 @@ function importPolicyCategories(policyID: string, categories: PolicyCategory[])
categories: JSON.stringify([...categories.map((category) => ({name: category.name, enabled: category.enabled, 'GL Code': String(category['GL Code'])}))]),
};

API.write(WRITE_COMMANDS.IMPORT_CATEGORIES_SREADSHEET, parameters, onyxData);
API.write(WRITE_COMMANDS.IMPORT_CATEGORIES_SPREADSHEET, parameters, onyxData);
}

function renamePolicyCategory(policyID: string, policyCategory: {oldName: string; newName: string}) {
Expand Down Expand Up @@ -998,6 +1001,19 @@ function setPolicyDistanceRatesDefaultCategory(policyID: string, currentCustomUn
API.write(WRITE_COMMANDS.SET_POLICY_DISTANCE_RATES_DEFAULT_CATEGORY, params, {optimisticData, successData, failureData});
}

function downloadCategoriesCSV(policyID: string) {
const finalParameters = enhanceParameters(WRITE_COMMANDS.EXPORT_CATEGORIES_CSV, {
policyID,
});

const formData = new FormData();
Object.entries(finalParameters).forEach(([key, value]) => {
formData.append(key, String(value));
});

fileDownload(ApiUtils.getCommandURL({command: WRITE_COMMANDS.EXPORT_CATEGORIES_CSV}), 'Categories.csv', '', false, formData, CONST.NETWORK.METHOD.POST);
}

function setWorkspaceCategoryDescriptionHint(policyID: string, categoryName: string, commentHint: string) {
const originalCommentHint = allPolicyCategories?.[`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`]?.[categoryName]?.commentHint;

Expand Down Expand Up @@ -1311,4 +1327,5 @@ export {
setPolicyCategoryApprover,
setPolicyCategoryTax,
importPolicyCategories,
downloadCategoriesCSV,
};
3 changes: 1 addition & 2 deletions src/libs/fileDownload/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ const postDownloadFile = (url: string, fileName?: string, formData?: FormData, o
})
.then((fileData) => {
const finalFileName = FileUtils.appendTimeToFileName(fileName ?? 'Expensify');
const downloadPath = `${RNFS.DownloadDirectoryPath}/Expensify/${finalFileName}`;

const downloadPath = `${RNFS.DownloadDirectoryPath}/${finalFileName}`;
return RNFS.writeFile(downloadPath, fileData, 'utf8').then(() => downloadPath);
})
.then((downloadPath) =>
Expand Down
16 changes: 14 additions & 2 deletions src/pages/workspace/categories/WorkspaceCategoriesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import Navigation from '@libs/Navigation/Navigation';
import type {FullScreenNavigatorParamList} from '@libs/Navigation/types';
import * as PolicyUtils from '@libs/PolicyUtils';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import * as Modal from '@userActions/Modal';
import {deleteWorkspaceCategories, setWorkspaceCategoryEnabled} from '@userActions/Policy/Category';
import * as Category from '@userActions/Policy/Category';
import CONST from '@src/CONST';
Expand Down Expand Up @@ -300,15 +301,26 @@ function WorkspaceCategoriesPage({route}: WorkspaceCategoriesPageProps) {
const menuItems = [
{
icon: Expensicons.Table,
text: translate('common.importSpreadsheet'),
text: translate('spreadsheet.importSpreadsheet'),
onSelected: () => {
if (isOffline) {
setIsOfflineModalVisible(true);
Modal.close(() => setIsOfflineModalVisible(true));
return;
}
Navigation.navigate(ROUTES.WORKSPACE_CATEGORIES_IMPORT.getRoute(policyId));
},
},
{
icon: Expensicons.Download,
text: translate('spreadsheet.downloadCSV'),
onSelected: () => {
if (isOffline) {
Modal.close(() => setIsOfflineModalVisible(true));
return;
}
Category.downloadCategoriesCSV(policyId);
},
},
];

return menuItems;
Expand Down

0 comments on commit 7e04e86

Please sign in to comment.