diff --git a/backend/package-lock.json b/backend/package-lock.json index 73a9f77..8f2909e 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "MIT", "dependencies": { + "cors": "^2.8.5", "dotenv": "^16.4.5", "express": "^4.19.2", "mongoose": "^8.5.4" @@ -247,6 +248,19 @@ "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==", "license": "MIT" }, + "node_modules/cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "license": "MIT", + "dependencies": { + "object-assign": "^4", + "vary": "^1" + }, + "engines": { + "node": ">= 0.10" + } + }, "node_modules/debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -960,6 +974,15 @@ "node": ">=0.10.0" } }, + "node_modules/object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/object-inspect": { "version": "1.13.2", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.2.tgz", diff --git a/backend/package.json b/backend/package.json index 86653d6..49d4d58 100644 --- a/backend/package.json +++ b/backend/package.json @@ -10,6 +10,7 @@ "author": "Jason", "license": "MIT", "dependencies": { + "cors": "^2.8.5", "dotenv": "^16.4.5", "express": "^4.19.2", "mongoose": "^8.5.4" diff --git a/backend/server.js b/backend/server.js index 6b85673..e797b10 100644 --- a/backend/server.js +++ b/backend/server.js @@ -1,4 +1,5 @@ const express = require('express'); +const cors = require('cors'); require('dotenv').config(); const port = process.env.PORT || 5000; const connectDB = require('./config/db'); @@ -16,6 +17,13 @@ app.get('/', (req, res) => { res.json({ message: 'Welcome to the RandomIdeas API' }); }); +//cors middleware +app.use( + cors({ + origin: ['http://localhost:5000', 'http://localhost:5173'], + }) +); + //registering the routes const ideasRouter = require('./routes/ideas'); app.use('/api/ideas', ideasRouter); diff --git a/README.md b/frontend/README.md similarity index 100% rename from README.md rename to frontend/README.md diff --git a/components/IdeaForm.js b/frontend/components/IdeaForm.js similarity index 63% rename from components/IdeaForm.js rename to frontend/components/IdeaForm.js index 4603dcb..081eab2 100644 --- a/components/IdeaForm.js +++ b/frontend/components/IdeaForm.js @@ -1,6 +1,11 @@ +import IdeasApi from '../services/ideasApi'; +import IdeaList from './IdeaList'; + class IdeaForm { constructor() { this._formModal = document.querySelector('#form-modal'); + //instantiating the idea list since it is a class + this._ideaList = new IdeaList(); } //add all event listeners in a function @@ -8,9 +13,22 @@ class IdeaForm { this._form.addEventListener('submit', this.handleSubmit.bind(this)); } - handleSubmit(e) { + async handleSubmit(e) { + //made this function async because we are dealing with a promise (axios) e.preventDefault(); //stops the submit and allows us to determine when it should run + if ( + !this._form.elements.text.value || + !this._form.elements.tag.value || + !this._form.elements.username.value + ) { + alert('Please enter all fields'); + return; + } + + //save user to local storage + localStorage.setItem('username', this._form.elements.username.value); + //grabs values from the form const idea = { text: this._form.elements.text.value, @@ -18,10 +36,17 @@ class IdeaForm { username: this._form.elements.username.value, }; + //add idea to database + const newIdea = await IdeasApi.createIdea(idea); + + //add idea to list and call render on the DOM + this._ideaList.addIdeaToList(newIdea.data.data_base); + //clear form fields after submit this._form.elements.text.value = ''; this._form.elements.tag.value = ''; this._form.elements.username.value = ''; + this.render(); //using the dispatch event method of the document object to dispatch a custom event //to the Modal from the IdeaForm, telling the Modal to close the form on submit @@ -29,12 +54,18 @@ class IdeaForm { } //create a render function like react that will output the HTML + //adding a turnary to check for username and saved in localstorage render() { this._formModal.innerHTML = `