-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal messages / sales dashboard impl
- Loading branch information
Showing
9 changed files
with
210 additions
and
32 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
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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { LiveViewRouter } from "../liveview/types"; | ||
import { LicenseLiveViewComponent } from "./license_liveview"; | ||
import { LightLiveViewComponent } from "./light_liveview"; | ||
import { SalesDashboardLiveViewComponent } from "./sales_dashboard_liveview"; | ||
|
||
export const router: LiveViewRouter = { | ||
"/license": new LicenseLiveViewComponent(), | ||
"/light": new LightLiveViewComponent() | ||
"/light": new LightLiveViewComponent(), | ||
'/sales-dashboard': new SalesDashboardLiveViewComponent() | ||
} |
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,108 @@ | ||
import escapeHtml from "../liveview/templates"; | ||
import { LiveViewComponent, LiveViewContext, LiveViewExternalEventListener, LiveViewInternalEventListener } from "../liveview/types"; | ||
import { PhxSocket } from "../socket/types"; | ||
import { sendInternalMessage } from "../socket/websocket_server"; | ||
import { numberToCurrency } from "../utils"; | ||
|
||
// generate a random number between min and max | ||
const random = (min: number, max: number): () => number => { | ||
return () => Math.floor(Math.random() * (max - min + 1)) + min; | ||
} | ||
|
||
const randomSalesAmount = random(100, 1000); | ||
const randomNewOrders = random(5, 20); | ||
const randomSatisfaction = random(95, 100); | ||
|
||
|
||
export interface SalesDashboardContext { | ||
newOrders: number; | ||
salesAmount: number; | ||
satisfaction: number; | ||
} | ||
|
||
export class SalesDashboardLiveViewComponent implements | ||
LiveViewComponent<SalesDashboardContext>, | ||
LiveViewExternalEventListener<SalesDashboardContext, "refresh", any>, | ||
LiveViewInternalEventListener<SalesDashboardContext, "tick"> | ||
{ | ||
|
||
mount(params: any, session: any, socket: PhxSocket) { | ||
if(socket.connected) { | ||
const intervalId = setInterval(() => { | ||
sendInternalMessage(socket, this, "tick"); | ||
}, 1000); | ||
} | ||
return { | ||
data: { | ||
...generateSalesDashboardContext() | ||
} | ||
} | ||
}; | ||
|
||
render(context: LiveViewContext<SalesDashboardContext>) { | ||
return escapeHtml` | ||
<h1>Sales Dashboard</h1> | ||
<div id="dashboard"> | ||
<div class="stats"> | ||
<div class="stat"> | ||
<span class="value"> | ||
${context.data.newOrders} | ||
</span> | ||
<span class="name"> | ||
New Orders | ||
</span> | ||
</div> | ||
<div class="stat"> | ||
<span class="value"> | ||
${ numberToCurrency(context.data.salesAmount)} | ||
</span> | ||
<span class="name"> | ||
Sales Amount | ||
</span> | ||
</div> | ||
<div class="stat"> | ||
<span class="value"> | ||
${context.data.satisfaction} | ||
</span> | ||
<span class="name"> | ||
Satisfaction | ||
</span> | ||
</div> | ||
</div> | ||
<button phx-click="refresh"> | ||
<img src="images/refresh.svg" /> | ||
Refresh | ||
</button> | ||
</div> | ||
` | ||
} | ||
|
||
handleEvent(event: "refresh", params: any, socket: any): LiveViewContext<SalesDashboardContext> { | ||
return { | ||
data: { | ||
...generateSalesDashboardContext() | ||
} | ||
} | ||
} | ||
|
||
handleInfo(event: "tick", socket: PhxSocket): LiveViewContext<SalesDashboardContext> { | ||
return { | ||
data: { | ||
...generateSalesDashboardContext() | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
function generateSalesDashboardContext(): SalesDashboardContext { | ||
return { | ||
newOrders: randomNewOrders(), | ||
salesAmount: randomSalesAmount(), | ||
satisfaction: randomSatisfaction() | ||
} | ||
} | ||
|
||
|
||
|
||
|
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
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,7 @@ | ||
export function numberToCurrency(amount: number) { | ||
var formatter = new Intl.NumberFormat('en-US', { | ||
style: 'currency', | ||
currency: 'USD' | ||
}); | ||
return formatter.format(amount); | ||
} |