Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Fix notifications settings that were not remembered #75

Merged
merged 6 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/app/Features/Profiles/Components/ProfileSwitcher.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React, { useEffect, useState } from "react";
import { useAppDispatch, useAppSelector } from '@/app/redux/hooks';
import { addConfigProfile, setCurrentProfileById} from '@/app/redux/configSlice'
import { setSyncData} from '@/app/redux/threeCommas/threeCommasSlice'

import { useHistory } from 'react-router-dom'

import PersonIcon from '@mui/icons-material/Person';
Expand Down Expand Up @@ -55,6 +57,8 @@ const ProfileSwitcher = () => {
onClick={() => {
updateCurrentProfileId(p);
dispatch(setCurrentProfileById({profileId: p}))
dispatch(setSyncData({syncCount: 0, time: 0}))

window.ThreeCPM.Repository.Config.set('current', p)
popupState.close()
}}
Expand Down
4 changes: 2 additions & 2 deletions src/app/Pages/ActiveDeals/ActiveDeals.scss
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
justify-content: flex-start;
padding-bottom: 4px;

.syncToggles{
.notificationsSettings {
flex: 1;
align-self: flex-start;
display: flex;
Expand All @@ -76,7 +76,7 @@
}


.tableButtons{
.tableButtons {
flex-basis: 20%;
align-items: center;
display: flex;
Expand Down
4 changes: 2 additions & 2 deletions src/app/Pages/ActiveDeals/ActiveDeals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { formatDeals } from '@/app/Components/DataTable/Index'

import { useAppSelector } from '@/app/redux/hooks';
import { Card_ActiveDeals, Card_totalInDeals, Card_ActiveDealReserve, Card_TotalDayProfit, Card_TotalUnrealizedProfit, Card_TotalRoi } from '@/app/Components/Charts/DataCards';
import { SyncToggles } from './Components/index';
import { NotificationsSettings } from './Components/index';

import './ActiveDeals.scss'

Expand Down Expand Up @@ -51,7 +51,7 @@ const ActiveDealsPage = () => {
<div className="boxData flex-column" style={{padding: '.5em 1em 1em', overflow: 'hidden'}}>
<div className="tableSettings">

<SyncToggles />
<NotificationsSettings />

<div className="filters tableButtons" >
<ToggleRefreshButton style={{ width: '250px', margin: '5px', height: '38px' }} className={"ToggleRefreshButton"} />
Expand Down
67 changes: 67 additions & 0 deletions src/app/Pages/ActiveDeals/Components/NotificationsSettings.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { useAppSelector } from '@/app/redux/hooks';
import { Checkbox, FormControlLabel } from '@mui/material';
import React, { useState } from "react";
import { updateNotificationsSettingsGlobal } from '@/app/redux/configActions';

/**
*
* @returns Checkboxes for configuring the state of auto sync.
*/
const NotificationsSettings = () => {

const { enabled: storeEnabled, summary: storeSummary } = useAppSelector(state => state.config.config.globalSettings.notifications);

const [summary, setSummary] = useState(() => storeEnabled)
const [enabled, setEnabled] = useState(() => storeSummary)

const changeSummary = (event: React.ChangeEvent<HTMLInputElement>) => {
const checked = event.target.checked
updateNotificationsSettingsGlobal({ summary: checked })
setSummary(checked)
}

const changeEnabled = (event: React.ChangeEvent<HTMLInputElement>) => {
const checked = event.target.checked
if(!checked) {
updateNotificationsSettingsGlobal({ summary: false })
setSummary(false)
}
updateNotificationsSettingsGlobal({ enabled: checked })
setEnabled(checked)
}

return (
<div className="notificationsSettings">
<FormControlLabel
control={
<Checkbox
checked={enabled}
onChange={changeEnabled}
name="notifications"
color="primary"
style={{ color: 'var(--color-secondary)' }}

/>
}
label="Enable Notifications"

/>
<FormControlLabel
control={
<Checkbox
checked={summary}
onChange={changeSummary}
name="summary"
style={{ color: 'var(--color-secondary)' }}

/>
}
label="Summarize Notifications"
/>


</div>
)
}

export default NotificationsSettings;
71 changes: 0 additions & 71 deletions src/app/Pages/ActiveDeals/Components/SyncToggles.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions src/app/Pages/ActiveDeals/Components/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

import SyncToggles from "./SyncToggles";
import NotificationsSettings from "./NotificationsSettings";
import SubRowAsync from "./Subrow";

export {
SyncToggles,
NotificationsSettings,
SubRowAsync
}
13 changes: 9 additions & 4 deletions src/app/redux/configActions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { setConfig, setCurrentProfile, updateCurrentProfileByPath, deleteProfileById } from '@/app/redux/configSlice'
import { setConfig, setCurrentProfile, updateCurrentProfileByPath, deleteProfileById, updateNotificationsSettings } from '@/app/redux/configSlice'
import {setSyncData} from '@/app/redux/threeCommas/threeCommasSlice'

import { TconfigValues, Type_Profile, Type_ReservedFunds } from '@/types/config';
import { TconfigValues, Type_NotificationsSettings, Type_Profile, Type_ReservedFunds } from '@/types/config';

import { removeDuplicatesInArray } from '@/utils/helperFunctions';

Expand Down Expand Up @@ -31,6 +31,7 @@ const updateCurrentProfile = (profileData: Type_Profile) => {
store.dispatch(setCurrentProfile(profileData));
// setting this to zero here to prevent a spam of notifications with auto sync enabled.
store.dispatch(setSyncData({syncCount: 0, time: 0}))

}

/**
Expand Down Expand Up @@ -123,12 +124,16 @@ const deleteProfileByIdGlobal = (config: TconfigValues, profileId:string, setOpe
}
}


const updateNotificationsSettingsGlobal = async (settings: Partial<Type_NotificationsSettings>) => {
store.dispatch(updateNotificationsSettings(settings))
await storeConfigInFile()
}

export {
updateConfig,
updateReservedFundsArray,
updateNestedCurrentProfile,
storeConfigInFile,
deleteProfileByIdGlobal
deleteProfileByIdGlobal,
updateNotificationsSettingsGlobal,
}
36 changes: 20 additions & 16 deletions src/app/redux/configSlice.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
import { createSlice } from '@reduxjs/toolkit'
import type { RootState } from './store'
import { TconfigValues, Type_Profile, Type_ReservedFunds } from '@/types/config';
import { defaultConfig, defaultProfile } from '@/utils/defaultConfig';
import { createSlice } from '@reduxjs/toolkit';
import { v4 as uuidv4 } from 'uuid';

import { Type_Profile, TconfigValues, Type_ReservedFunds } from '@/types/config'
import { defaultProfile, defaultConfig } from '@/utils/defaultConfig'


const defaultReservedFunds = {
id: 0,
account_name: '',
reserved_funds: 0,
is_enabled: false
}


// Define the initial state using that type
const initialState = {
config: <TconfigValues>defaultConfig,
currentProfile: <Type_Profile>defaultProfile,
reservedFunds: <Type_ReservedFunds[]>[defaultReservedFunds]
}

const configPaths = {
Expand All @@ -45,6 +34,12 @@ const configPaths = {
general: {
defaultCurrency: 'general.defaultCurrency'
},
globalSettings: {
notifications: {
enabled: 'globalSettings.notifications.enabled',
summary: 'globalSettings.notifications.summary',
}
}
}

export const configSlice = createSlice({
Expand Down Expand Up @@ -126,15 +121,24 @@ export const configSlice = createSlice({
},
addConfigProfile: state => {
state.currentProfile = { ...defaultProfile, id: uuidv4() }
}
},
updateNotificationsSettings: (state, action) => {
const newConfig = { ...state.config }
newConfig.globalSettings.notifications = {
...state.config.globalSettings.notifications,
...action.payload,
}
state.config = newConfig
},
}
})

export const {
setConfig, setCurrentProfile,
updateCurrentProfileByPath, deleteProfileById, addConfigProfile,
setCurrentProfileById,
updateLastSyncTime
updateLastSyncTime,
updateNotificationsSettings,
} = configSlice.actions;
export { configPaths }

Expand Down
17 changes: 10 additions & 7 deletions src/app/redux/threeCommas/Actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,15 @@ const updateAllData = async (offset: number = 1000, profileData: Type_Profile, t
const syncOptions = store.getState().threeCommas.syncOptions
coltoneshaw marked this conversation as resolved.
Show resolved Hide resolved
store.dispatch(setIsSyncing(true))

let time = (syncOptions.time) ? syncOptions.time : 0;
let syncCount = (syncOptions.syncCount) ? syncOptions.syncCount : 0;
if(type === 'fullSync') syncCount = 0;
const originalTime = syncOptions.time || new Date().getTime()
let time = originalTime
let syncCount = syncOptions.syncCount || 0
if (type === 'fullSync') {
syncCount = 0
time = 0
}
const options = {
syncCount,
summary: (syncOptions.summary) ? syncOptions.summary : false,
notifications: (syncOptions.notifications) ? syncOptions.notifications : false,
time,
offset
}
Expand All @@ -243,10 +245,11 @@ const updateAllData = async (offset: number = 1000, profileData: Type_Profile, t
.then((lastSyncTime) => {
store.dispatch(setSyncData({
syncCount:(type === 'autoSync') ? options.syncCount + 1 : 0,
time: (type === 'autoSync') ? options.time + 15000 : 0
// don't override syncOptions.time in case of a fullSync
// because there might be a concurrent autoSync running
time: (type === 'autoSync') ? originalTime + 15000 : originalTime
}))
store.dispatch(updateLastSyncTime({data: lastSyncTime }))

})

} catch (error) {
Expand Down
8 changes: 2 additions & 6 deletions src/app/redux/threeCommas/initialState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ export const initialState = {
isSyncing: false,
isSyncingTime: 0,
syncOptions: <Type_SyncData>{
summary: false,
notifications: true,
time: 0,
syncCount: 0
},
Expand All @@ -61,10 +59,8 @@ export const initialState = {
export type typeString = 'botData' | 'profitData' | 'activeDeals' | 'performanceData' | 'metricsData' | 'accountData' | 'balanceData'

export type Type_SyncData = {
summary?: boolean,
notifications?: boolean,
time?: number,
syncCount?: number
time: number,
syncCount: number
}

export type setDataType =
Expand Down
4 changes: 1 addition & 3 deletions src/app/redux/threeCommas/threeCommasSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ export const threeCommasSlice = createSlice({
state.isSyncingTime = Date.now()
},
setSyncData: (state, action: PayloadAction<Type_SyncData>) => {
// TODO - need to figure out a way to handle this better when multiple things are syncing at the same time
// right now this will just merge the sync data.
state.syncOptions = { ...state.syncOptions, ...action.payload }
state.syncOptions = action.payload
},
setAutoRefresh: (state, action: PayloadAction<boolean>) => {
state.autoRefresh = action.payload
Expand Down
Loading