@@ -3,8 +3,10 @@ import type RouterService from '@ember/routing/router-service';
3
3
import { inject as service } from '@ember/service' ;
4
4
import type UserModel from 'codecrafters-frontend/models/user' ;
5
5
import type AuthenticatorService from 'codecrafters-frontend/services/authenticator' ;
6
+ import type MetaDataService from 'codecrafters-frontend/services/meta-data' ;
6
7
import BaseRoute from 'codecrafters-frontend/utils/base-route' ;
7
8
import RouteInfoMetadata , { HelpscoutBeaconVisibility } from 'codecrafters-frontend/utils/route-info-metadata' ;
9
+ import config from 'codecrafters-frontend/config/environment' ;
8
10
9
11
export type ModelType = UserModel | undefined ;
10
12
@@ -13,23 +15,65 @@ export default class UserRoute extends BaseRoute {
13
15
@service declare authenticator : AuthenticatorService ;
14
16
@service declare router : RouterService ;
15
17
@service declare store : Store ;
18
+ @service declare metaData : MetaDataService ;
19
+
20
+ previousMetaImageUrl : string | undefined ;
21
+ previousMetaTitle : string | undefined ;
22
+ previousMetaDescription : string | undefined ;
16
23
17
24
afterModel ( model : ModelType ) : void {
18
25
if ( ! model ) {
19
26
this . router . transitionTo ( 'not-found' ) ;
27
+
28
+ return ;
20
29
}
30
+
31
+ this . previousMetaImageUrl = this . metaData . imageUrl ;
32
+ this . previousMetaTitle = this . metaData . title ;
33
+ this . previousMetaDescription = this . metaData . description ;
34
+
35
+ this . metaData . imageUrl = `${ config . x . metaTagUserProfilePictureBaseURL } ${ model . username } ` ;
36
+ this . metaData . title = `${ model . username } 's CodeCrafters Profile` ;
37
+ this . metaData . description = `View ${ model . username } 's profile on CodeCrafters` ;
21
38
}
22
39
23
40
buildRouteInfoMetadata ( ) {
24
41
return new RouteInfoMetadata ( { beaconVisibility : HelpscoutBeaconVisibility . Hidden } ) ;
25
42
}
26
43
27
- async model ( params : { username : string } ) : Promise < ModelType > {
28
- const users = ( await this . store . query ( 'user' , {
29
- username : params . username ,
30
- include : 'course-participations.language,course-participations.course.stages,course-participations.current-stage,profile-events' ,
31
- } ) ) as unknown as UserModel [ ] ;
44
+ deactivate ( ) {
45
+ this . metaData . imageUrl = this . previousMetaImageUrl ;
46
+ this . metaData . title = this . previousMetaTitle ;
47
+ this . metaData . description = this . previousMetaDescription ;
48
+ }
49
+
50
+ async #fetchUserRecord( username : string ) {
51
+ return (
52
+ ( await this . store . query ( 'user' , {
53
+ username,
54
+ include : 'course-participations.language,course-participations.course.stages,course-participations.current-stage,profile-events' ,
55
+ } ) ) as unknown as UserModel [ ]
56
+ ) [ 0 ] ;
57
+ }
58
+
59
+ #findCachedUserRecord( username : string ) {
60
+ return this . store . peekAll ( 'user' ) . find ( ( u ) => u . username === username ) ;
61
+ }
62
+
63
+ async model ( { username } : { username : string } ) : Promise < ModelType > {
64
+ // Look up the record in the store, in case it's already there,
65
+ // for example, inserted via FastBoot Shoebox
66
+ const existingRecord = this . #findCachedUserRecord( username ) ;
67
+
68
+ if ( existingRecord ) {
69
+ // Trigger a fetch anyway to refresh the data
70
+ this . #fetchUserRecord( username ) ;
71
+
72
+ // Return existing record, otherwise the page will blink after loading
73
+ return existingRecord ;
74
+ }
32
75
33
- return users [ 0 ] ;
76
+ // If the record doesn't exist - fetch it and return
77
+ return this . #fetchUserRecord( username ) ;
34
78
}
35
79
}
0 commit comments