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 7 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
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
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 {RecentWaypoints, Transaction} from '../../types/onyx';

let recentWaypoints = [];
let recentWaypoints: RecentWaypoints[] = [];
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 @@ -24,10 +24,7 @@ Onyx.connect({
},
});

/**
* @param {String} transactionID
*/
function createInitialWaypoints(transactionID) {
function createInitialWaypoints(transactionID: string) {
Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, {
comment: {
waypoints: {
Expand All @@ -40,14 +37,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 @@ -69,11 +63,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: RecentWaypoints) {
Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, {
kubabutkiewicz marked this conversation as resolved.
Show resolved Hide resolved
comment: {
waypoints: {
Expand All @@ -82,7 +73,8 @@ function saveWaypoint(transactionID, index, waypoint) {
},
// Empty out errors when we're saving a new waypoint as this indicates the user is updating their input
errorFields: {
route: null,
// TODO: check if its ok to put undefined
route: undefined,
kubabutkiewicz marked this conversation as resolved.
Show resolved Hide resolved
},

// Clear the existing route so that we don't show an old route
Expand All @@ -102,39 +94,39 @@ function saveWaypoint(transactionID, index, waypoint) {
return;
}

const recentWaypointAlreadyExists = _.find(recentWaypoints, (recentWaypoint) => recentWaypoint.address === waypoint.address);
const recentWaypointAlreadyExists = recentWaypoints.find((recentWaypoint) => recentWaypoint?.address === waypoint?.address);
if (!recentWaypointAlreadyExists) {
const clonedWaypoints = _.clone(recentWaypoints);
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);
waypointValues.splice(index, 1);

const reIndexedWaypoints = {};
const reIndexedWaypoints: Record<string, RecentWaypoints> = {};
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
const newTransaction = {
const newTransaction: Transaction = {
...transaction,
comment: {
...transaction.comment,
Expand All @@ -143,8 +135,10 @@ function removeWaypoint(transactionID, currentIndex) {
// Clear the existing route so that we don't show an old route
routes: {
route0: {
distance: null,
geometry: {
coordinates: null,
type: '',
},
},
},
Expand All @@ -155,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: RecentWaypoints) {
API.read(
kubabutkiewicz marked this conversation as resolved.
Show resolved Hide resolved
'GetRoute',
{
Expand Down
2 changes: 1 addition & 1 deletion src/types/onyx/RecentWaypoints.ts
kubabutkiewicz marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ type RecentWaypoints = {

/** The longitude of the waypoint */
lng: number;
};
} | null;
kubabutkiewicz marked this conversation as resolved.
Show resolved Hide resolved

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

type Comment = {
comment?: string;
waypoints?: Record<string, RecentWaypoints>;
};

type GeometryType = 'LineString' | '';
kubabutkiewicz marked this conversation as resolved.
Show resolved Hide resolved

type Geometry = {
coordinates: number[][];
type: 'LineString';
coordinates: number[][] | null;
type: GeometryType;
kubabutkiewicz marked this conversation as resolved.
Show resolved Hide resolved
};

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

Expand All @@ -29,6 +33,7 @@ type Transaction = {
created: string;
pendingAction: OnyxCommon.PendingAction;
errors: OnyxCommon.Errors;
errorFields: OnyxCommon.ErrorFields;
modifiedAmount?: number;
modifiedCreated?: string;
modifiedCurrency?: string;
Expand Down
Loading