Selaa lähdekoodia

Partly finished feature 685

feature/685
Djordje Mitrovic 3 vuotta sitten
vanhempi
commit
373bd658f6

+ 6
- 5
src/components/Cards/ProfileCard/EditProfile/EditProfile.js Näytä tiedosto

import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { import {
editProfile, editProfile,
fetchAllProfiles,
editProfileAsAdmin,
fetchAllProfilesAsAdmin,
fetchMineProfile, fetchMineProfile,
} from "../../../../store/actions/profile/profileActions"; } from "../../../../store/actions/profile/profileActions";
import { useDispatch, useSelector } from "react-redux"; import { useDispatch, useSelector } from "react-redux";
const handleApiResponseSuccess = () => { const handleApiResponseSuccess = () => {
if (dynamicRouteMatches(PROFILE_PAGE)) dispatch(fetchMineProfile(userId)); if (dynamicRouteMatches(PROFILE_PAGE)) dispatch(fetchMineProfile(userId));
if (routeMatches(ADMIN_USERS_PAGE) || routeMatches(ADMIN_HOME_PAGE)) if (routeMatches(ADMIN_USERS_PAGE) || routeMatches(ADMIN_HOME_PAGE))
dispatch(fetchAllProfiles());
dispatch(fetchAllProfilesAsAdmin());
props.reFetchProfile(); props.reFetchProfile();
}; };


