Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions frontend/src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable camelcase */
/* eslint-disable @typescript-eslint/no-explicit-any */
import axios, { AxiosResponse } from "axios";
import Cookies from "universal-cookie";
import {
Expand Down Expand Up @@ -91,7 +93,6 @@ const cookies = new Cookies();

axios.defaults.headers.common.Authorization = `Token ${cookies.get("Token")}`;

// @ts-ignore
const API_HOST = import.meta.env.VITE_API_URL;

axios.defaults.baseURL = API_HOST;
Expand All @@ -107,22 +108,14 @@ export const instance = axios.create({

const responseBody = (response: AxiosResponse) => response.data;

// make a function that takes an object as input and if it finds a key with the name subscription_filters, it json encodes it, and then returns the whole object
const encodeSubscriptionFilters = (obj: any) => {
if (obj.subscription_filters) {
obj.subscription_filters = JSON.stringify(obj.subscription_filters);
}
return obj;
};

const requests = {
get: (url: string, params?: {}) =>
get: (url: string, params?: object) =>
instance.get(url, params).then(responseBody),
post: (url: string, body: {}, params?: {}) =>
post: (url: string, body: object, params?: object) =>
instance.post(url, body, { params }).then(responseBody),
patch: (url: string, body: {}, params?: {}) =>
patch: (url: string, body: object, params?: object) =>
instance.patch(url, body, { params }).then(responseBody),
delete: (url: string, params?: {}) =>
delete: (url: string, params?: object) =>
instance.delete(url, { params }).then(responseBody),
};

Expand Down Expand Up @@ -321,7 +314,7 @@ export const Authentication = {
organization_name: string;
};
}> => requests.post("app/demo_login/", { username, password }),
logout: (): Promise<{}> => requests.post("app/logout/", {}),
logout: (): Promise<object> => requests.post("app/logout/", {}),
registerCreate: (
register: CreateOrgAccountType
): Promise<{
Expand Down Expand Up @@ -452,9 +445,9 @@ export const Metrics = {
getMetrics: (): Promise<MetricType[]> => requests.get("app/metrics/"),
createMetric: (post: CreateMetricType): Promise<MetricType> =>
requests.post("app/metrics/", post),
deleteMetric: (id: number): Promise<{}> =>
deleteMetric: (id: number): Promise<object> =>
requests.delete(`app/metrics/${id}`),
archiveMetric: (id: string): Promise<{}> =>
archiveMetric: (id: string): Promise<object> =>
requests.patch(`app/metrics/${id}/`, { status: "archived" }),
};

Expand Down
6 changes: 3 additions & 3 deletions frontend/src/components/Plans/PlanTags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface PlanTagsProps {

function PlansTags({ tags, showAddTagButton }: PlanTagsProps) {
return (
<>
<span>
{!tags.length ? (
<Badge className="bg-[#E0E7FF] text-[#3730A3] text-[12px] px-[6px] py-2">
<Badge.Content>+ Add Tag</Badge.Content>
Expand Down Expand Up @@ -37,7 +37,7 @@ function PlansTags({ tags, showAddTagButton }: PlanTagsProps) {
</span>
) : (
tags.map((tag) => (
<span className="flex gap-2">
<span key={tag.tag_name} className="flex gap-2">
<span className="flex gap-2" key={tag.tag_name}>
<Badge className="text-[12px] px-[5px] py-2 bg-white text-black whitespace-nowrap">
<div className="flex gap-2 items-center">
Expand All @@ -57,7 +57,7 @@ function PlansTags({ tags, showAddTagButton }: PlanTagsProps) {
</span>
</span>
)}
</>
</span>
);
}
export default PlansTags;
154 changes: 95 additions & 59 deletions frontend/src/pages/ViewPlans.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ const ViewPlans: FC = () => {
},
[]
);
const { data }: UseQueryResult<PlanType[]> = useQuery<PlanType[]>(
["plan_list"],
() => Plan.getPlans().then((res) => res),
{
onSuccess: (data) => {
setPlans(data);
},
refetchOnMount: "always",
}
);
const createTag = useMutation(
({
plan_id,
Expand All @@ -151,69 +161,74 @@ const ViewPlans: FC = () => {
tags,
}),
{
onSuccess: (_, { plan_id }) => {
queryClient.invalidateQueries("plan_list");
onSuccess: (newData, { plan_id }) => {
if (data) {
const oldData = [...data];
const index = oldData.findIndex((plan) => plan.plan_id === plan_id);
const changedElement = oldData.find(
(plan) => plan.plan_id === plan_id
);
if (index && changedElement) {
changedElement.tags = newData.tags as PlanType["tags"];
oldData[index] = changedElement;
setPlans(oldData);
}
}

queryClient.invalidateQueries(["plan_detail", plan_id]);
queryClient.invalidateQueries("organization");
},
}
);
const { data }: UseQueryResult<PlanType[]> = useQuery<PlanType[]>(
["plan_list"],
() => Plan.getPlans().then((res) => res),
{
onSuccess: (data) => {
setPlans(data);
},
refetchOnMount: "always",
}
);

const getFilteredPlans = useCallback(
() => {
switch (activeKey) {
case "0":
if (!searchQuery && allPlans) {
return allPlans;
}
return allPlans.filter(
(plan) =>
plan.plan_id.toLowerCase().includes(searchQuery.toLowerCase()) ||
plan.plan_name.toLowerCase().includes(searchQuery.toLowerCase())
);
const getFilteredPlans = useCallback(() => {
switch (activeKey) {
case "0":
if (!searchQuery && allPlans) {
return allPlans;
}
return allPlans.filter(
(plan) =>
plan.plan_id.toLowerCase().includes(searchQuery.toLowerCase()) ||
plan.plan_name.toLowerCase().includes(searchQuery.toLowerCase())
);

case "1":
if (!searchQuery) {
return monthlyPlans;
}
return monthlyPlans.filter(
(plan) =>
plan.plan_id.toLowerCase().includes(searchQuery.toLowerCase()) ||
plan.plan_name.toLowerCase().includes(searchQuery.toLowerCase())
);
case "2":
if (!searchQuery) {
return quarterlyPlans;
}
return quarterlyPlans.filter(
(plan) =>
plan.plan_id.toLowerCase().includes(searchQuery.toLowerCase()) ||
plan.plan_name.toLowerCase().includes(searchQuery.toLowerCase())
);
default:
if (!searchQuery) {
return yearlyPlans;
}
return yearlyPlans.filter(
(plan) =>
plan.plan_id.toLowerCase().includes(searchQuery.toLowerCase()) ||
plan.plan_name.toLowerCase().includes(searchQuery.toLowerCase())
);
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[data, searchQuery, activeKey]
);
case "1":
if (!searchQuery) {
return monthlyPlans;
}
return monthlyPlans.filter(
(plan) =>
plan.plan_id.toLowerCase().includes(searchQuery.toLowerCase()) ||
plan.plan_name.toLowerCase().includes(searchQuery.toLowerCase())
);
case "2":
if (!searchQuery) {
return quarterlyPlans;
}
return quarterlyPlans.filter(
(plan) =>
plan.plan_id.toLowerCase().includes(searchQuery.toLowerCase()) ||
plan.plan_name.toLowerCase().includes(searchQuery.toLowerCase())
);
default:
if (!searchQuery) {
return yearlyPlans;
}
return yearlyPlans.filter(
(plan) =>
plan.plan_id.toLowerCase().includes(searchQuery.toLowerCase()) ||
plan.plan_name.toLowerCase().includes(searchQuery.toLowerCase())
);
}
}, [
activeKey,
searchQuery,
allPlans,
monthlyPlans,
quarterlyPlans,
yearlyPlans,
]);
const getFilteredTagsAllPlans = useCallback(
(tagName: string) => {
switch (activeKey) {
Expand Down Expand Up @@ -410,6 +425,9 @@ const ViewPlans: FC = () => {

const result = getFilteredTagsAllPlans(filter);

if (result === undefined) {
return;
}
setPlansWithTagsFilter(
plansWithTagsFilter.filter(
(plan) => plan.plan_name === result?.plan_name
Expand All @@ -422,6 +440,9 @@ const ViewPlans: FC = () => {
const result = getFilteredTagsAllPlans(
tag.tag_name.toLowerCase()
);
if (result === undefined) {
return;
}
setPlansWithTagsFilter((prev) =>
prev
.filter((prev) => prev.from === true)
Expand Down Expand Up @@ -501,7 +522,9 @@ const ViewPlans: FC = () => {
}

const result = getFilteredTagsAllPlans(filter);

if (result === undefined) {
return;
}
setPlansWithTagsFilter(
plansWithTagsFilter.filter(
(plan) => plan.plan_name === result?.plan_name
Expand All @@ -514,6 +537,9 @@ const ViewPlans: FC = () => {
const result = getFilteredTagsAllPlans(
tag.tag_name.toLowerCase()
);
if (result === undefined) {
return;
}
setPlansWithTagsFilter((prev) =>
prev
.filter((prev) => prev.from === true)
Expand Down Expand Up @@ -592,7 +618,9 @@ const ViewPlans: FC = () => {
}

const result = getFilteredTagsAllPlans(filter);

if (result === undefined) {
return;
}
setPlansWithTagsFilter(
plansWithTagsFilter.filter(
(plan) => plan.plan_name === result?.plan_name
Expand All @@ -605,6 +633,9 @@ const ViewPlans: FC = () => {
const result = getFilteredTagsAllPlans(
tag.tag_name.toLowerCase()
);
if (result === undefined) {
return;
}
setPlansWithTagsFilter((prev) =>
prev
.filter((prev) => prev.from === true)
Expand Down Expand Up @@ -682,7 +713,9 @@ const ViewPlans: FC = () => {
}

const result = getFilteredTagsAllPlans(filter);

if (result === undefined) {
return;
}
setPlansWithTagsFilter(
plansWithTagsFilter.filter(
(plan) => plan.plan_name === result?.plan_name
Expand All @@ -695,6 +728,9 @@ const ViewPlans: FC = () => {
const result = getFilteredTagsAllPlans(
tag.tag_name.toLowerCase()
);
if (result === undefined) {
return;
}
setPlansWithTagsFilter((prev) =>
prev
.filter((prev) => prev.from === true)
Expand Down