Adds Planner component
This commit is contained in:
@@ -76,8 +76,19 @@ mod api {
|
|||||||
Json( recipes::delete(&conn, id) )
|
Json( recipes::delete(&conn, id) )
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct TemplateItems {
|
||||||
|
key: (String, String),
|
||||||
|
value: Option<RecipeObject>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct TemplateObject {
|
||||||
|
items: Vec<TemplateItems>
|
||||||
|
}
|
||||||
|
|
||||||
#[get("/solver/one")]
|
#[get("/solver/one")]
|
||||||
pub fn one_solution(conn: CookbookDbConn) -> Json<String> {
|
pub fn one_solution(conn: CookbookDbConn) -> Json<TemplateObject> {
|
||||||
use planner::{
|
use planner::{
|
||||||
template,
|
template,
|
||||||
solver::{Domain, Problem}
|
solver::{Domain, Problem}
|
||||||
@@ -93,8 +104,18 @@ mod api {
|
|||||||
.add_constraint(|_| true)
|
.add_constraint(|_| true)
|
||||||
.finish();
|
.finish();
|
||||||
let results = problem.solve_all();
|
let results = problem.solve_all();
|
||||||
|
let one_result = results.first().unwrap();
|
||||||
|
|
||||||
Json(format!("{:?}", results.first().unwrap()))
|
Json(TemplateObject {
|
||||||
|
items: one_result
|
||||||
|
.into_iter()
|
||||||
|
.map(|(k,v)| {
|
||||||
|
TemplateItems {
|
||||||
|
key: (format!("{}", k.0), format!("{:?}", k.1)),
|
||||||
|
value: v.map(|r| RecipeObject::from(&conn, r.clone())),
|
||||||
|
}})
|
||||||
|
.collect(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,16 +2,52 @@
|
|||||||
<div class="box">
|
<div class="box">
|
||||||
<h2 class="subtitle">Week</h2>
|
<h2 class="subtitle">Week</h2>
|
||||||
<button @click="fetchSolution" class="button is-primary">FetchSolution</button>
|
<button @click="fetchSolution" class="button is-primary">FetchSolution</button>
|
||||||
<p>{{ template }}</p>
|
<hr />
|
||||||
|
<div v-if="template">
|
||||||
|
<div class="columns">
|
||||||
|
<div v-for="day_meals in itemsGroupedByDay"
|
||||||
|
class="column">
|
||||||
|
<strong> {{ day_meals[0] }}</strong>
|
||||||
|
<p v-for="meal in day_meals[1]">
|
||||||
|
<span class="tag is-light">{{ meal.value.title }}</span>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
|
||||||
|
const groupBy = function(items, keyGetter) {
|
||||||
|
const map = new Map();
|
||||||
|
items.forEach((item) => {
|
||||||
|
const key = keyGetter(item);
|
||||||
|
const collection = map.get(key);
|
||||||
|
if (!collection) {
|
||||||
|
map.set(key, [item]);
|
||||||
|
} else {
|
||||||
|
collection.push(item);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return map;
|
||||||
|
};
|
||||||
|
|
||||||
|
const DAYS = ["Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche"];
|
||||||
|
const compareWeekDay = function(a, b) {
|
||||||
|
return DAYS.indexOf(a[0]) - DAYS.indexOf(b[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
const MEALS = ["Breakfast", "Lunch", "Dinner"];
|
||||||
|
const compareMealType = function(mealA, mealB) {
|
||||||
|
return MEALS.indexOf(mealA.key[1]) - MEALS.indexOf(mealB.key[1]);
|
||||||
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'Planner',
|
name: 'Planner',
|
||||||
data () {
|
data () {
|
||||||
return {
|
return {
|
||||||
template: "",
|
template: null,
|
||||||
};},
|
};},
|
||||||
methods: {
|
methods: {
|
||||||
fetchSolution: function() {
|
fetchSolution: function() {
|
||||||
@@ -20,6 +56,16 @@ export default {
|
|||||||
.then((data) => this.template = data)
|
.then((data) => this.template = data)
|
||||||
.catch((err) => console.log(err));
|
.catch((err) => console.log(err));
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
computed: {
|
||||||
|
itemsGroupedByDay: function() {
|
||||||
|
let grouped = groupBy(this.template.items, item => item.key[0]);
|
||||||
|
console.log(grouped);
|
||||||
|
let sorted = new Map([...grouped.entries()].sort(compareWeekDay));
|
||||||
|
console.log(sorted);
|
||||||
|
sorted.forEach((meals) => meals.sort(compareMealType));
|
||||||
|
return sorted;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user