Skip to content

Commit

Permalink
Merge pull request #92 from YosephSE/final-fix
Browse files Browse the repository at this point in the history
Final fix
  • Loading branch information
YosephSE authored Sep 17, 2024
2 parents 3392fb6 + 2d9a704 commit 865a17e
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 21 deletions.
7 changes: 5 additions & 2 deletions client/app/LoginChecker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { useEffect } from 'react'
import { useAppDispatch } from '@/redux/Hooks'
import { resetSuccess as resetUser, fetchuser as userLogin } from '@/redux/slices/UserSlice'
import { fetchuser as adminLogin, resetSuccess as resetAdmin } from '@/redux/slices/AdminSlice'
import { useRouter } from 'next/navigation'


const LoginChecker = ({
Expand All @@ -11,9 +12,10 @@ const LoginChecker = ({
children: React.ReactNode
}) => {
const dispatch = useAppDispatch()
const router = useRouter()
useEffect(() => {
const userToken = sessionStorage.getItem('userToken');
const adminToken = sessionStorage.getItem('adminToken');
const userToken = localStorage.getItem('userToken');
const adminToken = localStorage.getItem('adminToken');
const fetchUser = async()=>{
if (userToken) {
await dispatch(userLogin({ token: userToken }));
Expand All @@ -22,6 +24,7 @@ const LoginChecker = ({
if (adminToken) {
await dispatch(adminLogin({ token: adminToken }));
dispatch(resetAdmin())
router.push('/admin/alljobs')
}
}

Expand Down
1 change: 1 addition & 0 deletions client/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const Header = ({ page }: Props) => {
await dispatch(logout())
dispatch(resetSuccess())
handleMenuClose()
router.push('/')
}

return (
Expand Down
5 changes: 2 additions & 3 deletions client/components/JobForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@ interface Props {
const JobForm = ({ page, id}: Props) => {
const postStatus = useAppSelector((state: RootState) => state.jobs);
const dispatch = useAppDispatch()
console.log(postStatus.currentJob)
useEffect(() => {
if(!id){
dispatch(resetCurrentJob())
}
})
dispatch(resetSuccess())
}, [])

useEffect(() => {
const fetchUser = async (_id: string) => {
await dispatch(singlejob(_id))
console.log("requested")
dispatch(resetSuccess())
}
if (id){
Expand Down
17 changes: 9 additions & 8 deletions client/components/admin/JobsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ export default function DataTable() {
const [loading, setLoading] = useState<boolean>(true); // Add loading state
const { resolvedTheme } = useTheme()

useEffect(() => {
const fetchJobs = async () => {
setLoading(true);
await dispatch(alljobs());
};
fetchJobs();
}, [dispatch]);

console.log(allJobs)
useEffect(() => {
if (allJobs) {
setLocalJobs(
Expand Down Expand Up @@ -129,14 +138,6 @@ export default function DataTable() {

const paginationModel = { page: 0, pageSize: 10 };

useEffect(() => {
const fetchJobs = async () => {
setLoading(true);
await dispatch(alljobs());
};
fetchJobs();
}, [dispatch]);

return (
<div className="h-auto w-full max-w-full overflow-x-auto">
{loading ? (
Expand Down
4 changes: 2 additions & 2 deletions client/redux/slices/AdminSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const login = createAsyncThunk(
async (user: LoginUserPayload, { rejectWithValue }) => {
try {
const response = await axios.post(`${api}/admins/login`, user);
sessionStorage.setItem('adminToken', response.data.token);
localStorage.setItem('adminToken', response.data.token);
return response.data;
} catch (error: any) {
return rejectWithValue(error.response?.data?.error || error.response.data.message);
Expand All @@ -54,7 +54,7 @@ export const logout = createAsyncThunk(
async (_, { rejectWithValue }) => {
try {
const response = await axios.post(`${api}/candidates/logout`);
sessionStorage.removeItem('adminToken')
localStorage.removeItem('adminToken')
return response.data;
} catch (error: any) {
return rejectWithValue(error.response?.data?.error || error.response.data.message);
Expand Down
1 change: 0 additions & 1 deletion client/redux/slices/JobSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ const jobSlice = createSlice({
.addCase(postjob.fulfilled, (state, action) => {
state.isLoading = false;
state.isSuccess = true;
state.allJobs?.push(action.payload);
})
.addCase(postjob.rejected, (state, action) => {
state.isLoading = false;
Expand Down
6 changes: 3 additions & 3 deletions client/redux/slices/UserSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const register = createAsyncThunk(
async (user: RegisterUserPayload, { rejectWithValue }) => {
try {
const response = await axios.post(`${api}/candidates/register`, user);
sessionStorage.setItem('userToken', response.data.token);
localStorage.setItem('userToken', response.data.token);
return response.data;
} catch (error: any) {
return rejectWithValue(error.response?.data?.error || error.response.data.message);
Expand All @@ -43,7 +43,7 @@ export const login = createAsyncThunk(
async (user: LoginUserPayload, { rejectWithValue }) => {
try {
const response = await axios.post(`${api}/candidates/login`, user);
sessionStorage.setItem('userToken', response.data.token);
localStorage.setItem('userToken', response.data.token);
return response.data;
} catch (error: any) {
return rejectWithValue(error.response?.data?.error || error.response.data.message);
Expand All @@ -56,7 +56,7 @@ export const logout = createAsyncThunk(
async (_, { rejectWithValue }) => {
try {
const response = await axios.post(`${api}/candidates/logout`);
sessionStorage.removeItem('userToken')
localStorage.removeItem('userToken')
return response.data;
} catch (error: any) {
return rejectWithValue(error.response?.data?.error || error.response.data.message);
Expand Down
4 changes: 2 additions & 2 deletions server/src/controllers/jobs.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ const createJob = asyncHandler(async (req: CustomRequest, res: Response) => {
const newJob = new Job(req.body);
const postedBy = req.user._id;
newJob.postedBy = postedBy;
await newJob.save();
res.status(201).json({ message: "Job created successfully" });
const job = await newJob.save();
res.status(201).json(job);
});

const updateJob = asyncHandler(async (req: Request, res: Response) => {
Expand Down

0 comments on commit 865a17e

Please sign in to comment.