wasp/web/docs/tutorials/dev-excuses-app/04-updating-main-page-js-file.md
Max Khamrovskyi 75245747d2
Docs update for Hacktoberfest (#731)
* Initial docs update for Hacktoberfest

* Contributing.md update

* Update "Before you begin" section

* Update web/docs/tutorials/dev-excuses-app/creating-the-project.md

Co-authored-by: Martin Šošić <Martinsos@users.noreply.github.com>

* Update web/docs/tutorials/dev-excuses-app/updating-main-page-js-file.md

Co-authored-by: Martin Šošić <Martinsos@users.noreply.github.com>

* Update CONTRIBUTING.md

Co-authored-by: Martin Šošić <Martinsos@users.noreply.github.com>

* Update web/docs/tutorials/dev-excuses-app/modifying-main-wasp-file.md

Co-authored-by: Martin Šošić <Martinsos@users.noreply.github.com>

* Update the section names

* Update CONTRIBUTING.md

Co-authored-by: Matija Sosic <matija.sosic@gmail.com>

* Update web/docs/tutorials/dev-excuses-app/updating-main-page-js-file.md

Co-authored-by: Matija Sosic <matija.sosic@gmail.com>

* Update web/docs/contributing.md

Co-authored-by: Matija Sosic <matija.sosic@gmail.com>

* Update web/docs/contributing.md

Co-authored-by: Matija Sosic <matija.sosic@gmail.com>

* Update web/docs/contributing.md

Co-authored-by: Matija Sosic <matija.sosic@gmail.com>

* Add Discord component and some minor fixes

* Add unclear notice

* Update web/docs/pick-a-tutorial.md

Co-authored-by: Matija Sosic <matija.sosic@gmail.com>

* Update web/docs/tutorials/dev-excuses-app.md

Co-authored-by: Matija Sosic <matija.sosic@gmail.com>

* Update web/docs/tutorials/dev-excuses-app.md

Co-authored-by: Matija Sosic <matija.sosic@gmail.com>

* Update web/docs/tutorials/dev-excuses-app.md

Co-authored-by: Matija Sosic <matija.sosic@gmail.com>

* Update CONTRIBUTING.md

Co-authored-by: Matija Sosic <matija.sosic@gmail.com>

* Update web/docs/tutorials/dev-excuses-app/modifying-main-wasp-file.md

Co-authored-by: Matija Sosic <matija.sosic@gmail.com>

* Update web/docs/getting-started.md

Co-authored-by: Matija Sosic <matija.sosic@gmail.com>

* Update web/docs/tutorials/dev-excuses-app/modifying-main-wasp-file.md

Co-authored-by: Matija Sosic <matija.sosic@gmail.com>

* Review updates

* Update web/docs/tutorials/dev-excuses-app/modifying-main-wasp-file.md

Co-authored-by: Matija Sosic <matija.sosic@gmail.com>

* Update suggestion

* Update web/docs/tutorials/dev-excuses-app/perform-migration-and-run.md

Co-authored-by: Matija Sosic <matija.sosic@gmail.com>

* Update web/docs/tutorials/dev-excuses-app/perform-migration-and-run.md

Co-authored-by: Matija Sosic <matija.sosic@gmail.com>

* Update

* Update web/docs/tutorials/dev-excuses-app/updating-main-page-js-file.md

Co-authored-by: Matija Sosic <matija.sosic@gmail.com>

* Update CONTRIBUTING.md

Co-authored-by: Matija Sosic <matija.sosic@gmail.com>

* Remove lines

* Update CONTRIBUTING.md

Co-authored-by: Matija Sosic <matija.sosic@gmail.com>

* Update CONTRIBUTING.md

Co-authored-by: Matija Sosic <matija.sosic@gmail.com>

* Update CONTRIBUTING.md

Co-authored-by: Matija Sosic <matija.sosic@gmail.com>

* Update getExcuse function

* Update page naming convention

* Remove Main.css

* Update migration text

* Update image

* Update "Wasp's compiler is built with Haskell"

* Add tutorials description

* Update web/docs/pick-a-tutorial.md

Co-authored-by: Matija Sosic <matija.sosic@gmail.com>

* Update Discord link and tutorial ending

* Update links

Co-authored-by: mkhamrov <mkhamrov@redhat.com>
Co-authored-by: Martin Šošić <Martinsos@users.noreply.github.com>
Co-authored-by: Matija Sosic <matija.sosic@gmail.com>
2022-09-30 15:31:09 +03:00

2.6 KiB
Raw Blame History

id title
04-updating-main-page-js-file Updating MainPage.js file

import useBaseUrl from '@docusaurus/useBaseUrl';

This is the most complex part, but it really comes down to mostly writing React. To make our life easier - lets erase everything we had in the MainPage.js file and substitute it with our new UI markup.

import React, { useState } from 'react'
import { useQuery } from '@wasp/queries'
import getExcuse from '@wasp/queries/getExcuse'
import getAllSavedExcuses from '@wasp/queries/getAllSavedExcuses'
import saveExcuse from '@wasp/actions/saveExcuse'

const MainPage = () => {
  const [currentExcuse, setCurrentExcuse] = useState({ text: "" })
  const { data: excuses } = useQuery(getAllSavedExcuses)

  const handleGetExcuse = async () => {
    try {
      setCurrentExcuse(await getExcuse())
    } catch (err) {
      window.alert('Error while getting the excuse: ' + err.message)
    }
  }

  const handleSaveExcuse = async () => {
    if (currentExcuse.text) {
      try {
        await saveExcuse(currentExcuse)
      } catch (err) {
        window.alert('Error while saving the excuse: ' + err.message)
      }
    }
  }

  return (
    <div className="grid grid-cols-2 text-3xl">
      <div>
          <button onClick={handleGetExcuse} className="mx-2 my-1 p-2 bg-blue-600 hover:bg-blue-400 text-white rounded"> Get excuse </button>
          <button onClick={handleSaveExcuse} className="mx-2 my-1 p-2 bg-blue-600 hover:bg-blue-400 text-white rounded"> Save excuse </button>
        <Excuse excuse={currentExcuse} />
      </div>
      <div>
        <div className="px-6 py-2 bg-blue-600 text-white"> Saved excuses: </div>
        {excuses && <ExcuseList excuses={excuses} />}
      </div>
    </div>
  )
}

const ExcuseList = (props) => { 
  return props.excuses?.length ?  props.excuses.map((excuse, idx) => <Excuse excuse={excuse} key={idx} />) : 'No saved excuses'
}

const Excuse = ({ excuse }) => {
  return (
    <div className="px-6 py-2">
      {excuse.text}
    </div>
  )
}

export default MainPage

Our page consists of three components: MainPage, ExcuseList, and Excuse. It may seem at first that this file is pretty complex, but let's take a closer look:

Excuse is just a div with an excuse text, ExcuseList checks if there are any excuses. If the list is empty show a message No saved excuses. In other case excuses will be displayed.

MainPage contains info about the current excuses and the list of already saved excuses. Two button click handlers are handleGetExcuse and handleSaveExcuse. Plus, the markup itself with some Tailwind flavor.