Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
05aa9cb
basic home
AlbertProfe Jan 13, 2025
8dffb74
sandbox component Card
AlbertProfe Jan 13, 2025
e4c02d1
front docs add image
AlbertProfe Jan 13, 2025
e1841fb
management git-gh resources pronunciationApp v0.1 doc
AlbertProfe Jan 20, 2025
c57534b
resources pronunciationApp v0.1 images
AlbertProfe Jan 20, 2025
6ad82fe
resources pronunciationApp v0.1 images
AlbertProfe Jan 20, 2025
f25d8e3
add mui, cards, axios, hooks, useEffect and hero image
AlbertProfe Jan 20, 2025
322b112
decouple axios data api as function import
AlbertProfe Jan 20, 2025
3714946
resources image where to see
AlbertProfe Jan 20, 2025
ec64258
variant gradient css adn transition card
AlbertProfe Jan 20, 2025
a175404
doc: renaming images
AlbertProfe Jan 21, 2025
2fb3b8d
doc: words.json added
AlbertProfe Jan 21, 2025
b4c7dc5
doc:: how React component works
AlbertProfe Jan 21, 2025
c15ae93
doc: updated pronunciationApp-v0.1
AlbertProfe Jan 21, 2025
e5097cc
doc: update PronunciationApp v0.1
AlbertProfe Jan 22, 2025
2e269aa
doc: material ui box
AlbertProfe Jan 22, 2025
953af59
doc: updated pronunciationApp-v0.1 typescript
AlbertProfe Jan 22, 2025
5b4f72b
doc: create README.md
emsmccoy Jan 22, 2025
12b1306
feat: connect to mock server
emsmccoy Jan 22, 2025
584c7c9
feat: connect to mock server
emsmccoy Jan 22, 2025
a2246f4
doc: update readme
emsmccoy Jan 28, 2025
786b1e9
feat: create fetchUsers
emsmccoy Jan 28, 2025
bbe884e
feat: create Users component
emsmccoy Jan 28, 2025
b574e82
feat: create Users component
emsmccoy Jan 28, 2025
9d2e379
feat: create user profile card with avatar image
emsmccoy Jan 29, 2025
5d8bfff
doc: rendering error documentation and time spent on user with avatar
emsmccoy Jan 29, 2025
0d6fce9
feat: first approach to implementing chips for difficulty and synonym…
emsmccoy Jan 31, 2025
312f816
feat: implement difficulty filter, refactored card distribution and d…
emsmccoy Jan 31, 2025
e104687
doc: include images and video demo. update md
emsmccoy Jan 31, 2025
bb7d8df
doc: Update README.md
emsmccoy Jan 31, 2025
c6e8e03
doc: Update README.md
emsmccoy Jan 31, 2025
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
Binary file added PRA/PRA01Demo.webm
Binary file not shown.
Binary file added PRA/PRA01Filter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PRA/PRA01OnHover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
167 changes: 167 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
# PRA#01-React Components Frontend Development

## Overview

This document serves as both a guide and a log for the PRA#01 React frontend development task.<br/>
Images and a video demo can be found in the [PRA](/PRA) folder

---

## PR Submission Checklist

### **Tasks Completed**:

    **Common**

- [x] Postman mock server is set up with correct endpoint.

- [x] Axios function is implemented in a decoupled manner.

- [x] User JSON structure includes varied data types.

- [x] User component renders correctly with avatar.

- [x] Difficulty levels and filtering are functional.

- [x] Synonyms rendering is implemented and error-free.

**Optional**

- [x] CSS Enhancements: transient effect on hover

- [ ] Animation Libraries

- [ ] Enhanced Card Features

---

## Estimated Time for Tasks

### Common Part

| Task | Estimated Time | Actual Time | Impediments and errors |
| --------------------------------------------- | -------------- | ------------------------------ | ---------------------------------------------------------------------------------------- |
| Set up Postman mock server | 1 hour | 2:30h | mock server JSON and the data data-api fetched had different structures |
| Implement axios function for data fetching | 1 hour | 1:15 (and back to mock server) | Needed to install font roboto, emotion and material ui. |
| Create user JSON structure | 1 hour | 10 min | |
| Develop user component with avatar | 2 hours | 4 hours | Potsman example didn't have the URL. <br/> Error rendering due to useEffect() and find() |
| Add difficulty levels and implement filtering | 2 hours | 1:30 hours | |
| **Total** | **7 hours** | **9:25 hours** | |

