0

I have 4 buttons that when clicked are supposed to display the current time (for check in, out, lunch in, out). I'm using a Fetch API to record these times and I'm trying to use a switch case so it'll diferentiate between activities and record the activity type. Althought it is not giving me an error message, it doesn't really do anything either.

Here's what I'm working with:

function handleClick(elementName) {

    var fieldName
    switch (elementName) { // #lblCheckin, #lblCheckout, ......
        case "#lbl-checkin":
            fieldName = 'checkInTime'
            break;
            
            case "#lbl-lunchout":
            fieldName = 'lunchOutTime'
            break;

            case "#lbl-lunchin":
            fieldName = 'lunchInTime'
            break;

            case "#lbl-checkout":
            fieldName = 'checkOutTime'
            break;
    
        default:
            throw new Error('Nao foi possivel armazenar o tipo de evento')
    }

    var label = document.querySelector(elementName)
    var now = new Date()
    var currentTime = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds()
    label.textContent = currentTime
    submitValues(fieldName, currentTime);
}


function submitValues(currentFieldName, currentTimeValue) {
    fetch('https://func-kxp-checkin-api-dev-01.azurewebsites.net/api/createCheckin', {
        method: 'POST',
        body: JSON.stringify({
            currentFieldName: currentTimeValue
        }),
        headers: {
          'Content-Type': 'application/json'
        }
      })
        .then(response => response.json())
        .then(data => console.log(data))
    }      

This is what the API displays:

[
  {
    "currentFieldName": "17:32:30",
    "date": "2023-01-04"
  },
  {
    "currentFieldName": "17:35:11",
    "date": "2023-01-04"
  },
  {
    "currentFieldName": "17:40:48",
    "date": "2023-01-04"
  },
  {
    "currentFieldName": "17:40:49",
    "date": "2023-01-04"
  },
  {
    "currentFieldName": "17:40:50",
    "date": "2023-01-04"
  }
]

Any ideas on how to get it working properly?

One thing to note is that VSCode states that this highlighted part of the code is there but it's not being read:

function submitValues(**currentFieldName**

I'm trying to apply a switch case to my API but nothing happens.

maj1n
  • 1
  • 2
  • Show how you're calling the function. We need to see how you're passing the `elementName` argument. I suspect it doesn't actually begin with `#`. – Barmar Jan 04 '23 at 20:51
  • 1
    `{currentFieldName: currentTimeValue}` should be `[currentFieldName]: currentTimeValue}`. You need the brackets to make the property name dynamic. – Barmar Jan 04 '23 at 20:53

0 Answers0