Skip to content

Commit

Permalink
#71 Added progress bar and sagas for controlling.
Browse files Browse the repository at this point in the history
  • Loading branch information
artzub committed Dec 28, 2020
1 parent b308bd7 commit 250ee55
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/components/Layout.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React from 'react';
import Header from '@/components/Header';
import Main from '@/components/Main';
import Progress from "@/components/Progress";

const Layout = () => (
<React.Fragment>
<Progress />
<Header />
<Main />
</React.Fragment>
Expand Down
34 changes: 34 additions & 0 deletions src/components/Progress/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from "react";
import slice from '@/redux/modules/progress';
import LinearProgressOrigin from '@material-ui/core/LinearProgress';
import { useSelector } from "react-redux";
import styled from "styled-components";

const Container = styled.div`
position: absolute;
bottom: 2px;
left: 0;
right: 0;
z-index: 10;
`;

const LinearProgress = styled(LinearProgressOrigin)`
height: 2px;
`;

const normalize = ({ value, min, max }) => (value - min) * 100 / (max - min);

const Progress = () => {
const state = useSelector(slice.selectors.getState);

const value = normalize(state);
const valueBuffer = normalize({ ...state, value: state.valueBuffer });

return !state.show ? null : (
<Container>
<LinearProgress variant="buffer" value={value} valueBuffer={valueBuffer} />
</Container>
);
};

export default Progress;
2 changes: 2 additions & 0 deletions src/redux/modules/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { all } from 'redux-saga/effects';
import profiles from "./profiles";
import progress from "./progress";
import ui from './ui';

// Put modules that have their reducers nested in other (root) reducers here
Expand All @@ -9,6 +10,7 @@ const nestedSlices = [];
const rootSlices = [
profiles,
ui,
progress,
];

const sagas = [...rootSlices, ...nestedSlices]
Expand Down
40 changes: 40 additions & 0 deletions src/redux/modules/progress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { createSlice } from "@/redux/utils";

const initialState = {
max: 100,
min: 0,
value: 0,
valueBuffer: 0,
show: false,
};

export default createSlice({
name: 'progress',
initialState,
reducers: {
change: (state, { payload }) => {
return {
...state,
...payload,
};
},

incValue: (state, { payload }) => {
state.value = Math.min(state.value + payload, state.max);
},

incValueBuffer: (state, { payload }) => {
state.valueBuffer = Math.min(state.valueBuffer + payload, state.max);
},

toggle: (state, { payload }) => {
state.show = payload ?? !state.show;
},

clear() {
return {
...initialState,
};
},
},
});

0 comments on commit 250ee55

Please sign in to comment.