4.0 KiB
4.0 KiB
Building Out The Frontend
Clearing the Vite Boilerplate
- Delete the
src
folder and thepublic
folder. - Create a new
src
folder and a newpublic
folder. - Create a new
main.jsx
file in thesrc
folder and add the following boilerplate code.
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App'
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<App />
</React.StrictMode>,
)
- Create a new
App.jsx
file in thesrc
folder and add the following boilerplate code.
function App() {
return (
<div>Hello</div>
)
}
export default App
Integrating TailwindCSS
- Install TailwindCSS and PostCSS in your
frontend
directory.
npm install -D tailwindcss postcss autoprefixer
- Create a new
postcss.config.cjs
file in the root of the project and add the following code.
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
- Create a new
tailwind.config.cjs
file in the root of the project and add the following code.
module.exports = {
purge: ['./src/**/*.{js,jsx,ts,tsx}', './public/index.html'],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {},
},
variants: {
extend: {},
},
plugins: [],
}
- Create a new
index.css
file in thesrc
folder and add the following code.
@tailwind base;
@tailwind components;
@tailwind utilities;
- Update the
App.jsx
file to be as follows.
function App() {
return (
<div className="bg-gray-100 h-screen flex justify-center items-center">
<div className="bg-white rounded-lg shadow-lg p-8">
<h1 className="text-2xl font-bold mb-4">Welcome to the Budget App</h1>
<p className="text-gray-500 mb-4">This is a simple app that will help you categorize your expenses.</p>
<p className="text-gray-500 mb-4">Upload a CSV file and we will do the rest.</p>
<div className="flex justify-center">
<button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
Upload CSV
</button>
</div>
</div>
</div>
)
}
- You should now see a basic app with a button that says "Upload CSV". We can now start building out the functionality of the app and any styling can continu to be done with TailwindCSS.
Adding File Upload Functionality
- Import useState into
App.jsx
and add a state variable calledfile
that will hold the file that is uploaded.
import { useState } from 'react'
- Add the following code to the
App
function.
const [file, setFile] = useState(null)
- Add a file selector input just above the
UploadCSV
button.
<input type="file" onChange={(e) => setFile(e.target.files[0])} />
- Add an onClick attribute to the
UploadCSV
button and add a callback function to handle the upload. - Next, define the callback function that will handle the upload in between your useState and return statements. For now, we will just console log the file name.
console.log(file.name)
- You should now be able to select a file and see the file name in the console. We can now start building out the backend to handle the file upload to the server.
- Next we will make a form to handle the request to the backend server. Replace the console.log statement with the following code.
const formData = new FormData()
formData.append('file', file)
- Next, we will make a POST request to the backend server. Make the following fetch request.
fetch('/api/upload', {
method: 'POST',
body: formData,
})
- fetch is a network request that returns a promise. We can use the promise to handle the response from the server. Add the following code to handle the response.
.then((res) => res.json())
.then((data) => console.log(data))
- You should now be able to upload a file and see the response in the console. We can now start building out the backend to handle the file upload to the server.