Fixed sorting/adding bug
This commit is contained in:
		
							parent
							
								
									793f8ba79a
								
							
						
					
					
						commit
						a57ca025f5
					
				
					 3 changed files with 15 additions and 4 deletions
				
			
		| 
						 | 
				
			
			@ -1,7 +1,7 @@
 | 
			
		|||
<div class="rounded-md shadow-md">
 | 
			
		||||
  <SortableGroup
 | 
			
		||||
    @model={{this.items}}
 | 
			
		||||
    @onChange={{action this.reorderItems}}
 | 
			
		||||
    @model={{this.sortedItems}}
 | 
			
		||||
    @onChange={{action this.reorderMeals}}
 | 
			
		||||
    as |group|>
 | 
			
		||||
    {{#each group.model as |meal|}}
 | 
			
		||||
      <group.item
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -13,13 +13,23 @@ export default class RecipeListComponent extends Component {
 | 
			
		|||
    this.assignExistingItems();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  get sortedItems() {
 | 
			
		||||
    console.log('sorted items called');
 | 
			
		||||
    return this.items.sortBy('listOrder');
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  async assignExistingItems() {
 | 
			
		||||
    let items = await this.store.peekAll('meal');
 | 
			
		||||
    this.items = items;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @action
 | 
			
		||||
  reorderItems(reorderedItems) {
 | 
			
		||||
    this.items = reorderedItems;
 | 
			
		||||
  async reorderMeals(reorderedMeals) {
 | 
			
		||||
    let orderedIds = reorderedMeals.map(meal => meal.id);
 | 
			
		||||
 | 
			
		||||
    for (let index = 0; index < orderedIds.length; index++) {
 | 
			
		||||
      let meal = await this.store.peekRecord('meal', orderedIds[index]);
 | 
			
		||||
      meal.listOrder = index;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -11,4 +11,5 @@ export default class MealModel extends Model {
 | 
			
		|||
  @attr tags;
 | 
			
		||||
  @attr youtube;
 | 
			
		||||
  @attr source;
 | 
			
		||||
  @attr('number', { defaultValue: Infinity }) listOrder;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue