-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
202 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import styled from "styled-components"; | ||
import PageLayout from "../../components/layouts/PageLayout"; | ||
import { getLoggedUser, useAuth } from "../../providers/Auth"; | ||
import { getFullKit, getKitMeasures, getUserDetails, getUserMemberships } from "../../services/data-api"; | ||
|
||
function Dashboard({ }) { | ||
const { user, isLogged } = useAuth(); | ||
|
||
return ( | ||
<PageLayout | ||
metaTitle={"Home"} | ||
metaDescription={ | ||
"The AstroPlant community platform. Read the latest astroplant news, share your results and stories with your fellow space farmers! Manage your kits, and find help and support for your kit issues. AstroPlant, growing a new generation of urban and space farmers." | ||
} | ||
> | ||
</PageLayout> | ||
); | ||
} | ||
|
||
export async function getServerSideProps(ctx) { | ||
let mainKit = null; | ||
const user = getLoggedUser(ctx.req.headers.cookie); | ||
|
||
let completeUser = null; | ||
let memberships = null; | ||
|
||
if (user) { | ||
completeUser = await getUserDetails(user.username); | ||
memberships = await getUserMemberships(user.username); | ||
|
||
if (Array.isArray(memberships) && memberships[0]?.kit) { | ||
const kit = await getFullKit(memberships[0].kit.serial); | ||
kit.measures = await getKitMeasures(memberships[0].kit.serial, {}); | ||
mainKit = kit; | ||
} | ||
} | ||
|
||
return { | ||
props: { | ||
mainKit: mainKit, | ||
}, | ||
}; | ||
} | ||
|
||
export default Dashboard; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { makeAutoObservable } from "mobx" | ||
import { createContext } from "react" | ||
import { WS_API_URL } from "../services/data-api"; | ||
|
||
class Measurements { | ||
|
||
socket = null; | ||
serial = ""; | ||
measurements = [] | ||
|
||
constructor() { | ||
makeAutoObservable(this) | ||
} | ||
|
||
setMeasurements = (measurements) => { | ||
this.measurements = measurements; | ||
} | ||
|
||
addMeasurement = (measurement) => { | ||
this.measurements = this.measurements.concat(measurement); | ||
} | ||
|
||
connect = () => { | ||
|
||
if (this.socket?.readyState < 2) { | ||
this.disconnect(); | ||
} | ||
|
||
this.socket = new WebSocket(WS_API_URL); | ||
|
||
this.socket?.addEventListener("open", () => { | ||
|
||
if (!this.serial) return; | ||
|
||
this.socket.send(JSON.stringify({ | ||
id: 1, | ||
jsonrpc: "2.0", | ||
method: "subscribe_rawMeasurements", | ||
params: { kitSerial: this.serial } | ||
})); | ||
|
||
this.socket.addEventListener("message", (event) => { | ||
const data = JSON.parse(event.data); | ||
if (data.method === "rawMeasurements" && data.params.result) { | ||
this.addMeasurement(data.params.result); | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
disconnect = () => { | ||
this.socket?.close(); | ||
} | ||
|
||
setSerial = (newSerial) => { | ||
this.serial = newSerial; | ||
this.connect(); | ||
} | ||
} | ||
|
||
export const measurementsStore = new Measurements(); | ||
export const measurementCtx = createContext(measurementsStore); |