GithubHelp home page GithubHelp logo

treyway's Introduction

fixx

Summary

Main logic in 86 idiomatic lines of code
No third party library: I previously worked with React Hook Form, although for this job, I achieved the spec requirements and further without it.
Built on Hooks: controlled navigation progress with state persistence.
Live link ๐Ÿ‘ˆ onClick

How to get started

  1. Open terminal
  2. Run git clone https://github.com/vercetti11/treyway.git
  3. Run cd treyway
  4. Run npm i
  5. Run yarn start

Usage

User can only advance until the last unsubmitted page.
To advance further: fill the fields, refill if necessary, hit submit button.
After validation user sees next page, but he can go back to a previous page and change input values(they persist).

Tests

Run yarn testin a new terminal window.
Tests are functional and made from a user point of view, just as the spec:

  • Show one page of the form at a time.
  • Show the current page position of the form.
  • Validate the input fields upon submission of each page.
  • If there are any validation errors, show an error message in the form and block progress.
  • Extend the app

    Change configuration of a page

    With the current design, each form page is a component, therefore its a matter of editing the corresponding element.

    How is navigation implemented

    Mapping over formPages renders three buttons, one for each element in the array.
    User clicks on a button to navigate, and the button sets a new state using the index from mapping over formPages.
    Next, a switch statement assesses what component to render depending on the initial value (0) or the index passed from the previously clicked button.
    Notice how this is a lowly coupled design.

    const formPages = ["User", "Privacy", "Done"];
    const [activePage, setActivePage] = useState(0);
    const [progress, setProgress] = useState(0);
    
    {formPages.map((page, index) => (
        <button
          key={page}
          disabled={progress < index}
          onClick={() => setActivePage(index)}
          className={page === formPages[activePage] ? "active" : ""}
        >
          {page}
        </button>
      ))}
      // const [activePage, setActivePage] can use the strings inside the formPages array  
      // instead of their indexes, but then the cases in the switch would also depend on 
      // strings which are more cumbersome to write. Legibility is traded for extensibility.
      
      {...}
      
       switch (activePage) {
        case 0:
            return <FormPageUser/>
        case n:
            return <FormPageN/>
       }

    Add a new page

    In App.js do the following:

    1. Insert name of the new page in const formPages = ["User", "New Page", "Privacy", "Done"];
    2. Wire up a component in the Switch statement with the appropriate index and props.
    3. Implement mentioned component as needed.

    Thoughts

    I can improve the app with better validation messages by using react-hook-form.

    State can be stored to localstorage to persist an accidental reload; alternatively, accidental reload can be prevented by setting an observer on beforeunload event.

    React Hook Form praises it's superiority to Redux Form library, but it doesn't mean it is against Redux. The approach I take using react-hook-form with Redux is to dispatch actions on submit instead of <input onChange.

    All the manual wiring up of components can be abstracted, but that leads to overengineering in this particular case.

    Programing it's like gardening.

    Thank you! ๐Ÿ™Œ

    treyway's People

    Contributors

    vercetti11 avatar

    Stargazers

    Roman avatar

    Watchers

     avatar

    Recommend Projects

    • React photo React

      A declarative, efficient, and flexible JavaScript library for building user interfaces.

    • Vue.js photo Vue.js

      ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

    • Typescript photo Typescript

      TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

    • TensorFlow photo TensorFlow

      An Open Source Machine Learning Framework for Everyone

    • Django photo Django

      The Web framework for perfectionists with deadlines.

    • D3 photo D3

      Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

    Recommend Topics

    • javascript

      JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

    • web

      Some thing interesting about web. New door for the world.

    • server

      A server is a program made to process requests and deliver data to clients.

    • Machine learning

      Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

    • Game

      Some thing interesting about game, make everyone happy.

    Recommend Org

    • Facebook photo Facebook

      We are working to build community through open source technology. NB: members must have two-factor auth.

    • Microsoft photo Microsoft

      Open source projects and samples from Microsoft.

    • Google photo Google

      Google โค๏ธ Open Source for everyone.

    • D3 photo D3

      Data-Driven Documents codes.