Skip to content

Commit

Permalink
Simplify data formats
Browse files Browse the repository at this point in the history
  • Loading branch information
tgolen committed Sep 22, 2023
1 parent d3b31ad commit da2a13e
Showing 1 changed file with 62 additions and 60 deletions.
122 changes: 62 additions & 60 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -664,15 +664,16 @@ function updateDistanceRequest(transactionID, transactionThreadReportID, transac
const transactionDetails = ReportUtils.getTransactionDetails(updatedTransaction);

const params = {
transactionID,
...transactionDetails,
transactionID,
// This needs to be a JSON string since we're sending this to the MapBox API
waypoints: JSON.stringify(transactionDetails.waypoints),
};

// Step 3: Build the modified expense report actions
// We don't create a modified report action if we're updating the waypoints,
// since there isn't actually any optimistic data we can create for them.
// since there isn't actually any optimistic data we can create for them and the report action is created on the server
// with the response from the MapBox API
if (!_.has(transactionChanges, 'waypoints')) {
const updatedReportAction = ReportUtils.buildOptimisticModifiedExpenseReportAction(transactionThread, transaction, transactionChanges, isFromExpenseReport);
params.reportActionID = updatedReportAction.reportActionID;
Expand Down Expand Up @@ -725,68 +726,69 @@ function updateDistanceRequest(transactionID, transactionThreadReportID, transac
}
}

API.write('UpdateDistanceRequest', params, {
optimisticData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`,
value: {
comment: {
waypoints: null,
},
errorFields: null,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${temporaryTransactionID}`,
value: {
pendingFields,
isLoading: true,
errorFields: null,
},
},
],
successData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`,
value: {
comment: {
waypoints: {
...transactionDetails.comment.waypoints,
},
},
errorFields: null,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${temporaryTransactionID}`,
value: {
pendingFields: clearedPendingFields,
isLoading: false,
errorFields: null,
},
// Optimistically modify the transaction
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`,
value: {
comment: {
waypoints: null,
},
],
failureData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${temporaryTransactionID}`,
value: {
pendingFields: clearedPendingFields,
isLoading: false,
errorFields,
errorFields: null,
},
});
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${temporaryTransactionID}`,
value: {
pendingFields,
isLoading: true,
errorFields: null,
},
});

// Clear out the error fields and loading states on success
successData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`,
value: {
comment: {
waypoints: {
...transactionDetails.comment.waypoints,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`,
value: iouReport,
},
],
errorFields: null,
},
});
successData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${temporaryTransactionID}`,
value: {
pendingFields: clearedPendingFields,
isLoading: false,
errorFields: null,
},
});

// Clear out loading states, pending fields, and error fields on failures
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${temporaryTransactionID}`,
value: {
pendingFields: clearedPendingFields,
isLoading: false,
errorFields,
},
});

// Reset the iouReport to it's original state
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`,
value: iouReport,
});

API.write('UpdateDistanceRequest', params, {optimisticData, successData, failureData});
}

/**
Expand Down

0 comments on commit da2a13e

Please sign in to comment.