From aacebae37301c2ef25daf2a656f2539e94268b30 Mon Sep 17 00:00:00 2001 From: eglehelms Date: Mon, 5 Dec 2022 16:10:38 +0100 Subject: [PATCH] apply 1st review comments --- .../internal/views/carriers/form.tsx | 13 +++++- .../internal/views/carriers/index.tsx | 43 +++++++++++++++++-- .../internal/views/phone-numbers/index.tsx | 12 +++++- .../internal/views/recent-calls/index.tsx | 17 ++++++-- .../internal/views/speech-services/index.tsx | 8 +++- src/containers/internal/views/users/index.tsx | 17 ++++++-- 6 files changed, 95 insertions(+), 15 deletions(-) diff --git a/src/containers/internal/views/carriers/form.tsx b/src/containers/internal/views/carriers/form.tsx index 4013274..dbb3432 100644 --- a/src/containers/internal/views/carriers/form.tsx +++ b/src/containers/internal/views/carriers/form.tsx @@ -440,7 +440,10 @@ export const CarrierForm = ({ const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); - if (user?.scope === USER_ACCOUNT && user.account_sid !== accountSid) { + if ( + user?.scope === USER_ACCOUNT && + (user.account_sid !== accountSid || !accountSid) + ) { toastError("You do not have permissions to make changes to this Carrier"); return; } @@ -722,7 +725,13 @@ export const CarrierForm = ({ label="Used By" required={false} defaultOption={checkOptions()} - disabled={!!checkOptions()} + disabled={ + user?.scope !== USER_ACCOUNT + ? false + : user.account_sid !== accountSid + ? true + : false + } /> {accountSid && hasLength(applications) && ( <> diff --git a/src/containers/internal/views/carriers/index.tsx b/src/containers/internal/views/carriers/index.tsx index d1ef48a..19513fd 100644 --- a/src/containers/internal/views/carriers/index.tsx +++ b/src/containers/internal/views/carriers/index.tsx @@ -1,4 +1,4 @@ -import React, { useState, useMemo } from "react"; +import React, { useState, useMemo, useEffect } from "react"; import { Link } from "react-router-dom"; import { Button, H1, Icon, M } from "jambonz-ui"; import { @@ -19,6 +19,8 @@ import { } from "src/components"; import { hasLength, hasValue, useFilteredResults } from "src/utils"; import { + API_ACCOUNTS, + API_SERVICE_PROVIDERS, API_SIP_GATEWAY, API_SMPP_GATEWAY, USER_ACCOUNT, @@ -30,8 +32,9 @@ import { Gateways } from "./gateways"; export const Carriers = () => { const user = useSelectState("user"); + const currentServiceProvider = useSelectState("currentServiceProvider"); const [carrier, setCarrier] = useState(null); - const [carriers, refetch] = useServiceProviderData("VoipCarriers"); + const [carriers, setCarriers] = useState(); const [accounts] = useServiceProviderData("Accounts"); const [accountSid, setAccountSid] = useState(""); const [filter, setFilter] = useState(""); @@ -44,13 +47,23 @@ export const Carriers = () => { : carrier.account_sid === null ) : []; - }, [accountSid, carriers]); + }, [accountSid, carrier, carriers]); const filteredCarriers = useFilteredResults( filter, carriersFiltered ); + const getCarriers = (url: string) => { + getFetch(url) + .then(({ json }) => { + setCarriers(json); + }) + .catch((error) => { + toastError(error.msg); + }); + }; + const handleDelete = () => { if (carrier) { if (user?.scope === USER_ACCOUNT && user.account_sid !== accountSid) { @@ -87,7 +100,15 @@ export const Carriers = () => { ) ); }); - refetch(); + if ((user && user?.scope === USER_ACCOUNT) || accountSid) { + getCarriers( + `${API_ACCOUNTS}/${user?.account_sid || accountSid}/VoipCarriers` + ); + } else { + getCarriers( + `${API_SERVICE_PROVIDERS}/${currentServiceProvider?.service_provider_sid}/VoipCarriers` + ); + } setCarrier(null); toastSuccess( <> @@ -101,6 +122,20 @@ export const Carriers = () => { } }; + useEffect(() => { + if (accountSid) { + getCarriers( + `${API_ACCOUNTS}/${user?.account_sid || accountSid}/VoipCarriers` + ); + } else { + if (currentServiceProvider) { + getCarriers( + `${API_SERVICE_PROVIDERS}/${currentServiceProvider.service_provider_sid}/VoipCarriers` + ); + } + } + }, [user, accountSid, currentServiceProvider]); + return ( <>
diff --git a/src/containers/internal/views/phone-numbers/index.tsx b/src/containers/internal/views/phone-numbers/index.tsx index 8e09cab..9c3f6f5 100644 --- a/src/containers/internal/views/phone-numbers/index.tsx +++ b/src/containers/internal/views/phone-numbers/index.tsx @@ -7,7 +7,7 @@ import { putPhoneNumber, useServiceProviderData, } from "src/api"; -import { toastError, toastSuccess } from "src/store"; +import { toastError, toastSuccess, useSelectState } from "src/store"; import { Icons, Section, @@ -30,8 +30,10 @@ import { import { DeletePhoneNumber } from "./delete"; import type { Account, PhoneNumber, Carrier, Application } from "src/api/types"; +import { USER_ACCOUNT } from "src/api/constants"; export const PhoneNumbers = () => { + const user = useSelectState("user"); const [accounts] = useServiceProviderData("Accounts"); const [applications] = useServiceProviderData("Applications"); const [carriers] = useServiceProviderData("VoipCarriers"); @@ -123,7 +125,13 @@ export const PhoneNumbers = () => { /> acct.account_sid === user.account_sid + ) + : accounts + } defaultOption />
diff --git a/src/containers/internal/views/recent-calls/index.tsx b/src/containers/internal/views/recent-calls/index.tsx index fa4feba..6493e47 100644 --- a/src/containers/internal/views/recent-calls/index.tsx +++ b/src/containers/internal/views/recent-calls/index.tsx @@ -3,8 +3,12 @@ import { ButtonGroup, H1, M, MS } from "jambonz-ui"; import dayjs from "dayjs"; import { getRecentCalls, useServiceProviderData } from "src/api"; -import { DATE_SELECTION, PER_PAGE_SELECTION } from "src/api/constants"; -import { toastError } from "src/store"; +import { + DATE_SELECTION, + PER_PAGE_SELECTION, + USER_ACCOUNT, +} from "src/api/constants"; +import { toastError, useSelectState } from "src/store"; import { Section, AccountFilter, @@ -30,6 +34,7 @@ const statusSelection = [ ]; export const RecentCalls = () => { + const user = useSelectState("user"); const [accounts] = useServiceProviderData("Accounts"); const [accountSid, setAccountSid] = useState(""); const [dateFilter, setDateFilter] = useState("today"); @@ -86,7 +91,13 @@ export const RecentCalls = () => {
acct.account_sid === user.account_sid + ) + : accounts + } /> {
acct.account_sid === user.account_sid + ) + : accounts + } label="Used by" defaultOption /> diff --git a/src/containers/internal/views/users/index.tsx b/src/containers/internal/views/users/index.tsx index 8cad50c..3878668 100644 --- a/src/containers/internal/views/users/index.tsx +++ b/src/containers/internal/views/users/index.tsx @@ -4,7 +4,11 @@ import { Link } from "react-router-dom"; import { useApiData, useServiceProviderData } from "src/api"; import { ROUTE_INTERNAL_USERS } from "src/router/routes"; -import { USER_SCOPE_SELECTION, USER_ADMIN } from "src/api/constants"; +import { + USER_SCOPE_SELECTION, + USER_ADMIN, + USER_ACCOUNT, +} from "src/api/constants"; import { Section, @@ -20,6 +24,7 @@ import type { Account, User } from "src/api/types"; import { useSelectState } from "src/store"; export const Users = () => { + const user = useSelectState("user"); const currentServiceProvider = useSelectState("currentServiceProvider"); const [users] = useApiData("Users"); const [filter, setFilter] = useState(""); @@ -82,8 +87,14 @@ export const Users = () => { /> acct.account_sid === user.account_sid + ) + : accounts + } + defaultOption />