@@ -5,6 +5,7 @@ import LoginForm from './LoginForm'
55import Message from './Message'
66import ArticleForm from './ArticleForm'
77import Spinner from './Spinner'
8+ import axios from 'axios' ;
89
910const articlesUrl = 'http://localhost:9000/api/articles'
1011const loginUrl = 'http://localhost:9000/api/login'
@@ -34,15 +35,17 @@ export default function App() {
3435 const login = ( { username, password } ) => {
3536 setMessage ( '' ) ;
3637 setSpinnerOn ( true ) ;
37- axios . post ( loginURL , { username, password } )
38+
39+ axios . post ( loginUrl , { username, password } )
3840 . then ( response => {
3941 const token = response . data . token ;
4042 localStorage . setItem ( 'token' , token ) ;
4143 setMessage ( 'Login successful!' ) ;
4244 redirectToArticles ( ) ;
45+ getArticles ( )
4346 } )
44- . catch ( error => {
45- setMessage ( ' Login failed. Please check your credentials.' ) ;
47+ . catch ( ( ) => {
48+ setMessage ( 'Login failed. Please check your credentials.' ) ;
4649 } )
4750 . finally ( ( ) => {
4851 setSpinnerOn ( false ) ;
@@ -52,22 +55,24 @@ export default function App() {
5255 const getArticles = ( ) => {
5356 setMessage ( '' ) ; // Clear any previous messages
5457 setSpinnerOn ( true ) ; // Show the spinner while fetching articles
55-
5658 const token = localStorage . getItem ( 'token' ) ; // Retrieve the token from local storage
5759
5860 axios . get ( articlesUrl , {
59- headers : { Authorization : token } // Pass the token in the Authorization header
61+ headers : { Authorization : token } , // Pass the token in the Authorization header
6062 } )
61- . then ( response => {
62- setArticles ( response . data ) ; // Update the articles state with the response data
63- setMessage ( 'Articles retrieved successfully!' ) ; // Set a success message
63+ . then ( ( response ) => {
64+ // Log the response to debug its structure
65+ console . log ( 'Response data:' , response . data ) ;
66+ setArticles ( response . data . articles ) ; // Use the array directly
67+ setMessage ( response . data . message ) ;
6468 } )
65- . catch ( error => {
69+ . catch ( ( error ) => {
70+ console . error ( 'Error fetching articles:' , error ) ;
6671 if ( error . response ?. status === 401 ) {
67- setMessage ( 'Session expired. Please log in again.' ) ; // Set a message for unauthorized access
68- redirectToLogin ( ) ; // Redirect to the login page
72+ setMessage ( 'Session expired. Please log in again.' ) ;
73+ redirectToLogin ( ) ;
6974 } else {
70- setMessage ( 'Failed to retrieve articles. Please try again.' ) ; // General error message
75+ setMessage ( 'Failed to retrieve articles. Please try again.' ) ;
7176 }
7277 } )
7378 . finally ( ( ) => {
@@ -86,10 +91,12 @@ export default function App() {
8691 headers : { Authorization : token } // Pass the token in the Authorization header
8792 } )
8893 . then ( response => {
89- setArticles ( [ ...articles , response . data ] ) ; // Add the new article to the state
90- setMessage ( 'Article successfully added!' ) ; // Set a success message
94+ console . log ( "postresponse" , response ) ;
95+ setArticles ( [ ...articles , response . data . article ] ) ; // Add the new article to the state
96+ setMessage ( response . data . message ) ; // Set a success message
97+
9198 } )
92- . catch ( error => {
99+ . catch ( ( ) => {
93100 setMessage ( 'Failed to add article. Please try again.' ) ; // Set an error message
94101 } )
95102 . finally ( ( ) => {
@@ -109,11 +116,11 @@ export default function App() {
109116 } )
110117 . then ( response => {
111118 setArticles ( articles . map ( art =>
112- art . article_id === article_id ? response . data : art // Replace updated article
119+ art . article_id === article_id ? response . data . article : art // Replace updated article
113120 ) ) ;
114- setMessage ( 'Article successfully updated!' ) ; // Set a success message
121+ setMessage ( response . data . message ) ; // Set a success message
115122 } )
116- . catch ( error => {
123+ . catch ( ( ) => {
117124 setMessage ( 'Failed to update article. Please try again.' ) ; // Set an error message
118125 } )
119126 . finally ( ( ) => {
@@ -131,11 +138,11 @@ export default function App() {
131138 axios . delete ( `${ articlesUrl } /${ article_id } ` , {
132139 headers : { Authorization : token } // Pass the token in the Authorization header
133140 } )
134- . then ( ( ) => {
141+ . then ( ( response ) => {
135142 setArticles ( articles . filter ( art => art . article_id !== article_id ) ) ; // Remove the deleted article
136- setMessage ( 'Article successfully deleted!' ) ; // Set a success message
143+ setMessage ( response . data . message ) ; // Set a success message
137144 } )
138- . catch ( error => {
145+ . catch ( ( ) => {
139146 setMessage ( 'Failed to delete article. Please try again.' ) ; // Set an error message
140147 } )
141148 . finally ( ( ) => {
@@ -160,15 +167,17 @@ export default function App() {
160167 < Route path = "articles" element = {
161168 < >
162169 < ArticleForm
163- currentArticleId = { currentArticleId }
170+ currentArticle = { articles . find ( art => art . article_id == currentArticleId ) }
164171 postArticle = { postArticle }
165172 updateArticle = { updateArticle }
166173 setCurrentArticleId = { setCurrentArticleId }
167174 />
168175 < Articles
169176 articles = { articles }
177+ getArticles = { getArticles }
170178 deleteArticle = { deleteArticle }
171179 setCurrentArticleId = { setCurrentArticleId }
180+ currentArticleId = { currentArticleId }
172181 />
173182 </ >
174183 } />
0 commit comments