Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix form not acting correctly when config changes #64

Merged
merged 1 commit into from
Jan 15, 2025
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
5 changes: 3 additions & 2 deletions src/components/inputs/CheckboxInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ export default function CheckboxInput(props: ConfigurableInputProps) {

const [checked, setChecked] = React.useState(data.defaultValue);

const resetState = React.useCallback(() => {
if (data.preserveDataOnReset || props.preserveSection) {
const resetState = React.useCallback((force = false) => {
if (!force && (data.preserveDataOnReset || props.preserveSection)) {
return;
}
setChecked(data.defaultValue);
}, [data.defaultValue]);

useEvent('resetFields', resetState);
useEvent('forceResetFields', () => resetState(true) );

useEffect(() => {
updateValue(props.code, checked);
Expand Down
6 changes: 5 additions & 1 deletion src/components/inputs/CounterInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ export default function CounterInput(props: ConfigurableInputProps) {

const [value, setValue] = useState(data.defaultValue);

const resetState = useCallback(() => {
const resetState = useCallback((force = false) => {
if(!force && (data.preserveDataOnReset || props.preserveSection)) {
return;
};
setValue(data.defaultValue);
}, []);

useEvent('resetFields', resetState);
useEvent('forceResetFields', () => resetState(true) );

const handleChange = useCallback(
(increment: number) => {
Expand Down
7 changes: 4 additions & 3 deletions src/components/inputs/NumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@ export default function NumberInput(props: ConfigurableInputProps) {

const [value, setValue] = React.useState<number | ''>(data.defaultValue);

const resetState = useCallback(() => {
if (data.preserveDataOnReset || props.preserveSection) {
const resetState = useCallback((force = false) => {
if (!force && (data.preserveDataOnReset || props.preserveSection)) {
return;
}
if (data.autoIncrementOnReset) {
if (!force && data.autoIncrementOnReset) {
setValue(value ?? 0 + 1);
} else {
setValue(data.defaultValue);
}
}, [data.defaultValue]);

useEvent('resetFields', resetState);
useEvent('forceResetFields', () => resetState(true) );

useEffect(() => {
updateValue(props.code, value);
Expand Down
5 changes: 3 additions & 2 deletions src/components/inputs/RangeInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ export default function RangeInput(props: ConfigurableInputProps) {

const [value, setValue] = useState(data.defaultValue);

const resetState = useCallback(() => {
if (data.preserveDataOnReset || props.preserveSection) {
const resetState = useCallback((force = false) => {
if (!force && (data.preserveDataOnReset || props.preserveSection)) {
return;
}
setValue(data.defaultValue);
}, [data.defaultValue]);

useEvent('resetFields', resetState);
useEvent('forceResetFields', () => resetState(true) );

const handleChange = useCallback((value: number[]) => {
setValue(value[0]);
Expand Down
5 changes: 3 additions & 2 deletions src/components/inputs/SelectInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ export default function SelectInput(props: ConfigurableInputProps) {
updateValue(props.code, value);
}, [value]);

const resetState = useCallback(() => {
if (data.preserveDataOnReset || props.preserveSection) {
const resetState = useCallback((force = false) => {
if (!force && (data.preserveDataOnReset || props.preserveSection)) {
return;
}
setValue(data.defaultValue);
}, [data.defaultValue]);

useEvent('resetFields', resetState);
useEvent('forceResetFields', () => resetState(true) );

const handleSelect = useCallback((value: string) => {
setValue(value);
Expand Down
5 changes: 3 additions & 2 deletions src/components/inputs/StringInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ export default function StringInput(props: ConfigurableInputProps) {

const [value, setValue] = React.useState(data.defaultValue);

const resetState = useCallback(() => {
if (data.preserveDataOnReset || props.preserveSection) {
const resetState = useCallback((force = false) => {
if (!force && (data.preserveDataOnReset || props.preserveSection)) {
return;
}
setValue(data.defaultValue);
}, [data.defaultValue]);

useEvent('resetFields', resetState);
useEvent('forceResetFields', () => resetState(true) );

useEffect(() => {
updateValue(props.code, value);
Expand Down
7 changes: 4 additions & 3 deletions src/components/inputs/TimerInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export default function TimerInput(props: ConfigurableInputProps) {

const average = useMemo(() => getAvg(times), [times]);

const resetState = useCallback(() => {
if (data.preserveDataOnReset || props.preserveSection) {
const resetState = useCallback((force = false) => {
if (!force && (data.preserveDataOnReset || props.preserveSection)) {
return;
}

Expand All @@ -43,7 +43,8 @@ export default function TimerInput(props: ConfigurableInputProps) {
}, []);

useEvent('resetFields', resetState);

useEvent('forceResetFields', () => resetState(true) );

function startStop() {
toggleTimer(!isRunning);
}
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface AppEvent<PayloadType = unknown> extends Event {
export interface CustomWindowEventMap extends WindowEventMap {
/* Custom Event */
resetFields: AppEvent<string>;
forceResetFields: AppEvent<string>;
}

export const useEvent = <PayloadType = unknown>(
Expand Down
11 changes: 9 additions & 2 deletions src/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,17 @@ export function resetFields() {
window.dispatchEvent(new CustomEvent('resetFields', { detail: 'reset' }));
}

export function forceResetFields() {
window.dispatchEvent(new CustomEvent('forceResetFields', { detail: 'forceReset' }));
}

export function setFormData(config: Config) {
const oldState = useQRScoutState.getState();

useQRScoutState.setState({ ...oldState, formData: config });
forceResetFields();
const newFieldValues = config.sections.flatMap(s =>
s.fields.map(f => ({ code: f.code, value: f.defaultValue })),
);
useQRScoutState.setState({ ...oldState, fieldValues: newFieldValues, formData: config });
}

export function setConfig(configText: string): Result<void> {
Expand Down
Loading