Skip to content

Commit

Permalink
fixed profile screen date formatting, added reducer for resetting ses…
Browse files Browse the repository at this point in the history
…sion metrics, stats reset when first opening app
  • Loading branch information
andyvo2004 committed Nov 3, 2024
1 parent b6205b8 commit 408a532
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 24 deletions.
32 changes: 32 additions & 0 deletions src/redux/reducers/gameDetailsReducer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,37 @@ const gameDetailsReducer = createSlice({
state.lastSessionsMetrics[0].writing.attempted = false;
state.lastSessionsMetrics[0].trivia.attempted = false;
},
resetLastSessionMetrics(state) {
state.lastSessionsMetrics[0] = {
date: "",
math: {
attempted: false,
questionsAttempted: 0,
questionsCorrect: 0,
finalDifficultyScore: 0,
timePerQuestion: 0,
},
trivia: {
attempted: false,
questionsAttempted: 0,
questionsCorrect: 0,
timePerQuestion: 0,
},
reading: {
attempted: false,
passagesRead: 0,
timePerPassage: 0,
wordsPerMinute: 0,
skipped: false,
},
writing: {
attempted: false,
questionsAnswered: 0,
timePerQuestion: 0,
skipped: false,
},
};
},
updateFullState(state, action: { payload: GameDetails; type: string }) {
return {
...initialState,
Expand Down Expand Up @@ -113,6 +144,7 @@ export default gameDetailsReducer.reducer;

export const {
resetAttempted,
resetLastSessionMetrics,
updateFullState,
completedMath,
completedReading,
Expand Down
4 changes: 2 additions & 2 deletions src/screens/Auth/AuthGuard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import SignUpScreen from "../SignUp/SignUp";
import PersonalInfoScreen from "../SignUp/PersonalInfo";
import { RootState } from "../../redux/rootReducer";
import { getUserAnalytics } from "../../firebase/email_signin";
import { updateFullState } from "../../redux/reducers/gameDetailsReducer";
// import { updateFullState } from "../../redux/reducers/gameDetailsReducer";

type Props = { children: React.ReactNode };

Expand All @@ -32,7 +32,7 @@ function AuthGuard({ children }: Props) {
authenticated: true,
};
dispatch(login(newState));
dispatch(updateFullState(userAnalytics.gameDetails));
// dispatch(updateFullState(userAnalytics.gameDetails));
});
} else {
dispatch(logout());
Expand Down
4 changes: 2 additions & 2 deletions src/screens/Home/HomeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Subject from "../../components/Home/ExerciseSubjects";
import { RootState } from "../../redux/rootReducer";
import { AuthUser } from "../../redux/reducers/authReducer/types";

import { resetAttempted } from "../../redux/reducers/gameDetailsReducer";
import { resetLastSessionMetrics } from "../../redux/reducers/gameDetailsReducer";

const logo = require("../../assets/bei.jpg") as AVPlaybackSource;

Expand All @@ -29,7 +29,7 @@ type Props = NativeStackScreenProps<RootStackParamList, "GameOverview">;
function HomeScreen({ navigation }: Props) {
const dispatch = useDispatch();
useEffect(() => {
dispatch(resetAttempted());
dispatch(resetLastSessionMetrics());
}, [dispatch]);

const userInfo = useSelector<RootState>((state) => state.auth) as AuthUser;
Expand Down
48 changes: 28 additions & 20 deletions src/screens/Profile/ProfileScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function ProfileScreen({ navigation }: Props) {
const panelRef = useRef<SlidingUpPanel>(null);

const [name, setName] = useState("Johannes Qian");
const [dob, setDob] = useState("01-01-2024");
const [dob, setDob] = useState(new Date("2000-12-31T05:00:00.000Z"));
const [areaCode, setAreaCode] = useState("+1");
const [phoneNumber, setPhoneNumber] = useState("1231231234");
const [email, setEmail] = useState("[email protected]");
Expand All @@ -37,7 +37,7 @@ function ProfileScreen({ navigation }: Props) {

const [formData, setFormData] = useState({
name: "Johannes Qian",
dob: "01-01-2024",
dob: "12312000",
areaCode: "+1",
phoneNumber: "1231231234",
email: "[email protected]",
Expand All @@ -54,9 +54,9 @@ function ProfileScreen({ navigation }: Props) {
}

if (userInfo.birthDate) {
setDob(userInfo.birthDate);
setDob(new Date(userInfo.birthDate));
} else {
setDob("01-01-2024");
setDob(new Date("2000-12-31T05:00:00.000Z"));
}

if (userInfo.phoneNumber) {
Expand Down Expand Up @@ -94,11 +94,16 @@ function ProfileScreen({ navigation }: Props) {
}
}, [userInfo]);

const formatDOB = (unformatteddob: Date) => {
return `${unformatteddob.getMonth() + 1} / ${
unformatteddob.getDate() + 1
} / ${unformatteddob.getFullYear()}`;
};

function open() {
const formatteddob = dob.replace(/-/g, "");
setFormData({
name,
dob: formatteddob,
dob: formatDOB(dob).replace(/[ /]/g, ""),
areaCode,
phoneNumber,
email,
Expand Down Expand Up @@ -140,7 +145,7 @@ function ProfileScreen({ navigation }: Props) {
}
};

const formatDOB = (input: string) => {
const formatDOBstring = (input: string) => {
const digitsOnly = input.replace(/-/g, "");
if (digitsOnly.length <= 2) {
return digitsOnly;
Expand All @@ -161,8 +166,10 @@ function ProfileScreen({ navigation }: Props) {
dob: newDob,
}));
};

const cleanInput = input.replace(/[ ./]/g, "");
let digitsOnlyDOB = formData.dob.replace(/-/g, "");
let digitsOnlyDOB = formData.dob;

if (cleanInput.length < digitsOnlyDOB.length) {
// backspace
digitsOnlyDOB = digitsOnlyDOB.slice(0, -1);
Expand Down Expand Up @@ -225,12 +232,12 @@ function ProfileScreen({ navigation }: Props) {
return false;
}

const formatteddob = `${formData.dob.substring(
const formatteddob = `${formData.dob.substring(4)}-${formData.dob.substring(
0,
2,
)}-${formData.dob.substring(2, 4)}-${formData.dob.substring(4)}`;
)}-${formData.dob.substring(2, 4)}`;

if (!/^\d{2}-\d{2}-\d{4}/.test(formatteddob)) {
if (!/^\d{4}-\d{2}-\d{2}/.test(formatteddob)) {
setError("Please enter a valid date of birth");
return false;
}
Expand Down Expand Up @@ -264,18 +271,24 @@ function ProfileScreen({ navigation }: Props) {
);
const secondContactName = userInfo.patientDetails.secondaryContactName;
const secondContactNumber = userInfo.patientDetails.secondaryContactPhone;
const newDob = new Date(
`${formData.dob.substring(4)}-${formData.dob.substring(
0,
2,
)}-${formData.dob.substring(2, 4)}`,
);
try {
const body: Record<string, string> = {
email: formData.email,
firstName,
lastName,
phoneNumber: formData.phoneNumber,
birthDate: formData.dob,
birthDate: newDob.toISOString(),
secondaryContactName: secondContactName,
secondaryContactPhone: secondContactNumber,
};
await internalRequest<UserAnalytics>({
url: "/api/patient/auth/signup",
url: "/api/patient/auth/signup", // replace with edit user endpoint
body,
method: HttpMethod.POST,
});
Expand All @@ -284,12 +297,7 @@ function ProfileScreen({ navigation }: Props) {
return;
}
setName(formData.name);
setDob(
`${formData.dob.substring(0, 2)}-${formData.dob.substring(
2,
4,
)}-${formData.dob.substring(4)}`,
);
setDob(newDob);
setAreaCode(formData.areaCode);
setPhoneNumber(formData.phoneNumber);
setEmail(formData.email);
Expand Down Expand Up @@ -660,7 +668,7 @@ function ProfileScreen({ navigation }: Props) {
paddingHorizontal: "4%",
paddingVertical: "2%",
}}
value={formatDOB(formData.dob)}
value={formatDOBstring(formData.dob)}
onChangeText={(e) => handleChange(e, "dob")}
placeholder="Enter Date of Birth"
accessibilityHint='The text written in this input field will be saved as the user"s date of birth'
Expand Down

0 comments on commit 408a532

Please sign in to comment.