Skip to content

Commit

Permalink
Modified config state names to match react rules. Moved profile featu…
Browse files Browse the repository at this point in the history
…re components

fixed bug in the state, reverted to one large export
  • Loading branch information
coltoneshaw committed Sep 13, 2021
1 parent 7b45a55 commit f911b92
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 141 deletions.
21 changes: 11 additions & 10 deletions src/app/Context/Config.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React, { createContext, useState, useEffect, SetStateAction} from 'react';
import dotProp from 'dot-prop';
import { v4 as uuidv4 } from 'uuid'

import {
configDate,
configApiData,
configCurrency,
configAccountID,
configReservedFunds } from "@/app/Context/Config/HelperFunctions";
useConfigApiData,
useConfigDate,
useConfigCurrency,
useConfigAccountID,
useConfigReservedFunds } from "@/app/Context/Config/HelperFunctions";

import { TconfigValues, Type_Profile, Type_ConfigContext } from '@/types/config'

Expand All @@ -27,11 +28,11 @@ const ConfigProvider = ({ children }: any) => {
const [config, updateConfig] = useState<TconfigValues>(defaultConfig);


const {date, updateDate, setNewStatDate} = configDate();
const {apiData, setNewApiKeys, updateApiData} = configApiData();
const {currency, updateCurrency, setNewCurrency} = configCurrency();
const {accountID, updateAccountID, setNewAccountIdArray} = configAccountID();
const {reservedFunds, updateReservedFunds, setNewReservedFunds, fetchReservedFundsUpdate} = configReservedFunds();
const {date, updateDate, setNewStatDate} = useConfigDate();
const {apiData, setNewApiKeys, updateApiData} = useConfigApiData();
const {currency, updateCurrency, setNewCurrency} = useConfigCurrency();
const {accountID, updateAccountID, setNewAccountIdArray} = useConfigAccountID();
const {reservedFunds, updateReservedFunds, setNewReservedFunds, fetchReservedFundsUpdate} = useConfigReservedFunds();
const fetchAccountsForRequiredFunds = async (key:string, secret:string, mode:string) => await fetchReservedFundsUpdate(key, secret, mode, updateReservedFunds, reservedFunds)

// current profile config values.
Expand Down
142 changes: 77 additions & 65 deletions src/app/Context/Config/HelperFunctions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ const useProfileState = () => {

}

const configDate = () => {
const useConfigDate = () => {
// setting the default state to be 90 days in the past.
const [date, updateDate] = useState(() => getTime(sub(new Date(), { days: 90 })))

const setNewStatDate = (profile: Type_Profile) => {
updateDate(() => {
const startDate: number | undefined = dotProp.get(profile, 'statSettings.startDate')
return (startDate) ? startDate : getTime(sub(new Date(), { days: 90 })) ;
return (startDate) ? startDate : getTime(sub(new Date(), { days: 90 }));
})
}

Expand All @@ -70,7 +70,7 @@ const configDate = () => {
}
}

const configApiData = () => {
const useConfigApiData = () => {
const [apiData, updateApiData] = useState({ key: '', secret: '', mode: 'real' });

const setNewApiKeys = (profile: Type_Profile) => {
Expand All @@ -94,7 +94,7 @@ const configApiData = () => {

}

const configCurrency = () => {
const useConfigCurrency = () => {
const [currency, updateCurrency] = useState<string[]>([])

const setNewCurrency = (profile: Type_Profile) => {
Expand All @@ -112,7 +112,7 @@ const configCurrency = () => {

}

const configAccountID = () => {
const useConfigAccountID = () => {
const [accountID, updateAccountID] = useState<number[]>([]);
const setNewAccountIdArray = (profile: Type_Profile) => {
updateAccountID(() => {
Expand All @@ -129,81 +129,88 @@ const configAccountID = () => {

}

const fetchReservedFundsUpdate = async (key:string, secret:string, mode:string, updateReservedFunds: CallableFunction, reservedFunds: Type_ReservedFunds[]) => {
const fetchReservedFundsUpdate = async (key: string, secret: string, mode: string, updateReservedFunds: CallableFunction, reservedFunds: Type_ReservedFunds[]) => {
// @ts-ignore
const accountSummary = await electron.api.getAccountData(key, secret, mode)

if (accountSummary !== undefined || accountSummary.length > 0) {
updateReservedFunds( ( prevState: Type_ReservedFunds[]) => {

// 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 (prevState.length === 0 || prevState === []) {
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.
return 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((prevState: Type_ReservedFunds[]) => {

// 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 (prevState.length === 0 || prevState === []) {
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.
return 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
}
})

})
}

}

const configReservedFunds = () => {
const useConfigReservedFunds = () => {
const [reservedFunds, updateReservedFunds] = useState<Type_ReservedFunds[]>([]);
const setNewReservedFunds = (profile: Type_Profile) => {
updateReservedFunds( () => {
updateReservedFunds(() => {
const reservedFundsArray: Type_ReservedFunds[] | undefined = dotProp.get(profile, 'statSettings.reservedFunds')
return (reservedFundsArray) ? reservedFundsArray : [];
})
}

return {
reservedFunds,
updateReservedFunds,
reservedFunds,
updateReservedFunds,
setNewReservedFunds,
fetchReservedFundsUpdate
}
}

const updateProfileConfig = (profileId:string, data: {}) => {
const returnProfileById = (profileId: string) => {
// const [allProfiles, updateAppProfiles] = useState<Type_Profile>(defaultProfile)
const configState = useGlobalState();
const { config } = configState;
return config.profiles[profileId];
}

const updateProfileConfig = (profileId: string, data: {}) => {

console.log({profileId, data})
console.log({ profileId, data })

// accepts profile ID, filters the config based on this
// updates the needed fields in the config state
Expand All @@ -221,21 +228,26 @@ const updateProfileConfig = (profileId:string, data: {}) => {
// ...data
// };


// return {...newState}
// })
}


const state = {

}


export {
returnCurrentProfile,
updateProfileConfig,
returnProfileById,
useConfigDate,
useProfileState,
configDate,
configApiData,
configCurrency,
configAccountID,
configReservedFunds,
updateProfileConfig
useConfigApiData,
useConfigCurrency,
useConfigAccountID,
useConfigReservedFunds,

}
7 changes: 7 additions & 0 deletions src/app/Features/Profiles/Components/Index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ProfileNameEditor from "./ProfileNameEditor";
import ProfileSwitcher from "./ProfileSwitcher";

export {
ProfileNameEditor,
ProfileSwitcher
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { TextField } from '@material-ui/core';
import { returnCurrentProfile, updateProfileConfig } from '@/app/Context/Config/HelperFunctions';


const ProfileName = ({currentName,currentProfileId}: { currentName: string, currentProfileId:any }) => {
const ProfileNameEditor = ({currentName,currentProfileId}: { currentName: string, currentProfileId:any }) => {

const [name, updateName] = useState('')

Expand Down Expand Up @@ -35,4 +35,4 @@ const ProfileName = ({currentName,currentProfileId}: { currentName: string, curr
)
}

export default ProfileName;
export default ProfileNameEditor;
64 changes: 0 additions & 64 deletions src/app/Pages/Settings/Components/SaveSubmitButtons.tsx

This file was deleted.

0 comments on commit f911b92

Please sign in to comment.