1818
1919import angular from 'angular' ;
2020
21- import type { AngularModule , $Log , $Http } from 'angular' ;
21+ import type { AngularModule , $Log } from 'angular' ;
2222
2323import constants from '../../../constants' ;
2424import settings , { Settings } from '../../settings' ;
@@ -29,6 +29,7 @@ import { convertDataUritoBlob } from '../util';
2929import type { $DecoratedScope } from '../decorateScope' ;
3030import type { ColorService } from '../services/colorService' ;
3131import type { ComicService } from '../services/comicService' ;
32+ import type { ItemService } from '../services/itemService' ;
3233import type { MessageReportingService } from '../services/messageReportingService' ;
3334import type { StyleService } from '../services/styleService' ;
3435import type { DecoratedItemData , ItemRelationData , ItemImageData } from '../api/itemData' ;
@@ -37,14 +38,14 @@ export class ItemDetailsController {
3738 static $inject : string [ ] ;
3839
3940 $log : $Log ;
40- $http : $Http ;
4141 $scope : $DecoratedScope < ItemDetailsController > ;
4242 colorService : ColorService ;
4343 comicService : ComicService ;
4444 messageReportingService : MessageReportingService ;
4545 styleService : StyleService ;
46+ itemService : ItemService ;
4647
47- isLoading : number ;
48+ isLoading : boolean ;
4849 isUpdating : boolean ;
4950 settings : Settings ;
5051 itemData : DecoratedItemData ;
@@ -57,7 +58,7 @@ export class ItemDetailsController {
5758
5859 constructor (
5960 $log : $Log ,
60- $http : $Http ,
61+ itemService : ItemService ,
6162 $scope : $DecoratedScope < ItemDetailsController > ,
6263 colorService : ColorService ,
6364 comicService : ComicService ,
@@ -67,14 +68,14 @@ export class ItemDetailsController {
6768 $log . debug ( 'START ItemDetailsController' ) ;
6869
6970 this . $log = $log ;
70- this . $http = $http ;
71+ this . itemService = itemService ;
7172 this . $scope = $scope ;
7273 this . colorService = colorService ;
7374 this . comicService = comicService ;
7475 this . messageReportingService = messageReportingService ;
7576 this . styleService = styleService ;
7677
77- this . isLoading = 1 ;
78+ this . isLoading = true ;
7879 this . isUpdating = false ;
7980 this . settings = settings ;
8081
@@ -95,89 +96,30 @@ export class ItemDetailsController {
9596 this . $log . debug ( 'ItemDetailsController::showModal() - item id:' ,
9697 itemId ) ;
9798
98- function handleRelationData ( response ) : ?ItemRelationData [ ] {
99- if ( response . status === 200 ) {
100- const relationData = ( response . data : ItemRelationData [ ] ) ;
101-
102- $ . each ( relationData , ( _ : number , relation ) => {
103- relation . percentage = relation . count /
104- self . itemData . appearances * 100 ;
105- } ) ;
106-
107- return relationData ;
108- }
109- return null ;
110- }
111-
112- function handleItemFriendsData ( response ) {
113- let friends = handleRelationData ( response ) ;
114- self . $scope . safeApply ( ( ) => {
115- self . itemData . friends = friends || [ ] ;
116- self . isLoading -- ;
117- } ) ;
118- }
119-
120- function handleItemLocationsData ( response ) {
121- let locations = handleRelationData ( response ) ;
122- self . $scope . safeApply ( ( ) => {
123- self . itemData . locations = locations || [ ] ;
124- self . isLoading -- ;
125- } ) ;
126- }
127-
128- function handleItemImageData ( response ) {
129- if ( response . status === 200 ) {
130- let images = response . data ;
131- let imagePaths : string [ ] = [ ] ;
132- $ . each ( images , ( _ : number , image : ItemImageData ) => {
133- imagePaths . push ( `${ constants . itemDataUrl } image/${ image . id } ` ) ;
134- } ) ;
135- self . $scope . safeApply ( ( ) => {
136- self . imagePaths = imagePaths ;
137- self . currentImagePath = 0 ;
138- self . isLoading -- ;
139- } ) ;
140- }
141- }
142-
14399 this . itemData = ( ( { } : any ) : DecoratedItemData ) ;
144- this . isLoading = 1 ;
100+ this . isLoading = true ;
145101
146102 this . imagePaths = [ ] ;
147103 this . currentImagePath = 0 ;
148104 this . isImagePreview = false ;
149105 this . imageFile = null ;
150106 this . imageFileInfo = null ;
151107
152- const itemDataResponse = await this . $http . get ( constants . itemDataUrl + itemId ) ;
153- if ( itemDataResponse . status === 200 ) {
154- const itemData = ( itemDataResponse . data : DecoratedItemData ) ;
155-
156- itemData . highlightColor = this . colorService
157- . createTintOrShade ( itemData . color ) ;
158-
108+ const itemData = await this . itemService . getItemData ( itemId ) ;
109+ if ( itemData ) {
159110 this . $log . debug ( 'qcItemDetails::showModal() - ' +
160111 'item data:' , itemData ) ;
161112
162- this . isLoading += 2 ;
163- const friendDataRequest = this . $http . get ( constants . itemFriendDataUrl + itemId ) ;
164- const locationDataRequest = this . $http . get ( constants . itemLocationDataUrl + itemId ) ;
165- this . $http . get ( constants . itemFriendDataUrl + itemId )
166- . then ( handleItemFriendsData ) ;
167- this . $http . get ( constants . itemLocationDataUrl + itemId )
168- . then ( handleItemLocationsData ) ;
169- if ( itemData . hasImage ) {
170- this . isLoading ++ ;
171- this . $http . get ( constants . itemDataUrl + itemId + '/images' )
172- . then ( handleItemImageData ) ;
173- }
174-
175- // If the color changes, also update the
176- // highlight color
177113 this . $scope . safeApply ( ( ) => {
178114 this . itemData = itemData ;
179- this . isLoading -- ;
115+ this . isLoading = false ;
116+ this . isUpdating = false ;
180117
118+ this . imagePaths = itemData . imageUrls ;
119+ this . currentImagePath = 0 ;
120+
121+ // If the color changes, also update the
122+ // highlight color
181123 this . $scope . $watch ( ( ) => {
182124 return this . itemData . color ;
183125 } , ( ) => {
@@ -188,16 +130,7 @@ export class ItemDetailsController {
188130 } ) ;
189131 } ) ;
190132 } else {
191- this . $scope . safeApply ( ( ) => {
192- this . isLoading -- ;
193- } ) ;
194- if ( itemDataResponse . status !== 503 ) {
195- this . messageReportingService . reportError ( itemDataResponse . data ) ;
196- } else {
197- this . messageReportingService . reportError (
198- constants . messages . maintenance ) ;
199- this . close ( ) ;
200- }
133+ this . close ( ) ;
201134 }
202135 }
203136
@@ -231,37 +164,19 @@ export class ItemDetailsController {
231164 this . $scope . safeApply ( ( ) => {
232165 this . isUpdating = true ;
233166 } ) ;
234- const data = {
235- token : settings . values . editModeToken ,
236- item : this . itemData . id ,
237- property : property ,
238- value : this . itemData [ property ]
239- } ;
240- try {
241- const response = await this . $http . post ( constants . setItemDataPropertyUrl , data ) ;
242- this . $scope . safeApply ( ( ) => {
243- this . isUpdating = false ;
244- } ) ;
245-
246- if ( response . status === 200 ) {
247- if ( property === 'color' ) {
248- this . $log . debug ( 'ItemDetailsController::update() - ' +
249- 'update item color' ) ;
250- this . styleService . removeItemStyle (
251- this . itemData . id ) ;
252- }
253- } else {
254- this . $scope . safeApply ( ( ) => {
255- this . isUpdating = false ;
256- } ) ;
167+ const success = await this . itemService . updateProperty ( this . itemData . id , property , this . itemData [ property ] ) ;
168+ this . $scope . safeApply ( ( ) => {
169+ this . isUpdating = false ;
170+ } ) ;
171+ if ( success ) {
172+ if ( property === 'color' ) {
173+ this . $log . debug ( 'ItemDetailsController::update() - ' +
174+ 'update item color' ) ;
175+ this . styleService . removeItemStyle (
176+ this . itemData . id ) ;
257177 }
258- return this . _onSuccessRefreshElseErrorLog ( response ) ;
259- }
260- catch ( r ) {
261- this . $scope . safeApply ( ( ) => {
262- this . isUpdating = false ;
263- } ) ;
264- return this . _onErrorLog ( r ) ;
178+ this . comicService . refreshComicData ( ) ;
179+ this . _getItemDetails ( ) ;
265180 }
266181 }
267182
@@ -280,22 +195,19 @@ export class ItemDetailsController {
280195 }
281196 }
282197
283- uploadImage ( ) {
198+ async uploadImage ( ) {
284199 if ( this . imageFile && this . imageFileInfo . type == 'image/png' ) {
285200 const imageBlob = convertDataUritoBlob ( this . imageFile ) ;
286201
287- const formData = new FormData ( ) ;
288- formData . append ( 'ItemId' , String ( this . itemData . id ) ) ;
289- formData . append ( 'Image' , imageBlob , this . imageFileInfo . name ) ;
290- formData . append ( 'Token' , settings . values . editModeToken ) ;
291-
292- this . $http . post ( constants . itemDataUrl + 'image/upload' , formData , { contentType : undefined , dataTransform : ( d ) => d } )
293- . then ( ( ) => {
294- this . _getItemDetails ( ) ;
295- } ) . catch ( ( error ) => {
296- self . messageReportingService . reportError (
297- error ) ;
298- } ) ;
202+ this . $scope . safeApply ( ( ) => {
203+ this . isUpdating = true ;
204+ } ) ;
205+ const success = await this . itemService . uploadImage ( this . itemData . id , imageBlob , this . imageFileInfo . name ) ;
206+ if ( success ) {
207+ this . _getItemDetails ( ) ;
208+ }
209+ } else {
210+ this . messageReportingService . reportError ( 'Only PNG images are supported' ) ;
299211 }
300212 }
301213
@@ -313,7 +225,7 @@ export class ItemDetailsController {
313225 }
314226 }
315227}
316- ItemDetailsController . $inject = [ '$log' , '$http ' , '$scope' , 'colorService' ,
228+ ItemDetailsController . $inject = [ '$log' , 'itemService ' , '$scope' , 'colorService' ,
317229 'comicService' , 'messageReportingService' , 'styleService' ] ;
318230
319231export default function ( app : AngularModule ) {
0 commit comments