Skip to content

Commit

Permalink
Implement conditional guest / attendee removal (#7)
Browse files Browse the repository at this point in the history
* Progress on conditional removal

* Get conditional removal working
  • Loading branch information
joyvuu-dave authored Aug 23, 2017
1 parent 2bb43db commit b72671a
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 89 deletions.
12 changes: 6 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ GEM
bootsnap (1.1.2)
msgpack (~> 1.0)
builder (3.2.3)
byebug (9.0.6)
byebug (9.1.0)
case_transform (0.2)
activesupport
coderay (1.1.1)
Expand Down Expand Up @@ -152,7 +152,7 @@ GEM
activerecord
kaminari-core (= 1.0.1)
kaminari-core (1.0.1)
libv8 (5.9.211.38.1)
libv8 (6.0.286.44.0beta1)
loofah (2.0.3)
nokogiri (>= 1.5.9)
mail (2.6.6)
Expand All @@ -162,8 +162,8 @@ GEM
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mini_portile2 (2.2.0)
mini_racer (0.1.12)
libv8 (~> 5.9)
mini_racer (0.1.13)
libv8 (= 6.0.286.44.0beta1)
minitest (5.10.3)
monetize (1.7.0)
money (~> 6.9)
Expand All @@ -185,7 +185,7 @@ GEM
pg (0.21.0)
polyamorous (1.3.1)
activerecord (>= 3.0)
puma (3.9.1)
puma (3.10.0)
pundit (1.1.0)
activesupport (>= 3.0.0)
pusher (1.3.1)
Expand Down Expand Up @@ -349,4 +349,4 @@ RUBY VERSION
ruby 2.4.1p111

BUNDLED WITH
1.15.3
1.15.4
4 changes: 2 additions & 2 deletions app/controllers/api/v1/meals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Api
module V1
class MealsController < ApplicationController
before_action :set_meal, except: [:index]
before_action :set_meal_resident, only: [:destroy_resident, :update_resident]
before_action :set_meal_resident, only: [:destroy_meal_resident, :update_meal_resident]
after_action :trigger_pusher, except: [:index, :show, :show_cooks]

def index
Expand Down Expand Up @@ -55,7 +55,7 @@ def create_guest
end

def destroy_guest
if @meal.guests.find_by(resident_id: params[:resident_id])&.destroy
if @meal.guests.find_by(id: params[:guest_id])&.destroy
render json: { message: 'Guest was destroyed.' } and return
else
render json: { message: 'Guest could not be destroyed.' }, status: :bad_request and return
Expand Down
26 changes: 16 additions & 10 deletions app/javascript/components/attendees_box.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ const styles = {
position: "sticky",
top: 0,
backgroundColor: "var(--almost-white)",
zIndex: "99999"
zIndex: "9999"
},
disabled: {
cursor: "not-allowed",
opacity: "0.5"
}
};

Expand All @@ -46,10 +50,7 @@ const AttendeeComponent = inject("store")(
class AttendeeComponent extends React.Component {
render() {
const resident = this.props.resident;
const store = this.props.store;
const guests = store.guestStore.guests
.values()
.filter(guest => guest.resident_id === resident.id);
const guests = resident.guests;
const vegGuestsCount = guests.filter(guest => guest.vegetarian === true)
.length;
const meatGuestsCount = guests.filter(
Expand All @@ -60,7 +61,12 @@ const AttendeeComponent = inject("store")(
<tr>
<td
onClick={e => resident.toggleAttending()}
style={resident.attending ? styles.yes : styles.no}
style={Object.assign(
{},
resident.attending && styles.yes,
!resident.attending && styles.no,
resident.attending && !resident.canRemove && styles.disabled
)}
>
{resident.name}
</td>
Expand All @@ -83,8 +89,8 @@ const AttendeeComponent = inject("store")(
type="checkbox"
className="switch"
key={`late_switch_${resident.id}`}
defaultChecked={resident.late}
onClick={e => resident.toggleLate()}
checked={resident.late}
onChange={e => resident.toggleLate()}
disabled={
store.meal.closed &&
!resident.attending &&
Expand Down Expand Up @@ -136,7 +142,7 @@ const AttendeeComponent = inject("store")(
<button
style={styles.lowerButton}
onClick={e => resident.removeGuest()}
disabled={store.meal.closed || resident.guests === 0}
disabled={!resident.canRemoveGuest}
>
- Guest
</button>
Expand All @@ -154,7 +160,7 @@ const AttendeeForm = inject("store")(
render() {
return (
<div style={styles.main}>
<table className="table-striped" style={styles.table}>
<table style={styles.table}>
<thead>
<tr>
<th style={styles.sticky}>
Expand Down
6 changes: 5 additions & 1 deletion app/javascript/stores/data_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ export const DataStore = types.model(
// If meal has been opened, re-set extras value
if (val === false) {
self.meal.resetExtras();
self.meal.resetClosedAt();
} else {
self.meal.setClosedAt();
}
}
})
Expand Down Expand Up @@ -333,6 +336,7 @@ export const DataStore = types.model(
// Assign Meal Data
this.meal.description = data.description;
this.meal.closed = data.closed;
this.meal.closed_at = new Date(data.closed_at);

if (data.max === null) {
this.meal.extras = null;
Expand All @@ -345,7 +349,7 @@ export const DataStore = types.model(
this.meal.extras = data.max - (residentsCount + guestsCount);
}

let residents = data.residents.sort(function(a, b) {
let residents = data.residents.sort((a, b) => {
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
return 0;
Expand Down
19 changes: 14 additions & 5 deletions app/javascript/stores/guest_store.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { types, getParent } from "mobx-state-tree";
import Guest from "./guest";

const GuestStore = types.model("GuestStore", {
guests: types.map(Guest),
get form() {
return getParent(this);
const GuestStore = types.model(
"GuestStore",
{
guests: types.map(Guest),
get form() {
return getParent(this);
}
},
{
removeGuest(id) {
this.guests.delete(id);
return true;
}
}
});
);

export default GuestStore;
11 changes: 11 additions & 0 deletions app/javascript/stores/meal.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ const Meal = types.model(
console.log("Extras reset to null.");
return null;
},
resetClosedAt() {
this.closed_at = null;
console.log("Closed At reset to null.");
return null;
},
setClosedAt() {
const time = new Date();
this.closed_at = time;
console.log("Closed At updated to current time.");
return time;
},
setExtras(val) {
const previousExtras = this.extras;
const self = this;
Expand Down
Loading

0 comments on commit b72671a

Please sign in to comment.