Skip to content

Commit

Permalink
Merge pull request #117 from ligangty/2.0-refactor
Browse files Browse the repository at this point in the history
Implement Enable/Disable for repo view page
  • Loading branch information
ligangty authored Dec 13, 2023
2 parents fb3d77d + 102d55a commit df80cf6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,58 @@ import React from 'react';
import {useNavigate} from 'react-router-dom';
import {PropTypes} from 'prop-types';
import {Utils} from '#utils/AppUtils';
import {jsonRest,http} from '#utils/RestClient';
import {jsonRest,http, logErrors} from '#utils/RestClient';
import {STORE_API_BASE_URL} from '../../ComponentConstants';

const save = (store, mode, postSuccessHandler) => {
const saveUrl = `${STORE_API_BASE_URL}/${store.packageType}/${store.type}/${store.name}`;
const saveStore = async () => {
let response = {};
if(mode==="new"){
response = await jsonRest.post(saveUrl, store);
}else if(mode ==="edit"){
response = await jsonRest.put(saveUrl, store);
}
if (!response.ok){
// TODO: find another way to do error handling
logErrors(response);
}
if(response.status >= 200 || response.status < 300){
postSuccessHandler(store);
}
};
saveStore();
};

const StoreViewControlPanel = function({store}){
const navigate = useNavigate();
const handleEnable = () =>{
// TODO: need to implement
store.disabled = false;
if(store.metadata){
store.metadata.changelog = "Enabling via UI";
}else{
store.metadata = {changelog: "Enabling via UI"};
}
save(store, "edit", st => navigate(`/${st.type}/${st.packageType}/view/${st.name}`));
};
const handleDisable = () =>{
// TODO: need to implement
store.disabled = true;
if(store.metadata){
store.metadata.changelog = "Disabling indefinitely via UI";
}else{
store.metadata = {changelog: "Disabling indefinitely via UI"};
}
save(store, "edit", st => navigate(`/${st.type}/${st.packageType}/view/${st.name}`));
};
const [enableText, enableHandler] = store.disabled?["Enable",handleEnable]:["Disable",handleDisable];
const navigate = useNavigate();

const [pkgType, storeType, storeName] = [store.packageType, store.type, store.name];
const storeUrl = `${STORE_API_BASE_URL}/${pkgType}/${storeType}/${storeName}`;
const handleRemove = async ()=>{
const response = await http.delete(storeUrl);
if(!response.ok && response.status >= 400){
// TODO: Some other way to handle errors?
response.text().then(error=>Utils.logMessage(error));
logErrors(response);
}
if(response.status===204){
// TODO: Some other way to show deletion success?
Expand Down Expand Up @@ -68,24 +100,8 @@ StoreViewControlPanel.propTypes={

const StoreEditControlPanel = ({mode, store, handleSubmit}) =>{
const navigate = useNavigate();
const save = () => {
const saveUrl = `${STORE_API_BASE_URL}/${store.packageType}/${store.type}/${store.name}`;
const saveStore = async () => {
let response = {};
if(mode==="new"){
response = await jsonRest.post(saveUrl, store);
}else if(mode ==="edit"){
response = await jsonRest.put(saveUrl, store);
}
if (!response.ok){
// TODO: find another way to do error handling
response.text().then(error=>Utils.logMessage(error));
}
if(response.status >= 200 || response.status < 300){
navigate(`/${store.type}/${store.packageType}/view/${store.name}`);
}
};
saveStore();
const postSuccessHandler = st =>{
navigate(`/${st.type}/${st.packageType}/view/${st.name}`);
};

const handleCancel = () => {
Expand Down Expand Up @@ -115,14 +131,14 @@ const StoreEditControlPanel = ({mode, store, handleSubmit}) =>{
}
};

let handleSave = () => save();
let handleSave = () => save(store, mode, postSuccessHandler);
if(handleSubmit && typeof handleSubmit === 'function'){
// console.log(handleSubmit);
handleSave = handleSubmit(data=>{
data.disabled = !data.enabled;
data.enabled = undefined;
Utils.rewriteTargetObject(data, store);
save();
save(store, mode, postSuccessHandler);
});
}
return <div className="cp-row">
Expand Down
10 changes: 9 additions & 1 deletion src/main/webui/src/app/utils/RestClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,12 @@ const jsonRest ={
put: (url, payload) => httpCall(url, "PUT", {"Content-type": "application/json"}, JSON.stringify(payload))
};

export {http, jsonRest};
const logErrors = response => {
if(response.text()){
response.text().then(error=>console.log(error));
}else{
console.log(`Something wrong: ${response.status} -> ${response.statusText}`);
}
};

export {http, jsonRest, logErrors};

0 comments on commit df80cf6

Please sign in to comment.