const handleSubmit = (values) => { const handleSubmit = (values) => {
if (props.isAdmin) { if (props.isAdmin) {
dispatch( dispatch(
editProfile({
editProfileAsAdmin({
userId: props.userId, userId: props.userId,
...values, ...values,
firmLogo: profileImage,
firmLogo: profileImage === props.profile.image ? null : profileImage,
handleApiResponseSuccess, handleApiResponseSuccess,
}) })
); );
dispatch( dispatch(
editProfile({ editProfile({
...values, ...values,
firmLogo: profileImage,
firmLogo: profileImage === props.profile.image ? null : profileImage,
handleApiResponseSuccess, handleApiResponseSuccess,
}) })
); );

+ 1
- 0
src/components/Cards/ProfileCard/ProfileCard.js Näytä tiedosto

{editProfileModal && ( {editProfileModal && (
<EditProfile <EditProfile
profile={profile} profile={profile}
isAdmin={props.isAdmin}
closeModalHandler={closeModalHandler} closeModalHandler={closeModalHandler}
reFetchProfile={reFetchProfile} reFetchProfile={reFetchProfile}
/> />

+ 2
- 2
src/pages/AdminHomePage/AdminUsersPage/AdminUsersPage.js Näytä tiedosto

import PropTypes from "prop-types"; import PropTypes from "prop-types";
import { useDispatch, useSelector } from "react-redux"; import { useDispatch, useSelector } from "react-redux";
import { selectAllProfiles } from "../../../store/selectors/profileSelectors"; import { selectAllProfiles } from "../../../store/selectors/profileSelectors";
import { fetchAllProfiles } from "../../../store/actions/profile/profileActions";
import { fetchAllProfilesAsAdmin } from "../../../store/actions/profile/profileActions";
import { import {
AdminUsersHeader, AdminUsersHeader,
AdminUsersList, AdminUsersList,
[allUsers] [allUsers]
); );
useEffect(() => { useEffect(() => {
dispatch(fetchAllProfiles());
dispatch(fetchAllProfilesAsAdmin());
}, []); }, []);


const handleSearch = () => {}; const handleSearch = () => {};

+ 3
- 1
src/request/apiEndpoints.js Näytä tiedosto

invite: "/users/invite", invite: "/users/invite",
getProfile: "users/", getProfile: "users/",
editProfile: "users", editProfile: "users",
getAllProfiles: "users"
editProfileAsAdmin: "admin/users/{userId}",
getAllProfiles: "users",
getAllProfilesAsAdmin: "admin/users"
}, },
applications: { applications: {
application: "/applications/{applicationUid}", application: "/applications/{applicationUid}",

+ 8
- 1
src/request/profileRequest.js Näytä tiedosto

import { getRequest, putRequest } from ".";
import { getRequest, putRequest, replaceInUrl } from ".";
import apiEndpoints from "./apiEndpoints"; import apiEndpoints from "./apiEndpoints";


export const attemptFetchProfile = (payload) => export const attemptFetchProfile = (payload) =>


export const attemptFetchAllProfiles = () => export const attemptFetchAllProfiles = () =>
getRequest(apiEndpoints.users.getAllProfiles); getRequest(apiEndpoints.users.getAllProfiles);
export const attemptFetchAllProfilesAsAdmin = () =>
getRequest(apiEndpoints.users.getAllProfilesAsAdmin);


export const attemptEditProfile = (payload, requestData) => export const attemptEditProfile = (payload, requestData) =>
putRequest(apiEndpoints.users.editProfile + "/" + payload, requestData); putRequest(apiEndpoints.users.editProfile + "/" + payload, requestData);
export const attemptEditProfileAsAdmin = (payload, requestData) =>
putRequest(
replaceInUrl(apiEndpoints.users.editProfileAsAdmin, { userId: payload }),
requestData
);

+ 10
- 0
src/store/actions/profile/profileActionConstants.js Näytä tiedosto

export const PROFILE_ALL_SUCCESS = createSuccessType(PROFILE_ALL_SCOPE); export const PROFILE_ALL_SUCCESS = createSuccessType(PROFILE_ALL_SCOPE);
export const PROFILE_ALL_ERROR = createErrorType(PROFILE_ALL_SCOPE); export const PROFILE_ALL_ERROR = createErrorType(PROFILE_ALL_SCOPE);


export const PROFILE_ALL_ADMIN_SCOPE = "PROFILE_ALL_ADMIN_SCOPE";
export const PROFILE_ALL_ADMIN_FETCH = createFetchType(PROFILE_ALL_ADMIN_SCOPE);
export const PROFILE_ALL_ADMIN_SUCCESS = createSuccessType(PROFILE_ALL_ADMIN_SCOPE);
export const PROFILE_ALL_ADMIN_ERROR = createErrorType(PROFILE_ALL_ADMIN_SCOPE);

export const PROFILE_MINE_SCOPE = "PROFILE_MINE_SCOPE"; export const PROFILE_MINE_SCOPE = "PROFILE_MINE_SCOPE";
export const PROFILE_MINE_FETCH = createFetchType(PROFILE_MINE_SCOPE); export const PROFILE_MINE_FETCH = createFetchType(PROFILE_MINE_SCOPE);
export const PROFILE_MINE_FETCH_SUCCESS = createSuccessType(PROFILE_MINE_SCOPE); export const PROFILE_MINE_FETCH_SUCCESS = createSuccessType(PROFILE_MINE_SCOPE);
export const PROFILE_EDIT_SUCCESS = createSuccessType(PROFILE_EDIT_SCOPE); export const PROFILE_EDIT_SUCCESS = createSuccessType(PROFILE_EDIT_SCOPE);
export const PROFILE_EDIT_ERROR = createErrorType(PROFILE_EDIT_SCOPE); export const PROFILE_EDIT_ERROR = createErrorType(PROFILE_EDIT_SCOPE);


const PROFILE_EDIT_ADMIN_SCOPE = "PROFILE_EDIT_ADMIN_SCOPE";
export const PROFILE_EDIT_ADMIN = createFetchType(PROFILE_EDIT_ADMIN_SCOPE);
export const PROFILE_EDIT_ADMIN_SUCCESS = createSuccessType(PROFILE_EDIT_ADMIN_SCOPE);
export const PROFILE_EDIT_ADMIN_ERROR = createErrorType(PROFILE_EDIT_ADMIN_SCOPE);

export const PROFILE_CLEAR = createClearType("PROFILE_CLEAR"); export const PROFILE_CLEAR = createClearType("PROFILE_CLEAR");

+ 32
- 2
src/store/actions/profile/profileActions.js Näytä tiedosto

PROFILE_SET, PROFILE_SET,
PROFILE_SUCCESS, PROFILE_SUCCESS,
PROFILE_EDIT, PROFILE_EDIT,
PROFILE_MINE_FETCH_SUCCESS,
PROFILE_EDIT_SUCCESS, PROFILE_EDIT_SUCCESS,
PROFILE_MINE_FETCH_ERROR,
PROFILE_EDIT_ERROR, PROFILE_EDIT_ERROR,
PROFILE_EDIT_ADMIN,
PROFILE_EDIT_ADMIN_SUCCESS,
PROFILE_EDIT_ADMIN_ERROR,
PROFILE_MINE_FETCH_SUCCESS,
PROFILE_MINE_FETCH_ERROR,
PROFILE_ALL_FETCH, PROFILE_ALL_FETCH,
PROFILE_ALL_SUCCESS, PROFILE_ALL_SUCCESS,
PROFILE_ALL_ERROR, PROFILE_ALL_ERROR,
PROFILE_ALL_ADMIN_FETCH,
PROFILE_ALL_ADMIN_SUCCESS,
PROFILE_ALL_ADMIN_ERROR,
PROFILE_ALL_SET, PROFILE_ALL_SET,
} from "./profileActionConstants"; } from "./profileActionConstants";


payload, payload,
}); });


export const fetchAllProfilesAsAdmin = (payload) => ({
type: PROFILE_ALL_ADMIN_FETCH,
payload,
});
export const fetchAllProfilesAsAdminSuccess = (payload) => ({
type: PROFILE_ALL_ADMIN_SUCCESS,
payload,
});
export const fetchAllProfilesAsAdminError = (payload) => ({
type: PROFILE_ALL_ADMIN_ERROR,
payload,
});

export const fetchMineProfile = () => ({ export const fetchMineProfile = () => ({
type: PROFILE_MINE_FETCH, type: PROFILE_MINE_FETCH,
}); });
type: PROFILE_EDIT_ERROR, type: PROFILE_EDIT_ERROR,
}); });


