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

Set Default Temp to 26 #124

Merged
merged 5 commits into from
May 26, 2021
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
6 changes: 3 additions & 3 deletions src/components/RemoteControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useAppDispatch, useAppSelector } from "../app/hooks";
import {
decreaseTemperature,
increaseTemperature,
setMode,
toggleMode,
toggleStatus,
} from "../features/ac/acSlice";
import { RootState } from "../app/store";
Expand Down Expand Up @@ -158,7 +158,7 @@ export default function RemoteControl() {
aria-label="cold"
className={classes.margin}
onClick={() => {
dispatch(setMode("cold"));
dispatch(toggleMode("cold"));
}}
>
<AcUnitIcon />
Expand All @@ -181,7 +181,7 @@ export default function RemoteControl() {
className={classes.margin}
style={{ backgroundColor: "orange", color: "white" }}
onClick={() => {
dispatch(setMode("hot"));
dispatch(toggleMode("hot"));
}}
>
<WbSunnyIcon />
Expand Down
35 changes: 34 additions & 1 deletion src/features/ac/acSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ export const acItemKey = {
temperature: namespace + "temperature",
};

// https://baike.baidu.com/item/26度空调节能倡导行动
const defaultTemperature = 26;

const initialState: AcState = {
status: false,
mode: (localStorage.getItem(acItemKey.mode) as AcMode) || "cold",
temperature:
parseInt(localStorage.getItem(acItemKey.temperature) || "") || 20,
parseInt(localStorage.getItem(acItemKey.temperature) || "") ||
defaultTemperature,
};

const maxTemperature = 31;
Expand Down Expand Up @@ -135,4 +139,33 @@ export const decreaseTemperature = (): AppThunk => (dispatch, getState) => {
}
};

/**
* 切换模式
* @param mode
* @returns
*/
export const toggleMode =
(mode: AcMode): AppThunk =>
(dispatch, getState) => {
dispatch(setMode(mode));
const currentTemperature = selectTemperature(getState());
const goodColdTemperature = 26;
const goodHotTemperature = 20;

const recommendedSlogan = (mode: AcMode, temperature: number) =>
`建议将空调的制${
mode === "cold" ? "冷" : "热"
}温度调至 ${temperature} 度以${
mode === "cold" ? "上" : "下"
},为节能减排贡献一份力量!`;

if (mode === "cold" && currentTemperature < goodColdTemperature) {
dispatch(setMessage(recommendedSlogan("cold", goodColdTemperature)));
dispatch(setOpen(true));
} else if (mode === "hot" && currentTemperature > goodHotTemperature) {
dispatch(setMessage(recommendedSlogan("hot", goodHotTemperature)));
dispatch(setOpen(true));
}
};

export default acSlice.reducer;