diff --git a/nodemon.json b/nodemon.json index ed46727b..0293b25e 100644 --- a/nodemon.json +++ b/nodemon.json @@ -2,7 +2,7 @@ "watch": [ "src/main.ts", "src/preload.ts", - "src/app/*" + "src/electron/*" ], "exec": "webpack --config ./webpack.electron.js && electron .", "ext": "ts" diff --git a/src/app/Context/Config.tsx b/src/app/Context/Config.tsx index fec71e15..5a04cd81 100644 --- a/src/app/Context/Config.tsx +++ b/src/app/Context/Config.tsx @@ -235,13 +235,4 @@ const useGlobalState = () => { }; -// const useGlobalDispatch = () => { -// const context = React.useContext(GlobalDispatchContext); -// if (context === undefined) { -// throw new Error( -// "useGlobalDispatch must be used within a GlobalContextProvider" -// ); -// } -// return context; -// }; export { ConfigContext, ConfigProvider, useGlobalState } \ No newline at end of file diff --git a/src/app/Features/Profiles/Components/ProfileNameEditor.tsx b/src/app/Features/Profiles/Components/ProfileNameEditor.tsx index e5da3d34..cd9cd7e8 100644 --- a/src/app/Features/Profiles/Components/ProfileNameEditor.tsx +++ b/src/app/Features/Profiles/Components/ProfileNameEditor.tsx @@ -1,24 +1,22 @@ import React, { useEffect, useState } from "react"; import { TextField } from '@material-ui/core'; -import { useAppDispatch, useAppSelector } from '@/app/redux/hooks'; -import { storeEditingProfileData, setEditingProfile } from "@/app/redux/configSlice"; +import { useAppSelector } from '@/app/redux/hooks'; +import {configPaths } from "@/app/redux/configSlice"; + +import { updateNestedEditingProfile } from "@/app/redux/configActions"; const ProfileNameEditor = () => { const profile = useAppSelector(state => state.config.editingProfile) - const dispatch = useAppDispatch(); - const [editingProfile, updateEditingProfile] = useState(profile) + const [name, updateName] = useState('') const handleChange = (e: any) => { - - const tempProfile = {...profile, name: e.target.value} - updateEditingProfile(tempProfile) - dispatch(setEditingProfile(tempProfile)) - dispatch(storeEditingProfileData()) + updateName(e.target.value) + updateNestedEditingProfile(e.target.value, configPaths.name) } useEffect(() => { - updateEditingProfile(profile) + updateName(profile.name) }, [profile]) return ( @@ -26,7 +24,7 @@ const ProfileNameEditor = () => { id="ProfileName" label="Profile Name" name="ProfileName" - value={editingProfile.name} + value={name} onChange={handleChange} className="settings-left" style={{ diff --git a/src/app/Features/Profiles/Components/ProfileSwitcher.tsx b/src/app/Features/Profiles/Components/ProfileSwitcher.tsx index c3c5a38c..19dbadcc 100644 --- a/src/app/Features/Profiles/Components/ProfileSwitcher.tsx +++ b/src/app/Features/Profiles/Components/ProfileSwitcher.tsx @@ -41,6 +41,7 @@ const ProfileSwitcher = () => { const styles = (p === currentProfileId) ? { backgroundColor: 'lightBlue' } : {}; return ( { updateCurrentProfileId(p); dispatch(setConfig({...config, current: p})) diff --git a/src/app/Pages/Settings/Components/ApiSettings.tsx b/src/app/Pages/Settings/Components/ApiSettings.tsx index b16ee8ff..24d5259a 100644 --- a/src/app/Pages/Settings/Components/ApiSettings.tsx +++ b/src/app/Pages/Settings/Components/ApiSettings.tsx @@ -1,45 +1,43 @@ -import React, { useEffect } from 'react'; -import dotProp from 'dot-prop'; +import React, { useEffect, useState } from 'react'; -import {TextField, Button, InputLabel, FormControl, MenuItem, Select} from '@material-ui/core'; +import { useAppSelector } from '@/app/redux/hooks'; +import { configPaths } from '@/app/redux/configSlice' +import { updateReservedFundsArray, updateNestedEditingProfile } from '@/app/redux/configActions'; -import { useGlobalState } from '@/app/Context/Config'; -import {Type_ApiKeys, Type_Profile} from '@/types/config' +import { TextField, Button, InputLabel, FormControl, MenuItem, Select } from '@material-ui/core'; - -const ApiSettings = () => { - const { state: { updateApiData, apiData }, currentProfile, actions: { fetchAccountsForRequiredFunds } } = useGlobalState(); - - - const updateKeys = (profile: Type_Profile) => { - if (dotProp.has(profile, 'apis.threeC')) - return profile.apis.threeC - return { key: '', secret: '', mode: 'real' } - } +import { Type_ApiKeys, Type_Profile, Type_ReservedFunds } from '@/types/config' +const ApiSettings = () => { + const profile = useAppSelector(state => state.config.editingProfile); + const reservedFunds = useAppSelector(state => state.config.reservedFunds); + const [apiData, updateApiData] = useState(() => ({ key: "", mode: "", secret: "" })) useEffect(() => { - updateApiData(updateKeys(currentProfile)); - }, [currentProfile]); + updateApiData(profile.apis.threeC) + }, [profile]) const handleChange = (e: any) => { - if(!e.target.name) { - console.debug('Failed to change API setting due to blank name') + const validKeys = ["key", 'secret', 'mode'] + + if (!e.target.name || !validKeys.includes(e.target.name)) { + console.debug('Failed to change API setting due to invalid config path.') console.debug(e) return } + updateApiData((prevState) => { + let newState = { ...prevState } + //@ts-ignore + newState[e.target.name as keyof Type_Profile] = e.target.value - const validKeys = ['key', 'secret', 'mode'] - updateApiData((prevState: Type_ApiKeys) => { - let newState = {...prevState} - - if(!validKeys.includes(e.target.name)) return prevState - - newState[e.target.name as keyof Type_ApiKeys] = e.target.value + updateNestedEditingProfile(newState, configPaths.apis.threeC.main) return newState }) + } + const handleUpdatingReservedFunds = (reservedFunds: Type_ReservedFunds[]) => { + updateNestedEditingProfile(reservedFunds, configPaths.statSettings.reservedFunds) } @@ -47,7 +45,7 @@ const ApiSettings = () => {

