Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(open-spark): fixed minor functinal chnges in dashboard, trade det… #2785

Merged
merged 1 commit into from
Dec 17, 2024
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
11 changes: 1 addition & 10 deletions apps/open-spark/components/energyPurchaseForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,7 @@ export default function EnergyPurchaseForm({ preferenceType }: EnergyPurchaseFor

if (response.status === 200 || response.status === 204) {
console.log('Trade created successfully:', response.data)
if (role !== ROLE.PRODUCER) {
router.push({
pathname: '/',
query: { id: response.data?.id }
})
} else {
router.push({
pathname: '/'
})
}
router.push('/')
}
} catch (error) {
console.error('Error creating trade:', error)
Expand Down
26 changes: 25 additions & 1 deletion apps/open-spark/components/signUp/signUp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { AuthRootState } from '@store/auth-slice'
import { useSelector } from 'react-redux'
import { ROLE, ROUTE_TYPE } from '@lib/config'
import axios from '@services/axios'
import Cookies from 'js-cookie'

interface RegisterFormProps extends SignUpFormProps {
utilityCompany: string
Expand Down Expand Up @@ -113,6 +114,26 @@ const SignUp = () => {
}))
}

const createTradeCatalogue = async () => {
const { name } = formData
const tradeCatalogueData = {
provider_name: `${name} Energy`,
short_desc: `${name} Energy Company`,
long_desc: `${name} Energy Company - Providing energy solutions`,
domain_name: 'uei:p2p_trading'
}

try {
const response = await axios.post(`${strapiUrl}${ROUTE_TYPE.PRODUCER}/profile`, tradeCatalogueData, {
headers: { Authorization: `Bearer ${Cookies.get('authToken') || ''}` },
withCredentials: true
})
console.log('Trade catalogue created:', response.data)
} catch (error) {
console.error('An error occurred:', error)
}
}

// Handle sign-up action
const handleSignUp = async () => {
const errors = signUpValidateForm(formData)
Expand All @@ -130,7 +151,10 @@ const SignUp = () => {
try {
let registerResponse = null
if (role === ROLE.CONSUMER) registerResponse = await bapTradeRegister(signUpData)
if (role === ROLE.PRODUCER) registerResponse = await bppTradeRegister(signUpData)
if (role === ROLE.PRODUCER) {
registerResponse = await bppTradeRegister(signUpData)
createTradeCatalogue()
}

if (!registerResponse || (registerResponse as { error: FetchBaseQueryError })?.error)
throw new Error('Could not register')
Expand Down
43 changes: 22 additions & 21 deletions apps/open-spark/pages/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,30 +104,31 @@ const Dashboard = () => {
}, [role, bapDashboardData, bppDashboardData, startDate, endDate])

const fetchLastTradeData = async () => {
const routerQueryId = router.query?.id
if (routerQueryId) {
try {
const response = await axios.get(`${strapiUrl}${ROUTE_TYPE[role!]}/trade?id=${routerQueryId}`, {
headers: { Authorization: `Bearer ${bearerToken}` },
withCredentials: true
})
try {
const response = await axios.get(`${strapiUrl}${ROUTE_TYPE[role!]}/trade`, {
headers: { Authorization: `Bearer ${bearerToken}` },
withCredentials: true
})

const result = response.data
const mappedTrade: TradeData = {
id: result.id,
quantity: result.quantity,
price: result.price || 0
}
const result = response.data

setCurrentTradeData([mappedTrade])
const statusData = createStatusData(result)
setCurrentStatusData(statusData)
const lastTrade = result[result.length - 1]

const tags = [result.trusted_source && 'Trusted Source', result.cred_required && 'Solar Energy'].filter(Boolean)
setPreferencesTags(tags)
} catch (error) {
console.error('Error fetching last trade data:', error)
const mappedTrade: TradeData = {
id: lastTrade.id,
quantity: lastTrade.quantity,
price: lastTrade.price || 0
}

setCurrentTradeData([mappedTrade])
const statusData = createStatusData(lastTrade)
setCurrentStatusData(statusData)

const tags = [lastTrade.trusted_source && 'Trusted Source', lastTrade.cred_required && 'Solar Energy'].filter(
Boolean
)
} catch (error) {
console.error('Error fetching last trade data:', error)
}
}

Expand Down Expand Up @@ -275,7 +276,7 @@ const Dashboard = () => {
/>
<QuestionOutlineIcon />
</HStack>
{currentTradeData.length === 0 ? (
{currentTradeData.length === 0 || latestStatus?.status === 'SUCCESS' ? (
<></>
) : (
<LiaPenSolid
Expand Down
2 changes: 1 addition & 1 deletion apps/open-spark/pages/tradeDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const TradeDetails = () => {
}

setTradeDetails({
orderId: result.orderId,
orderId: result.order?.id || null,
name: result.item_name,
price: result.price || 0,
quantity: result.quantity,
Expand Down