Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[No QA] [TS Migration] Transaction lib #27138

Merged
merged 14 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ type OnyxValues = {
[ONYXKEYS.NVP_BLOCKED_FROM_CONCIERGE]: OnyxTypes.BlockedFromConcierge;
[ONYXKEYS.NVP_PRIVATE_PUSH_NOTIFICATION_ID]: string;
[ONYXKEYS.NVP_LAST_PAYMENT_METHOD]: Record<string, string>;
[ONYXKEYS.NVP_RECENT_WAYPOINTS]: OnyxTypes.RecentWaypoints[];
[ONYXKEYS.NVP_RECENT_WAYPOINTS]: OnyxTypes.RecentWaypoint[];
[ONYXKEYS.PUSH_NOTIFICATIONS_ENABLED]: boolean;
[ONYXKEYS.PLAID_DATA]: OnyxTypes.PlaidData;
[ONYXKEYS.IS_PLAID_DISABLED]: boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import _ from 'underscore';
import Onyx from 'react-native-onyx';
import lodashGet from 'lodash/get';
import lodashHas from 'lodash/has';
import lodashClone from 'lodash/clone';
import ONYXKEYS from '../../ONYXKEYS';
import * as CollectionUtils from '../CollectionUtils';
import * as API from '../API';
import {RecentWaypoint, Transaction} from '../../types/onyx';
import {WaypointCollection} from '../../types/onyx/Transaction';
import * as TransactionUtils from '../TransactionUtils';

let recentWaypoints = [];
let recentWaypoints: RecentWaypoint[] = [];
Onyx.connect({
key: ONYXKEYS.NVP_RECENT_WAYPOINTS,
callback: (val) => (recentWaypoints = val || []),
callback: (val) => (recentWaypoints = val ?? []),
});

const allTransactions = {};
const allTransactions: Record<string, Transaction> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.TRANSACTION,
callback: (transaction, key) => {
Expand All @@ -25,10 +26,7 @@ Onyx.connect({
},
});

/**
* @param {String} transactionID
*/
function createInitialWaypoints(transactionID) {
function createInitialWaypoints(transactionID: string) {
Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, {
comment: {
waypoints: {
Expand All @@ -41,14 +39,11 @@ function createInitialWaypoints(transactionID) {

/**
* Add a stop to the transaction
*
* @param {String} transactionID
* @param {Number} newLastIndex
*/
function addStop(transactionID) {
const transaction = lodashGet(allTransactions, transactionID, {});
const existingWaypoints = lodashGet(transaction, 'comment.waypoints', {});
const newLastIndex = _.size(existingWaypoints);
function addStop(transactionID: string) {
const transaction = allTransactions?.[transactionID] ?? {};
const existingWaypoints = transaction?.comment?.waypoints ?? {};
const newLastIndex = Object.keys(existingWaypoints).length;

Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, {
comment: {
Expand All @@ -61,11 +56,8 @@ function addStop(transactionID) {

/**
* Saves the selected waypoint to the transaction
* @param {String} transactionID
* @param {String} index
* @param {Object} waypoint
*/
function saveWaypoint(transactionID, index, waypoint) {
function saveWaypoint(transactionID: string, index: string, waypoint: RecentWaypoint | null) {
Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, {
comment: {
waypoints: {
Expand Down Expand Up @@ -93,41 +85,39 @@ function saveWaypoint(transactionID, index, waypoint) {
if (!lodashHas(waypoint, 'lat') || !lodashHas(waypoint, 'lng')) {
return;
}

const recentWaypointAlreadyExists = _.find(recentWaypoints, (recentWaypoint) => recentWaypoint.address === waypoint.address);
if (!recentWaypointAlreadyExists) {
const clonedWaypoints = _.clone(recentWaypoints);
const recentWaypointAlreadyExists = recentWaypoints.find((recentWaypoint) => recentWaypoint?.address === waypoint?.address);
if (!recentWaypointAlreadyExists && waypoint !== null) {
const clonedWaypoints = lodashClone(recentWaypoints);
clonedWaypoints.unshift(waypoint);
Onyx.merge(ONYXKEYS.NVP_RECENT_WAYPOINTS, clonedWaypoints.slice(0, 5));
}
}

function removeWaypoint(transactionID, currentIndex) {
function removeWaypoint(transactionID: string, currentIndex: string) {
// Index comes from the route params and is a string
kubabutkiewicz marked this conversation as resolved.
Show resolved Hide resolved
const index = Number(currentIndex);
const transaction = lodashGet(allTransactions, transactionID, {});
const existingWaypoints = lodashGet(transaction, 'comment.waypoints', {});
const totalWaypoints = _.size(existingWaypoints);

const transaction = allTransactions?.[transactionID] ?? {};
const existingWaypoints = transaction?.comment?.waypoints ?? {};
const totalWaypoints = Object.keys(existingWaypoints).length;
// Prevents removing the starting or ending waypoint but clear the stored address only if there are only two waypoints
if (totalWaypoints === 2 && (index === 0 || index === totalWaypoints - 1)) {
saveWaypoint(transactionID, index, null);
saveWaypoint(transactionID, index.toString(), null);
return;
}

const waypointValues = _.values(existingWaypoints);
const waypointValues = Object.values(existingWaypoints);
const removed = waypointValues.splice(index, 1);
const isRemovedWaypointEmpty = removed.length > 0 && !TransactionUtils.waypointHasValidAddress(removed[0]);
const isRemovedWaypointEmpty = removed.length > 0 && !TransactionUtils.waypointHasValidAddress(removed[0] ?? {});

const reIndexedWaypoints = {};
const reIndexedWaypoints: WaypointCollection = {};
waypointValues.forEach((waypoint, idx) => {
reIndexedWaypoints[`waypoint${idx}`] = waypoint;
});

// Onyx.merge won't remove the null nested object values, this is a workaround
// to remove nested keys while also preserving other object keys
// Doing a deep clone of the transaction to avoid mutating the original object and running into a cache issue when using Onyx.set
let newTransaction = {
let newTransaction: Transaction = {
...transaction,
comment: {
...transaction.comment,
Expand All @@ -145,6 +135,7 @@ function removeWaypoint(transactionID, currentIndex) {
// Clear the existing route so that we don't show an old route
routes: {
route0: {
distance: null,
geometry: {
coordinates: null,
},
Expand All @@ -158,10 +149,8 @@ function removeWaypoint(transactionID, currentIndex) {
/**
* Gets the route for a set of waypoints
* Used so we can generate a map view of the provided waypoints
* @param {String} transactionID
* @param {Object} waypoints
*/
function getRoute(transactionID, waypoints) {
function getRoute(transactionID: string, waypoints: WaypointCollection) {
API.read(
'GetRoute',
{
Expand Down
2 changes: 1 addition & 1 deletion src/types/onyx/OnyxCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import CONST from '../../CONST';

type PendingAction = ValueOf<typeof CONST.RED_BRICK_ROAD_PENDING_ACTION>;

type ErrorFields = Record<string | number, Record<string, string>>;
type ErrorFields = Record<string | number, Record<string, string> | null>;

type Errors = Record<string, string>;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type RecentWaypoints = {
type RecentWaypoint = {
/** The full address of the waypoint */
address: string;

Expand All @@ -9,4 +9,4 @@ type RecentWaypoints = {
lng: number;
};

export default RecentWaypoints;
export default RecentWaypoint;
13 changes: 10 additions & 3 deletions src/types/onyx/Transaction.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import {ValueOf} from 'type-fest';
import * as OnyxCommon from './OnyxCommon';
import CONST from '../../CONST';
import RecentWaypoint from './RecentWaypoint';

type WaypointCollection = Record<string, RecentWaypoint | null>;
type Comment = {
comment?: string;
waypoints?: WaypointCollection;
};

type GeometryType = 'LineString';

type Geometry = {
coordinates: number[][];
type: 'LineString';
coordinates: number[][] | null;
type?: GeometryType;
};

type Route = {
distance: number;
distance: number | null;
geometry: Geometry;
};

Expand All @@ -25,6 +30,7 @@ type Transaction = {
created: string;
currency: string;
errors: OnyxCommon.Errors;
errorFields?: OnyxCommon.ErrorFields;
// The name of the file used for a receipt (formerly receiptFilename)
filename?: string;
merchant: string;
Expand All @@ -44,3 +50,4 @@ type Transaction = {
};

export default Transaction;
export type {WaypointCollection};
4 changes: 2 additions & 2 deletions src/types/onyx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import ReportActionReactions from './ReportActionReactions';
import SecurityGroup from './SecurityGroup';
import Transaction from './Transaction';
import Form, {AddDebitCardForm} from './Form';
import RecentWaypoints from './RecentWaypoints';
import RecentWaypoint from './RecentWaypoint';
import RecentlyUsedCategories from './RecentlyUsedCategories';
import RecentlyUsedTags from './RecentlyUsedTags';
import PolicyTag from './PolicyTag';
Expand Down Expand Up @@ -91,8 +91,8 @@ export type {
Form,
AddDebitCardForm,
OnyxUpdatesFromServer,
RecentWaypoint,
OnyxUpdateEvent,
RecentWaypoints,
RecentlyUsedCategories,
RecentlyUsedTags,
PolicyTag,
Expand Down
Loading