Skip to content

Instantly share code, notes, and snippets.

@mansha99
Created May 22, 2023 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mansha99/19455925f13d19adab77def553d72bde to your computer and use it in GitHub Desktop.
Save mansha99/19455925f13d19adab77def553d72bde to your computer and use it in GitHub Desktop.
export const fanSpeedInitial = {
level: 0,
status: 'OFF'
};
export const ROTATE_RIGHT_ACTION =
{ type: 'ROTATE_RIGHT' };
export const ROTATE_LEFT_ACTION =
{ type: 'ROTATE_LEFT' }
export function fanSpeedReducer(fanSpeed: any, action: any) {
if (action.type == 'ROTATE_RIGHT') {
if (fanSpeed.level == 0) {
return { level: 1, status: 'SLOW' };
}
if (fanSpeed.level == 1) {
return { level: 2, status: 'MEDIUM' };
}
if (fanSpeed.level == 2) {
return { level: 3, status: 'FAST' };
}
return { level: 0, status: 'OFF' };
}
if (action.type == 'ROTATE_LEFT') {
if (fanSpeed.level == 3) {
return { level: 2, status: 'MEDIUM' };
}
if (fanSpeed.level == 2) {
return { level: 1, status: 'SLOW' };
}
if (fanSpeed.level == 1) {
return { level: 0, status: 'OFF' };
}
return { level: 3, status: 'FAST' };
}
throw new Error();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment