djbowen95 / Hit-the-Bucket

Front-end BUCKET LIST application built with REACT. Users can add, update or delete items from a bucket list - which persists when they refresh or revisit the page. Uses STATE, HOOKS, COMPONENTS, PROPS, conditional rendering, forms and event handling. Built from starter code.
https://djbowen95.github.io/Hit-the-Bucket/
MIT License
0 stars 0 forks source link

Write logic to add a bucket list item #1

Closed djbowen95 closed 1 year ago

djbowen95 commented 1 year ago

This is the first criteria that needs to be solved from the brief:

It's done when I write logic to add a bucket-list item in components/BucketList.js.

To do this, I need to:

djbowen95 commented 1 year ago

Setup

Before I started working on this again, I found there was an issue when I cloned the repository, ran npm install, and then subsequently ran npm run start: I was presented with the following error:

 [ 'error:03000086:digital envelope routines::initialization
 error' ],
  library: 'digital envelope routines',
  reason: 'unsupported',
  code: 'ERR_OSSL_EVP_UNSUPPORTED'
}

This is an issue from the starter code (which is several years old): I researched this online and found it is an issue to do with webpack. I decided the easiest / fastest solution would be to create a new react application and install the most recent version of the packages. With the package.json updated; React now successfully launched, and I set out to manually test the starter code and make sure there were no new errors from this approach.

The application would not immediately run - but this was because of problems in the starter code itself. This starter code was incomplete / had several components that called missing functions, and this prevented the application from launching at all. To quickly fix this / run everything present in the starter code, I commented out code importing every component which crashed the application, and reintroduced the files only when I had commented out any application breaking code.

djbowen95 commented 1 year ago

As I needed these changes begin work, I made all of these changes on the main branch: I also carried on and resolved the present issue and the following issue, ( #5 ), on the main branch, in order to have a basic working application before adding features.

djbowen95 commented 1 year ago

Add to a bucket list stored in state

To solve the issue itself, I first needed to create a new function, addBucketList, that would take a new bucket list item and add it to an array stored in state. To build this function, I:

Each time the button was pressed, an additional "Its Me" appeared on the page: this meant that each time my function was run, it was successfully adding an additional string value to the array stored in state.

djbowen95 commented 1 year ago

To test this new function would continue to add changing values to the array, I created a simple helper function. This function returns a random string from an array (a new bucket list item) with six different possible values it might return.

const getNewItem = () => {
    // Array of items that could be on a bucket list.
    const items = ["Buy Things", "Do Things", "See a Dolphin", "Eat a Cake", "Go Scuba Diving", "Pay Off Mortgage"];
    // Random number to select an item from the array.
    const position = Math.floor(Math.random()*6);
    // Returns a random item to be added to the list.
    return items[position];
  } 

In the addBucketItem function, I replaced the string with a call of this function: setBucket([...bucket, getNewItem()]);

Each time the button was clicked, it would add a new value to the bucket - and these new values would display immediately on the rendered component.

djbowen95 commented 1 year ago

Integrating with the bucket list form

I needed the state modifying function I had just built to be run when the form was submitted, so a user could add their own bucket list items. To integrate this new function with the BucketList component from the starter code, I:

I now considered this issue resolved: and moved to related issue #5 .