export const editProfileAsAdmin = (payload) => ({
type: PROFILE_EDIT_ADMIN,
payload,
});
export const editProfileAsAdminSuccess = () => ({
type: PROFILE_EDIT_ADMIN_SUCCESS,
});
export const editProfileAsAdminError = () => ({
type: PROFILE_EDIT_ADMIN_ERROR,
});

export const clearProfile = () => ({ export const clearProfile = () => ({
type: PROFILE_CLEAR, type: PROFILE_CLEAR,
}); });

+ 54
- 29
src/store/saga/profileSaga.js Näytä tiedosto

import { all, call, put, takeLatest, select } from "@redux-saga/core/effects"; import { all, call, put, takeLatest, select } from "@redux-saga/core/effects";
import { import {
attemptEditProfile, attemptEditProfile,
attemptEditProfileAsAdmin,
attemptFetchAllProfiles, attemptFetchAllProfiles,
attemptFetchAllProfilesAsAdmin,
attemptFetchProfile, attemptFetchProfile,
} from "../../request/profileRequest"; } from "../../request/profileRequest";
import { import {
PROFILE_MINE_FETCH, PROFILE_MINE_FETCH,
PROFILE_EDIT, PROFILE_EDIT,
PROFILE_ALL_FETCH, PROFILE_ALL_FETCH,
PROFILE_EDIT_ADMIN,
PROFILE_ALL_ADMIN_FETCH,
} from "../actions/profile/profileActionConstants"; } from "../actions/profile/profileActionConstants";
import { import {
editProfileAsAdminError,
editProfileAsAdminSuccess,
editProfileError, editProfileError,
editProfileSuccess, editProfileSuccess,
fetchAllProfilesAsAdminError,
fetchAllProfilesAsAdminSuccess,
fetchAllProfilesError, fetchAllProfilesError,
fetchAllProfilesSuccess, fetchAllProfilesSuccess,
fetchErrorProfile, fetchErrorProfile,
} }
} }


