-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Organization List Component, as well as user data retrieval from back…
…end (#3) * Suite of changes to enable the organization selector, as well as user data retrieval from the v3_users_current endpoint * remove nav service * use user attributes for simplicity * do not include an unused response type * do not include an unused response type * remove some red underlines * fix some additional linter errors * Organization updates --------- Co-authored-by: Alex Swindler <[email protected]>
- Loading branch information
1 parent
71fa046
commit 14c7f2f
Showing
27 changed files
with
340 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
export type Column = { | ||
id: number; | ||
name: string; | ||
organization_id: number; | ||
table_name: 'PropertyState' | 'TaxLotState'; | ||
merge_protection: 'Favor New' | 'Favor Existing'; | ||
shared_field_type: 'None' | 'Public'; | ||
column_name: string; | ||
is_extra_data: boolean; | ||
unit_name: null; | ||
unit_type: null; | ||
display_name: string; | ||
data_type: | ||
| 'number' | ||
| 'float' | ||
| 'integer' | ||
| 'string' | ||
| 'geometry' | ||
| 'datetime' | ||
| 'date' | ||
| 'boolean' | ||
| 'area' | ||
| 'eui' | ||
| 'ghg_intensity' | ||
| 'ghg' | ||
| 'wui' | ||
| 'water_use'; | ||
is_matching_criteria: boolean; | ||
is_updating: boolean; | ||
geocoding_order: number; | ||
recognize_empty: boolean; | ||
comstock_mapping: string | null; | ||
column_description: string; | ||
derived_column: number | null; | ||
is_excluded_from_hash: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './column.types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export type Cycle = { | ||
name: string; | ||
start: string; | ||
end: string; | ||
organization: number; | ||
user: number | null; | ||
id: number; | ||
} | ||
|
||
export type ListCyclesResponse = { | ||
status: string; | ||
cycles: Cycle[]; | ||
} | ||
|
||
export type GetCycleResponse = { | ||
status: string; | ||
cycles: Cycle; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './cycle.types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './organization.service' | ||
export * from './organization.types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { HttpClient } from '@angular/common/http' | ||
import { inject, Injectable } from '@angular/core' | ||
import type { Observable } from 'rxjs' | ||
import { map, ReplaySubject, tap } from 'rxjs' | ||
import { naturalSort } from '../../utils' | ||
import type { BriefOrganization, Organization, OrganizationsResponse } from './organization.types' | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class OrganizationService { | ||
private _httpClient = inject(HttpClient) | ||
private _organizations: ReplaySubject<BriefOrganization[]> = new ReplaySubject<BriefOrganization[]>(1) | ||
organizations$ = this._organizations.asObservable() | ||
|
||
get(): Observable<Organization[]> { | ||
return this._get(false) as Observable<Organization[]> | ||
} | ||
|
||
getBrief(): Observable<BriefOrganization[]> { | ||
return this._get(true) | ||
} | ||
|
||
private _get(brief = false): Observable<(BriefOrganization | Organization)[]> { | ||
const url = brief ? '/api/v3/organizations/?brief=true' : '/api/v3/organizations/' | ||
return this._httpClient.get<OrganizationsResponse>(url).pipe( | ||
map(({ organizations }) => { | ||
return organizations.toSorted((a, b) => naturalSort(a.name, b.name)) | ||
}), | ||
tap((organizations) => { | ||
// TODO not sure if we actually want to cache this in the replaySubject | ||
if (brief) { | ||
this._organizations.next(organizations) | ||
} | ||
}), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import type { UserRole } from '@seed/api/user' | ||
import type { Column } from '../column' | ||
|
||
type OrgCycle = { | ||
name: string; | ||
cycle_id: number; | ||
num_properties: number; | ||
num_taxlots: number; | ||
} | ||
|
||
type OrgUser = { | ||
first_name: string; | ||
last_name: string; | ||
email: string; | ||
id: number; | ||
} | ||
|
||
export type BriefOrganization = { | ||
name: string; | ||
org_id: number; | ||
parent_id: number | null; | ||
is_parent: boolean; | ||
id: number; | ||
user_role: UserRole; | ||
display_decimal_places: number; | ||
salesforce_enabled: boolean; | ||
access_level_names: string[]; | ||
audit_template_conditional_import: boolean; | ||
property_display_field: string; | ||
taxlot_display_field: string; | ||
} | ||
|
||
export type Organization = BriefOrganization & { | ||
number_of_users: number; | ||
user_is_owner: boolean; | ||
owners: OrgUser[]; | ||
sub_orgs: (Organization & { is_parent: false })[]; | ||
parent_id: number; | ||
display_units_eui: string; | ||
display_units_ghg: string; | ||
display_units_ghg_intensity: string; | ||
display_units_water_use: string; | ||
display_units_wui: string; | ||
display_units_area: string; | ||
cycles: OrgCycle[]; | ||
created: string; | ||
mapquest_api_key: string; | ||
geocoding_enabled: boolean; | ||
better_analysis_api_key: string; | ||
better_host_url: string; | ||
display_meter_units: Record<string, string>; | ||
display_meter_water_units: Record<string, string>; | ||
thermal_conversion_assumption: number; | ||
comstock_enabled: boolean; | ||
new_user_email_from: string; | ||
new_user_email_subject: string; | ||
new_user_email_content: string; | ||
new_user_email_signature: string; | ||
at_organization_token: string; | ||
at_host_url: string; | ||
audit_template_user: string; | ||
audit_template_password: string; | ||
audit_template_city_id: number | null; | ||
audit_template_report_type: string; | ||
audit_template_status_types: string; | ||
audit_template_sync_enabled: boolean; | ||
ubid_threshold: number; | ||
inventory_count: number; | ||
public_feed_enabled: boolean; | ||
public_feed_labels: boolean; | ||
public_geojson_enabled: boolean; | ||
default_reports_x_axis_options: Column[]; | ||
default_reports_y_axis_options: Column[]; | ||
require_2fa: boolean; | ||
} | ||
export type OrganizationsResponse = { | ||
organizations: (BriefOrganization | Organization)[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './user.service' | ||
export * from './user.types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { HttpClient } from '@angular/common/http' | ||
import { inject, Injectable } from '@angular/core' | ||
import type { Observable } from 'rxjs' | ||
import { ReplaySubject, switchMap, take, tap } from 'rxjs' | ||
import type { CurrentUser, SetDefaultOrganizationResponse } from '@seed/api/user' | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class UserService { | ||
private _httpClient = inject(HttpClient) | ||
private _currentUser: ReplaySubject<CurrentUser> = new ReplaySubject<CurrentUser>(1) | ||
currentUser$ = this._currentUser.asObservable() | ||
|
||
/** | ||
* Get the current signed-in user data | ||
*/ | ||
getCurrentUser(): Observable<CurrentUser> { | ||
return this._httpClient.get<CurrentUser>('/api/v3/users/current/').pipe( | ||
tap((user: CurrentUser) => { | ||
this._currentUser.next(user) | ||
}), | ||
) | ||
} | ||
|
||
/** | ||
* Set default org | ||
*/ | ||
setDefaultOrganization(organizationId: number): Observable<SetDefaultOrganizationResponse> { | ||
return this.currentUser$.pipe( | ||
take(1), | ||
switchMap(({ id: userId }) => { | ||
return this._httpClient.put<SetDefaultOrganizationResponse>( | ||
`/api/v3/users/${userId}/default_organization/?organization_id=${organizationId}`, | ||
{}, | ||
) | ||
}), | ||
tap(() => { | ||
// Refresh user info after changing the organization | ||
this.getCurrentUser().subscribe() | ||
}), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export type UserRole = 'viewer' | 'member' | 'owner' | ||
|
||
export type CurrentUser = { | ||
org_id: number; | ||
org_name: string; | ||
org_role: UserRole; | ||
ali_name: string; | ||
ali_id: number; | ||
is_ali_root: boolean; | ||
is_ali_leaf: boolean; | ||
pk: number; | ||
id: number; | ||
first_name: string; | ||
last_name: string; | ||
email: string; | ||
username: string; | ||
is_superuser: boolean; | ||
api_key: string; | ||
} | ||
|
||
export type SetDefaultOrganizationResponse = { | ||
status: string; | ||
user: { | ||
id: number; | ||
access_level_instance: { | ||
id: number; | ||
name: string; | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const { compare: naturalSort } = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.