Added logic for local switch update for visual flow

no issue

- Currently, the newsletter switch works on the value update from the API which can have slight delay causing sluggish feeling on using the switch
- Adds logic to locally update the switch state before resetting to whatever value comes from the update so the behavior feels instant
This commit is contained in:
Rish 2020-10-13 18:22:08 +05:30
parent ac162cf1ea
commit e1c99346c7

View File

@ -1,4 +1,5 @@
import React from 'react';
import React, {useContext, useEffect, useState} from 'react';
import AppContext from '../../AppContext';
export const SwitchStyles = `
.gh-portal-for-switch label,
@ -74,17 +75,24 @@ export const SwitchStyles = `
`;
function Switch({id, label, onToggle, checked = false}) {
const {action} = useContext(AppContext);
const [isChecked, setIsChecked] = useState(checked);
const isActionChanged = ['updateNewsletter:failed', 'updateNewsletter:success'].includes(action);
useEffect(() => {
setIsChecked(checked);
}, [checked, isActionChanged]);
return (
<div className="gh-portal-for-switch">
<label className="switch" htmlFor={id}>
<input
type="checkbox"
checked={checked}
checked={isChecked}
id={id}
onChange={() => {}}
aria-label={label}
/>
<span className="input-toggle-component" onClick={(e) => {
setIsChecked(!isChecked);
onToggle(e);
}} data-testid="switch-input"></span>
</label>