Skip to content

Commit

Permalink
Fixes & improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
nasaownsky committed Feb 7, 2025
1 parent bfd5fbd commit a015d10
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 109 deletions.
15 changes: 14 additions & 1 deletion publisher/src/components/AdminPanel/AdminPanel.styles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =============================================================================

import { CustomDropdownToggle } from "@justice-counts/common/components/Dropdown";
import {
CustomDropdownMenuItem,
CustomDropdownToggle,
CustomDropdownToggleLabel,
} from "@justice-counts/common/components/Dropdown";
import {
HEADER_BAR_HEIGHT,
palette,
Expand Down Expand Up @@ -1002,4 +1006,13 @@ export const ReportingAgencyDropdownWrapper = styled.div`
& ${CustomDropdownToggle} {
height: 32px;
}
& ${CustomDropdownToggleLabel} {
display: block;
text-overflow: ellipsis;
text-align: left;
overflow: hidden;
}
& ${CustomDropdownMenuItem} {
white-space: unset;
}
`;
2 changes: 1 addition & 1 deletion publisher/src/components/AdminPanel/AgencyProvisioning.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ export const AgencyProvisioning: React.FC<ProvisioningProps> = observer(
);

/** Shows mini loader while fetching agency's team members & reporting agencies */
if (teamMemberListLoading && !reportingAgencyMetadata) {
if (teamMemberListLoading || !reportingAgencyMetadata) {
return (
<Styled.ModalContainer>
<Styled.MiniLoaderCenteredContainer>
Expand Down
21 changes: 12 additions & 9 deletions publisher/src/components/AdminPanel/MetricsReportingAgency.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ export const MetricsReportingAgency: React.FC<MetricsReportingAgencyProps> =
)}
<Styled.ReportingAgencyMetricsContainer>
{metrics.map((metric) => {
const currentAgency =
const initialReportingAgency =
reportingAgencyMetadata.reporting_agencies[system].find(
(agency) => agency.metric_key === metric.key
);
const updatedAgency = reportingAgenciesUpdates.find(
const updatedReportingAgency = reportingAgenciesUpdates.find(
(agency) => agency.metric_key === metric.key
);
const selectedAgency = updatedAgency || currentAgency;
const selecteReportingdAgency =
updatedReportingAgency || initialReportingAgency;

const dropdownOptions = [
{
Expand All @@ -80,7 +81,7 @@ export const MetricsReportingAgency: React.FC<MetricsReportingAgencyProps> =
true
),
highlight:
selectedAgency?.reporting_agency_id ===
selecteReportingdAgency?.reporting_agency_id ===
Number(selectedIDToEdit),
},
...reportingAgencyMetadata.reporting_agency_options.map(
Expand All @@ -95,7 +96,7 @@ export const MetricsReportingAgency: React.FC<MetricsReportingAgencyProps> =
false
),
highlight:
selectedAgency?.reporting_agency_id ===
selecteReportingdAgency?.reporting_agency_id ===
option.reporting_agency_id,
})
),
Expand All @@ -104,18 +105,20 @@ export const MetricsReportingAgency: React.FC<MetricsReportingAgencyProps> =
label: "None",
onClick: () =>
updateReportingAgencies(metric.key, null, null, null),
highlight: selectedAgency?.reporting_agency_id === null,
highlight:
selecteReportingdAgency?.reporting_agency_id === null,
},
];

const hasReportingAgency = Boolean(
selectedAgency?.reporting_agency_id
selecteReportingdAgency?.reporting_agency_id
);

const reportingAgencyName =
hasReportingAgency && !selectedAgency?.reporting_agency_name
hasReportingAgency &&
selecteReportingdAgency?.is_self_reported
? "Current Agency"
: selectedAgency?.reporting_agency_name;
: selecteReportingdAgency?.reporting_agency_name;

return (
<Styled.ReportingAgencyMetricWrapper key={metric.key}>
Expand Down
200 changes: 102 additions & 98 deletions publisher/src/stores/AdminPanelStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,45 +376,6 @@ class AdminPanelStore {
}
}

async addOrEditVendor(name: string, url: string, id?: number) {
try {
const response = (await this.api.request({
path: `admin/vendors`,
method: "PUT",
body: { id: id ?? null, name, url },
})) as Response;
const vendorResponse = (await response.json()) as Vendor[];

if (response.status !== 200) {
throw new Error(
`There was an issue ${id ? "editing" : "adding"} vendor.`
);
} else {
await this.fetchVendors();
}

return vendorResponse;
} catch (error) {
if (error instanceof Error) return new Error(error.message);
}
}

async deleteVendor(vendorID: number) {
try {
const response = (await this.api.request({
path: `admin/vendors/${vendorID}`,
method: "DELETE",
})) as Response;

await this.fetchVendors();

return response;
} catch (error) {
if (error instanceof Error)
return new Error(`There was an issue deleting vendor ID ${vendorID}.`);
}
}

async fetchReportingAgency(agencyID: string) {
try {
const response = (await this.api.request({
Expand All @@ -435,65 +396,6 @@ class AdminPanelStore {
}
}

async saveReportingAgencies(
agencyID: string,
reportingAgencies: ReportingAgency[]
) {
try {
const response = (await this.api.request({
path: `admin/agency/${agencyID}/reporting-agency`,
method: "PUT",
body: { reporting_agencies: reportingAgencies },
})) as Response;
const agenciesResponse = (await response.json()) as Response;

if (response.status !== 200) {
throw new Error(`There was an issue saving reporting agencies.`);
}

return agenciesResponse;
} catch (error) {
if (error instanceof Error) return new Error(error.message);
}
}

updateReportingAgencies = (
metricKey: string,
reportingAgencyId: number | null,
reportingAgencyName: string | null,
isSelfReported: boolean | null
) => {
const updatedEntry = {
metric_key: metricKey,
reporting_agency_id: reportingAgencyId,
reporting_agency_name: reportingAgencyName,
is_self_reported: isSelfReported,
};

const existingIndex = this.reportingAgenciesUpdates.findIndex(
(item) => item.metric_key === metricKey
);

if (existingIndex !== -1) {
// Update the existing object
this.reportingAgenciesUpdates[existingIndex] = updatedEntry;
} else {
// Add a new object to the array
this.reportingAgenciesUpdates.push(updatedEntry);
}

/** Return an object in the desired backend data structure */
return {
metric_key: metricKey,
reporting_agency_id: reportingAgencyId,
is_self_reported: isSelfReported,
};
};

resetReportingAgenciesUpdates() {
this.reportingAgenciesUpdates = [];
}

async fetchUsersAndAgencies() {
await Promise.all([
this.fetchUsersOverview(),
Expand Down Expand Up @@ -754,6 +656,108 @@ class AdminPanelStore {
}
}

/** Metrics Reporting Agency */

async saveReportingAgencies(
agencyID: string,
reportingAgencies: ReportingAgency[]
) {
try {
const response = (await this.api.request({
path: `admin/agency/${agencyID}/reporting-agency`,
method: "PUT",
body: { reporting_agencies: reportingAgencies },
})) as Response;
const agenciesResponse = (await response.json()) as Response;

if (response.status !== 200) {
throw new Error(`There was an issue saving reporting agencies.`);
}

return agenciesResponse;
} catch (error) {
if (error instanceof Error) return new Error(error.message);
}
}

updateReportingAgencies = (
metricKey: string,
reportingAgencyId: number | null,
reportingAgencyName: string | null,
isSelfReported: boolean | null
) => {
const updatedEntry = {
metric_key: metricKey,
reporting_agency_id: reportingAgencyId,
reporting_agency_name: reportingAgencyName,
is_self_reported: isSelfReported,
};

const existingIndex = this.reportingAgenciesUpdates.findIndex(
(item) => item.metric_key === metricKey
);

if (existingIndex !== -1) {
// Update the existing object
this.reportingAgenciesUpdates[existingIndex] = updatedEntry;
} else {
// Add a new object to the array
this.reportingAgenciesUpdates.push(updatedEntry);
}

/** Return an object in the desired backend data structure */
return {
metric_key: metricKey,
reporting_agency_id: reportingAgencyId,
is_self_reported: isSelfReported,
};
};

resetReportingAgenciesUpdates() {
this.reportingAgenciesUpdates = [];
}

/** Vendors Management */

async addOrEditVendor(name: string, url: string, id?: number) {
try {
const response = (await this.api.request({
path: `admin/vendors`,
method: "PUT",
body: { id: id ?? null, name, url },
})) as Response;
const vendorResponse = (await response.json()) as Vendor[];

if (response.status !== 200) {
throw new Error(
`There was an issue ${id ? "editing" : "adding"} vendor.`
);
} else {
await this.fetchVendors();
}

return vendorResponse;
} catch (error) {
if (error instanceof Error) return new Error(error.message);
}
}

async deleteVendor(vendorID: number) {
try {
const response = (await this.api.request({
path: `admin/vendors/${vendorID}`,
method: "DELETE",
})) as Response;

await this.fetchVendors();

return response;
} catch (error) {
if (error instanceof Error)
return new Error(`There was an issue deleting vendor ID ${vendorID}.`);
}
}

/** Helpers */

/**
Expand Down

0 comments on commit a015d10

Please sign in to comment.