1+ import { getPost , getLoggedInUserInfo , removePost , updatePost , uploadImage } from "./postDetailModel.js" ;
2+ import { buildPostDetail_EditableView , buildPostDetail_ReadOnlyView } from "./postDetailView.js" ;
3+
4+
5+ export const postDetailController = async ( postContainer , postId ) => {
6+
7+
8+ let postDetail = null ;
9+ let user = null ;
10+ let isOwner = false
11+
12+ try {
13+ //----------------------------------------------------
14+ loadStarted ( ) ;
15+ //----------------------------------------------------
16+
17+ try {
18+ //===================================
19+ postDetail = await getPost ( postId ) ;
20+ //===================================
21+ } catch ( error ) {
22+ dispatchNotification ( 'post-error' , error . message ) ;
23+ }
24+
25+ if ( ! localStorage . getItem ( 'accessToken' ) ) {
26+ readOnlyView ( postDetail , isOwner )
27+ }
28+
29+ try {
30+ //===================================
31+ user = await getLoggedInUserInfo ( ) ;
32+ //===================================
33+ } catch ( error ) {
34+ dispatchNotification ( 'post-error' , error . message ) ;
35+ }
36+
37+ isOwner = user . id === postDetail . userId ;
38+
39+ if ( isOwner ) {
40+ editableView ( postDetail )
41+ } else {
42+ readOnlyView ( postDetail , isOwner )
43+ }
44+
45+ } catch ( error ) {
46+ dispatchNotification ( 'post-error' , error . message ) ;
47+ } finally {
48+ //----------------------------------------------------
49+ loadFinished ( ) ;
50+ //----------------------------------------------------
51+ }
52+
53+ //===================================================================================================================
54+ function readOnlyView ( post , isOwner ) {
55+ postContainer . innerHTML = buildPostDetail_ReadOnlyView ( post , isOwner ) ;
56+ }
57+
58+ //===================================================================================================================
59+ function editableView ( post ) {
60+
61+ const editPost = postContainer . querySelector ( "#edit-post" )
62+ editPost . addEventListener ( "click" , ( ) => render_EditableView ( post ) ) ;
63+
64+ const deletePost = postContainer . querySelector ( "#delete-post" )
65+ deletePost . addEventListener ( "click" , ( ) => handleDelete ( post . id ) ) ;
66+
67+
68+ postContainer . innerHTML = buildPostDetail_EditableView ( post ) ;
69+
70+
71+ const editForm = document . getElementById ( "edit-post-form" ) ;
72+
73+ editForm . addEventListener ( 'submit' , ( event ) => {
74+ event . preventDefault ( ) ;
75+
76+ handleSave ( post ) ;
77+ } ) ;
78+
79+ const cancel = postContainer . querySelector ( "#cancel-edit" )
80+ cancel . addEventListener ( "click" , ( event ) => {
81+ event . preventDefault ( ) ;
82+
83+ render_ReadOnlyView ( postDetail , true ) ;
84+ } ) ;
85+ }
86+
87+ //------------------------------------------------------------------------
88+ async function handleDelete ( postId ) {
89+ if ( confirm ( "Are you sure about deleting the post?" ) ) {
90+ try {
91+ //----------------------------------------------------
92+ loadStarted ( ) ;
93+ //----------------------------------------------------
94+
95+ //===================================
96+ await removePost ( postId ) ;
97+ //===================================
98+
99+ dispatchNotification ( 'post-success' , {
100+ message : "Post successfully deleted." ,
101+ type : 'success' ,
102+ type_success : 'post-deleted'
103+ } ) ;
104+ } catch ( error ) {
105+ dispatchNotification ( 'post-error' , error . message ) ;
106+ } finally {
107+ //----------------------------------------------------
108+ loadFinished ( ) ;
109+ //----------------------------------------------------
110+ }
111+ }
112+ }
113+
114+ //------------------------------------------------------------------------
115+ async function handleSave ( post ) {
116+
117+ const editForm = document . getElementById ( "edit-post-form" ) ;
118+
119+ const image = editForm . querySelector ( '#post-image' ) . files [ 0 ] ;
120+
121+ const name = editForm . querySelector ( '#post-name' ) . value ;
122+ const description = editForm . querySelector ( '#post-description' ) . value ;
123+ const price = editForm . querySelector ( '#post-price' ) . value ;
124+
125+ const tag = editForm . querySelector ( '#post-tag' ) . value ;
126+
127+ const isPurchase = editForm . querySelector ( 'input[name="transactionType"]:checked' ) . value === 'purchase' ;
128+
129+ try {
130+ //----------------------------------------------------
131+ loadStarted ( )
132+ //----------------------------------------------------
133+
134+ let imageURL
135+
136+ if ( image ) {
137+ //===================================
138+ imageURL = await uploadImage ( image ) ;
139+ //===================================
140+
141+ } else {
142+ imageURL = '../../../../public/no-image-available.jpg' ;
143+ }
144+
145+ post . image = imageURL ;
146+ post . name = name ;
147+ post . description = description ;
148+ post . price = price ;
149+ post . tag = tag || null ;
150+ post . isPurchase = isPurchase ;
151+
152+ //===================================
153+ await updatePost ( post ) ;
154+ //===================================
155+
156+ let getUpdatedPost ;
157+
158+ const postId = post . id
159+ //===================================
160+ getUpdatedPost = await getPost ( postId ) ;
161+ //===================================
162+
163+ postDetail = getUpdatedPost ;
164+
165+ dispatchNotification ( 'post-success' , {
166+ message : "Post successfully updated." ,
167+ type : 'success' ,
168+ } ) ;
169+
170+ render_ReadOnlyView ( postDetail , true ) ;
171+
172+ } catch ( error ) {
173+ dispatchNotification ( 'post-error' , error . message ) ;
174+ } finally {
175+ //----------------------------------------------------
176+ loadFinished ( )
177+ //----------------------------------------------------
178+ }
179+ }
180+
181+ //===================================================================================================================
182+ function dispatchNotification ( eventType , message ) {
183+ const event = new CustomEvent ( eventType , { detail : message } ) ;
184+
185+ if ( message . type_success === 'post-deleted' ) {
186+ setTimeout ( ( ) => window . location = '/index.html' , 500 ) ;
187+ }
188+
189+ postContainer . dispatchEvent ( event ) ;
190+ }
191+
192+ function loadStarted ( ) {
193+ const event = new CustomEvent ( "load-post-started" ) ;
194+ postContainer . dispatchEvent ( event ) ;
195+ }
196+
197+ function loadFinished ( ) {
198+ const event = new CustomEvent ( "load-post-finished" ) ;
199+ postContainer . dispatchEvent ( event ) ;
200+ }
201+ } ;
0 commit comments