Skip to content

Commit

Permalink
fixed logic issues
Browse files Browse the repository at this point in the history
  • Loading branch information
MariaBonde committed Jan 3, 2025
1 parent 645e877 commit bdfc187
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@ import { ToggleSwitch } from "./SwitchToggle";
import { useIsCustomerActive } from "@/hooks/CustomerFilter/useIsCustomerActive";

export default function CustomerSidebarWithFilters() {
const [checked, setChecked] = useState(false);
function handleChange(nextChecked: boolean) {
setChecked(nextChecked);
}
const { isCustomerActive, toggleActive } = useIsCustomerActive(
FilteredCustomerContext,
);
const { isCustomerActive, toggleActive } = useIsCustomerActive();
console.log("customer activa", isCustomerActive);
return (
<>
<div className="sidebar z-10 bg-background_grey h-full flex flex-col gap-6 p-4 w-[300px] overflow-y-auto">
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/CustomerTable/SwitchToggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function ToggleSwitch({
value,
onChange,
}: {
value: boolean;
value: boolean | string;
onChange: () => any;
}) {
return (
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/hooks/CustomerFilter/CustomerFilterProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ interface UpdateFilterParams {
search?: string;
engagementIsBillable?: boolean;
bookingType?: EngagementState;
isCustomerActive: boolean;
isCustomerActive: boolean | string;
}
export type UpdateFilters = (updateParams: UpdateFilterParams) => void;
const defaultFilters: CustomerFilters = {
searchFilter: "",
engagementIsBillableFilter: "",
bookingTypeFilter: "",
isCustomerActiveFilter: false,
isCustomerActiveFilter: "",
};

type CustomerFilterContextType = {
Expand Down Expand Up @@ -93,7 +93,7 @@ function useUrlRouteFilter(): [CustomerFilters, UpdateFilters] {
const { bookingType = bookingTypeFilter } = updateParams;
const { isCustomerActive = isCustomerActiveFilter } = updateParams;

const url = `${pathname}?search=${search}&engagementIsBillable=${engagementIsBillable}&bookingType=${bookingType}}&isCustomerActive=${isCustomerActive}`;
const url = `${pathname}?search=${search}&isCustomerActive=${isCustomerActive}`;

setSearchFilter(search);
setEngagementIsBillableFilter(engagementIsBillable);
Expand Down
26 changes: 21 additions & 5 deletions frontend/src/hooks/CustomerFilter/useIsCustomerActive.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import { Context, useContext, useEffect, useState } from "react";
import { FilteredCustomerContext } from "./CustomerFilterProvider";
import { parse } from "next/dist/build/swc";

export function useIsCustomerActive(context: Context<any>) {
const { updateFilters, activeFilters } = useContext(context);
function parseBooleanOrString(value: string | boolean) {
if (typeof value === "string") {
if (value === "true") {
return true;
} else if (value === "false") {
return false;
} else {
return ""; //
}
} else {
return value;
}
}
export function useIsCustomerActive() {
const { updateFilters, activeFilters } = useContext(FilteredCustomerContext);
const isCustomerActiveFilter = activeFilters.isCustomerActiveFilter;

const [isCustomerActive, setIsCustomerActive] = useState<boolean>(
isCustomerActiveFilter,
const parsedActive = parseBooleanOrString(isCustomerActiveFilter);
const valueForToggle = parsedActive === "" ? true : parsedActive;
const [isCustomerActive, setIsCustomerActive] = useState<boolean | string>(
valueForToggle,
);
function toggleActive() {
setIsCustomerActive(!isCustomerActive);
Expand Down

0 comments on commit bdfc187

Please sign in to comment.