Skip to content

Commit

Permalink
Merge pull request #8 from sinchiw/terms
Browse files Browse the repository at this point in the history
Added more reducer logic. Will pull it and merge our end.
  • Loading branch information
MinchanJun authored Sep 2, 2020
2 parents a2a35aa + 3826a6c commit 9a31e0d
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 18 deletions.
16 changes: 7 additions & 9 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
const electron = require("electron");
const { app, BrowserWindow } = electron;


let mainWindow;

app.on("ready", () => {

mainWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: true,
Expand All @@ -18,7 +16,7 @@ app.on("ready", () => {

mainWindow.webContents.openDevTools();

mainWindow.on('closed', function () {
mainWindow.on("closed", function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
Expand All @@ -30,18 +28,18 @@ app.on("ready", () => {
});

// Quit when all windows are closed.
app.on('window-all-closed', function () {
app.on("window-all-closed", function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
if (process.platform !== "darwin") {
app.quit();
}
});

app.on('activate', function () {
app.on("activate", function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
createWindow();
}
});
});
14 changes: 12 additions & 2 deletions src/actions/actions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as types from "../constants/actionTypes";

export const addContainer = (data) => ({
type: types.ADD_CONTAINER,
export const addRunningContainer = (data) => ({
type: types.ADD_RUNNING_CONTAINER,
payload: data,
});

Expand All @@ -19,3 +19,13 @@ export const stopContainer = (id) => ({
type: types.STOP_RUNNING_CONTAINERS,
payload: id,
});

export const addStoppedContainer = () => ({
type: types.ADD_STOPPED_CONTAINER,
payload: data,
});

export const runStoppedContainer = () => ({
type: types.RUN_STOPPED_CONTAINER,
payload: id,
});
44 changes: 39 additions & 5 deletions src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ import { stderr } from "process";

const App = (props) => {
const dispatch = useDispatch();
const addContainer = (data) => dispatch(actions.addContainer(data));
const addRunningContainer = (data) =>
dispatch(actions.addRunningContainer(data));
const removeContainer = (id) => dispatch(actions.removeContainer(id));
const stopContainer = (id) => dispatch(actions.stopContainer(id));
const addStoppedContainer = (data) =>
dispatch(actions.addStoppedContainer(data));
const runStoppedContainer = (id) => dispatch(actions.runStoppedContainer(id));

// const getRunningContainers = (data) => dispatch(actions.getRunningContainers(data))

// useSelector Allows you to extract data from the Redux store state, using a selector function.
const data = useSelector((state) => state.containers.containerList);

// on app start-up, get the containers that are already running by calling initialAdd
const initialAdd = () => {
const initialRunning = () => {
exec("docker stats --no-stream", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
Expand All @@ -35,7 +39,7 @@ const App = (props) => {
console.log(stdout);
// do some operations here to create a container with the information retrieved from stdout
// then, for each container created, call addContainer by passing in the created container as argument
return addContainer(stdout.split("\n")[1]);
return addRunningContainer(stdout.split("\n")[1]);
});
};

Expand Down Expand Up @@ -67,10 +71,40 @@ const App = (props) => {
});
};

//CREATE
const initialStopped = () => {
exec('docker ps -f "status=exited"', (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(stdout);
// do some operations here to create a container with the information retrieved from stdout
// then, for each container created, call StopContainer by passing in the created container as argument
addStoppedContainer(stdout);
});
};

const runStopped = (id) => {
exec(`docker start ${id}`, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
runStoppedContainer(id);
});
};

useEffect(() => {
initialAdd();
initialRunning();
initialStopped();
}, []);

// useEffect(() => {
Expand Down
4 changes: 3 additions & 1 deletion src/constants/actionTypes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const ADD_CONTAINER = "ADD_CONTAINER";
export const ADD_RUNNING_CONTAINER = "ADD_RUNNING_CONTAINER";
export const REMOVE_CONTAINER = "REMOVE_CONTAINER";
export const GET_RUNNING_CONTAINERS = "GET_RUNNING_CONTAINERS";
export const STOP_CONTAINER = `STOP_CONTAINER`;
export const ADD_STOPPED_CONTAINER = `ADD_STOPPED_CONTAINER`;
export const RUN_STOPPED_CONTAINER = `RUN_STOPPED_CONTAINER`;
23 changes: 22 additions & 1 deletion src/reducers/ContainersReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const initialState = {

const containersReducer = (state = initialState, action) => {
switch (action.type) {
case types.ADD_CONTAINER:
case types.ADD_RUNNING_CONTAINER:
const newRunningList = state.runningList.slice();
newRunningList.push(action.payload);
return { ...state, runningList: newRunningList };
Expand Down Expand Up @@ -40,6 +40,27 @@ const containersReducer = (state = initialState, action) => {
stoppedList: newStoppedList,
};

case types.ADD_STOPPED_CONTAINER:
const newerStoppedList = state.stoppedList.slice();
newerStoppedList.push(action.payload);
return { ...state, stoppedList: newerStoppedList };

case types.RUN_STOPPED_CONTAINER:
const runningListCopy = state.stoppedList.slice();
const newerStoppedContainer = [];
for (let container of state.stoppedList) {
if (container.id === action.payload) {
runningListCopy.push(container);
} else {
newerStoppedContainer.push(container);
}
}
return {
...state,
runningList: runningListCopy,
stoppedList: newerStoppedContainer,
};

default:
return state;
}
Expand Down

0 comments on commit 9a31e0d

Please sign in to comment.