API Settings

This app requires "Bots read", "Smart trades read", and "Accounts read" within 3commas.

-
+
{ />
-
- +
+ Mode } - // style={{width: "100%"}} - // @ts-ignore - renderValue={() => (currency.length > 0) ? returnCurrencyValues(currencyArray, currency) : ""} + renderValue={() => (currency.length > 0) ? currency.join(', ') : ""} MenuProps={MenuProps} style={{padding: "10px 0 10px 0", marginLeft: "10px"}} > - {/* Need to think through All because it's now a selector. */} - {/* */} {currencyArray.map((c) => ( diff --git a/src/app/Pages/Settings/Components/ReservedBankroll.tsx b/src/app/Pages/Settings/Components/ReservedBankroll.tsx index fb6ff5b8..750f0db9 100644 --- a/src/app/Pages/Settings/Components/ReservedBankroll.tsx +++ b/src/app/Pages/Settings/Components/ReservedBankroll.tsx @@ -4,11 +4,12 @@ import { Switch } from '@material-ui/core'; import { CustomTable } from '@/app/Components/DataTable/Index' import styled from 'styled-components' +import { useAppDispatch, useAppSelector } from '@/app/redux/hooks'; - +import { setReservedFunds, configPaths } from '@/app/redux/configSlice' +import { updateReservedFundsArray, updateNestedEditingProfile } from '@/app/redux/configActions'; import { Type_ReservedFunds } from '@/types/config'; -import { useGlobalState } from '@/app/Context/Config'; const Styles = styled.div` @@ -69,155 +70,166 @@ const Styles = styled.div` ` interface Cell { - value: { - initialValue: string - } - row: { - original: Type_ReservedFunds, - } - column: { - id:string - } - updateReservedFunds: any + value: { + initialValue: string } - - // Create an editable cell renderer - const EditableCell = ({ - value: initialValue, - row: { original }, - column: { id: column }, - updateReservedFunds, // This is a custom function that we supplied to our table instance - }:Cell) => { - // We need to keep and update the state of the cell normally - const [value, setValue] = useState(String(initialValue)) - // const [size, setSize] = useState(() => String(initialValue).length * 1.5) - - const onChange = (e: React.ChangeEvent) => { - setValue(e.target.value) - - - } - - // We'll only update the external data when the input is blurred - const onBlur = () => { - updateReservedFunds(original.id, column, value, original) - } + row: { + original: Type_ReservedFunds, + } + column: { + id: string + } + updateReservedFunds: any +} + +// Create an editable cell renderer +const EditableCell = ({ + value: initialValue, + row: { original }, + column: { id: column }, + updateReservedFunds, // This is a custom function that we supplied to our table instance +}: Cell) => { + // We need to keep and update the state of the cell normally + const [value, setValue] = useState(String(initialValue)) + // const [size, setSize] = useState(() => String(initialValue).length * 1.5) + + const onChange = (e: React.ChangeEvent) => { + setValue(e.target.value) + - useEffect(() => { - setValue(String(initialValue)) - }, [initialValue]) - - return } + // We'll only update the external data when the input is blurred + const onBlur = () => { + updateReservedFunds(original.id, column, value, original) + } + + useEffect(() => { + setValue(String(initialValue)) + }, [initialValue]) + + return +} + const ReservedBankroll = () => { - // config state - const configState = useGlobalState() - const { state: { reservedFunds, updateReservedFunds } } = configState - - const columns = useMemo( - () => [ - { - Header: 'Enabled?', - accessor: 'is_enabled', - Cell: ({ cell }: any) => ( ) - }, - { - Header: 'Account Name', - accessor: 'account_name' - }, - { - Header: 'Reserved Funds', - accessor: 'reserved_funds', - Cell: EditableCell, - - } - ], - [] - ) - - - const handleOnOff = (e: any) => { - updateReservedFunds((prevState: Type_ReservedFunds[]) => { - return prevState.map(row => { - if (e != undefined && e.target !== null) { - if (e.target.name == row.id) { - console.log(row.is_enabled) - row.is_enabled = !row.is_enabled - } - } - return row - }) - }) - } + const profile = useAppSelector(state => state.config.editingProfile); + const [reservedFunds, updateReservedFunds] = useState([]) - const handleEditCellChangeCommitted = (id:number, column:string, value:string) => { + useEffect(() => { + if (profile.statSettings.reservedFunds) updateReservedFunds(profile.statSettings.reservedFunds) - updateReservedFunds((prevState: Type_ReservedFunds[]) => { - return prevState.map(row => { - if (id == row.id) { + }, [profile]) - // @ts-ignore - validate props - row[column] = value - // console.log(`changed ${e.field} to ${e.value}`) + useEffect(() => { + + }, [reservedFunds]) - } - return row - }) - }) - } + const updateData = (data:Type_ReservedFunds[]) => { + updateNestedEditingProfile(data, configPaths.statSettings.reservedFunds) + return data + } + + + const columns = useMemo( + () => [ + { + Header: 'Enabled?', + accessor: 'is_enabled', + Cell: ({ cell }: any) => () + }, + { + Header: 'Account Name', + accessor: 'account_name' + }, + { + Header: 'Reserved Funds', + accessor: 'reserved_funds', + Cell: EditableCell, + + } + ], + [] + ) + + + const handleOnOff = (e: any) => { + updateReservedFunds(prevState => ( + updateData(prevState.map(row => { + let newRow = {...row} + if (e.target.name == newRow.id) { + newRow.is_enabled = !newRow.is_enabled + } + return newRow + }) + ))) + } + + const handleEditCellChangeCommitted = (id: number, column: string, value: string) => { + + updateReservedFunds(prevState => ( + updateData(prevState.map(row => { + const newRow = {...row} + if (id == newRow.id) { + //@ts-ignore + newRow[column] = value + + } + return newRow + }) + ))) + } + + return ( +
+
+ + + ({ + // onClick: () => setSort(column.id), + style: { + height: '44px', + + }, + + })} + + getColumnProps={() => ({ + + })} + + getRowProps={() => ({ + + })} + //@ts-ignore + getCellProps={cellInfo => ({ + + style: { + color: (cellInfo.column.id === 'actual_usd_profit' || cellInfo.column.id === 'actual_profit_percentage') ? (cellInfo.row.original.in_profit) ? 'var(--color-green)' : 'var(--color-red)' : null, + fontWeight: (cellInfo.column.id === 'actual_usd_profit' || cellInfo.column.id === 'actual_profit_percentage') ? '600' : null + } - return ( -
-
- - - ({ - // onClick: () => setSort(column.id), - style: { - height: '44px', - - }, - - })} - - getColumnProps={() => ({ - - })} - - getRowProps={() => ({ - - })} - //@ts-ignore - getCellProps={cellInfo => ({ - - style: { - color: (cellInfo.column.id === 'actual_usd_profit' || cellInfo.column.id === 'actual_profit_percentage') ? (cellInfo.row.original.in_profit) ? 'var(--color-green)' : 'var(--color-red)' : null, - fontWeight: (cellInfo.column.id === 'actual_usd_profit' || cellInfo.column.id === 'actual_profit_percentage') ? '600' : null - } - - })} - /> - -
-
- ); + })} + /> +
+
+
+ ); } export default ReservedBankroll; \ No newline at end of file diff --git a/src/app/Pages/Settings/Components/SaveDeleteButtons.tsx b/src/app/Pages/Settings/Components/SaveDeleteButtons.tsx index 59c7a4ba..e96f8453 100644 --- a/src/app/Pages/Settings/Components/SaveDeleteButtons.tsx +++ b/src/app/Pages/Settings/Components/SaveDeleteButtons.tsx @@ -4,6 +4,8 @@ import { Button } from '@material-ui/core'; import { useGlobalState } from '@/app/Context/Config'; import { useGlobalData } from '@/app/Context/DataContext'; + +import {storeConfigInFile} from '@/app/redux/configActions' import LoaderIcon from '@/app/Components/icons/Loading/Loading' interface SubmitButtons { @@ -49,10 +51,10 @@ const SaveDeleteButtons = ({setOpen}: SubmitButtons) => { onClick={ async () => { setLoaderIcon(true) try{ - const cfg = await setConfigBulk() - if(cfg){ - await updateAllData(1000, callback) - } + const cfg = await storeConfigInFile() + // if(cfg){ + // await updateAllData(1000, callback) + // } } catch (error) { console.error(error) } diff --git a/src/app/Pages/Settings/Components/StartDatePicker.tsx b/src/app/Pages/Settings/Components/StartDatePicker.tsx index 871ff15f..6500110b 100644 --- a/src/app/Pages/Settings/Components/StartDatePicker.tsx +++ b/src/app/Pages/Settings/Components/StartDatePicker.tsx @@ -4,50 +4,33 @@ import { utcToZonedTime } from 'date-fns-tz'; import React, { useState, useEffect } from 'react'; import DateFnsUtils from '@date-io/date-fns'; -import { useAppDispatch, useAppSelector } from '@/app/redux/hooks'; -import { storeEditingProfileData, setEditingProfile } from "@/app/redux/configSlice"; +import { useAppSelector } from '@/app/redux/hooks'; +import { configPaths } from '@/app/redux/configSlice' +import { updateNestedEditingProfile } from '@/app/redux/configActions'; import { MuiPickersUtilsProvider, KeyboardDatePicker, } from '@material-ui/pickers'; -import { useGlobalState } from '@/app/Context/Config'; export default function StartDatePicker() { const profile = useAppSelector(state => state.config.editingProfile) - const dispatch = useAppDispatch(); - const [editingProfile, updateEditingProfile] = useState(profile) - const [date, updateDate] = useState('') + const [date, updateDate] = useState(() => 0) useEffect(() => { - updateEditingProfile(profile) - - if(profile && editingProfile.statSettings.startDate) { - const adjustedTime = profile.statSettings.startDate + ((new Date()).getTimezoneOffset() * 60000) - const dateString = new Date(adjustedTime).toUTCString() - updateDate(dateString) - } - + if(profile.statSettings.startDate) updateDate(profile.statSettings.startDate) + }, [profile]) const handleDateChange = (date: any) => { - - // const timeZone = getTimeZoneValue() - startOfDay(date) if (date != undefined && isValid(new Date(date))) { - const isoDate = formatISO( startOfDay(addMinutes(date, new Date().getTimezoneOffset()) )).split('T')[0] - const tempProfile = { ...profile } - const utcDate = isoDate + 'T00:00:00Z' - tempProfile.statSettings.startDate = getTime(parseISO(utcDate)) - - // console.log(tempProfile.statSettings.startDate) - updateEditingProfile(tempProfile) - dispatch(setEditingProfile(tempProfile)) - dispatch(storeEditingProfileData()) + const newDate = startOfDay( addMinutes( new Date(date), new Date().getTimezoneOffset() )).getTime(); + updateDate(newDate) + updateNestedEditingProfile(newDate, configPaths.statSettings.startDate) } }; @@ -56,13 +39,6 @@ export default function StartDatePicker() { return new Date(adjustedTime).toUTCString() } - // // converting the date into a ISO date and storing it. - // useEffect(() => { - // const adjustedTime = date + ((new Date()).getTimezoneOffset() * 60000) - // const dateString = new Date(adjustedTime).toUTCString() - // setLocalDate(dateString) - // }, []) - return ( @@ -73,7 +49,7 @@ export default function StartDatePicker() { margin="normal" id="date-picker-inline" label="Stats Start Date" - value={date} + value={modifyDate(date)} onChange={handleDateChange} KeyboardButtonProps={{ 'aria-label': 'change date', diff --git a/src/app/Pages/Settings/Settings.tsx b/src/app/Pages/Settings/Settings.tsx index 64ce8bdf..7e132419 100644 --- a/src/app/Pages/Settings/Settings.tsx +++ b/src/app/Pages/Settings/Settings.tsx @@ -1,16 +1,9 @@ import React, { useEffect, useState } from 'react'; -import { useAppDispatch, useAppSelector } from '@/app/redux/hooks'; -// import {get} from '@/app/redux/configSlice' import './Settings.scss' // @ts-ignore import { version } from '#/package.json'; -// import { useGlobalState } from '@/app/Context/Config'; -// import { returnProfileById } from "@/app/Context/Config/HelperFunctions"; -// import { Type_Profile } from '@/types/config'; - - import { ButtonGroup, @@ -36,12 +29,6 @@ import { ChangelogModal, ToastNotifcations } from '@/app/Features/Index'; // this state is either the current profile active, or the profile that was selected to be edited in the modal. const SettingsPage = () => { - const config = useAppSelector(state => state.config.config) - const dispatch = useAppDispatch() - - // const configState = useGlobalState(); - // const { config, state: { currentlyEditingProfileId, currency, currentProfileId } } = configState; - const [open, setOpen] = useState(false); @@ -60,20 +47,6 @@ const SettingsPage = () => { setOpenChangelog(true); }; - - - // let editingProfile = returnProfileById(currentlyEditingProfileId) - - // let profileId; - - // useEffect(() => { - // console.log('updating the ID from the settings', currentlyEditingProfileId) - // }, [currentlyEditingProfileId]) - - // useEffect(() => { - // console.log('this updated here') - // }, [currentProfileId]) - return ( <> {/*

Settings

*/} diff --git a/src/app/app.tsx b/src/app/app.tsx index adae50a5..b047e039 100644 --- a/src/app/app.tsx +++ b/src/app/app.tsx @@ -17,6 +17,7 @@ import { useAppDispatch, useAppSelector } from '@/app/redux/hooks'; import { updateConfig } from '@/app/redux/configActions'; const App = () => { + // const classes = useStyles(); const themeEngine = useThemeProvidor(); const { styles } = themeEngine diff --git a/src/app/redux/configActions.ts b/src/app/redux/configActions.ts index e5e40f57..e14d6f15 100644 --- a/src/app/redux/configActions.ts +++ b/src/app/redux/configActions.ts @@ -1,31 +1,113 @@ -import {setConfig, setCurrentProfile, setEditingProfile, setEditingProfileId} from '@/app/redux/configSlice' +import { setConfig, setCurrentProfile, setEditingProfile, setEditingProfileId, setReservedFunds, updateOnEditingProfile, storeEditingProfileData } from '@/app/redux/configSlice' -import {TconfigValues, Type_Profile} from '@/types/config'; +import { TconfigValues, Type_Profile, Type_ReservedFunds } from '@/types/config'; + +import { removeDuplicatesInArray } from '@/utils/helperFunctions'; import store from './store' const updateConfig = async () => { - + //@ts-ignore await electron.config.get() - .then((config:any) => { + .then((config: any) => { store.dispatch(setConfig(config)); updateCurrentProfile(config.profiles[config.current]) updateEditingProfile(config.profiles[config.current], config.current) }) +} + +const storeConfigInFile = async () => { + //@ts-ignore + await electron.config.set(null, store.getState().config.config) + updateConfig() } -const updateCurrentProfile = (profileData:Type_Profile) => { +const updateCurrentProfile = (profileData: Type_Profile) => { store.dispatch(setCurrentProfile(profileData)); } -const updateEditingProfile = (profileData:Type_Profile, profileId: string) => { +const updateEditingProfile = (profileData: Type_Profile, profileId: string) => { store.dispatch(setEditingProfile(profileData)); store.dispatch(setEditingProfileId(profileId)); } +/** + * + * @param data data to be stored on the editing profile + * @param path string path to represent where to store the data. + */ +const updateNestedEditingProfile = (data: string | {} | [], path:string) => { + + store.dispatch(updateOnEditingProfile({data, path})) + store.dispatch(storeEditingProfileData()) + +} + +const updateReservedFundsArray = async (key: string, secret: string, mode: string, updateReservedFunds: CallableFunction, reservedFunds: Type_ReservedFunds[]) => { + + console.log({key, secret, mode}) + // @ts-ignore + const accountSummary = await electron.api.getAccountData(key, secret, mode) + + if (accountSummary !== undefined || accountSummary.length > 0) { + + const prevState = []; + + // new data coming in, removing the dups from the array + const filteredAccountData = removeDuplicatesInArray(accountSummary, 'id') + // console.log(filteredAccountData) + + // checking to see if any reserved funds exist + if (reservedFunds.length === 0 || reservedFunds === []) { + console.log('setting since there are no account IDs!') + return filteredAccountData.map(account => { + const { id, name } = account + return { + id, + account_name: name, + reserved_funds: 0, + is_enabled: false + } + }) + } + + // getting account IDs from the reserved funds + const configuredAccountIds = removeDuplicatesInArray(reservedFunds.map(account => account.id), 'id') + console.log(configuredAccountIds) + + // finding any accounts that did not exist since the last sync. + const reservedFundsArray = filteredAccountData + // .filter( account => !configuredAccountIds.includes(account.id) ) + .map(account => { + let { id, name } = account + let reserved_funds = 0; + let is_enabled = false; + + let filteredAccount = prevState.find(account => account.id == id) + if (filteredAccount != undefined) { + reserved_funds = filteredAccount.reserved_funds; + is_enabled = filteredAccount.is_enabled; + } + return { + id, + account_name: name, + reserved_funds, + is_enabled + } + }) + + updateReservedFunds(reservedFundsArray) + + } + +} + export { - updateConfig + updateConfig, + updateReservedFundsArray, + updateNestedEditingProfile, + storeConfigInFile } \ No newline at end of file diff --git a/src/app/redux/configSlice.ts b/src/app/redux/configSlice.ts index fa6c6c79..7618d704 100644 --- a/src/app/redux/configSlice.ts +++ b/src/app/redux/configSlice.ts @@ -1,15 +1,25 @@ import { createSlice } from '@reduxjs/toolkit' import type { RootState } from './store' +import dotProp from 'dot-prop' -import { Type_Profile, TconfigValues } from '@/types/config' +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 a type for the slice state interface ConfigState { config: TconfigValues currentProfile: Type_Profile editingProfile: Type_Profile editingProfileId: string + reservedFunds: Type_ReservedFunds[] } // Define the initial state using that type @@ -17,7 +27,28 @@ const initialState: ConfigState = { config: defaultConfig, currentProfile: defaultProfile, editingProfile: defaultProfile, - editingProfileId: defaultConfig.current + editingProfileId: defaultConfig.current, + reservedFunds: [defaultReservedFunds] +} + +const configPaths = { + apis: { + threeC: { + main: 'apis.threeC', + key: 'apis.threeC.key', + secret: 'apis.threeC.secret', + mode: 'apis.threeC.mode' + } + }, + statSettings: { + reservedFunds: 'statSettings.reservedFunds', + startDate: 'statSettings.startDate', + account_id: 'statSettings.account_id', + }, + name: 'name', + general: { + defaultCurrency: 'general.defaultCurrency' + } } export const configSlice = createSlice({ @@ -38,18 +69,48 @@ export const configSlice = createSlice({ state.editingProfileId = action.payload }, storeEditingProfileData: state => { - // const currentConfig = const newConfig = { ...state.config } const editingProfile = newConfig.profiles[state.editingProfileId] newConfig.profiles[state.editingProfileId] = { ...editingProfile, ...state.editingProfile } state.config = newConfig + }, + setReservedFunds: (state, action) => { + state.reservedFunds = action.payload + }, + updateOnEditingProfile: (state, action) => { + console.log(action) + const { data, path } = action.payload + let newProfile = Object.assign({}, { ...state.editingProfile }) + console.log(newProfile) + + switch (path) { + case configPaths.apis.threeC.main: // update all the api data. + newProfile.apis.threeC = data + break + case configPaths.statSettings.reservedFunds: // update all the api data. + newProfile.statSettings.reservedFunds = data + break + case configPaths.name: // update all the api data. + newProfile.name = data + break + case configPaths.general.defaultCurrency: // update all the currency data + newProfile.general.defaultCurrency = data + break + case configPaths.statSettings.startDate: // update all the api data. + newProfile.statSettings.startDate = data + break + default: + newProfile = newProfile + } + + state.editingProfile = newProfile } } }) -export const { setConfig, setCurrentProfile, setEditingProfile, setEditingProfileId, storeEditingProfileData } = configSlice.actions; - +export const { setConfig, setCurrentProfile, setEditingProfile, setEditingProfileId, storeEditingProfileData, setReservedFunds, updateOnEditingProfile } = configSlice.actions; +export { configPaths } // export const selectCount = (state: RootState) => state.config.config export default configSlice.reducer \ No newline at end of file