Open PaulPatterson48 opened 11 months ago
GIVEN: as an authenticated user, when I create a member, the member object should include my uid.
add uid to the PropType obj.. .
initial state in for form
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'); }); }); } };
/book/${obj.firebaseKey}
return (
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);
}, [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 (