PricingBlog

Input value not updating on API fetch

  • tomthebigtree-1275732847038173196

    Tom Ireland

    1 year ago

    I have an input element where the value attribute is data from an API call to the database. When the page loads, the data is fetched and the value of the input is set - all good.

    I have a change event that updates the database record with the changed value and on success or error, it will re-fetch the records.

    When re-fetching the records, on error, the value of the input is not updated. To test, I stored the API data in a variable which is set when fetch is called and that data is updating, so it would suggest that 'On error' in the API call is picking up the fact there is an error, however I cannot figure out why the value is not updating.

    Any clues as to what I'm missing?

    I have a short Loom uploading, so can share if that helps visualise things when it's ready.
    1
  • alexsiale_17363-1275739136807931927

    Alex

    1 year ago

    Keen to see the loom to understand better. If there's an error, wouldn't there be nothing to update?
  • tomthebigtree-1275739500605214814

    Tom Ireland

    1 year ago

    That's right, nothing gets updated in the database, so the original value still remains and should be available upon re-fetching the data, so - in theory - should repopulate the input value with the value from the database.
  • Still waiting for Loom... 🤔
  • max.kayr-1275741128141967413

    Max

    1 year ago

    Waiting patiently for loom
    😂1
  • tomthebigtree-1275741185511784520

    Tom Ireland

    1 year ago

    Me too.
  • alexsiale_17363-1275741318844256266

    Alex

    1 year ago

    haha we all are
  • tomthebigtree-1275743740488716298

    Tom Ireland

    1 year ago

    Uploading to YT unlisted instead. 😄
  • tomthebigtree-1275745282889617408

    Tom Ireland

    1 year ago

  • plusmin-1275746723318464542

    Armand

    1 year ago

    Watched the video and also not sure what it could be. And I agree that it might be something in the nature of how inputs work.
  • chrislaupama-1275751076234268760

    Chris Laupama

    1 year ago

    Maybe try moving the fetch outside of the API call and have it part of the event instead.

    Given that this is triggering based on error or success could be part of the issue here, but given that you are doing it regardless of if you are successful or not could help, it's just the timing is the only issue... hmmm
  • max.kayr-1275751086011191378

    Max

    1 year ago

    The input is not updating because it's value has not changed. I'd change the variable for your inputs directly on change. That way the values go back to the initial value
  • I'm in my phone at the moment, so not 100% sure 😬
  • tomthebigtree-1275756967587090443

    Tom Ireland

    1 year ago

    Thanks for the input, gents. At work now but pondering what you mentioned, it got me thinking about how forms would usually work i.e. if you submit something that's not right an error is returned and the form is not submitted. Maybe I just need an error to be displayed upon error from the API to say that the field cannot be empty instead. Or maybe just prevent update if it's empty and show an error client-side and leave the db constraint as a fallback, which is normally the best way to ensure that data is valid and cannot be accidentally changed incorrectly if something in the UI client-side doesn't validate it properly.
  • tomthebigtree-1275772431746863117

    Tom Ireland

    1 year ago

    Cracked it thanks to your advice, @Max . I ended up setting the data array variable to map a new object, then a conditional to find and set the object updated and keep the rest i.e. the array is the same but with the value of one item updated. This means that on change the value is updated to whatever it is e.g. new value or empty string, which then updates on fetch. Just need to set some error logic for empty strings so that the user knows why it has changed back to the original.
    💪2
  • tomthebigtree-1275775089815715934

    Tom Ireland

    1 year ago

    👍1
  • andreasmoller-1275784029819637850

    Andreas Møller

    1 year ago

    Yes Max is right This is a known issue, but due to the way toddle works, it is really hard to fix.

    We have has some internal conversations about moving to the new javascript signals (https://github.com/tc39/proposal-signals) which I believe would fix it, but it is not a simple change.
  • tomthebigtree-1275789203128713217

    Tom Ireland

    1 year ago

    A bit over my head but good that this is being talked about and there are ideas on how to solve it. In my case, it's just remembering this behaviour for now and working around things to keep stuff in check. 🤞
  • andreasmoller-1275789350290063503

    Andreas Møller

    1 year ago

    Mine too @Jacob Kofoed Will fix it
    😄1
  • tomthebigtree-1275789429046382592

    Tom Ireland

    1 year ago

    Well delegated. 😄
  • lucasg-1275792286088958104

    Lucas G

    1 year ago

    Between Jacob, son of Odin, and the god of code himself, we can do anything
  • lucasg-1275792543635734528

    Lucas G

    1 year ago

    (I still think Jacob has some Hemsworth resemblances) lol