@@ -6,10 +6,12 @@ export const postDetailController = async (postContainer, postId) => {
66 let postDetail = null ;
77 let user = null ;
88 let isEditing = false ;
9-
9+
1010 try {
11+ //----------------------------------------------------
1112 const event = new CustomEvent ( "load-posts-started" ) ;
1213 postContainer . dispatchEvent ( event ) ;
14+ //----------------------------------------------------
1315
1416 try {
1517 //===================================
@@ -34,8 +36,10 @@ export const postDetailController = async (postContainer, postId) => {
3436 } catch ( error ) {
3537 dispatchNotification ( 'post-error' , error . message ) ;
3638 } finally {
39+ //----------------------------------------------------
3740 const event = new CustomEvent ( "load-posts-finished" ) ;
3841 postContainer . dispatchEvent ( event ) ;
42+ //----------------------------------------------------
3943 }
4044
4145 //===================================================================================================================
@@ -83,7 +87,8 @@ export const postDetailController = async (postContainer, postId) => {
8387
8488 dispatchNotification ( 'post-success' , {
8589 message : "Post successfully deleted." ,
86- type : 'success'
90+ type : 'success' ,
91+ type_success : 'post-deleted'
8792 } ) ;
8893 } catch ( error ) {
8994 dispatchNotification ( 'post-error' , error . message ) ;
@@ -93,40 +98,44 @@ export const postDetailController = async (postContainer, postId) => {
9398
9499 //------------------------------------------------------------------------
95100 async function handleSave ( post ) {
101+
96102 const editPostForm = postContainer . querySelector ( '#editPostForm' ) ;
97103
104+ const image = editPostForm . querySelector ( '#post-image' ) . files [ 0 ] ;
105+
98106 const name = editPostForm . querySelector ( '#post-name' ) . value ;
99107 const description = editPostForm . querySelector ( '#post-description' ) . value ;
100108 const price = editPostForm . querySelector ( '#post-price' ) . value ;
101- const tag = editPostForm . querySelector ( '#edit-tag' ) . value ;
102- const isPurchase = editPostForm . querySelector ( 'input[name="transactionType"]:checked' ) . value === 'purchase' ;
103109
104- const fileInput = editPostForm . querySelector ( '#post-image' ) ;
105- const newImage = fileInput . files [ 0 ] ;
110+ const tag = editPostForm . querySelector ( '#post-tag' ) . value ;
106111
112+ const isPurchase = editPostForm . querySelector ( 'input[name="transactionType"]:checked' ) . value === 'purchase' ;
113+
114+ post . image = image ;
107115 post . name = name ;
108116 post . description = description ;
109117 post . price = price ;
110- post . tag = tag ;
118+ post . tag = tag || null ;
111119 post . isPurchase = isPurchase ;
112120
113- if ( newImage ) {
114- post . photo = newImage ;
121+ if ( ! post . image ) {
122+ post . image = '../../../../public/no-image-available.jpg' ;
115123 }
116124
117125 try {
118126 //===================================
119- const updatedPost = await updatePost ( post . id , post ) ;
127+ const updatedPost = await updatePost ( post ) ;
120128 //===================================
121129
122- postDetail = updatedPost ; // actualizar info local
130+ postDetail = updatedPost ;
131+
123132 dispatchNotification ( 'post-success' , {
124133 message : "Post successfully updated." ,
125- type : 'success'
134+ type : 'success' ,
126135 } ) ;
127136
128- renderReadOnlyView ( updatedPost , true ) ;
129-
137+ renderReadOnlyView ( postDetail , true ) ;
138+
130139 } catch ( error ) {
131140 dispatchNotification ( 'post-error' , error . message ) ;
132141 }
@@ -136,8 +145,8 @@ export const postDetailController = async (postContainer, postId) => {
136145 function dispatchNotification ( eventType , message ) {
137146 const event = new CustomEvent ( eventType , { detail : message } ) ;
138147
139- if ( message . type === 'success ' ) {
140- setTimeout ( ( ) => window . location = '/index.html' , 7000 ) ;
148+ if ( message . type_success === 'post-deleted ' ) {
149+ setTimeout ( ( ) => window . location = '/index.html' , 500 ) ;
141150 }
142151
143152 postContainer . dispatchEvent ( event ) ;
0 commit comments