| import { useTranslation } from "react-i18next"; | import { useTranslation } from "react-i18next"; | ||||
| import { USER_DETAILS_LOADING } from "../../store/actions/users/usersActionConstants"; | import { USER_DETAILS_LOADING } from "../../store/actions/users/usersActionConstants"; | ||||
| import { selectIsLoadingByActionType } from "../../store/selectors/loadingSelectors"; | import { selectIsLoadingByActionType } from "../../store/selectors/loadingSelectors"; | ||||
| import { selectIsCategoriesChecked } from "../../store/selectors/categoriesSelector"; | |||||
| import { changeIsCheckedCategory, setIsCategoriesCheckedReq } from "../../store/actions/categories/categoriesAction"; | |||||
| import { | |||||
| selectChangedCategories, | |||||
| selectIsCategoriesChecked, | |||||
| } from "../../store/selectors/categoriesSelector"; | |||||
| import { | |||||
| changeIsCheckedCategory, | |||||
| setGrantCategoriesReq, | |||||
| setIsCategoriesCheckedReq, | |||||
| } from "../../store/actions/categories/categoriesAction"; | |||||
| import Button from "../../components/Button/Button"; | import Button from "../../components/Button/Button"; | ||||
| const UserDetails = ({ history }) => { | const UserDetails = ({ history }) => { | ||||
| const [showDelete, setDelete] = useState(false); | const [showDelete, setDelete] = useState(false); | ||||
| const { t } = useTranslation(); | const { t } = useTranslation(); | ||||
| const categories = useSelector(selectIsCategoriesChecked); | const categories = useSelector(selectIsCategoriesChecked); | ||||
| const changedCategories = useSelector(selectChangedCategories); | |||||
| const { user, errorMessage } = useSelector((s) => s.userDetails); | const { user, errorMessage } = useSelector((s) => s.userDetails); | ||||
| ); | ); | ||||
| const handleChangingCategoriesIsChecked = (id, isChecked) => { | const handleChangingCategoriesIsChecked = (id, isChecked) => { | ||||
| dispatch(changeIsCheckedCategory({id, isChecked})) | |||||
| dispatch(changeIsCheckedCategory({ id, isChecked })); | |||||
| }; | |||||
| const onGrantCategoriesHandler = () => { | |||||
| dispatch( | |||||
| setGrantCategoriesReq({ userId: id, categories: changedCategories }) | |||||
| ); | |||||
| }; | }; | ||||
| return isLoading ? ( | return isLoading ? ( | ||||
| <div style={{ display: "flex", justifyContent: "center" }}> | <div style={{ display: "flex", justifyContent: "center" }}> | ||||
| <Button | <Button | ||||
| variant="contained" | variant="contained" | ||||
| disabled | |||||
| disabled={ | |||||
| !changedCategories || changedCategories.length === 0 | |||||
| } | |||||
| sx={{ mt: 3, mb: 2 }} | sx={{ mt: 3, mb: 2 }} | ||||
| fullWidth | fullWidth | ||||
| className="c-btn c-btn--primary w-289 f" | className="c-btn c-btn--primary w-289 f" | ||||
| type="submit" | type="submit" | ||||
| onClick={onGrantCategoriesHandler} | |||||
| > | > | ||||
| Apply Changes | Apply Changes | ||||
| </Button> | </Button> |
| }, | }, | ||||
| categories: { | categories: { | ||||
| allCategories: base + "/categories/names", | allCategories: base + "/categories/names", | ||||
| isCategoriesChecked: base + "/categories/granted-categories" | |||||
| isCategoriesChecked: base + "/categories/granted-categories", | |||||
| grantCategory: base + "/users/grant-category", | |||||
| }, | }, | ||||
| tags: { | tags: { | ||||
| allTags: base + "/tags/names", | allTags: base + "/tags/names", | ||||
| fileTags: { | fileTags: { | ||||
| filters: base + "/tags", | filters: base + "/tags", | ||||
| }, | }, | ||||
| files:{ | |||||
| files: { | |||||
| uploadFile: base + "/files", | uploadFile: base + "/files", | ||||
| all: base + "/files/filtered" | |||||
| } | |||||
| all: base + "/files/filtered", | |||||
| }, | |||||
| }; | }; |
| import { getRequest } from "."; | |||||
| import { getRequest, postRequest } from "."; | |||||
| import apiEndpoints from "./apiEndpoints"; | import apiEndpoints from "./apiEndpoints"; | ||||
| export const getAllCategories = () => | export const getAllCategories = () => | ||||
| export const getIsCategoriesChecked = (userId) => | export const getIsCategoriesChecked = (userId) => | ||||
| getRequest(apiEndpoints.categories.isCategoriesChecked + "?userId=" + userId); | getRequest(apiEndpoints.categories.isCategoriesChecked + "?userId=" + userId); | ||||
| export const grantCategoryRequest = (payload) => | |||||
| postRequest(apiEndpoints.categories.grantCategory, payload); |
| FETCH_IS_CATEGORIES_CHECKED_ERR, | FETCH_IS_CATEGORIES_CHECKED_ERR, | ||||
| FETCH_IS_CATEGORIES_CHECKED_REQ, | FETCH_IS_CATEGORIES_CHECKED_REQ, | ||||
| FETCH_IS_CATEGORIES_CHECKED_SUCCESS, | FETCH_IS_CATEGORIES_CHECKED_SUCCESS, | ||||
| CHANGE_IC_CHECKED_CATEGORY | |||||
| CHANGE_IC_CHECKED_CATEGORY, | |||||
| GRANT_CATEGORY_REQ, | |||||
| GRANT_CATEGORY_ERR, | |||||
| GRANT_CATEGORY_SUCCESS | |||||
| } from "./categoriesActionConstants"; | } from "./categoriesActionConstants"; | ||||
| export const setCategoriesReq = () => ({ | export const setCategoriesReq = () => ({ | ||||
| payload, | payload, | ||||
| }); | }); | ||||
| export const setGrantCategoriesReq = (payload) => ({ | |||||
| type: GRANT_CATEGORY_REQ, | |||||
| payload | |||||
| }); | |||||
| export const setGrantCategoriesError = (payload) => ({ | |||||
| type: GRANT_CATEGORY_ERR, | |||||
| payload, | |||||
| }); | |||||
| export const setGrantCategories = (payload) => ({ | |||||
| type: GRANT_CATEGORY_SUCCESS, | |||||
| payload, | |||||
| }); | |||||
| const FETCH_CATEGORIES_SCOPE = "FETCH_CATEGORIES"; | const FETCH_CATEGORIES_SCOPE = "FETCH_CATEGORIES"; | ||||
| const FETCH_IS_CATEGORIES_CHECKED_SCOPE = "FETCH_IS_CATEGORIES_CHECKED"; | const FETCH_IS_CATEGORIES_CHECKED_SCOPE = "FETCH_IS_CATEGORIES_CHECKED"; | ||||
| const GRANT_CATEGORY_SCOPE = "GRANT_CATEGORY"; | |||||
| export const FETCH_CATEGORIES_REQ = createFetchType(FETCH_CATEGORIES_SCOPE); | export const FETCH_CATEGORIES_REQ = createFetchType(FETCH_CATEGORIES_SCOPE); | ||||
| export const FETCH_CATEGORIES_ERR = createErrorType(FETCH_CATEGORIES_SCOPE); | export const FETCH_CATEGORIES_ERR = createErrorType(FETCH_CATEGORIES_SCOPE); | ||||
| FETCH_IS_CATEGORIES_CHECKED_SCOPE | FETCH_IS_CATEGORIES_CHECKED_SCOPE | ||||
| ); | ); | ||||
| export const GRANT_CATEGORY_REQ = createFetchType(GRANT_CATEGORY_SCOPE); | |||||
| export const GRANT_CATEGORY_ERR = createErrorType(GRANT_CATEGORY_SCOPE); | |||||
| export const GRANT_CATEGORY_SUCCESS = createSuccessType(GRANT_CATEGORY_SCOPE); | |||||
| export const CHANGE_IC_CHECKED_CATEGORY = "CHANGE_IC_CHECKED_CATEGORY"; | export const CHANGE_IC_CHECKED_CATEGORY = "CHANGE_IC_CHECKED_CATEGORY"; |
| FETCH_CATEGORIES_SUCCESS, | FETCH_CATEGORIES_SUCCESS, | ||||
| FETCH_IS_CATEGORIES_CHECKED_ERR, | FETCH_IS_CATEGORIES_CHECKED_ERR, | ||||
| FETCH_IS_CATEGORIES_CHECKED_SUCCESS, | FETCH_IS_CATEGORIES_CHECKED_SUCCESS, | ||||
| GRANT_CATEGORY_ERR, | |||||
| GRANT_CATEGORY_SUCCESS, | |||||
| } from "../../actions/categories/categoriesActionConstants"; | } from "../../actions/categories/categoriesActionConstants"; | ||||
| import createReducer from "../../utils/createReducer"; | import createReducer from "../../utils/createReducer"; | ||||
| [FETCH_CATEGORIES_ERR]: setCategoriesErrorMessage, | [FETCH_CATEGORIES_ERR]: setCategoriesErrorMessage, | ||||
| [FETCH_IS_CATEGORIES_CHECKED_SUCCESS]: setStateIsCategoriesChecked, | [FETCH_IS_CATEGORIES_CHECKED_SUCCESS]: setStateIsCategoriesChecked, | ||||
| [FETCH_IS_CATEGORIES_CHECKED_ERR]: setIsCategoriesCheckedErrorMessage, | [FETCH_IS_CATEGORIES_CHECKED_ERR]: setIsCategoriesCheckedErrorMessage, | ||||
| [CHANGE_IC_CHECKED_CATEGORY]: setIsCheckedCategory | |||||
| [CHANGE_IC_CHECKED_CATEGORY]: setIsCheckedCategory, | |||||
| [GRANT_CATEGORY_SUCCESS]: setStateGrantCategories, | |||||
| [GRANT_CATEGORY_ERR]: setGrantCategoriesErrorMessage, | |||||
| }, | }, | ||||
| initialState | initialState | ||||
| ); | ); | ||||
| ); | ); | ||||
| var tmpChangedCategories = [...state.changedCategories]; | var tmpChangedCategories = [...state.changedCategories]; | ||||
| if (tmpChangedCategoriesIndex === -1) { | |||||
| if (tmpChangedCategoriesIndex === -1) { | |||||
| tmpChangedCategories = [ | tmpChangedCategories = [ | ||||
| ...tmpChangedCategories, | ...tmpChangedCategories, | ||||
| { id: action.payload.id, isChecked: !action.payload.isChecked }, | { id: action.payload.id, isChecked: !action.payload.isChecked }, | ||||
| tmpChangedCategories.splice(tmpChangedCategoriesIndex, 1); | tmpChangedCategories.splice(tmpChangedCategoriesIndex, 1); | ||||
| } | } | ||||
| const tmpIndex = state.isCategoriesChecked.findIndex((x) => x.id === action.payload.id); | |||||
| const tmpIndex = state.isCategoriesChecked.findIndex( | |||||
| (x) => x.id === action.payload.id | |||||
| ); | |||||
| if (tmpIndex === -1) { | if (tmpIndex === -1) { | ||||
| return state; | return state; | ||||
| } | } | ||||
| return { | |||||
| return { | |||||
| ...state, | ...state, | ||||
| isCategoriesChecked: state.isCategoriesChecked.map((category, index) => | isCategoriesChecked: state.isCategoriesChecked.map((category, index) => | ||||
| tmpIndex === index | tmpIndex === index | ||||
| changedCategories: tmpChangedCategories, | changedCategories: tmpChangedCategories, | ||||
| }; | }; | ||||
| } | } | ||||
| function setStateGrantCategories(state) { | |||||
| return { | |||||
| ...state, | |||||
| changedCategories: [] | |||||
| }; | |||||
| } | |||||
| function setGrantCategoriesErrorMessage(state, action) { | |||||
| return { | |||||
| ...state, | |||||
| errorMessage: action.payload, | |||||
| }; | |||||
| } |
| setCategoriesError, | setCategoriesError, | ||||
| setIsCategoriesChecked, | setIsCategoriesChecked, | ||||
| setIsCategoriesCheckedError, | setIsCategoriesCheckedError, | ||||
| setGrantCategories, | |||||
| setGrantCategoriesError | |||||
| } from "../actions/categories/categoriesAction"; | } from "../actions/categories/categoriesAction"; | ||||
| import { authScopeStringGetHelper } from "../../util/helpers/authScopeHelpers"; | import { authScopeStringGetHelper } from "../../util/helpers/authScopeHelpers"; | ||||
| import { JWT_TOKEN } from "../../constants/localStorage"; | import { JWT_TOKEN } from "../../constants/localStorage"; | ||||
| import { | import { | ||||
| FETCH_CATEGORIES_REQ, | FETCH_CATEGORIES_REQ, | ||||
| FETCH_IS_CATEGORIES_CHECKED_REQ, | FETCH_IS_CATEGORIES_CHECKED_REQ, | ||||
| GRANT_CATEGORY_REQ, | |||||
| } from "../actions/categories/categoriesActionConstants"; | } from "../actions/categories/categoriesActionConstants"; | ||||
| import { | import { | ||||
| getAllCategories, | getAllCategories, | ||||
| getIsCategoriesChecked, | getIsCategoriesChecked, | ||||
| grantCategoryRequest, | |||||
| } from "../../request/categoriesRequest"; | } from "../../request/categoriesRequest"; | ||||
| export function* getCategories() { | export function* getCategories() { | ||||
| } | } | ||||
| } | } | ||||
| export function* grantCategoriesCheckedSaga({ payload }) { | |||||
| try { | |||||
| const JwtToken = yield call(authScopeStringGetHelper, JWT_TOKEN); | |||||
| yield call(addHeaderToken, JwtToken); | |||||
| const result = yield call(grantCategoryRequest, payload); | |||||
| yield put(setGrantCategories(result.data)); | |||||
| } catch (error) { | |||||
| const errorMessage = yield call(rejectErrorCodeHelper, error); | |||||
| yield put(setGrantCategoriesError(errorMessage)); | |||||
| } | |||||
| } | |||||
| export default function* categoriesSaga() { | export default function* categoriesSaga() { | ||||
| yield all([ | yield all([ | ||||
| takeLatest(FETCH_CATEGORIES_REQ, getCategories), | takeLatest(FETCH_CATEGORIES_REQ, getCategories), | ||||
| takeLatest(FETCH_IS_CATEGORIES_CHECKED_REQ, getIsCategoriesCheckedSaga), | takeLatest(FETCH_IS_CATEGORIES_CHECKED_REQ, getIsCategoriesCheckedSaga), | ||||
| takeLatest(GRANT_CATEGORY_REQ, grantCategoriesCheckedSaga), | |||||
| ]); | ]); | ||||
| } | } |
| categoriesSelector, | categoriesSelector, | ||||
| (state) => state.errorMessage | (state) => state.errorMessage | ||||
| ); | ); | ||||
| export const selectChangedCategories = createSelector( | |||||
| categoriesSelector, | |||||
| (state) => state.changedCategories | |||||
| ); |