0

I have a function ,that i'm using in multiple components . It collects data from components and stores it in db. My problem is that function has a lot of arguments and some of them might not be used in some components . Take a look :

export default async function addUserLogs(account, service, terminal,isSaved,isSentToGateway,amount ) {
  const obj = {
    serviceID: service,
    terminalID: terminal,
    amountName: account,
    amount: amount,
    isSaved: isSaved,
    isSentToGateway: isSentToGateway,
  };

  const db = await InitDb();
  const tx = db.transaction('userLogs', 'readwrite');
  const store = tx.objectStore('userLogs');
  const index = await store.put(obj);
  let params = {};
  if (window.localStorage.getItem('params') !== null) {
    params = JSON.parse(window.localStorage.getItem('params'));
  }
  window.localStorage.setItem(
    'params',
    JSON.stringify({ ...params, userLogIndex: index })
  );
} 

For example account and service arguments i'm passing to function in on one component, others arguments is not required . In another component I only need to pass amount argument, but I need to specify previous arguments to do not overwrite other values. But there is an error "account, service, terminal,isSaved,isSentToGateway is not defined". Could you please suggest me how to fix that problem

Yerlan Yeszhanov
  • 2,149
  • 12
  • 37
  • 67
  • in JS arguments are optional, so there is nothing you need to do except make a condition inside your function `if (arg1) { do something }` etc. – Michael Oct 02 '19 at 05:42
  • Because you access it when creating the `obj` and did not pass it, that is why you get the error. You should make the ones that are not mandatory have default values so that when you don't provide them there won't be any problem – harisu Oct 02 '19 at 05:47
  • I think you should refer it - https://stackoverflow.com/questions/894860/set-a-default-parameter-value-for-a-javascript-function – Atul Oct 02 '19 at 06:48
  • I think you should refer it - https://stackoverflow.com/questions/894860/set-a-default-parameter-value-for-a-javascript-function – Atul Oct 02 '19 at 06:49

3 Answers3

1

addUserLogs can receive an object with optional keys and merge the last received object with the new one:

export default async function addUserLogs(newParams) {
  const lastParams = <get the last params from storage>;
  const obj = Object.assign(lastParams, newParams);
  ...
} 

// usage examples
addUserLogs({amountName: 'x', serviceID: 1, terminalID: 2, isSaved: true ,isSentToGateway: false, amount: 10});
addUserLogs({isSentToGateway: false, amount: 10});
addUserLogs({amountName: 'x', serviceID: 1, terminalID: 2});

If you want to be more declarative about addUserLogs signature you can do something link:

export default async function addUserLogs({amountName, serviceID, terminalID, isSaved ,isSentToGateway, amount}) {
  const lastParams = <get the last params from storage>;
  const newParams = {
      amountName: amountName || lastParams.amountName, 
      serviceID: serviceID || lastParams.serviceID, 
      terminalID: terminalID || lastParams.terminalID, 
      isSaved: isSaved === undefined ? lastParams.isSaved : isSaved,
      isSentToGateway: isSentToGateway === undefined ? lastParams.isSentToGateway : isSentToGateway, 
      amount: amount === undefined ? lastParams.amount : amount
  };
  ...
}
Lior Hai
  • 477
  • 2
  • 5
0

In order to get ride of this and make your function more generic, You should provide default arguments to the function so that when you don't pass any the oject creation here

   serviceID: service,
   terminalID: terminal,
   amountName: account,
   amount: amount,
   isSaved: isSaved,
   isSentToGateway: isSentToGateway,
 };

will not break. To do that you use the below format

export default async function addUserLogs(account="default", service = "default", terminal = "default value",isSaved = "default value",isSentToGateway = "default value",amount = "default Value" ) 

provide default only for variables that are not going to be required in other places when called.

Note when you call this function and provide params they will replace the default but when you don't the function will use the default as such your function doesn't break with undefined variables

harisu
  • 1,376
  • 8
  • 17
0

in that example you can set null for some arguments that is not required and you don't want to use them when callig your function.

you can use some filters for your arguments like so:

export default async function addUserLogs(account, service, terminal,isSaved,isSentToGateway,amount ) {
  const obj = {};

  if(account) obj.amountName = account;
  if(service) obj.serviceID = service;
  if(terminal) obj.terminalID = terminal;
  if(isSaved !== null) obj.isSaved = isSaved;
  if(isSentToGateway !== null) obj.isSentToGateway = isSentToGateway;
  if(amount) obj.amount = amount;

  const db = await InitDb();
  const tx = db.transaction('userLogs', 'readwrite');
  const store = tx.objectStore('userLogs');
  const index = await store.put(obj);
  let params = {};
  if (window.localStorage.getItem('params') !== null) {
    params = JSON.parse(window.localStorage.getItem('params'));
  }
  window.localStorage.setItem(
    'params',
    JSON.stringify({ ...params, userLogIndex: index })
  );
} ```