Skip to content

Commit

Permalink
modal actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ross Perry authored and Ross Perry committed Dec 20, 2024
1 parent 48d1019 commit 6c4b6f5
Show file tree
Hide file tree
Showing 7 changed files with 297 additions and 275 deletions.
312 changes: 171 additions & 141 deletions seed/static/seed/js/controllers/inventory_create_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,146 +3,176 @@
* See also https://github.com/SEED-platform/seed/blob/main/LICENSE.md
*/
angular.module('SEED.controller.inventory_create', []).controller('inventory_create_controller', [
'$scope',
'$window',
'$stateParams',
'ah_service',
'inventory_service',
'access_level_tree',
'all_columns',
'cycles',
'profiles',
// eslint-disable-next-line func-names
function (
$scope,
$window,
$stateParams,
ah_service,
inventory_service,
access_level_tree,
all_columns,
cycles,
profiles,
) {
$scope.data = { state: { extra_data: {} } };
$scope.inventory_type = $stateParams.inventory_type;
$scope.inventory_types = ['Property', 'TaxLot'];
const table_name = $scope.inventory_type === 'taxlots' ? 'TaxLotState' : 'PropertyState';
$scope.cycles = cycles.cycles;
$scope.profiles = profiles;
$scope.profile = [];
$scope.columns = all_columns;

$scope.matching_columns = [];
$scope.extra_columns = [];
$scope.canonical_columns = [];
$scope.columns.forEach((c) => {
if (c.table_name == table_name) {
if (c.is_matching_criteria) $scope.matching_columns.push(c);
if (c.is_extra_data) $scope.extra_columns.push(c);
if (!c.is_extra_data && !c.derived_column) $scope.canonical_columns.push(c);
}
'$scope',
'$state',
'$stateParams',
'ah_service',
'inventory_service',
'Notification',
'simple_modal_service',
'spinner_utility',
'access_level_tree',
'all_columns',
'cycles',
'profiles',
// eslint-disable-next-line func-names
function (
$scope,
$state,
$stateParams,
ah_service,
inventory_service,
Notification,
simple_modal_service,
spinner_utility,
access_level_tree,
all_columns,
cycles,
profiles
) {
$scope.data = { state: { extra_data: {} } };
$scope.inventory_type = $stateParams.inventory_type;
$scope.inventory_types = ['Property', 'TaxLot'];
const table_name = $scope.inventory_type === 'taxlots' ? 'TaxLotState' : 'PropertyState';
$scope.cycles = cycles.cycles;
$scope.profiles = profiles;
$scope.profile = [];
$scope.columns = all_columns;

$scope.matching_columns = [];
$scope.extra_columns = [];
$scope.canonical_columns = [];
$scope.columns.forEach((c) => {
if (c.table_name === table_name) {
if (c.is_matching_criteria) $scope.matching_columns.push(c);
if (c.is_extra_data) $scope.extra_columns.push(c);
if (!c.is_extra_data && !c.derived_column) $scope.canonical_columns.push(c);
}
});
// create a copy, not a reference. from_column contents is a list of columns dicts
$scope.form_columns = [...$scope.matching_columns];
// form_values allows value persistance
$scope.form_values = [];

$scope.$watch('data', () => {
$scope.valid = $scope.data.cycle && $scope.data.access_level_instance && !_.isEqual($scope.data.state, { extra_data: {} });
}, true);

// ACCESS LEVEL TREE
$scope.access_level_tree = access_level_tree.access_level_tree;
$scope.level_names = access_level_tree.access_level_names.map((level, i) => ({
index: i,
name: level
}));
const access_level_instances_by_depth = ah_service.calculate_access_level_instances_by_depth($scope.access_level_tree);
$scope.change_selected_level_index = () => {
const new_level_instance_depth = parseInt($scope.data.level_name_index, 10) + 1;
$scope.potential_level_instances = access_level_instances_by_depth[new_level_instance_depth];
};
$scope.data.level_name_index = $scope.level_names.at(-1).index;
$scope.change_selected_level_index();
$scope.data.access_level_instance = $scope.potential_level_instances.at(0).id;

// COLUMN LIST PROFILES
$scope.set_columns = (type) => {
remove_empty_last_column();
switch (type) {
case 'canonical':
$scope.form_columns = Array.from(new Set([...$scope.form_columns, ...$scope.canonical_columns]));
break;
case 'extra':
$scope.form_columns = Array.from(new Set([...$scope.form_columns, ...$scope.extra_columns]));
break;
default:
$scope.form_columns = [...$scope.matching_columns];
$scope.form_columns = $scope.form_columns.map((c) => ({ ...c, value: null }));
$scope.form_values = [];
}
};

// FORM LOGIC
$scope.remove_column = (column, index) => {
$scope.form_columns.splice(index, 1);
$scope.form_values[index] = null;
set_column_value(column, null);
};

$scope.add_column = () => $scope.form_columns.push({ displayName: '', table_name });

const remove_empty_last_column = () => {
if (!_.isEmpty($scope.form_columns) && $scope.form_columns.at(-1).displayName === '') {
$scope.form_columns.pop();
}
};

$scope.change_profile = () => {
const profile_column_names = $scope.profile.columns.map((p) => p.column_name);
$scope.form_columns = $scope.columns.filter((c) => profile_column_names.includes(c.column_name));
};

$scope.select_column = (column, index) => {
column.value = $scope.form_values.at(index);
$scope.form_columns[index] = column;
};

$scope.change_column = (displayName, index) => {
const defaults = {
table_name,
is_extra_data: true,
is_matching_criteria: false,
data_type: 'string'
};
let column = $scope.columns.find((c) => c.displayName === displayName) || { displayName };
column = { ...defaults, ...column };

column.value = $scope.form_values.at(index);
$scope.form_columns[index] = column;
};

$scope.change_value = (column, index) => {
$scope.form_values[index] = column.value;
set_column_value(column, column.value);
};

const set_column_value = (column, value) => {
const column_name = column.column_name || column.displayName;
if (!column_name) return;
const target = column.is_extra_data ? $scope.data.state.extra_data : $scope.data.state;
target[column_name] = value;
};

$scope.save_inventory = () => {
const type_name = $scope.inventory_type === 'taxlots' ? 'Tax Lot' : 'Property';
const cycle_name = $scope.cycles.find((c) => c.id === $scope.data.cycle).name;
const ali_name = $scope.access_level_tree.find((ali) => ali.id === $scope.data.access_level_instance).name;
const modalOptions = {
type: 'default',
okButtonText: 'Confirm',
headerText: `Create new ${type_name}`,
bodyText: `Create ${ali_name} ${type_name} in Cycle ${cycle_name}?`
};
const successOptions = {
type: 'default',
okButtonText: `View ${type_name}`,
headerText: 'Success',
bodyText: `Successfully created ${type_name}`
};
simple_modal_service.showModal(modalOptions).then(() => {
spinner_utility.show();
inventory_service.create_inventory($scope.data, $scope.inventory_type).then((response) => {
Notification.success(`Successfully created ${type_name}`);
spinner_utility.hide();
return response.data.view_id;
}).then((view_id) => {
simple_modal_service.showModal(successOptions).then(() => {
window.location.href = `/app/#/${$scope.inventory_type}/${view_id}`;
}).catch(() => {
$state.reload();
});
}).catch(() => {
Notification.error(`Failed to create ${type_name}`);
});
// create a copy, not a reference. from_column contents is a list of columns dicts
$scope.form_columns =[...$scope.matching_columns];
// form_values allows value persistance
$scope.form_values = [];

$scope.$watch('data', () => {
console.log('watch')
$scope.valid = $scope.data.cycle && $scope.data.access_level_instance && !_.isEqual($scope.data.state, { extra_data: {} });
}, true)


// ACCESS LEVEL TREE
$scope.access_level_tree = access_level_tree.access_level_tree;
$scope.level_names = access_level_tree.access_level_names.map((level, i) => ({
index: i,
name: level
}));
const access_level_instances_by_depth = ah_service.calculate_access_level_instances_by_depth($scope.access_level_tree);
$scope.change_selected_level_index = () => {
const new_level_instance_depth = parseInt($scope.data.level_name_index, 10) + 1;
$scope.potential_level_instances = access_level_instances_by_depth[new_level_instance_depth];
};
$scope.data.level_name_index = $scope.level_names.at(-1).index;
$scope.change_selected_level_index();
$scope.data.access_level_instance = $scope.potential_level_instances.at(0).id;

// COLUMN LIST PROFILES
$scope.set_columns = (type) => {
remove_empty_last_column();
switch (type) {
case 'canonical':
$scope.form_columns = Array.from(new Set([...$scope.form_columns, ...$scope.canonical_columns]));
break;
case 'extra':
$scope.form_columns = Array.from(new Set([...$scope.form_columns, ...$scope.extra_columns]));
break;
default:
$scope.form_columns = [...$scope.matching_columns];
$scope.form_columns = $scope.form_columns.map(c => ({...c, value: null}));
$scope.form_values = [];
}
}

// FORM LOGIC
$scope.remove_column = (column, index) => {
$scope.form_columns.splice(index, 1)
$scope.form_values[index] = null;
set_column_value(column, null);
};

$scope.add_column = () => $scope.form_columns.push({ displayName: '', table_name: table_name });

const remove_empty_last_column = () => {
if (!_.isEmpty($scope.form_columns) && $scope.form_columns.at(-1).displayName === '') {
$scope.form_columns.pop();
}
}

$scope.change_profile = () => {
const profile_column_names = $scope.profile.columns.map(p => p.column_name);
$scope.form_columns = $scope.columns.filter(c => profile_column_names.includes(c.column_name))
}

$scope.select_column = (column, index) => {
column.value = $scope.form_values.at(index);
$scope.form_columns[index] = column;
}

$scope.change_column = (displayName, index) => {
const defaults = {
table_name: table_name,
is_extra_data: true,
is_matching_criteria: false,
data_type: 'string',
}
let column = $scope.columns.find(c => c.displayName === displayName) || {displayName: displayName};
column = {...defaults, ...column};

column.value = $scope.form_values.at(index);
$scope.form_columns[index] = column;
};

$scope.change_value = (column, index) => {
$scope.form_values[index] = column.value;
set_column_value(column, column.value)
}

const set_column_value = (column, value) => {
const column_name = column.column_name || column.displayName;
if (!column_name) return
const target = column.is_extra_data ? $scope.data.state.extra_data : $scope.data.state;
target[column_name] = value;
}

$scope.save_inventory = () => {
console.log($scope.data.state)
inventory_service.create_inventory($scope.data, $scope.inventory_type).then((data) => {
console.log('>>>', data)
})
}

}
});
};
}
]);
2 changes: 1 addition & 1 deletion seed/static/seed/js/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -2275,7 +2275,7 @@
const inventory_type = $stateParams.inventory_type === 'properties' ? 'Property' : 'Tax Lot';
return inventory_service.get_column_list_profiles('List View Profile', inventory_type);
}
],
]
}
})
.state({
Expand Down
12 changes: 5 additions & 7 deletions seed/static/seed/js/services/inventory_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -1294,13 +1294,11 @@ angular.module('SEED.service.inventory', []).factory('inventory_service', [
taxlot_view_ids
}).then((response) => response.data);

inventory_service.create_inventory = (data, inventory_type) => {
return $http.post(`/api/v3/${inventory_type}/form_create/`, data, {
params: {
organization_id: user_service.get_organization().id
}
});
}
inventory_service.create_inventory = (data, inventory_type) => $http.post(`/api/v3/${inventory_type}/form_create/`, data, {
params: {
organization_id: user_service.get_organization().id
}
});

return inventory_service;
}
Expand Down
Loading

0 comments on commit 6c4b6f5

Please sign in to comment.