Skip to content

Commit

Permalink
chore: Move brand store components into publisher (#4986)
Browse files Browse the repository at this point in the history
  • Loading branch information
steverydz authored Jan 20, 2025
1 parent e20c8e6 commit 090c92b
Show file tree
Hide file tree
Showing 104 changed files with 185 additions and 162 deletions.
131 changes: 0 additions & 131 deletions static/js/brand-store/hooks/index.ts

This file was deleted.

17 changes: 0 additions & 17 deletions static/js/brand-store/utils/index.ts

This file was deleted.

File renamed without changes.
2 changes: 1 addition & 1 deletion static/js/publisher/components/PrimaryNav/PrimaryNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
SideNavigationText,
} from "@canonical/react-components";

import { usePublisher } from "../../../brand-store/hooks";
import { usePublisher } from "../../hooks";

function PrimaryNav({
collapseNavigation,
Expand Down
18 changes: 18 additions & 0 deletions static/js/publisher/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,28 @@ import useValidationSets from "./useValidationSets";
import useValidationSet from "./useValidationSet";
import useMutateListingData from "./useMutateListingData";
import useVerified from "./useVerified";
import useBrandStores from "./useBrandStores";
import useSnaps from "./useSnaps";
import useMembers from "./useMembers";
import useInvites from "./useInvites";
import useSigningKeys from "./useSigningKeys";
import useModels from "./useModels";
import useBrand from "./useBrand";
import usePolicies from "./usePolicies";
import usePublisher from "./usePublisher";

export {
useValidationSets,
useValidationSet,
useMutateListingData,
useVerified,
useBrandStores,
useSnaps,
useMembers,
useInvites,
useSigningKeys,
useModels,
useBrand,
usePolicies,
usePublisher,
};
24 changes: 24 additions & 0 deletions static/js/publisher/hooks/useBrand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useQuery } from "react-query";

function useBrand(id: string | undefined) {
return useQuery({
queryKey: ["brand", id],
queryFn: async () => {
const response = await fetch(`/api/store/${id}/brand`);

if (!response.ok) {
throw new Error("There was a problem fetching models");
}

const brandData = await response.json();

if (!brandData.success) {
throw new Error(brandData.message);
}

return brandData.data;
},
});
}

export default useBrand;
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletions static/js/publisher/hooks/useModels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useQuery, UseQueryResult } from "react-query";
import type { Model as ModelType } from "../types/shared";

const useModels = (
brandId: string | undefined,
): UseQueryResult<ModelType[], Error> => {
return useQuery<ModelType[], Error>({
queryKey: ["models", brandId],
queryFn: async () => {
const response = await fetch(`/api/store/${brandId}/models`);

if (!response.ok) {
throw new Error("There was a problem fetching models");
}

const modelsData = await response.json();

if (!modelsData.success) {
throw new Error(modelsData.message);
}

return modelsData.data;
},
enabled: !!brandId,
});
};

export default useModels;
31 changes: 31 additions & 0 deletions static/js/publisher/hooks/usePolicies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useQuery } from "react-query";
import type { Policy } from "../types/shared";
import { UsePoliciesResponse, ApiError } from "../types/interfaces";

function usePolicies(
brandId: string | undefined,
modelId: string | undefined,
): UsePoliciesResponse {
return useQuery<Policy[], ApiError>({
queryKey: ["policies", brandId],
queryFn: async () => {
const response = await fetch(
`/api/store/${brandId}/models/${modelId}/policies`,
);

if (!response.ok) {
throw new Error("There was a problem fetching policies");
}

const policiesData = await response.json();

if (!policiesData.success) {
throw new Error(policiesData.message);
}

return policiesData.data;
},
});
}

export default usePolicies;
19 changes: 19 additions & 0 deletions static/js/publisher/hooks/usePublisher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useQuery } from "react-query";

function usePublisher() {
return useQuery("publisher", async () => {
const response = await fetch("/account.json");

if (!response.ok) {
return {
publisher: null,
};
}

const publisherData = await response.json();

return publisherData;
});
}

export default usePublisher;
27 changes: 27 additions & 0 deletions static/js/publisher/hooks/useSigningKeys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useQuery, UseQueryResult } from "react-query";
import type { SigningKey } from "../types/shared";

const useSigningKeys = (
brandId: string | undefined,
): UseQueryResult<SigningKey[], Error> => {
return useQuery<SigningKey[], Error>({
queryKey: ["signingKeys", brandId],
queryFn: async () => {
const response = await fetch(`/api/store/${brandId}/signing-keys`);

if (!response.ok) {
throw new Error("There was a problem fetching signing keys");
}

const signingKeysData = await response.json();

if (!signingKeysData.success) {
throw new Error(signingKeysData.message);
}

return signingKeysData.data;
},
});
};

export default useSigningKeys;
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ import PoliciesTable from "./PoliciesTable";
import CreatePolicyForm from "./CreatePolicyForm";
import Navigation from "../../components/Navigation";

import {
ApiError,
UsePoliciesResponse,
usePolicies,
useSigningKeys,
} from "../../hooks";
import { usePolicies, useSigningKeys } from "../../hooks";
import { ApiError, UsePoliciesResponse } from "../../types/interfaces";
import {
policiesListFilterState,
policiesListState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { MainTable, Button, Modal, Icon } from "@canonical/react-components";

import AppPagination from "../../components/AppPagination";

import { usePolicies, UsePoliciesResponse } from "../../hooks";
import { usePolicies } from "../../hooks";
import { UsePoliciesResponse } from "../../types/interfaces";
import { brandIdState } from "../../state/brandStoreState";
import { filteredPoliciesListState } from "../../state/policiesState";

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions static/js/publisher/types/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Policy } from "../types/shared";

export interface UsePoliciesResponse {
isLoading: boolean;
isError: boolean;
error: unknown;
refetch: () => void;
data: Policy[] | undefined;
}

export interface ApiError {
message: string;
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import checkModelNameExists from "./checkModelNameExists";
import checkModelNameExists from "../checkModelNameExists";

const models = [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import checkSigningKeyExists from "./checkSigningKeyExists";
import checkSigningKeyExists from "../checkSigningKeyExists";

const signingKeys = [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import isClosedPanel from "./isClosedPanel";
import isClosedPanel from "../isClosedPanel";

describe("isClosedPanel", () => {
it("returns true if key isn't at the end of the path", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import maskString from "./maskString";
import maskString from "../maskString";

describe("maskString", () => {
const str = "761bbec69e2844958fcdj48DfP";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sortByDateDescending from "./sortByDateDescending";
import sortByDateDescending from "../sortByDateDescending";

const earlyItem = { "created-at": "2023-06-21T14:10:07.108051" };
const lateItem = { "created-at": "2023-06-22T12:45:28.301419" };
Expand Down
File renamed without changes.
Loading

0 comments on commit 090c92b

Please sign in to comment.