-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: using states instead of saving on url
- Loading branch information
Showing
8 changed files
with
270 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 104 additions & 69 deletions
173
web/app/components/Validations/Configurations/UITab.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,124 @@ | ||
import { useLocation, useNavigate } from "@remix-run/react"; | ||
import { useContext } from "react"; | ||
import { Dispatch, SetStateAction, useContext, useEffect } from "react"; | ||
import { Input } from "~/components/Input"; | ||
import { ValidationsContext } from "~/contexts/validations.context"; | ||
import { SearchParams } from "~/utils"; | ||
import { Eras, IUiConfigs } from "~/interfaces"; | ||
import { | ||
AlonzoValidations, | ||
BabbageValidations, | ||
ByronValidations, | ||
ShelleyMAValidations, | ||
UIOptions, | ||
} from "~/utils"; | ||
|
||
export const UITab = () => { | ||
const { validations } = useContext(ValidationsContext); | ||
const navigate = useNavigate(); | ||
const location = useLocation(); | ||
const shownValidations = | ||
new URLSearchParams(location.search).get(SearchParams.LIST)?.split(",") ?? | ||
[]; | ||
export const UITab = ({ | ||
uiConfigs, | ||
setUiConfigs, | ||
}: { | ||
uiConfigs: IUiConfigs; | ||
setUiConfigs: Dispatch<SetStateAction<IUiConfigs>>; | ||
}) => { | ||
const { context, validations: contextValidations } = | ||
useContext(ValidationsContext); | ||
|
||
useEffect(() => { | ||
let newValidations = contextValidations.map((v) => v.name); | ||
switch (context.selectedEra) { | ||
case Eras.Byron: | ||
newValidations = ByronValidations; | ||
break; | ||
case Eras.ShelleyMA: | ||
newValidations = ShelleyMAValidations; | ||
break; | ||
case Eras.Alonzo: | ||
newValidations = AlonzoValidations; | ||
break; | ||
case Eras.Babbage: | ||
newValidations = BabbageValidations; | ||
break; | ||
} | ||
setUiConfigs((prev) => ({ | ||
...prev, | ||
validationsToSee: newValidations, | ||
})); | ||
}, [context.selectedEra]); | ||
|
||
const changeValidations = | ||
(validation: string) => (event: React.ChangeEvent<HTMLInputElement>) => { | ||
const isChecked = !event.target.checked; | ||
const newSearchParams = new URLSearchParams(location.search); | ||
const currentValues = | ||
newSearchParams.get(SearchParams.LIST)?.split(",") ?? []; | ||
|
||
if (isChecked && !currentValues.includes(validation)) { | ||
currentValues.push(validation); | ||
} else { | ||
const index = currentValues.indexOf(validation); | ||
if (index > -1) currentValues.splice(index, 1); | ||
} | ||
|
||
if (currentValues.length > 0) | ||
newSearchParams.set(SearchParams.LIST, currentValues.join(",")); | ||
else newSearchParams.delete(SearchParams.LIST); | ||
|
||
navigate(`?${newSearchParams.toString()}`); | ||
const isChecked = event.target.checked; | ||
setUiConfigs((prev) => ({ | ||
...prev, | ||
validationsToSee: isChecked | ||
? prev.validationsToSee.includes(validation) | ||
? prev.validationsToSee | ||
: [...prev.validationsToSee, validation] | ||
: prev.validationsToSee.filter((v) => v !== validation), | ||
})); | ||
}; | ||
|
||
const changeQuery = | ||
(q: SearchParams) => (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const changeUIOptions = | ||
(q: UIOptions) => (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const isChecked = e.target.checked; | ||
const newSearchParams = new URLSearchParams(location.search); | ||
|
||
newSearchParams.set(q, `${isChecked}`); | ||
|
||
navigate(`?${newSearchParams.toString()}`, { replace: true }); | ||
setUiConfigs((prev) => ({ ...prev, [q]: isChecked })); | ||
}; | ||
|
||
const searchParams = new URLSearchParams(location.search); | ||
const initialOpen = searchParams.get(SearchParams.OPEN) === "true"; | ||
const beginning = searchParams.get(SearchParams.BEGINNING) === "true"; | ||
const initialOpen = uiConfigs.alwaysOpen; | ||
const beginning = uiConfigs.beginning; | ||
|
||
const validations = | ||
context.selectedEra === Eras.Byron | ||
? ByronValidations | ||
: context.selectedEra === Eras.ShelleyMA | ||
? ShelleyMAValidations | ||
: context.selectedEra === Eras.Alonzo | ||
? AlonzoValidations | ||
: context.selectedEra === Eras.Babbage | ||
? BabbageValidations | ||
: []; | ||
|
||
return ( | ||
<div className="flex flex-col overflow-y-auto h-96"> | ||
<div className="text-left text-3xl mb-3">Select validations to show</div> | ||
{validations.map((validation, index, arr) => ( | ||
<div key={validation.name}> | ||
<Input | ||
name={validation.name} | ||
id={validation.name} | ||
label={validation.name} | ||
isCheckbox | ||
checked={shownValidations.includes(validation.name)} | ||
onChange={changeValidations(validation.name)} | ||
/> | ||
{index !== arr.length - 1 && <hr />} | ||
<div | ||
className={`${ | ||
context.selectedEra === Eras.Conway ? "hidden" : "block" | ||
}`} | ||
> | ||
<div className="text-left text-3xl mb-3"> | ||
Select validations to show | ||
</div> | ||
))} | ||
<hr className="border-2 border-black my-4" /> | ||
<div> | ||
<div className="text-left text-3xl mt-3">Others</div> | ||
{validations.map((validation, index, arr) => ( | ||
<div key={validation}> | ||
<Input | ||
name={validation} | ||
id={validation} | ||
label={validation} | ||
isCheckbox | ||
checked={uiConfigs.validationsToSee.includes(validation)} | ||
onChange={changeValidations(validation)} | ||
/> | ||
{index !== arr.length - 1 && <hr />} | ||
</div> | ||
))} | ||
|
||
<Input | ||
name={"alwaysOpen"} | ||
id={"alwaysOpen"} | ||
label={"Validations section always open"} | ||
isCheckbox | ||
checked={initialOpen} | ||
onChange={changeQuery(SearchParams.OPEN)} | ||
/> | ||
<Input | ||
name={"beginning"} | ||
id={"beginning"} | ||
label={"Show at beginning"} | ||
isCheckbox | ||
checked={beginning} | ||
onChange={changeQuery(SearchParams.BEGINNING)} | ||
/> | ||
<hr className="border-2 border-black my-4" /> | ||
</div> | ||
<div className="text-left text-3xl mt-3">Others</div> | ||
|
||
<Input | ||
name={"alwaysOpen"} | ||
id={"alwaysOpen"} | ||
label={"Validations section always open"} | ||
isCheckbox | ||
checked={initialOpen} | ||
onChange={changeUIOptions(UIOptions.OPEN)} | ||
/> | ||
<Input | ||
name={"beginning"} | ||
id={"beginning"} | ||
label={"Show at beginning"} | ||
isCheckbox | ||
checked={beginning} | ||
onChange={changeUIOptions(UIOptions.BEGINNING)} | ||
/> | ||
</div> | ||
); | ||
}; |
Oops, something went wrong.