Coder Social home page Coder Social logo

igooralm192 / liven-to-buy Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 7.42 MB

Virtual shop application developed for https://liven.tech training week.

Home Page: https://liventobuy.netlify.app

JavaScript 0.13% HTML 0.50% TypeScript 74.23% CSS 25.14%
typescript react react-router redux redux-thunk formik yup date-fns react-icons text-mask

liven-to-buy's Introduction

Project Logo

React JavaScript Redux Figma

๐Ÿš€ Liven's online store ๐Ÿ’ฅ

Project Design 1 Project Design 2

Project Design 3

Summary

๐Ÿ”– About

Liven to buy is an application that simulates a virtual store, where it is possible to add fake products to a cart and make a fake purchase.

This application was developed during @liven training week.

๐Ÿ–ผ๏ธ Layout

The layout of this application is available on Figma.

๐Ÿ’ป Demo

This application was hosted by Netlify and can be found here: Liven to Buy.

๐Ÿš€ Technologies

This application uses the following technologies:

๐Ÿ“– Learnings

  • Handling forms

Thanks to Formik and Yup, the management and validation of forms became easy to carry out the manipulation of the input data.

const { values, errors, handleChange, submitForm } = useFormik({
  initialValues: {
    email: '',
    password: '',
  },
  validationSchema: Yup.object({
    email: Yup.string().email('Invalid email.').required('Required field.'),
    password: Yup.string().required('Required field.'),
  }),
  onSubmit: ({ email, password }) => handleSubmitForm(email, password),
  validateOnChange: false,
})
  • Sharing logics

With the hook useProducts it was possible to reuse the logic of ordering the products at the beginning of the application.

const useProducts = (): {
  productsById: { [key: string]: Product }
  productsList: Product[]
} => {
  const dispatch = useDispatch()

  const productsById = useSelector((state: AppState) => state.products.byId)

  const productsList = useMemo(
    () => Object.keys(productsById).map(key => productsById[key]),
    [productsById],
  )

  useEffect(() => {
    if (Object.keys(productsById).length > 0) return

    dispatch(getProducts())
  }, [dispatch, productsById])

  return {
    productsById,
    productsList,
  }
}
  • Sharing simple states

With the contexts, it was possible to use a simpler way of sharing states between the components.

export const NotificationContext = React.createContext<NotificationData>(
  {} as NotificationData,
)

export const NotificationProvider: React.FC = ({ children }) => {
  const [notification, setNotification] = useState<Notification>({
    open: false,
    title: '',
    description: '',
  })

  const showNotification = useCallback((title: string, description: string) => {
    setNotification({
      open: true,
      title,
      description,
    })
  }, [])

  const hideNotification = useCallback(() => {
    setNotification(oldNotification => ({
      ...oldNotification,
      open: false,
    }))
  }, [])

  return (
    <NotificationContext.Provider
      value={{ notification, showNotification, hideNotification }}
    >
      {children}
      <Notification />
    </NotificationContext.Provider>
  )
}

const useNotification = (): NotificationData => {
  const context = useContext(NotificationContext)

  if (!context) {
    throw new Error(
      'useNotification must be used within a NotificationProvider',
    )
  }

  return context
}
  • Memorized values

With the hook useMemo, it was possible to create a memorized value for a variable and this value will only change if one of its dependencies changes.

const useCart = (): {
  // ...
} => {
  // ...

  const {
    products: selectedProducts,
    // ...
  } = useSelector((state: AppState) => state.cart)

  const { productsById } = useProducts()

  const cartProducts: Product[] = useMemo(() => {
    return selectedProducts.map(selectedProduct => ({
      ...productsById[selectedProduct.id],
      quantity: selectedProduct.quantity,
    }))
  }, [selectedProducts, productsById])

  const haveCartProducts = useMemo(() => cartProducts.length > 0, [
    cartProducts,
  ])

  // ...
}

๐Ÿ’ข Difficulties

  • Name HTML tags and CSS classes
  • Understand the flow of Redux Thunk
  • Handling input date
  • Handle cart loading on session
  • View purchase summary page

Made with ๐Ÿ’š by Igor Almeida

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.