From 93e82a36eec67afce1f0e518dd2062cead0bf25b Mon Sep 17 00:00:00 2001 From: pikiou Date: Fri, 21 Jan 2022 07:34:50 +0100 Subject: [PATCH] Add volunteerDayWishesSet in store --- src/pages/DayWishes/DayWishes.tsx | 93 ++++++++++++++++++++++++++ src/pages/DayWishes/index.tsx | 16 +++++ src/pages/DayWishes/styles.module.scss | 1 + src/pages/TeamWishes/TeamWishes.tsx | 18 ++--- src/routes/index.ts | 6 ++ src/server/gsheets/volunteers.ts | 52 +++++++++++--- src/server/index.ts | 2 + src/server/notificationsSubscribe.ts | 2 - src/services/volunteers.ts | 44 +++++++----- src/store/rootReducer.ts | 2 + src/store/volunteerDayWishesSet.ts | 58 ++++++++++++++++ 11 files changed, 256 insertions(+), 38 deletions(-) create mode 100644 src/pages/DayWishes/DayWishes.tsx create mode 100755 src/pages/DayWishes/index.tsx create mode 100755 src/pages/DayWishes/styles.module.scss create mode 100644 src/store/volunteerDayWishesSet.ts diff --git a/src/pages/DayWishes/DayWishes.tsx b/src/pages/DayWishes/DayWishes.tsx new file mode 100644 index 0000000..4745c7c --- /dev/null +++ b/src/pages/DayWishes/DayWishes.tsx @@ -0,0 +1,93 @@ +import { FC, memo, useCallback, useEffect, useState } from "react" +import { RouteComponentProps } from "react-router-dom" +import { useSelector, shallowEqual, useDispatch } from "react-redux" + +import { AppState, AppThunk } from "../../store" +import { + fetchVolunteerDayWishesSet, + fetchVolunteerDayWishesSetIfNeed, +} from "../../store/volunteerDayWishesSet" +import { VolunteerDayWishes } from "../../services/volunteers" +import { selectUserJwtToken } from "../../store/auth" + +export type Props = RouteComponentProps + +let prevWishes: VolunteerDayWishes | undefined + +const HomePage: FC = (): JSX.Element => { + const dispatch = useDispatch() + const jwtToken = useSelector(selectUserJwtToken) + + const wishesForm = useSelector((state: AppState) => { + const wishes = state.volunteerDayWishesSet?.entity + if (wishes) { + prevWishes = wishes + return wishes + } + return prevWishes + }, shallowEqual) + + const [dayWishes, setDayWishes] = useState(wishesForm?.dayWishes.join(",") || "") + const [dayWishesComment, setDayWishesComment] = useState(wishesForm?.dayWishesComment || "") + + useEffect(() => { + setDayWishes(wishesForm?.dayWishes.join(",") || "") + setDayWishesComment(wishesForm?.dayWishesComment || "") + }, [wishesForm]) + + const onDayWishesChanged = (e: React.ChangeEvent) => + setDayWishes(e.target.value) + const onDayWishesCommentChanged = (e: React.ChangeEvent) => + setDayWishesComment(e.target.value) + + const onSubmit = useCallback( + (event: React.SyntheticEvent): void => { + event.preventDefault() + if (!wishesForm) { + console.error("NO FORM WISHES RECEIVED") + return // Form should not even appear if this happens + } + dispatch( + fetchVolunteerDayWishesSet(jwtToken, 0, { + id: wishesForm.id, + dayWishes: (dayWishes || "").split(","), + dayWishesComment, + }) + ) + }, + [dispatch, jwtToken, wishesForm, dayWishes, dayWishesComment] + ) + + if (jwtToken === undefined) return

Loading...

+ + if (jwtToken) { + return ( +
+ +
+ + +
+ ) + } + return
Besoin d'être identifié
+} + +// Fetch server-side data here +export const loadData = (): AppThunk[] => [fetchVolunteerDayWishesSetIfNeed()] + +export default memo(HomePage) diff --git a/src/pages/DayWishes/index.tsx b/src/pages/DayWishes/index.tsx new file mode 100755 index 0000000..cfa70c9 --- /dev/null +++ b/src/pages/DayWishes/index.tsx @@ -0,0 +1,16 @@ +import loadable from "@loadable/component" + +import { Loading, ErrorBoundary } from "../../components" +import { Props, loadData } from "./DayWishes" + +const HomePage = loadable(() => import("./DayWishes"), { + fallback: , +}) + +export default (props: Props): JSX.Element => ( + + + +) + +export { loadData } diff --git a/src/pages/DayWishes/styles.module.scss b/src/pages/DayWishes/styles.module.scss new file mode 100755 index 0000000..b5c6f69 --- /dev/null +++ b/src/pages/DayWishes/styles.module.scss @@ -0,0 +1 @@ +@import "../../theme/mixins"; diff --git a/src/pages/TeamWishes/TeamWishes.tsx b/src/pages/TeamWishes/TeamWishes.tsx index a3b1854..a66a536 100644 --- a/src/pages/TeamWishes/TeamWishes.tsx +++ b/src/pages/TeamWishes/TeamWishes.tsx @@ -28,17 +28,17 @@ const HomePage: FC = (): JSX.Element => { }, shallowEqual) const [teamWishes, setTeamWishes] = useState(wishesForm?.teamWishes.join(",") || "") - const [teamWishComment, setTeamWishComment] = useState(wishesForm?.teamWishComment || "") + const [teamWishesComment, setTeamWishesComment] = useState(wishesForm?.teamWishesComment || "") useEffect(() => { setTeamWishes(wishesForm?.teamWishes.join(",") || "") - setTeamWishComment(wishesForm?.teamWishComment || "") + setTeamWishesComment(wishesForm?.teamWishesComment || "") }, [wishesForm]) const onTeamWishesChanged = (e: React.ChangeEvent) => setTeamWishes(e.target.value) - const onTeamWishCommentChanged = (e: React.ChangeEvent) => - setTeamWishComment(e.target.value) + const onTeamWishesCommentChanged = (e: React.ChangeEvent) => + setTeamWishesComment(e.target.value) const onSubmit = useCallback( (event: React.SyntheticEvent): void => { @@ -51,11 +51,11 @@ const HomePage: FC = (): JSX.Element => { fetchVolunteerTeamWishesSet(jwtToken, 0, { id: wishesForm.id, teamWishes: (teamWishes || "").split(","), - teamWishComment, + teamWishesComment, }) ) }, - [dispatch, jwtToken, wishesForm, teamWishes, teamWishComment] + [dispatch, jwtToken, wishesForm, teamWishes, teamWishesComment] ) if (jwtToken === undefined) return

Loading...

@@ -73,10 +73,10 @@ const HomePage: FC = (): JSX.Element => {