mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Injecting producer information into BPE
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
angular.module("ofn.admin").controller "AdminProductEditCtrl", [
|
||||
"$scope", "$timeout", "$http", "dataFetcher", "DirtyProducts", "VariantUnitManager",
|
||||
($scope, $timeout, $http, dataFetcher, DirtyProducts, VariantUnitManager) ->
|
||||
"$scope", "$timeout", "$http", "dataFetcher", "DirtyProducts", "VariantUnitManager", "producers",
|
||||
($scope, $timeout, $http, dataFetcher, DirtyProducts, VariantUnitManager, producers) ->
|
||||
$scope.updateStatusMessage =
|
||||
text: ""
|
||||
style: {}
|
||||
|
||||
$scope.producers = producers
|
||||
|
||||
$scope.columns =
|
||||
producer: {name: "Producer", visible: true}
|
||||
name: {name: "Name", visible: true}
|
||||
@@ -43,10 +45,7 @@ angular.module("ofn.admin").controller "AdminProductEditCtrl", [
|
||||
$scope.spree_api_key_ok = data.hasOwnProperty("success") and data["success"] == "Use of API Authorised"
|
||||
if $scope.spree_api_key_ok
|
||||
$http.defaults.headers.common["X-Spree-Token"] = spree_api_key
|
||||
dataFetcher("/api/enterprises/managed?template=bulk_index&q[is_primary_producer_eq]=true").then (data) ->
|
||||
$scope.producers = data
|
||||
# Need to have producers before we get products so we can match producers to product.producer
|
||||
$scope.fetchProducts()
|
||||
$scope.fetchProducts()
|
||||
else if authorise_api_reponse.hasOwnProperty("error")
|
||||
$scope.api_error_msg = authorise_api_reponse("error")
|
||||
else
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
Spree::Admin::ProductsController.class_eval do
|
||||
before_filter :load_spree_api_key, :only => :bulk_edit
|
||||
before_filter :load_bpe_data, :only => :bulk_edit
|
||||
|
||||
alias_method :location_after_save_original, :location_after_save
|
||||
|
||||
@@ -78,8 +78,9 @@ Spree::Admin::ProductsController.class_eval do
|
||||
|
||||
private
|
||||
|
||||
def load_spree_api_key
|
||||
def load_bpe_data
|
||||
current_user.generate_spree_api_key! unless spree_current_user.spree_api_key
|
||||
@spree_api_key = spree_current_user.spree_api_key
|
||||
@producers = Enterprise.managed_by(spree_current_user).is_primary_producer
|
||||
end
|
||||
end
|
||||
|
||||
@@ -12,6 +12,10 @@ module Admin
|
||||
admin_inject_json_ams_array "admin.shipping_methods", "shippingMethods", @shipping_methods, Api::Admin::IdNameSerializer
|
||||
end
|
||||
|
||||
def admin_inject_producers
|
||||
admin_inject_json_ams_array "ofn.admin", "producers", @producers, Api::Admin::IdNameSerializer
|
||||
end
|
||||
|
||||
def admin_inject_json_ams(ngModule, name, data, serializer, opts = {})
|
||||
json = serializer.new(data).to_json
|
||||
render partial: "admin/json/injection_ams", locals: {ngModule: ngModule, name: name, json: json}
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
%div#new_product(data-hook)
|
||||
|
||||
|
||||
|
||||
=admin_inject_producers
|
||||
%div{ 'ng-app' => 'ofn.admin', 'ng-controller' => 'AdminProductEditCtrl', 'ng-init' => "initialise('#{@spree_api_key}');loading=true;" }
|
||||
%div{ 'ng-show' => '!spree_api_key_ok' }
|
||||
{{ api_error_msg }}
|
||||
|
||||
@@ -237,6 +237,10 @@ describe "AdminProductEditCtrl", ->
|
||||
|
||||
beforeEach ->
|
||||
module "ofn.admin"
|
||||
module ($provide)->
|
||||
$provide.value "producers", []
|
||||
null
|
||||
|
||||
beforeEach inject((_$controller_, _$timeout_, $rootScope, _$httpBackend_, _DirtyProducts_) ->
|
||||
$scope = $rootScope.$new()
|
||||
$ctrl = _$controller_
|
||||
@@ -250,11 +254,9 @@ describe "AdminProductEditCtrl", ->
|
||||
describe "loading data upon initialisation", ->
|
||||
it "gets a list of producers and then resets products with a list of data", ->
|
||||
$httpBackend.expectGET("/api/users/authorise_api?token=api_key").respond success: "Use of API Authorised"
|
||||
$httpBackend.expectGET("/api/enterprises/managed?template=bulk_index&q[is_primary_producer_eq]=true").respond "list of producers"
|
||||
spyOn($scope, "fetchProducts").andReturn "nothing"
|
||||
$scope.initialise "api_key"
|
||||
$httpBackend.flush()
|
||||
expect($scope.producers).toEqual "list of producers"
|
||||
expect($scope.fetchProducts.calls.length).toEqual 1
|
||||
expect($scope.spree_api_key_ok).toEqual true
|
||||
|
||||
|
||||
Reference in New Issue
Block a user