Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding admin page #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

adding admin page #2

wants to merge 2 commits into from

Conversation

snlaight
Copy link
Collaborator

This PR adds in the admin page to the project, as well as the initial functionality of the admin page.

The admin page allows a user to create new products, which are then added to the list of products a user can choose from.

Comments
This PR only creates the product and pushes it to the products array, which is currently only visible via the console. It also automatically clears all the fields of the form as soon as the product is submitted. The products are shown in the admin page underneath the form submission -- if the user interacts with one of the inputs (for example, select and enter a space) the new product will be added to the list on the page.

LEFT TO COMPLETE
• Clean up form styles and location (this initial commit inserts a simple form, without much style or placement, to test functionality)
• Persist products list on indexedDB, so products still exist after refreshing the page.
• add method to dynamically update the product list shown on the admin page as soon as it is entered.
• add functionality to be able to edit / delete all of the products created from the /admin page, using the admin flag which already gets inserted.

pages/admin.js Outdated
import { ListProductComponent } from "../components/product/list";
import { useState, useEffect } from "react";

let clearPage = () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

direct dom interaction is usually frowned upon in the react world. you can use the useRef to target an element in dom and interact with it which is better compatible with react.

const [productPrice, setPrice] = useState("");
const [productDescription, setDescription] = useState("");

const submitNewProduct = (e) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrapping these in useCallback is always a good idea because otherwise, react will constantly recreate this definition whenever something in the state changes.
Also, this type of helper function should be declared outside of the component. the more logic you can take out of the component and isolate elsewhere, the cleaner and easier to understand your components will be.

@snlaight snlaight added enhancement New feature or request help wanted Extra attention is needed labels Jul 16, 2021
@snlaight
Copy link
Collaborator Author

This update changes how useState and useEffect are used , effectively adding the product to the DB and rendering the page again to automatically show it on the admin list of items in the product shop.

Would like a review on this, since I am using the clearPage() to update the state of setProduct simply by clearing the inputs on the page. This works, but I don't think in the real world would be effective to use.

@@ -1,10 +1,17 @@
import Dexie from "dexie";
import Dexie, { DBCoreRangeType } from "dexie";
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was imported on accident, will remove from the code now.

@snlaight snlaight self-assigned this Jul 16, 2021
});
db.version(3).stores({
cartItems: `name, price, description, image, count`,
products: `name, price, description, image, flag`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why you need 2 different versions with the same definition of database?


export default function Admin() {
console.log(Products)
let [product, setProduct] = useState({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you're not familiar with the useReducer hook, please look that up. that hook is more suitable for this kind of advanced data structure of states.

description: product.description,
price: priceNumber,
image: productImage.toString(),
flag: admin,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like 35-39 is the exact same as line 42-46. whenever you see duplication like this, you should refactor the code into a function.

})
};

useEffect(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what purpose is this empty useEffect serving?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants