PaulPatterson48 / INDIVIDUAL-ASSIGNMENT-Team-Roster

0 stars 0 forks source link

Create Members members object #9

Open PaulPatterson48 opened 11 months ago

PaulPatterson48 commented 11 months ago

User Story

GIVEN: as an authenticated user, when I create a member, the member object should include my uid.

Acceptance Criteria

add uid to the PropType obj.. .

Dependecies

initial state in for form

Dev Notes

import React, { useEffect, useState } from 'react'; import { useRouter } from 'next/router'; import PropTypes from 'prop-types'; import FloatingLabel from 'react-bootstrap/FloatingLabel'; import Form from 'react-bootstrap/Form'; import { Button } from 'react-bootstrap'; import { useAuth } from '../../utils/context/authContext'; import { getAuthors } from '../../api/authorData'; import { createBook, updateBook } from '../../api/bookData';

const initialState = { description: '', image: '', price: '', sale: false, title: '', };

function BookForm({ obj }) { const [formInput, setFormInput] = useState(initialState); const [authors, setAuthors] = useState([]); const router = useRouter(); const { user } = useAuth();

useEffect(() => { if (obj.firebaseKey) setFormInput(obj); getAuthors(user.uid).then(setAuthors); }, [obj, user]); // needed to mount the obj and the user

useEffect(() => { getAuthors(user.uid).then(setAuthors);

if (obj.firebaseKey) setFormInput(obj);

}, [obj, user]);

const handleChange = (e) => { const { name, value } = e.target; setFormInput((prevState) => ({ ...prevState,

}));

};

const handleSubmit = (e) => { e.preventDefault(); if (obj.firebaseKey) { updateBook(formInput).then(() => router.push(/book/${obj.firebaseKey})); } else { const payload = { ...formInput, uid: user.uid }; createBook(payload).then(({ name }) => { const patchPayload = { firebaseKey: name }; updateBook(patchPayload).then(() => { router.push('/books'); }); }); } };

return (

{obj.firebaseKey ? 'Update' : 'Create'} Book

{/* TITLE INPUT */} BookForm.propTypes = { obj: PropTypes.shape({ description: PropTypes.string, image: PropTypes.string, price: PropTypes.string, sale: PropTypes.bool, title: PropTypes.string, author_id: PropTypes.string, firebaseKey: PropTypes.string, }), }; BookForm.defaultProps = { obj: initialState, }; export default BookForm;