-
-
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.
Merge pull request #9 from floodfx/pagination
implemented pagination which required implementing pushPatch server m…
- Loading branch information
Showing
10 changed files
with
347 additions
and
15 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 |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import { options_for_select } from "../../server/templates/helpers/options_for_select"; | ||
import { live_patch } from "../../server/templates/helpers/live_patch"; | ||
import html, { HtmlSafeString, join } from "../../server/templates"; | ||
import { BaseLiveViewComponent, LiveViewExternalEventListener, LiveViewSocket, StringPropertyValues } from "../../server/types"; | ||
import { almostExpired, Donation, listItems } from "./data"; | ||
|
||
interface Options { | ||
page: number; | ||
perPage: number; | ||
} | ||
|
||
export interface PaginateContext { | ||
options: Options | ||
donations: Donation[] | ||
} | ||
|
||
export class PaginateLiveViewComponent extends BaseLiveViewComponent<PaginateContext, Options> implements LiveViewExternalEventListener<PaginateContext, "select-per-page", Pick<Options, "perPage">> { | ||
|
||
mount(params: any, session: any, socket: LiveViewSocket<PaginateContext>) { | ||
const options = { page: 1, perPage: 10 } | ||
return { | ||
options: options, | ||
donations: listItems(options.page, options.perPage) | ||
}; | ||
}; | ||
|
||
handleParams(params: StringPropertyValues<Options>, url: string, socket: LiveViewSocket<PaginateContext>): PaginateContext { | ||
const page = Number(params.page || 1); | ||
const perPage = Number(params.perPage || 10); | ||
return { | ||
options: { page, perPage }, | ||
donations: listItems(page, perPage) | ||
}; | ||
} | ||
|
||
render(context: PaginateContext) { | ||
const { options: { perPage, page }, donations } = context; | ||
return html` | ||
<h1>Food Bank Donations</h1> | ||
<div id="donations"> | ||
<form phx-change="select-per-page"> | ||
Show | ||
<select name="perPage"> | ||
${options_for_select([5, 10, 15, 20].map(n => String(n)), String(perPage))} | ||
</select> | ||
<label for="perPage">per page</label> | ||
</form> | ||
<div class="wrapper"> | ||
<table> | ||
<thead> | ||
<tr> | ||
<th class="item"> | ||
Item | ||
</th> | ||
<th> | ||
Quantity | ||
</th> | ||
<th> | ||
Days Until Expires | ||
</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
${this.renderDonations(donations)} | ||
</tbody> | ||
</table> | ||
<div class="footer"> | ||
<div class="pagination"> | ||
${page > 1 ? this.paginationLink("Previous", page - 1, perPage, "previous") : ""} | ||
${this.pageLinks(page, perPage)} | ||
${this.paginationLink("Next", page + 1, perPage, "next")} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
` | ||
}; | ||
|
||
handleEvent(event: "select-per-page", params: StringPropertyValues<Pick<Options, "perPage">>, socket: LiveViewSocket<PaginateContext>): PaginateContext { | ||
const page = socket.context.options.page; | ||
const perPage = Number(params.perPage || 10); | ||
|
||
this.pushPatch(socket, { to: { path: "/paginate", params: { page: String(page), perPage: String(perPage) } } }); | ||
|
||
return { | ||
options: { page, perPage }, | ||
donations: listItems(page, perPage) | ||
}; | ||
} | ||
|
||
pageLinks(page: number, perPage: number) { | ||
let links: HtmlSafeString[] = []; | ||
for (var p = page - 2; p <= page + 2; p++) { | ||
if (p > 0) { | ||
links.push(this.paginationLink(String(p), p, perPage, p === page ? "active" : "")) | ||
} | ||
} | ||
return join(links, "") | ||
} | ||
|
||
paginationLink(text: string, pageNum: number, perPageNum: number, className: string) { | ||
const page = String(pageNum); | ||
const perPage = String(perPageNum); | ||
return live_patch(text, { | ||
to: { | ||
path: "/paginate", | ||
params: { page, perPage } | ||
}, | ||
class: className | ||
}) | ||
} | ||
|
||
renderDonations(donations: Donation[]) { | ||
return donations.map(donation => html` | ||
<tr> | ||
<td class="item"> | ||
<span class="id">${donation.id}</span> | ||
${donation.emoji} ${donation.item} | ||
</td> | ||
<td> | ||
${donation.quantity} lbs | ||
</td> | ||
<td> | ||
<span class="${this.expiresClass(donation)}"> | ||
${donation.days_until_expires} | ||
</span> | ||
</td> | ||
</tr> | ||
`) | ||
} | ||
|
||
expiresClass(donation: Donation) { | ||
if (almostExpired(donation)) { | ||
return "eat-now" | ||
} else { | ||
return "fresh" | ||
} | ||
} | ||
|
||
} | ||
|
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,76 @@ | ||
|
||
|
||
export interface Donation { | ||
id: string; | ||
emoji: string | ||
item: string | ||
quantity: number | ||
days_until_expires: number | ||
} | ||
|
||
const items = [ | ||
{ emoji: "☕️", item: "Coffee" }, | ||
{ emoji: "🥛", item: "Milk" }, | ||
{ emoji: "🥩", item: "Beef" }, | ||
{ emoji: "🍗", item: "Chicken" }, | ||
{ emoji: "🍖", item: "Pork" }, | ||
{ emoji: "🍗", item: "Turkey" }, | ||
{ emoji: "🥔", item: "Potatoes" }, | ||
{ emoji: "🥣", item: "Cereal" }, | ||
{ emoji: "🥣", item: "Oatmeal" }, | ||
{ emoji: "🥚", item: "Eggs" }, | ||
{ emoji: "🥓", item: "Bacon" }, | ||
{ emoji: "🧀", item: "Cheese" }, | ||
{ emoji: "🥬", item: "Lettuce" }, | ||
{ emoji: "🥒", item: "Cucumber" }, | ||
{ emoji: "🐠", item: "Smoked Salmon" }, | ||
{ emoji: "🐟", item: "Tuna" }, | ||
{ emoji: "🐡", item: "Halibut" }, | ||
{ emoji: "🥦", item: "Broccoli" }, | ||
{ emoji: "🧅", item: "Onions" }, | ||
{ emoji: "🍊", item: "Oranges" }, | ||
{ emoji: "🍯", item: "Honey" }, | ||
{ emoji: "🍞", item: "Sourdough Bread" }, | ||
{ emoji: "🥖", item: "French Bread" }, | ||
{ emoji: "🍐", item: "Pear" }, | ||
{ emoji: "🥜", item: "Nuts" }, | ||
{ emoji: "🍎", item: "Apples" }, | ||
{ emoji: "🥥", item: "Coconut" }, | ||
{ emoji: "🧈", item: "Butter" }, | ||
{ emoji: "🧀", item: "Mozzarella" }, | ||
{ emoji: "🍅", item: "Tomatoes" }, | ||
{ emoji: "🍄", item: "Mushrooms" }, | ||
{ emoji: "🍚", item: "Rice" }, | ||
{ emoji: "🍜", item: "Pasta" }, | ||
{ emoji: "🍌", item: "Banana" }, | ||
{ emoji: "🥕", item: "Carrots" }, | ||
{ emoji: "🍋", item: "Lemons" }, | ||
{ emoji: "🍉", item: "Watermelons" }, | ||
{ emoji: "🍇", item: "Grapes" }, | ||
{ emoji: "🍓", item: "Strawberries" }, | ||
{ emoji: "🍈", item: "Melons" }, | ||
{ emoji: "🍒", item: "Cherries" }, | ||
{ emoji: "🍑", item: "Peaches" }, | ||
{ emoji: "🍍", item: "Pineapples" }, | ||
{ emoji: "🥝", item: "Kiwis" }, | ||
{ emoji: "🍆", item: "Eggplants" }, | ||
{ emoji: "🥑", item: "Avocados" }, | ||
{ emoji: "🌶", item: "Peppers" }, | ||
{ emoji: "🌽", item: "Corn" }, | ||
{ emoji: "🍠", item: "Sweet Potatoes" }, | ||
{ emoji: "🥯", item: "Bagels" }, | ||
{ emoji: "🥫", item: "Soup" }, | ||
{ emoji: "🍪", item: "Cookies" } | ||
] | ||
|
||
export const donations: Donation[] = items.map((item, id) => { | ||
const quantity = Math.floor(Math.random() * 20) + 1; | ||
const days_until_expires = Math.floor(Math.random() * 30) + 1; | ||
return { ...item, quantity, days_until_expires, id: (id + 1).toString() } | ||
}) | ||
|
||
export const listItems = (page: number, perPage: number) => { | ||
return donations.slice((page - 1) * perPage, page * perPage) | ||
} | ||
|
||
export const almostExpired = (donation: Donation) => donation.days_until_expires <= 10 |
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
Oops, something went wrong.