Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User service #38

Merged
merged 11 commits into from
Oct 2, 2021
4 changes: 4 additions & 0 deletions petshop-admin-panel/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import DefaultLayout from "./layouts/DefaultLayout";
import {Redirect} from "react-router";
import ProductCategory from "./containers/product/catgeory/ProductCategory";
import Product from "./containers/product/product/Product";
import User from "./containers/user/User";


function App(props) {
Expand All @@ -30,6 +31,9 @@ function App(props) {
<PrivateRoute exact path="/product" layout={DefaultLayout}
component={Product}
loggedIn={props.loggedIn}/>
<PrivateRoute exact path="/user" layout={DefaultLayout}
component={User}
loggedIn={props.loggedIn}/>
<Route
exact
path="/login"
Expand Down
152 changes: 152 additions & 0 deletions petshop-admin-panel/src/containers/user/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import React, {useEffect, useState} from "react";
import CssBaseline from "@material-ui/core/CssBaseline";
import Container from "@material-ui/core/Container";
import CustomTable from "../../components/CustomTable";
import {pageUsers, getUserById, deleteUserById} from "./UserService";
import {createConfirmAlert, createErrorAlert, createSuccessAlert} from "../../components/Alert";
import ViewUserModal from "./modal/ViewUserModal";


const columns = [
"id", "Ad", "Email", "Telefon", "Lokasyon", "İşlemler"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What columns to show/hide?

]

const deleteDialogText = "Kullanıcıyı silmek istediğinizden emin misiniz?"
const deleteSuccessDialogText = "Ürünü güncelleme işlemi başarılı"
const deleteErrorDialogText = "Ürünü güncelleme işlemi başarısız"

function User(props) {

const [viewModalOpen, setViewModalOpen] = React.useState(false);
const [page, setPage] = useState(0);
const [size, setSize] = useState(5);
const [count, setCount] = useState(0);
const [users, setUsers] = useState([]);
const [userDeleteSuccess, setUserDeleteSuccess] = useState(false);
const [userDeleteError, setUserDeleteError] = useState(false);
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
const [idToDelete, setIdToDelete] = useState(null);
const [viewUserData, setViewUserData] = useState([]);

useEffect(() => {
pageUsers(page, size).then(response => {
setUsers(response.data.content);
setCount(response.data.totalElements);
}).catch(e => {
console.log(e);
});
}, []);

useEffect(() => {
pageUsers(page, size).then(response => {
setUsers(response.data.content);
setCount(response.data.totalElements);
}).catch(e => {
console.log(e);
});
}, [page, size]);

function pageUsersDefault() {
pageUsers(0, 5).then(response => {
setPage(0);
setUsers(response.data.content);
setCount(response.data.totalElements);
}).catch(e => {
console.log(e);
});
}

useEffect(() => {
if (userDeleteSuccess) {
setTimeout(function () {
setUserDeleteSuccess(false);
}, 3000);
}
}, [userDeleteSuccess]);

useEffect(() => {
if (userDeleteError) {
setTimeout(function () {
setUserDeleteError(false);
}, 3000);
}
}, [userDeleteError]);

const handleDelete = (id) => {
setDeleteDialogOpen(true);
setIdToDelete(id);
}

const handleDeleteAfterConfirm = () => {

deleteUserById(idToDelete).then(response => {
pageUsersDefault();
setUserDeleteSuccess(true);
}).catch(e => {
console.log(e);
setUserDeleteError(true);
})
setDeleteDialogOpen(false);
}

const handleDeleteDialogClose = () => {
setDeleteDialogOpen(false);
}

const handleVisible = (id) => {
setViewModalOpen(true);
getUserById(id).then(response => {
setViewUserData(response.data);
}).catch(e => {
console.log(e);
})
}

const handleViewModalClose = () => {
setViewModalOpen(false);
}

const handlePageChange = page => {
setPage(page);
}

const handleRowsPerPageChange = rowsPerPage => {
setSize(rowsPerPage);
}

return (
<React.Fragment>
<CssBaseline/>
{createConfirmAlert(deleteDialogOpen, deleteDialogText, handleDeleteDialogClose, handleDeleteAfterConfirm)}
{userDeleteSuccess && <div> {createSuccessAlert(deleteSuccessDialogText)} <br/><br/></div>}
{userDeleteError && <div> {createErrorAlert(deleteErrorDialogText)} <br/><br/></div>}
<Container maxWidth="xl">
<div>
<ViewUserModal open={viewModalOpen}
userData={viewUserData}
categoryData={viewUserData.category}
handleClose={handleViewModalClose}/>

</div>
<div>
<h2>Kullanıcılar</h2>
</div>
<CustomTable rows={users}
columns={columns}
handlePageChange={handlePageChange}
handleRowsPerPageChange={handleRowsPerPageChange}
isOperation={true}
previewImageUrlField={"previewImageUrl"}
activePage={page}
count={count}
handleDelete={handleDelete}
handleVisible={handleVisible}
hiddenIndexes={[2,3,7,8,9,10,11,12,13,14,15,16,17,18]}
/>
</Container>
</React.Fragment>
)

}

export default User;
65 changes: 65 additions & 0 deletions petshop-admin-panel/src/containers/user/UserService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import request from "../../common/request";

/**
* Retrieves paged list of users.
* @param page Page number.
* @param size Page size.
* @returns {Promise<* | void>} Http status code returned from the server.
*/
export const pageUsers = (page, size) => {
let options = {
method: "GET",
path: "/api/v1/users",
params: ["page", "size"],
values: [page, size],
}

return request(options);
}

/**
* Retrieves user data from API.
* @param userId Id of the user.
* @returns {Promise<* | void>} User data as JSON.
*/
export const getUserById = (userId) => {
let options = {
method: "GET",
path: "/api/v1/users/" + userId,
}

return request(options);
}

/**
* Requests a user status change from API.
* @param id Id of the user.
* @returns {Promise<* | void>} Http status code returned from API.
*/
export const deleteUserById = (id) => {
let options = {
method: "DELETE",
path: "/api/v1/users/" + id,
}

return request(options)
}

/**
* Sends a user image addition request to API.
* @param data Image data.
* @returns {Promise<* | void>} Http status code returned from API.
*/
export const addUserImage= data => {
const formData = new FormData();
formData.append('file', data);

const addUserImageRequest = {
method: "POST",
path: "/api/v1/file",
data: formData,
params: ["firebasePath"],
values: ["product-images"]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Value should be user-image

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, do we have the implementation to add a user image?

}
return request(addUserImageRequest);
}
61 changes: 61 additions & 0 deletions petshop-admin-panel/src/containers/user/modal/ViewUserModal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React, {useEffect} from "react";
import Dialog from "@material-ui/core/Dialog";
import DialogTitle from "@material-ui/core/DialogTitle";
import {DialogContentText} from "@material-ui/core";
import DialogContent from "@material-ui/core/DialogContent";
import Button from "@material-ui/core/Button";
import DialogActions from "@material-ui/core/DialogActions";


function ViewUserModal(props) {

const {userData, open, handleClose} = props;

useEffect(() => {
if (open) {
}
}, [open]);

return (
<React.Fragment>
<Dialog open={open} onClose={handleClose}
aria-labelledby="form-dialog-title"
aria-describedby="alert-dialog-description"
PaperProps={{
style: {
minHeight: '40vh',
minWidth: '40vw',
},
}}>
<DialogTitle id="view-user">Kullanıcı Detay</DialogTitle>
<DialogContent>
<div>
<h4>Ad</h4>
<DialogContentText>{userData.name}</DialogContentText>
<h4>Soyad</h4>
<DialogContentText>{userData.lastName}</DialogContentText>
<h4>İlçe</h4>
<DialogContentText>{userData.town}</DialogContentText>
<h4>Şehir</h4>
<DialogContentText>{userData.city}</DialogContentText>
<h4>Cinsiyet</h4>
<DialogContentText>{userData.gender}</DialogContentText>
</div>
<div style={{clear: "both"}}>
<h4>Son giriş</h4>
<DialogContentText>{userData.lastLoggedIn}</DialogContentText>
<h4>Kayıt Tarihi</h4>
<DialogContentText>{userData.createdAt}</DialogContentText>
</div>
</DialogContent>
<DialogActions>
<Button onClick={handleClose} color="primary">
İptal
</Button>
</DialogActions>
</Dialog>
</React.Fragment>
);
}

export default ViewUserModal;
5 changes: 1 addition & 4 deletions petshop-admin-panel/src/layouts/DefaultLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,7 @@ const DefaultLayout = ({window: window, component: Component, ...rest}) => {
<ListItem>
<ListItemText>Kullanıcı Yönetimi</ListItemText>
</ListItem>
<ListItem button key={1}>
<ListItemIcon><AccountBoxIcon/></ListItemIcon>
<ListItemText primary={'Kullanıcılar'}/>
</ListItem>
<ListItemLink primary={'Kullanıcılar'} to='/user' icon={<AccountBoxIcon/>} />
</List>
<Divider/>
<List>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.turkninja.petshop.api.request.user;

import lombok.Data;

import java.util.Objects;

/**
* Request object for user image creation.
*
* @author mstar
* @version 1.0
* @since 1.0, 2021-09-24
*/
@Data
public class UserImageInsertRequest {
private Long userId;
private String imageUrl;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserImageInsertRequest that = (UserImageInsertRequest) o;
return Objects.equals(userId, that.userId) && Objects.equals(imageUrl, that.imageUrl);
}

@Override
public int hashCode() {
return Objects.hash(userId, imageUrl);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.turkninja.petshop.api.response.user;

import lombok.Data;

/**
* Response object for user image insert request.
*
* @author mstar
* @version 1.0
* @since 1.0, 2021-09-24
*/
@Data
public class UserImageInsertResponse {
private String url;
}