function* changeMineProfile(payload) {
function* fetchAllProfilesAsAdmin() {
try { try {
// console.log(payload);
// let image;
// if (payload.payload.firmLogo) {
// image = payload.payload.firmLogo;
// } else if (payload.payload.firmLogo === "") {
// image = "";
// }

// const reqData = {
// company: {
// name: payload.payload.firmName,
// PIB: payload.payload.firmPIB,
// contacts: {
// telephone: payload.payload.firmPhone.toString(),
// location: payload.payload.firmLocation ?? "",
// web: payload.payload.firmWebsite,
// },
// },
// image: image,
// };

// if (payload.payload.firmLogo?.includes("https")) delete reqData.image;
// if (reqData.company.contacts.telephone.length === 0)
// delete reqData.company.contacts.telephone;
// if (reqData.company.contacts.location.length === 0)
// delete reqData.company.contacts.location;
// if (reqData.company.contacts.web.length === 0)
// delete reqData.company.contacts.web;
const data = yield call(attemptFetchAllProfilesAsAdmin);
if (data) yield put(setAllProfiles(data.data.users));
yield put(fetchAllProfilesAsAdminSuccess());
} catch (e) {
yield put(fetchAllProfilesAsAdminError());
console.dir(e);
}
}


function* changeMineProfile(payload) {
try {
const requestBody = new FormData(); const requestBody = new FormData();
if (typeof payload.payload.firmLogo !== "string") if (typeof payload.payload.firmLogo !== "string")
requestBody.append("file", payload.payload.firmLogo); requestBody.append("file", payload.payload.firmLogo);
} }
} }


function* changeProfileAsAdmin(payload) {
try {
const requestBody = new FormData();
console.log(payload);
if (typeof payload.payload?.firmLogo !== "string")
requestBody.append("file", payload.payload.firmLogo);
requestBody.append("company[name]", payload.payload.firmName);
requestBody.append("company[PIB]", payload.payload.firmPIB);
if (payload.payload.firmPhone.toString().length !== 0)
requestBody.append(
"company[contacts][telephone]",
payload.payload.firmPhone
);
if (payload.payload.firmLocation.toString().length !== 0)
requestBody.append(
"company[contacts][location]",
payload.payload.firmLocation
);
if (payload.payload.firmWebsite.toString().length !== 0)
requestBody.append("company[contacts][web]", payload.payload.firmWebsite);

const userId = payload.payload.userId;
yield call(attemptEditProfileAsAdmin, userId, requestBody);
yield put(editProfileAsAdminSuccess());
if (payload.payload.handleApiResponseSuccess) {
yield call(payload.payload.handleApiResponseSuccess);
}
} catch (e) {
yield put(editProfileAsAdminError());
console.dir(e);
}
}

export default function* profileSaga() { export default function* profileSaga() {
yield all([ yield all([
takeLatest(PROFILE_FETCH, fetchProfile), takeLatest(PROFILE_FETCH, fetchProfile),
takeLatest(PROFILE_MINE_FETCH, fetchMineProfile), takeLatest(PROFILE_MINE_FETCH, fetchMineProfile),
takeLatest(PROFILE_EDIT, changeMineProfile), takeLatest(PROFILE_EDIT, changeMineProfile),
takeLatest(PROFILE_ALL_FETCH, fetchAllProfiles), takeLatest(PROFILE_ALL_FETCH, fetchAllProfiles),
takeLatest(PROFILE_ALL_ADMIN_FETCH, fetchAllProfilesAsAdmin),
takeLatest(PROFILE_EDIT_ADMIN, changeProfileAsAdmin),
]); ]);
} }

Loading…
Peruuta
Tallenna