### Optional Part

| Task | Estimated Time | Actual Time | Impediments and errors |
| ------------------------------------------- | -------------- | ----------- | ---------------------- |
| CSS Enhancements: transient effect on hover | 1:30 hours | 30 min | |
| Animation Libraries | 1 hour | | |
| Enhanced Card Features | 1 hour | | |
| **Total** | **3:30 hours** | 30 min | |

---

## Error Documentation and Solution

### Error: `TypeError: Cannot read properties of undefined (reading 'map')`

**Corresponding task:**

**Description:** This error occurs when trying to use the `.map()` function on a variable that is either `undefined` or `null`. In JavaScript, `map()` is a method available only on arrays, and attempting to call it on something that is not an array (or is undefined) will result in this error.

**Error Trace:**

- **Component:** `WordList`
- **File:** `Cards.jsx`
- **Line:** 40 (in `WordList`)
- **Stack Trace:**
- `WordList (Cards.jsx:40:18)`
- `App`

**Possible Causes:**

- The variable passed to `.map()` is `undefined` or `null`.
- The variable might not have been initialized or fetched correctly before calling `.map()`.

**Solution:**
The error was resolved by modifying the `data-api.js` file. The issue was in how the data was being returned from the API call.Original code in `data-api.js`:

```javascript
export const fetchWords = async () => { try { const response = await axios.get(`${BASE_URL}/words`);
return response.data.words; // This was causing the issue
} catch (error) {
console.error("Error fetching words:", error);
throw error;
} };
```

Updated code in `api.js`:

```javascript
export const fetchWords = async () => { try { const response = await axios.get(`${BASE_URL}/words`);
return response.data; // This fixed the issue
} catch (error) { console.error("Error fetching words:", error);
throw error; } };
```

**Explanation:**
The API was returning the array of words directly in the `response.data`, not nested under a `words` property. By changing `return response.data.words;` to `return response.data;`, we correctly access the array of words, allowing the `.map()` function to work as expected in the `WordList` component.

---

## **Undefined Errors During Initial Render: useEffect() and find()**

### **Initial Rendering:**

- The component is mounted with an initial empty state (`users = []`).
- The code tries to access `user.name` when `user` is `undefined`, causing an error.

### **Execution of `useEffect`:**

- `useEffect` runs **after** the initial render. React ensures the DOM is updated before running the effect.
- The `fetchUsers()` function is called inside `useEffect` to fetch the data.

### **State Update:**

- When the data is received, `setUsers(data)` updates the state, triggering a re-render of the component.

### **Rendering with Data:**

- The component is re-rendered with the correct data. `users.find()` now finds the correct user and renders their name.

---

## **Main Issue**

- The error occurs because `useEffect` does **not execute immediately** after the component renders; it runs after the initial render. This can lead to unexpected behavior and errors if not properly handled.

---

## **Solution**

To solve this error, implement a conditional check before rendering the content that depends on the data:

`return ( <> {user ? <h1>{user.name}</h1> : <p>Loading...</p>} </> );`

This solution prevents the error on the first render by showing a loading message while the data is still being fetched.

---

## **Best Practices**

1. **React Hooks Usage:** Ensure that React Hooks are always called at the top level of the component function. Do not place them inside conditions, loops, or nested functions, as React relies on the order of hook execution to manage state properly.
2. **Dependencies in `useEffect`:** Identify all dependencies used inside the `useEffect` hook and ensure they are included in the dependency array. This guarantees that the effect is re-executed when relevant values change.

### **Alternative Approach Using Loading State:**

`const [isLoading, setIsLoading] = useState(true); useEffect(() => { const fetchData = async () => { try { const data = await fetchUsers(); setUsers(data); } finally { setIsLoading(false); } }; fetchData(); }, []); return ( <> {isLoading ? <p>Loading...</p> : user && <h1>{user.name}</h1>} </> );`

This approach provides better control over the loading state and avoids errors related to unavailable data during the initial render.

---

## Future Improvements

- Add unit and integration tests for key components.
- Enhance UI/UX with additional CSS animations or interactive features.
- Refactor components for improved reusability and maintainability.

---
Loading