Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
feat : TickerContext for updating data when action commited
Browse files Browse the repository at this point in the history
  • Loading branch information
kasterra committed Aug 4, 2024
1 parent 61dff92 commit 51341b4
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 18 deletions.
57 changes: 57 additions & 0 deletions app/contexts/TickerContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React, { createContext, useContext, ReactNode, useReducer } from "react";

type TickerType = {
ticker: number;
};

const TickerContext = createContext<TickerType | undefined>(undefined);

type TickerActionType = {
type: "UPDATE_TICKER";
};

const TickerDispatchContext = createContext<
React.Dispatch<TickerActionType> | undefined
>(undefined);

const TickerReducer = (state: TickerType, action: TickerActionType) => {
switch (action.type) {
case "UPDATE_TICKER":
return { ticker: state.ticker + 1 };
default:
throw new Error(`Unhandled action type: ${action.type} in TickerReducer`);
}
};

type TickerProviderProps = {
children: ReactNode;
};

export const TickerProvider = ({ children }: TickerProviderProps) => {
const [state, dispatch] = useReducer(TickerReducer, {
ticker: 0,
});
return (
<TickerContext.Provider value={state}>
<TickerDispatchContext.Provider value={dispatch}>
{children}
</TickerDispatchContext.Provider>
</TickerContext.Provider>
);
};

export const useTicker = () => {
const context = useContext(TickerContext);
if (context === undefined) {
throw new Error("useTicker must be used within a TickerProvider");
}
return context;
};

export const useTickerDispatch = () => {
const context = useContext(TickerDispatchContext);
if (context === undefined) {
throw new Error("useTickerDispatch must be used within a TickerProvider");
}
return context;
};
8 changes: 6 additions & 2 deletions app/routes/_procted+/students+/$lectureId+/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ import { UserEntity } from "~/types/APIResponse";
import { resetPassword } from "~/API/user";
import { mapRoleToString } from "~/util";
import toast from "react-hot-toast";
import { useTicker, useTickerDispatch } from "~/contexts/TickerContext";

const TableHeader = () => {
const navigate = useNavigate();
const auth = useAuth();
const lectureData = useLectureData();
const tickerDispatch = useTickerDispatch();
const dispatchLectureData = useLectureDataDispatch();
const [lectureListLoading, setLectureListLoading] = useState(true);
const [lectureList, setLectureList] = useState<Lecture[]>([]);
Expand Down Expand Up @@ -224,18 +226,20 @@ const TableHeader = () => {
isOpen={isUserAddModalOpen}
onClose={() => {
setIsUserAddModalOpen(false);
tickerDispatch({ type: "UPDATE_TICKER" });
}}
/>
</div>
);
};

const Table = () => {
const auth = useAuth();
const [isLoading, setIsLoading] = useState(false);
const [users, setUsers] = useState<UserEntity[]>([]);
const params = useParams();
const lectureId = params.lectureId!;
const auth = useAuth();
const ticker = useTicker();

useEffect(() => {
async function getData() {
Expand All @@ -248,7 +252,7 @@ const Table = () => {
}
}
getData();
}, [isLoading, params.lectureId]);
}, [isLoading, params.lectureId, ticker]);

function responseDataToMap(res: UserEntity[]) {
const ret: Map<string, ReactNode>[] = [];
Expand Down
9 changes: 6 additions & 3 deletions app/routes/_procted+/students+/$lectureId+/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import styles from "./index.module.css";
import { useEffect } from "react";
import toast from "react-hot-toast";
import { MetaFunction, useNavigate } from "@remix-run/react";
import { TickerProvider } from "~/contexts/TickerContext";

const Wrapper = () => {
const auth = useAuth();
Expand All @@ -15,9 +16,11 @@ const Wrapper = () => {
}
}, []);
return (
<div className={styles.container}>
<Table />
</div>
<TickerProvider>
<div className={styles.container}>
<Table />
</div>
</TickerProvider>
);
};

Expand Down
32 changes: 19 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 51341b4

Please sign in to comment.