Commit f534c5d5 authored by janithgamage1.ed's avatar janithgamage1.ed

fix: update

Desc : user API frontend config
parent 302c6bba
......@@ -150,7 +150,7 @@ export const deleteUser = async (req, res) => {
await User.findByIdAndDelete(id);
res.status(200);
res.json({ code: "01", "message": "User Deleted Successfully" });
res.json({ code: "01", result: id, "message": "User Deleted Successfully" });
} catch (error) {
res.status(404);
res.json({ code: "00", "message": error.message });
......
// third-party
import { createSlice } from '@reduxjs/toolkit';
// project imports
import { axiosServices } from 'utils/axios';
import { dispatch } from '../index';
// types
import { DefaultRootStateProps, User } from 'types/user';
// ----------------------------------------------------------------------
const initialState: DefaultRootStateProps['user'] = {
error: null,
success: null,
users: [],
user: null,
isLoading: false
};
const slice = createSlice({
name: 'user',
initialState,
reducers: {
// TO INITIAL STATE
hasInitialState(state) {
state.error = null;
state.success = null;
state.isLoading = false;
},
// HAS ERROR
hasError(state, action) {
state.error = action.payload;
},
startLoading(state) {
state.isLoading = true;
},
finishLoading(state) {
state.isLoading = false;
},
// POST USER
addUserSuccess(state, action) {
state.users.push(action.payload.result);
state.success = "User created successfully."
},
// GET USER
fetchUserSuccess(state, action) {
state.user = action.payload.result;
state.success = null
},
// GET ALL USERS
fetchUsersSuccess(state, action) {
state.users = action.payload;
state.success = null
},
// UPDATE USER
updateUserSuccess(state, action) {
const updatedUserIndex = state.users.findIndex(user => user._id === action.payload.result._id);
if (updatedUserIndex !== -1) {
state.users[updatedUserIndex] = action.payload;
}
state.success = "User updated successfully."
},
// DELETE USER
deleteUserSuccess(state, action) {
state.users = state.users.filter(user => user._id !== action.payload.result);
state.success = "User deleted successfully."
},
}
});
// Reducer
export default slice.reducer;
// ----------------------------------------------------------------------
/**
* TO INITIAL STATE
* @returns
*/
export function toInitialState() {
return async () => {
dispatch(slice.actions.hasInitialState())
}
}
/**
* POST USER
* @param newUser
* @returns
*/
export function addUser(newUser: User) {
return async () => {
dispatch(slice.actions.startLoading());
try {
const response = await axiosServices.post('/user/sign-up', newUser);
dispatch(slice.actions.addUserSuccess(response.data));
} catch (error) {
dispatch(slice.actions.hasError(error));
} finally {
dispatch(slice.actions.finishLoading());
}
};
}
/**
* GET USER
* @param id
* @returns
*/
export function fetchUser(id: number) {
return async () => {
dispatch(slice.actions.startLoading());
try {
const response = await axiosServices.get(`/user/${id}`);
dispatch(slice.actions.fetchUserSuccess(response.data));
} catch (error) {
dispatch(slice.actions.hasError(error));
} finally {
dispatch(slice.actions.finishLoading());
}
};
}
/**
* GET ALL USERS
* @param queryParams
* @returns
*/
export function fetchUsers() {
return async () => {
dispatch(slice.actions.startLoading());
try {
const response = await axiosServices.get('/user/all');
dispatch(slice.actions.fetchUsersSuccess(response.data));
} catch (error) {
dispatch(slice.actions.hasError(error));
} finally {
dispatch(slice.actions.finishLoading());
}
};
}
/**
* UPDATE USER
* @param updatedUser
* @returns
*/
export function updateUser(updatedUser: User) {
return async () => {
dispatch(slice.actions.startLoading());
try {
const response = await axiosServices.put(`/user/${updatedUser._id}`, updateUser);
dispatch(slice.actions.updateUserSuccess(response.data));
} catch (error) {
dispatch(slice.actions.hasError(error));
} finally {
dispatch(slice.actions.finishLoading());
}
};
}
/**
* DELETE USER
* @param userId
* @returns
*/
export function deleteNutrition(userId: number) {
return async () => {
dispatch(slice.actions.startLoading());
try {
await axiosServices.delete(`/user/${userId}`);
dispatch(slice.actions.deleteUserSuccess(userId));
} catch (error) {
dispatch(slice.actions.hasError(error));
} finally {
dispatch(slice.actions.finishLoading());
}
};
}
// Nutritions list
// Nutrition list
export type Nutrition = {
id: string | number | undefined;
......
// User Type
export type User = {
_id: string | number | undefined;
firstName: string;
lastName: string;
email: string;
contactNumber: string;
type: string;
states: number;
createdAt: Date;
updatedAt: Date;
};
export type Users = {
_id: string | number | undefined;
firstName: string;
lastName: string;
email: string;
contactNumber: string;
type: string;
states: number;
createdAt: Date;
updatedAt: Date;
};
export interface UserStateProps {
users: Users[];
user: User | null;
error: object | string | null;
success: object | string | null;
isLoading: boolean
}
export interface DefaultRootStateProps {
user: UserStateProps;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment