diff --git a/static/js/brand-store/components/Navigation/Navigation.tsx b/static/js/brand-store/components/Navigation/Navigation.tsx index 12282df181..42ae25c47a 100644 --- a/static/js/brand-store/components/Navigation/Navigation.tsx +++ b/static/js/brand-store/components/Navigation/Navigation.tsx @@ -5,7 +5,8 @@ import { useParams, NavLink } from "react-router-dom"; import Logo from "./Logo"; -import { publisherState, brandIdState } from "../../state/atoms"; +import { publisherState } from "../../state/publisherState"; +import { brandIdState } from "../../state/brandStoreState"; import { brandStoresListSelector } from "../../state/selectors"; import { useBrand, usePublisher } from "../../hooks"; diff --git a/static/js/brand-store/pages/AccountDetails/AccountDetails.tsx b/static/js/brand-store/pages/AccountDetails/AccountDetails.tsx index bacca96abb..8381365e66 100644 --- a/static/js/brand-store/pages/AccountDetails/AccountDetails.tsx +++ b/static/js/brand-store/pages/AccountDetails/AccountDetails.tsx @@ -11,7 +11,7 @@ import { import Navigation from "../../components/Navigation"; -import { publisherState } from "../../state/atoms"; +import { publisherState } from "../../state/publisherState"; function AccountDetails(): ReactNode { const [subscriptionPreferencesChanged, setSubscriptionPreferencesChanged] = diff --git a/static/js/brand-store/pages/AccountDetails/__tests__/AccountDetails.test.tsx b/static/js/brand-store/pages/AccountDetails/__tests__/AccountDetails.test.tsx index 50fc9626b3..d1e45035f4 100644 --- a/static/js/brand-store/pages/AccountDetails/__tests__/AccountDetails.test.tsx +++ b/static/js/brand-store/pages/AccountDetails/__tests__/AccountDetails.test.tsx @@ -8,7 +8,7 @@ import { QueryClient, QueryClientProvider } from "react-query"; import { useRecoilValue } from "recoil"; import { store } from "../../../state/store"; import AccountDetails from "../AccountDetails"; -import { publisherState } from "../../../state/atoms"; +import { publisherState } from "../../../state/publisherState"; const queryClient = new QueryClient(); diff --git a/static/js/brand-store/pages/Model/CreatePolicyForm.tsx b/static/js/brand-store/pages/Model/CreatePolicyForm.tsx index 0651a973c5..bbdecfcb5d 100644 --- a/static/js/brand-store/pages/Model/CreatePolicyForm.tsx +++ b/static/js/brand-store/pages/Model/CreatePolicyForm.tsx @@ -16,9 +16,8 @@ import { useSigningKeys } from "../../hooks"; import { signingKeysListState, newSigningKeyState, - brandIdState, -} from "../../state/atoms"; -import { brandStoreState } from "../../state/selectors"; +} from "../../state/signingKeysState"; +import { brandIdState, brandStoreState } from "../../state/brandStoreState"; type Props = { setShowNotification: Dispatch>; diff --git a/static/js/brand-store/pages/Model/Model.tsx b/static/js/brand-store/pages/Model/Model.tsx index 91589d31e6..80fa40ada1 100644 --- a/static/js/brand-store/pages/Model/Model.tsx +++ b/static/js/brand-store/pages/Model/Model.tsx @@ -12,8 +12,8 @@ import { Notification, } from "@canonical/react-components"; -import { modelsListState, brandIdState } from "../../state/atoms"; -import { currentModelState, brandStoreState } from "../../state/selectors"; +import { modelsListState, currentModelState } from "../../state/modelsState"; +import { brandIdState, brandStoreState } from "../../state/brandStoreState"; import ModelNav from "./ModelNav"; import ModelBreadcrumb from "./ModelBreadcrumb"; diff --git a/static/js/brand-store/pages/Model/Policies.tsx b/static/js/brand-store/pages/Model/Policies.tsx index 9053e66c8f..9ed3986255 100644 --- a/static/js/brand-store/pages/Model/Policies.tsx +++ b/static/js/brand-store/pages/Model/Policies.tsx @@ -25,11 +25,12 @@ import { import { policiesListFilterState, policiesListState, +} from "../../state/policiesState"; +import { signingKeysListState, newSigningKeyState, - brandIdState, -} from "../../state/atoms"; -import { brandStoreState } from "../../state/selectors"; +} from "../../state/signingKeysState"; +import { brandIdState, brandStoreState } from "../../state/brandStoreState"; import { isClosedPanel, setPageTitle } from "../../utils"; diff --git a/static/js/brand-store/pages/Model/PoliciesTable.tsx b/static/js/brand-store/pages/Model/PoliciesTable.tsx index 0d5b24af61..2458194085 100644 --- a/static/js/brand-store/pages/Model/PoliciesTable.tsx +++ b/static/js/brand-store/pages/Model/PoliciesTable.tsx @@ -7,8 +7,8 @@ import { MainTable, Button, Modal, Icon } from "@canonical/react-components"; import AppPagination from "../../components/AppPagination"; import { usePolicies, UsePoliciesResponse } from "../../hooks"; -import { brandIdState } from "../../state/atoms"; -import { filteredPoliciesListState } from "../../state/selectors"; +import { brandIdState } from "../../state/brandStoreState"; +import { filteredPoliciesListState } from "../../state/policiesState"; import type { Policy } from "../../types/shared"; diff --git a/static/js/brand-store/pages/Models/CreateModelForm.tsx b/static/js/brand-store/pages/Models/CreateModelForm.tsx index f56791ce09..54b906ee15 100644 --- a/static/js/brand-store/pages/Models/CreateModelForm.tsx +++ b/static/js/brand-store/pages/Models/CreateModelForm.tsx @@ -8,15 +8,15 @@ import randomstring from "randomstring"; import { checkModelNameExists, setPageTitle } from "../../utils"; import { - brandStoresState, modelsListState, newModelState, - brandIdState, -} from "../../state/atoms"; -import { filteredModelsListState, +} from "../../state/modelsState"; +import { + brandStoresState, + brandIdState, brandStoreState, -} from "../../state/selectors"; +} from "../../state/brandStoreState"; import type { Store, Model } from "../../types/shared"; diff --git a/static/js/brand-store/pages/Models/Models.tsx b/static/js/brand-store/pages/Models/Models.tsx index 3840518ca5..38760d8849 100644 --- a/static/js/brand-store/pages/Models/Models.tsx +++ b/static/js/brand-store/pages/Models/Models.tsx @@ -13,11 +13,10 @@ import { UseQueryResult } from "react-query"; import { modelsListFilterState, modelsListState, - policiesListState, newModelState, - brandIdState, -} from "../../state/atoms"; -import { brandStoreState } from "../../state/selectors"; +} from "../../state/modelsState"; +import { policiesListState } from "../../state/policiesState"; +import { brandIdState, brandStoreState } from "../../state/brandStoreState"; import Filter from "../../components/Filter"; import ModelsTable from "./ModelsTable"; diff --git a/static/js/brand-store/pages/Models/ModelsTable.tsx b/static/js/brand-store/pages/Models/ModelsTable.tsx index c7e5e00d02..998a069266 100644 --- a/static/js/brand-store/pages/Models/ModelsTable.tsx +++ b/static/js/brand-store/pages/Models/ModelsTable.tsx @@ -8,7 +8,7 @@ import AppPagination from "../../components/AppPagination"; import { maskString } from "../../utils"; -import { filteredModelsListState } from "../../state/selectors"; +import { filteredModelsListState } from "../../state/modelsState"; import type { Model as ModelType } from "../../types/shared"; diff --git a/static/js/brand-store/pages/SigningKeys/CreateSigningKeyForm.tsx b/static/js/brand-store/pages/SigningKeys/CreateSigningKeyForm.tsx index 72faf13977..d79d7251e5 100644 --- a/static/js/brand-store/pages/SigningKeys/CreateSigningKeyForm.tsx +++ b/static/js/brand-store/pages/SigningKeys/CreateSigningKeyForm.tsx @@ -8,13 +8,10 @@ import { checkSigningKeyExists, setPageTitle } from "../../utils"; import { signingKeysListState, - newSigningKeyState, - brandIdState, -} from "../../state/atoms"; -import { filteredSigningKeysListState, - brandStoreState, -} from "../../state/selectors"; + newSigningKeyState, +} from "../../state/signingKeysState"; +import { brandIdState, brandStoreState } from "../../state/brandStoreState"; import type { SigningKey } from "../../types/shared"; diff --git a/static/js/brand-store/pages/SigningKeys/SigningKeys.tsx b/static/js/brand-store/pages/SigningKeys/SigningKeys.tsx index 89acc1c1b0..2e913c726e 100644 --- a/static/js/brand-store/pages/SigningKeys/SigningKeys.tsx +++ b/static/js/brand-store/pages/SigningKeys/SigningKeys.tsx @@ -14,11 +14,10 @@ import { useSigningKeys, useModels } from "../../hooks"; import { signingKeysListState, signingKeysListFilterState, - policiesListState, newSigningKeyState, - brandIdState, -} from "../../state/atoms"; -import { brandStoreState } from "../../state/selectors"; +} from "../../state/signingKeysState"; +import { policiesListState } from "../../state/policiesState"; +import { brandIdState, brandStoreState } from "../../state/brandStoreState"; import Filter from "../../components/Filter"; import SigningKeysTable from "./SigningKeysTable"; diff --git a/static/js/brand-store/pages/SigningKeys/SigningKeysTable.tsx b/static/js/brand-store/pages/SigningKeys/SigningKeysTable.tsx index 708d193e03..e0f5048aca 100644 --- a/static/js/brand-store/pages/SigningKeys/SigningKeysTable.tsx +++ b/static/js/brand-store/pages/SigningKeys/SigningKeysTable.tsx @@ -9,8 +9,11 @@ import DeactivateSigningKeyModal from "./DeactivateSigningKeyModal"; import { sortByDateDescending } from "../../utils"; -import { filteredSigningKeysListState } from "../../state/selectors"; -import { signingKeysListState, brandIdState } from "../../state/atoms"; +import { + filteredSigningKeysListState, + signingKeysListState, +} from "../../state/signingKeysState"; +import { brandIdState } from "../../state/brandStoreState"; import type { SigningKey } from "../../types/shared"; import type { ItemType } from "../../components/AppPagination/AppPagination"; diff --git a/static/js/brand-store/routes/App/App.tsx b/static/js/brand-store/routes/App/App.tsx index 485485b7d0..ab6a41969a 100644 --- a/static/js/brand-store/routes/App/App.tsx +++ b/static/js/brand-store/routes/App/App.tsx @@ -11,7 +11,7 @@ import { import { fetchStores } from "../../state/slices/brandStoreSlice"; import { brandStoresListSelector } from "../../state/selectors"; -import { brandStoresState } from "../../state/atoms"; +import { brandStoresState } from "../../state/brandStoreState"; import AccountDetails from "../../pages/AccountDetails"; import Snaps from "../../pages/Snaps"; diff --git a/static/js/brand-store/state/atoms/index.ts b/static/js/brand-store/state/atoms/index.ts deleted file mode 100644 index f9f82ce8ab..0000000000 --- a/static/js/brand-store/state/atoms/index.ts +++ /dev/null @@ -1,82 +0,0 @@ -import { atom } from "recoil"; -import type { - Store, - Model, - Policy, - SigningKey, - Publisher, -} from "../../types/shared"; - -const brandStoresState = atom({ - key: "brandStores", - default: [] as Array, -}); - -const modelsListState = atom({ - key: "modelsList", - default: [] as Array, -}); - -const modelsListFilterState = atom({ - key: "modelsListFilter", - default: "" as string, -}); - -const newModelState = atom({ - key: "newModel", - default: { - name: "", - apiKey: "", - }, -}); - -const policiesListState = atom({ - key: "policiesList", - default: [] as Array, -}); - -const policiesListFilterState = atom({ - key: "policiesListFilter", - default: "" as string, -}); - -const signingKeysListState = atom({ - key: "signingKeysListState", - default: [], -}); - -const signingKeysListFilterState = atom({ - key: "signingKeysListFilter", - default: "" as string, -}); - -const newSigningKeyState = atom({ - key: "newSigningKey", - default: { - name: "", - }, -}); - -const publisherState = atom({ - key: "publisher", - default: null as Publisher, -}); - -const brandIdState = atom({ - key: "brandId", - default: "", -}); - -export { - brandStoresState, - modelsListState, - modelsListFilterState, - newModelState, - policiesListState, - policiesListFilterState, - signingKeysListState, - signingKeysListFilterState, - newSigningKeyState, - publisherState, - brandIdState, -}; diff --git a/static/js/brand-store/state/brandStoreState.ts b/static/js/brand-store/state/brandStoreState.ts new file mode 100644 index 0000000000..40165653a9 --- /dev/null +++ b/static/js/brand-store/state/brandStoreState.ts @@ -0,0 +1,25 @@ +import { atom, selectorFamily } from "recoil"; + +import type { Store } from "../types/shared"; + +const brandStoresState = atom({ + key: "brandStores", + default: [] as Array, +}); + +const brandIdState = atom({ + key: "brandId", + default: "", +}); + +const brandStoreState = selectorFamily({ + key: "brandStore", + get: + (storeId) => + ({ get }) => { + const brandStores = get(brandStoresState); + return brandStores.find((store) => store.id === storeId); + }, +}); + +export { brandStoresState, brandIdState, brandStoreState }; diff --git a/static/js/brand-store/state/modelsState.ts b/static/js/brand-store/state/modelsState.ts new file mode 100644 index 0000000000..fef203939c --- /dev/null +++ b/static/js/brand-store/state/modelsState.ts @@ -0,0 +1,81 @@ +import { atom, selector, selectorFamily } from "recoil"; + +import { policiesListState } from "./policiesState"; + +import { Model } from "../types/shared"; + +function getFilteredModels(models: Array, filterQuery?: string | null) { + if (!filterQuery) { + return models; + } + + return models.filter((model: Model) => { + if ( + (model.name && model.name.includes(filterQuery)) || + (model["api-key"] && model["api-key"].includes(filterQuery)) || + (model["created-at"] && model["created-at"].includes(filterQuery)) || + (model["modified-at"] && model["modified-at"].includes(filterQuery)) + ) { + return true; + } + + return false; + }); +} + +const modelsListState = atom({ + key: "modelsList", + default: [] as Array, +}); + +const modelsListFilterState = atom({ + key: "modelsListFilter", + default: "" as string, +}); + +const newModelState = atom({ + key: "newModel", + default: { + name: "", + apiKey: "", + }, +}); + +const filteredModelsListState = selector>({ + key: "filteredModelsList", + get: ({ get }) => { + const filter = get(modelsListFilterState); + const models = get(modelsListState); + const policies = get(policiesListState); + const modelsWithPolicies = models.map((model) => { + const policy = policies.find( + (policy) => policy["model-name"] === model.name, + ); + + return { + ...model, + "policy-revision": policy ? policy.revision : undefined, + }; + }); + + return getFilteredModels(modelsWithPolicies, filter); + }, +}); + +const currentModelState = selectorFamily({ + key: "currentModel", + get: + (modelId) => + ({ get }) => { + const models = get(modelsListState); + return models.find((model) => model.name === modelId); + }, +}); + +export { + modelsListState, + modelsListFilterState, + newModelState, + filteredModelsListState, + currentModelState, +}; diff --git a/static/js/brand-store/state/policiesState.ts b/static/js/brand-store/state/policiesState.ts new file mode 100644 index 0000000000..1e6674b5d4 --- /dev/null +++ b/static/js/brand-store/state/policiesState.ts @@ -0,0 +1,68 @@ +import { atom, selector } from "recoil"; + +import { signingKeysListState } from "./signingKeysState"; + +import { Policy } from "../types/shared"; + +function getFilteredPolicies( + policies: Array, + filterQuery?: string | null, +) { + if (!filterQuery) { + return policies; + } + + return policies.filter((policy: Policy) => { + if ( + (policy["signing-key-name"] && + policy["signing-key-name"].includes(filterQuery)) || + policy["created-at"].includes(filterQuery) || + (policy["modified-at"] && policy["modified-at"].includes(filterQuery)) || + policy.revision.toString().includes(filterQuery) + ) { + return true; + } + + return false; + }); +} + +const policiesListState = atom({ + key: "policiesList", + default: [] as Array, +}); + +const policiesListFilterState = atom({ + key: "policiesListFilter", + default: "" as string, +}); + +const filteredPoliciesListState = selector>({ + key: "filteredPoliciesList", + get: ({ get }) => { + const filter = get(policiesListFilterState); + const policies = get(policiesListState); + const signingKeys = get(signingKeysListState); + const policiesWithKeys = policies.map((policy) => { + const signingKey = signingKeys.find( + (key) => key["sha3-384"] === policy["signing-key-sha3-384"], + ); + + return { + ...policy, + "signing-key-name": signingKey?.name, + }; + }); + + return getFilteredPolicies(policiesWithKeys, filter); + }, + set: ({ set }, newValue) => { + set(policiesListState, newValue); + }, +}); + +export { + policiesListState, + policiesListFilterState, + filteredPoliciesListState, +}; diff --git a/static/js/brand-store/state/publisherState.ts b/static/js/brand-store/state/publisherState.ts new file mode 100644 index 0000000000..cd12478128 --- /dev/null +++ b/static/js/brand-store/state/publisherState.ts @@ -0,0 +1,9 @@ +import { atom } from "recoil"; +import type { Publisher } from "../types/shared"; + +const publisherState = atom({ + key: "publisher", + default: null as Publisher, +}); + +export { publisherState }; diff --git a/static/js/brand-store/state/selectors/index.ts b/static/js/brand-store/state/selectors/index.ts index 515982b503..d239d428af 100644 --- a/static/js/brand-store/state/selectors/index.ts +++ b/static/js/brand-store/state/selectors/index.ts @@ -1,33 +1,11 @@ -import { selector, selectorFamily } from "recoil"; - -import { - modelsListFilterState, - modelsListState, - policiesListState, - policiesListFilterState, - signingKeysListState, - brandStoresState, - signingKeysListFilterState, -} from "../atoms"; - -import { - getFilteredModels, - getFilteredPolicies, - getFilteredSigningKeys, -} from "../../utils"; - import type { StoresSlice, CurrentStoreSlice, SnapsSlice, InvitesSlice, MembersSlice, - Model, - Policy, - SigningKey, } from "../../types/shared"; -// Redux selectors const brandStoresListSelector = (state: StoresSlice) => state.brandStores.brandStoresList; const currentStoreSelector = (state: CurrentStoreSlice) => @@ -36,111 +14,10 @@ const snapsSelector = (state: SnapsSlice) => state.snaps.snaps; const membersSelector = (state: MembersSlice) => state.members.members; const invitesSelector = (state: InvitesSlice) => state.invites.invites; -// Recoil selectors -const filteredModelsListState = selector>({ - key: "filteredModelsList", - get: ({ get }) => { - const filter = get(modelsListFilterState); - const models = get(modelsListState); - const policies = get(policiesListState); - const modelsWithPolicies = models.map((model) => { - const policy = policies.find( - (policy) => policy["model-name"] === model.name, - ); - - return { - ...model, - "policy-revision": policy ? policy.revision : undefined, - }; - }); - - return getFilteredModels(modelsWithPolicies, filter); - }, -}); - -const currentModelState = selectorFamily({ - key: "currentModel", - get: - (modelId) => - ({ get }) => { - const models = get(modelsListState); - return models.find((model) => model.name === modelId); - }, -}); - -const filteredPoliciesListState = selector>({ - key: "filteredPoliciesList", - get: ({ get }) => { - const filter = get(policiesListFilterState); - const policies = get(policiesListState); - const signingKeys = get(signingKeysListState); - const policiesWithKeys = policies.map((policy) => { - const signingKey = signingKeys.find( - (key) => key["sha3-384"] === policy["signing-key-sha3-384"], - ); - - return { - ...policy, - "signing-key-name": signingKey?.name, - }; - }); - - return getFilteredPolicies(policiesWithKeys, filter); - }, - set: ({ set }, newValue) => { - set(policiesListState, newValue); - }, -}); - -const brandStoreState = selectorFamily({ - key: "brandStore", - get: - (storeId) => - ({ get }) => { - const brandStores = get(brandStoresState); - return brandStores.find((store) => store.id === storeId); - }, -}); - -const filteredSigningKeysListState = selector>({ - key: "filteredSigningKeysList", - get: ({ get }) => { - const filter = get(signingKeysListFilterState); - const policies = get(policiesListState); - const signingKeys = get(signingKeysListState); - const signingKeysWithPolicies = signingKeys.map((signingKey) => { - const matchingPolicies = policies.filter((policy) => { - return policy["signing-key-sha3-384"] === signingKey["sha3-384"]; - }); - - const signingKeyModels: string[] = []; - - matchingPolicies.forEach((policy) => { - if (!signingKeyModels.includes(policy["model-name"])) { - signingKeyModels.push(policy["model-name"]); - } - }); - - return { - ...signingKey, - models: signingKeyModels, - policies: matchingPolicies, - }; - }); - - return getFilteredSigningKeys(signingKeysWithPolicies, filter); - }, -}); - export { brandStoresListSelector, currentStoreSelector, snapsSelector, membersSelector, invitesSelector, - filteredModelsListState, - currentModelState, - filteredPoliciesListState, - brandStoreState, - filteredSigningKeysListState, }; diff --git a/static/js/brand-store/state/signingKeysState.ts b/static/js/brand-store/state/signingKeysState.ts new file mode 100644 index 0000000000..eeeb6990d0 --- /dev/null +++ b/static/js/brand-store/state/signingKeysState.ts @@ -0,0 +1,84 @@ +import { atom, selector } from "recoil"; + +import { SigningKey } from "../types/shared"; + +import { policiesListState } from "./policiesState"; + +function getFilteredSigningKeys( + signingKeys: Array, + filterQuery?: string | null, +) { + if (!filterQuery) { + return signingKeys; + } + + return signingKeys.filter((signingKey: SigningKey) => { + if ( + (signingKey.name && signingKey.name.includes(filterQuery)) || + (signingKey["created-at"] && + signingKey["created-at"].includes(filterQuery)) || + (signingKey["modified-at"] && + signingKey["modified-at"].includes(filterQuery)) || + (signingKey.fingerprint && + signingKey.fingerprint.toString().includes(filterQuery)) + ) { + return true; + } + + return false; + }); +} + +const signingKeysListState = atom({ + key: "signingKeysListState", + default: [], +}); + +const signingKeysListFilterState = atom({ + key: "signingKeysListFilter", + default: "" as string, +}); + +const newSigningKeyState = atom({ + key: "newSigningKey", + default: { + name: "", + }, +}); + +const filteredSigningKeysListState = selector>({ + key: "filteredSigningKeysList", + get: ({ get }) => { + const filter = get(signingKeysListFilterState); + const policies = get(policiesListState); + const signingKeys = get(signingKeysListState); + const signingKeysWithPolicies = signingKeys.map((signingKey) => { + const matchingPolicies = policies.filter((policy) => { + return policy["signing-key-sha3-384"] === signingKey["sha3-384"]; + }); + + const signingKeyModels: string[] = []; + + matchingPolicies.forEach((policy) => { + if (!signingKeyModels.includes(policy["model-name"])) { + signingKeyModels.push(policy["model-name"]); + } + }); + + return { + ...signingKey, + models: signingKeyModels, + policies: matchingPolicies, + }; + }); + + return getFilteredSigningKeys(signingKeysWithPolicies, filter); + }, +}); + +export { + signingKeysListState, + signingKeysListFilterState, + newSigningKeyState, + filteredSigningKeysListState, +}; diff --git a/static/js/brand-store/utils/getFilteredModels.test.ts b/static/js/brand-store/utils/getFilteredModels.test.ts deleted file mode 100644 index 358a2485b9..0000000000 --- a/static/js/brand-store/utils/getFilteredModels.test.ts +++ /dev/null @@ -1,64 +0,0 @@ -import getFilteredModels from "./getFilteredModels"; - -const mockModels = [ - { - name: "model-1", - "api-key": "e15a9abc390b4514a35752f5851e27b8", - "created-at": "2022-03-29T13:03:11.095Z", - "created-by": { - "display-name": "John Doe", - email: "john.doe@example.com", - id: "johndoe123", - username: "johndoe", - validation: "unverified", - }, - "modified-at": "2023-06-27T13:06:25.541Z", - "modified-by": null, - }, - { - name: "model-2", - "api-key": "80eb997804364d1a9008777584082e3d", - "created-at": "2018-07-14T13:03:35.452Z", - "created-by": { - "display-name": "John Doe", - email: "john.doe@example.com", - id: "johndoe123", - username: "johndoe", - validation: "unverified", - }, - "modified-at": "2023-06-29T13:06:52.630Z", - "modified-by": null, - }, - { - name: "model-3", - "api-key": "fea502ee59c249369073b96034b56770", - "created-at": "2021-10-31T13:07:10.090Z", - "created-by": { - "display-name": "John Doe", - email: "john.doe@example.com", - id: "johndoe123", - username: "johndoe", - validation: "unverified", - }, - "modified-at": "2023-06-29T13:03:35.452Z", - "modified-by": null, - }, -]; - -describe("getFilteredModels", () => { - it("returns unfiltered models if no filter query", () => { - expect(getFilteredModels(mockModels).length).toEqual(mockModels.length); - expect(getFilteredModels(mockModels)[0].name).toEqual(mockModels[0].name); - expect(getFilteredModels(mockModels)[1].name).toEqual(mockModels[1].name); - expect(getFilteredModels(mockModels)[2].name).toEqual(mockModels[2].name); - }); - - it("returns no models if filter query doesn't match", () => { - expect(getFilteredModels(mockModels, "foobar").length).toEqual(0); - }); - - it("returns filtered models if query matches", () => { - expect(getFilteredModels(mockModels, "model-2").length).toBe(1); - expect(getFilteredModels(mockModels, "model-2")[0].name).toEqual("model-2"); - }); -}); diff --git a/static/js/brand-store/utils/getFilteredModels.ts b/static/js/brand-store/utils/getFilteredModels.ts deleted file mode 100644 index 725439967e..0000000000 --- a/static/js/brand-store/utils/getFilteredModels.ts +++ /dev/null @@ -1,22 +0,0 @@ -import type { Model } from "../types/shared"; - -function getFilteredModels(models: Array, filterQuery?: string | null) { - if (!filterQuery) { - return models; - } - - return models.filter((model: Model) => { - if ( - (model.name && model.name.includes(filterQuery)) || - (model["api-key"] && model["api-key"].includes(filterQuery)) || - (model["created-at"] && model["created-at"].includes(filterQuery)) || - (model["modified-at"] && model["modified-at"].includes(filterQuery)) - ) { - return true; - } - - return false; - }); -} - -export default getFilteredModels; diff --git a/static/js/brand-store/utils/getFilteredPolicies.test.ts b/static/js/brand-store/utils/getFilteredPolicies.test.ts deleted file mode 100644 index 3366de4f77..0000000000 --- a/static/js/brand-store/utils/getFilteredPolicies.test.ts +++ /dev/null @@ -1,72 +0,0 @@ -import getFilteredPolicies from "./getFilteredPolicies"; - -const mockPolicies = [ - { - "created-at": "2023-07-05T15:11:21.922Z", - "created-by": { - "display-name": "John Doe", - email: "john.doe@canonical.com", - id: "asdf79807asdf9807", - username: "johndoe", - validation: "unproven", - }, - "model-name": "model-1", - revision: 1, - "signing-key-sha3-384": - "95d3bf18447370259bab86ccc650ed3fe9ffb3ec2f8d55b4beafc09efc38405ccecb9e3c0de86ae6f89466493fbf8bbe", - }, - { - "created-at": "2023-07-05T15:12:00.978Z", - "created-by": { - "display-name": "John Doe", - email: "john.doe@canonical.com", - id: "asdf79807asdf9807", - username: "johndoe", - validation: "unproven", - }, - "model-name": "model-2", - revision: 5, - "signing-key-sha3-384": - "5d81aace4895e5d9d8e4acec88c5cfbde4dade62cecfe6aa26167ad8127aaa5b866ae4d1786f6be1667e87be3b7abbe6", - }, - { - "created-at": "2023-07-05T15:12:47.701Z", - "created-by": { - "display-name": "John Doe", - email: "john.doe@canonical.com", - id: "asdf79807asdf9807", - username: "johndoe", - validation: "unproven", - }, - "model-name": "model-3", - revision: 43, - "signing-key-sha3-384": - "7a46d3f9afa14ce259726b5abf7c154b9ee20df97b80403792c9076551ae618bdfdf63dd2a57e81b4dd0c1a1b68940b6", - }, -]; - -describe("getFilteredPolicies", () => { - it("returns unfiltered policies if no filter query", () => { - expect(getFilteredPolicies(mockPolicies).length).toEqual( - mockPolicies.length, - ); - expect(getFilteredPolicies(mockPolicies)[0]["revision"]).toEqual( - mockPolicies[0]["revision"], - ); - expect(getFilteredPolicies(mockPolicies)[1]["revision"]).toEqual( - mockPolicies[1]["revision"], - ); - expect(getFilteredPolicies(mockPolicies)[2]["revision"]).toEqual( - mockPolicies[2]["revision"], - ); - }); - - it("returns no policies if filter query doesn't match", () => { - expect(getFilteredPolicies(mockPolicies, "foobar").length).toEqual(0); - }); - - it("returns filtered policies if query matches", () => { - expect(getFilteredPolicies(mockPolicies, "43").length).toBe(1); - expect(getFilteredPolicies(mockPolicies, "43")[0]["revision"]).toEqual(43); - }); -}); diff --git a/static/js/brand-store/utils/getFilteredPolicies.ts b/static/js/brand-store/utils/getFilteredPolicies.ts deleted file mode 100644 index 8eed69eb42..0000000000 --- a/static/js/brand-store/utils/getFilteredPolicies.ts +++ /dev/null @@ -1,26 +0,0 @@ -import type { Policy } from "../types/shared"; - -function getFilteredPolicies( - policies: Array, - filterQuery?: string | null, -) { - if (!filterQuery) { - return policies; - } - - return policies.filter((policy: Policy) => { - if ( - (policy["signing-key-name"] && - policy["signing-key-name"].includes(filterQuery)) || - policy["created-at"].includes(filterQuery) || - (policy["modified-at"] && policy["modified-at"].includes(filterQuery)) || - policy.revision.toString().includes(filterQuery) - ) { - return true; - } - - return false; - }); -} - -export default getFilteredPolicies; diff --git a/static/js/brand-store/utils/getFilteredSigningKeys.test.ts b/static/js/brand-store/utils/getFilteredSigningKeys.test.ts deleted file mode 100644 index 00ee80bdc9..0000000000 --- a/static/js/brand-store/utils/getFilteredSigningKeys.test.ts +++ /dev/null @@ -1,103 +0,0 @@ -import getFilteredSigningKeys from "./getFilteredSigningKeys"; - -const mockSigningKeys = [ - { - name: "signing-key-1", - "created-at": "2022-07-18T13:03:11.095Z", - "created-by": { - "display-name": "John Doe", - email: "john.doe@canonical.com", - id: "asdf79807asdf9807", - username: "johndoe", - validation: "unproven", - }, - "modified-at": "2022-07-18T14:03:11.095Z", - "modified-by": { - "display-name": "John Doe", - email: "john.doe@canonical.com", - id: "asdf79807asdf9807", - username: "johndoe", - validation: "unproven", - }, - fingerprint: "fingerprint1", - "sha3-384": - "96c3bf18447370259bab86ccc650ed3fe9ffb3ec2f8d55b4beafc09efc38405ccecb9e3c0de86ae6f89466493fbf8bbe", - models: [], - }, - { - name: "signing-key-2", - "created-at": "2022-07-19T13:03:11.095Z", - "created-by": { - "display-name": "John Doe", - email: "john.doe@canonical.com", - id: "asdf79807asdf9807", - username: "johndoe", - validation: "unproven", - }, - "modified-at": "2022-07-19T15:03:11.095Z", - "modified-by": { - "display-name": "John Doe", - email: "john.doe@canonical.com", - id: "asdf79807asdf9807", - username: "johndoe", - validation: "unproven", - }, - fingerprint: "fingerprint1", - "sha3-384": - "98d5bf18447370259bab86ccc650ed3fe9ffb3ec2f8d55b4beafc09efc38405ccecb9e3c0de86ae6f89466493fbf8bbe", - models: [], - }, - { - name: "signing-key-3", - "created-at": "2022-07-20T13:03:11.095Z", - "created-by": { - "display-name": "John Doe", - email: "john.doe@canonical.com", - id: "asdf79807asdf9807", - username: "johndoe", - validation: "unproven", - }, - "modified-at": "2022-07-20T15:03:11.095Z", - "modified-by": { - "display-name": "John Doe", - email: "john.doe@canonical.com", - id: "asdf79807asdf9807", - username: "johndoe", - validation: "unproven", - }, - fingerprint: "fingerprint1", - "sha3-384": - "99a7bf18447370259bab86ccc650ed3fe9ffb3ec2f8d55b4beafc09efc38405ccecb9e3c0de86ae6f89466493fbf8bbe", - models: [], - }, -]; - -describe("getFilteredSigningKeys", () => { - it("returns unfiltered signing keys if no filter query", () => { - expect(getFilteredSigningKeys(mockSigningKeys).length).toEqual( - mockSigningKeys.length, - ); - expect(getFilteredSigningKeys(mockSigningKeys)[0].name).toEqual( - mockSigningKeys[0].name, - ); - expect(getFilteredSigningKeys(mockSigningKeys)[1].name).toEqual( - mockSigningKeys[1].name, - ); - expect(getFilteredSigningKeys(mockSigningKeys)[2].name).toEqual( - mockSigningKeys[2].name, - ); - }); - - it("returns no signing keys if filter query doesn't match", () => { - expect(getFilteredSigningKeys(mockSigningKeys, "foobar").length).toEqual(0); - }); - - it("returns filtered signing keys if query matches", () => { - expect( - getFilteredSigningKeys(mockSigningKeys, "signing-key-1").length, - ).toBe(1); - expect( - getFilteredSigningKeys(mockSigningKeys, "signing-key-2")[0].name, - ).toEqual("signing-key-2"); - }); -}); diff --git a/static/js/brand-store/utils/getFilteredSigningKeys.ts b/static/js/brand-store/utils/getFilteredSigningKeys.ts deleted file mode 100644 index 49bca5bea1..0000000000 --- a/static/js/brand-store/utils/getFilteredSigningKeys.ts +++ /dev/null @@ -1,28 +0,0 @@ -import type { SigningKey } from "../types/shared"; - -function getFilteredSigningKeys( - signingKeys: Array, - filterQuery?: string | null, -) { - if (!filterQuery) { - return signingKeys; - } - - return signingKeys.filter((signingKey: SigningKey) => { - if ( - (signingKey.name && signingKey.name.includes(filterQuery)) || - (signingKey["created-at"] && - signingKey["created-at"].includes(filterQuery)) || - (signingKey["modified-at"] && - signingKey["modified-at"].includes(filterQuery)) || - (signingKey.fingerprint && - signingKey.fingerprint.toString().includes(filterQuery)) - ) { - return true; - } - - return false; - }); -} - -export default getFilteredSigningKeys; diff --git a/static/js/brand-store/utils/index.ts b/static/js/brand-store/utils/index.ts index ee542091e5..afdd022068 100644 --- a/static/js/brand-store/utils/index.ts +++ b/static/js/brand-store/utils/index.ts @@ -1,22 +1,16 @@ import checkModelNameExists from "./checkModelNameExists"; -import getFilteredModels from "./getFilteredModels"; -import getFilteredPolicies from "./getFilteredPolicies"; import isClosedPanel from "./isClosedPanel"; import maskString from "./maskString"; import setPageTitle from "./setPageTitle"; -import getFilteredSigningKeys from "./getFilteredSigningKeys"; import checkSigningKeyExists from "./checkSigningKeyExists"; import sortByDateDescending from "./sortByDateDescending"; import getPolicies from "./getPolicies"; export { checkModelNameExists, - getFilteredModels, - getFilteredPolicies, isClosedPanel, maskString, setPageTitle, - getFilteredSigningKeys, checkSigningKeyExists, sortByDateDescending, getPolicies,