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

Set focus on the ManageList's inputs when users navigate to them using the List's buttons #78

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function App() {
}
/>
<Route
path="/manage-list"
path="/manage-list?/:param"
element={
<ManageList
data={data}
Expand Down
8 changes: 7 additions & 1 deletion src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ export async function shareList(listPath, currentUserId, recipientEmail) {
return { code: 'ok' };
}

const userIsListOwner = (id, path) => {
return path.includes(id);
};

/**
* Delete a user's list only if the current user is the list owner.
* @param {string} userId The id of the user who owns the list.
Expand All @@ -184,7 +188,7 @@ export async function shareList(listPath, currentUserId, recipientEmail) {
*/
export async function deleteList(userId, userEmail, listPath, listId) {
// Check if current user is owner.
if (!listPath.includes(userId)) {
if (!userIsListOwner(userId, listPath)) {
const usersCollectionRef = collection(db, 'users');
const userDocumentRef = doc(usersCollectionRef, userEmail);
const userSharedLists = (await getDoc(userDocumentRef)).data().sharedLists;
Expand All @@ -199,6 +203,8 @@ export async function deleteList(userId, userEmail, listPath, listId) {
// Delete list doc
const listCollectionRef = collection(db, userId);
const listDocumentRef = doc(listCollectionRef, listId);
const itemsCollectionRef = collection(listDocumentRef, 'items');
console.log(itemsCollectionRef);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Leftover console.log ^^

await deleteDoc(listDocumentRef);

// Update users doc that include a list reference
Expand Down
4 changes: 2 additions & 2 deletions src/components/ListButtons.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ const ListButtons = (props) => {
<div className="grid sm:grid-cols-3 grid-cols-2 gap-4 py-6 text-base sm:text-lg font-poppins">
<button
className={`sm:col-span-2 px-4 py-2 gap-6 shadow-lg ${buttonVariants[props.colorAdd]}`}
onClick={() => navigate('/manage-list')}
onClick={() => navigate('/manage-list/add')}
>
<i className="fa-solid fa-plus"></i>

<span>Add item</span>
</button>
<button
className={`sm:col-span-1 gap-6 shadow-lg ${buttonVariants[props.colorShare]}`}
onClick={() => navigate('/manage-list')}
onClick={() => navigate('/manage-list/share')}
>
<i className="fa-solid fa-share-nodes"></i>

Expand Down
24 changes: 22 additions & 2 deletions src/views/ManageList.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useState, useRef, useEffect } from 'react';
import { addItem } from '../api/firebase';
import { shareList } from '../api/firebase';
import ErrorMessage from '../components/ErrorMessage';
Expand All @@ -8,13 +8,31 @@ import {
inputHasOnlyNUmbers,
stringsHaveSameValue,
} from '../utils/inputValidation';
import { useParams } from 'react-router-dom';

const Params = {
Add: 'add',
Share: 'share',
};

export function ManageList({ data, listPath, userId, userEmail }) {
const [addItemErrMessage, setAddItemErrMessage] = useState('');
const [shareListErrMessage, setShareListErrMessage] = useState('');
const [addItemMessage, setAddItemMessage] = useState('');
const [shareListMessage, setShareListMessage] = useState('');

const addItemInput = useRef(null);
const shareListInput = useRef(null);
const { param } = useParams();

useEffect(() => {
if (param === Params.Add) {
addItemInput.current.focus();
} else if (param === Params.Share) {
shareListInput.current.focus();
}
}, []);

Copy link
Collaborator

Choose a reason for hiding this comment

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

34:5 warning React Hook useEffect has a missing dependency: 'param'. Either include it or remove the dependency array react-hooks/exhaustive-deps

Getting this warning, missing param on the dependency array.

let displayName;
for (let i = 0; i < listPath.length; i++) {
if (listPath[i] === '/') {
Expand Down Expand Up @@ -136,6 +154,7 @@ export function ManageList({ data, listPath, userId, userEmail }) {
type="text"
placeholder="Type a new item name"
name="item"
ref={addItemInput}
className="grow shrink bg-lightGrey border border-darkPurple rounded-md shadow-lg px-4 py-2 placeholder:text-darkPurple mb-5"
onChange={() => {
setAddItemErrMessage('');
Expand All @@ -149,7 +168,7 @@ export function ManageList({ data, listPath, userId, userEmail }) {
aria-label="When do you need this item?"
className="col-span-3 sm:col-span-2 bg-lightGrey text-base sm:text-lg border border-darkPurple rounded-md shadow-lg px-4 py-2 placeholder:text-darkPurple"
>
<option value="none" selected disabled hidden>
<option value="none" disabled hidden>
Choose item's likely need date
</option>

Expand Down Expand Up @@ -189,6 +208,7 @@ export function ManageList({ data, listPath, userId, userEmail }) {
type="email"
name="email"
id="email"
ref={shareListInput}
placeholder="Share this list with another user"
className="col-span-3 sm:col-span-2 bg-lightGrey border border-darkPurple rounded-md shadow-lg px-4 py-2 placeholder:text-darkPurple"
onChange={() => {
Expand Down
Loading