at the end of the day, it was inevitable

This commit is contained in:
Mo Elzubeir
2022-12-09 08:36:26 -06:00
commit 1218570914
1768 changed files with 887087 additions and 0 deletions
@@ -0,0 +1,44 @@
import GenericTable from './genericTable'
import * as api from '../../../../../api/notificationsApi'
class EmailFiltersTable extends GenericTable {
constructor () {
super(api)
}
getNamespace () {
return '[Email filters]'
}
getItems = (token, payload) => {
return this.api.getFilters(token, payload)
};
getDataFromResponse (response) {
return response['filters']
}
getTableState (state) {
return state.getIn(['appState', 'share', 'tables', 'emailFilters'])
}
getLoadTableRequestPayload (tableState) {
return {
...super.getLoadTableRequestPayload(tableState),
type: tableState.filterType
}
}
getInitialState () {
return {
...super.getInitialState(),
filterType: 'owner'
}
}
}
const instance = new EmailFiltersTable()
instance.init()
export default instance
@@ -0,0 +1,67 @@
import GenericTable from './genericTable'
import * as api from '../../../../../api/notificationsApi'
const SET_FILTER = 'Set filter'
const CLEAR_FILTER = 'Clear filter'
class EmailsTable extends GenericTable {
constructor () {
super(api)
this.updateRestrictionsAfterDelete = true
}
getNamespace () {
return '[Emails]'
}
getTableState (state) {
return state.getIn(['appState', 'share', 'tables', 'emails'])
}
getItems = (token, payload) => {
return this.api.getAllItems(token, payload)
}
getLoadTableRequestPayload (tableState) {
const payload = super.getLoadTableRequestPayload(tableState)
if (tableState.filter) {
payload.filterId = tableState.filter.id
payload.filterType = tableState.filter.type
}
delete payload.filter
return payload
}
defineActions () {
const setFilter = this.createAction(SET_FILTER)
const clearFilter = this.createAction(CLEAR_FILTER)
return {
...super.defineActions(),
setFilter,
clearFilter
}
}
getInitialState () {
return {
...super.getInitialState(),
filter: null
}
}
defineReducers () {
return {
...super.defineReducers(),
[SET_FILTER]: this.setReducer('filter'),
[CLEAR_FILTER]: this.resetReducer('filter', null)
}
}
}
const instance = new EmailsTable()
instance.init()
export default instance
@@ -0,0 +1,268 @@
import ReduxModule from '../../../abstract/reduxModule'
import { addAlert } from '../../../common/alerts'
import {tokenInject} from '../../../../utils/common'
import {getRestrictions} from '../../../common/auth'
const SELECT_TABLE_ROW = 'Select table row'
const SELECT_TABLE_ALL_ROWS = 'Select all rows'
const SET_TABLE_PARAMS = 'Set table parameters'
const LOAD_TABLE = 'Load table'
const CONFIRM_DELETE = 'Confirm delete'
const CANCEL_DELETE = 'Cancel delete'
const CHANGE_FILTERED = 'Change filtered'
const TOGGLE_FIELD = 'Toggle field'
const ASYNC_ACTION = 'Async action'
const DELETE_ITEMS = 'Delete items'
export default class GenericTableModule extends ReduxModule {
constructor (api) {
super()
this.api = api
this.updateRestrictionsAfterDelete = false
}
/*
state - full state
return - table state
*/
getTableState (state) {
//implement in subclasses
}
asyncToggleFieldAction (apiMethod, optionName) {
return (ids, optionValue) => {
return tokenInject((dispatch, getState, token) => {
const payload = {
ids,
[optionName]: optionValue
}
dispatch(this.asyncActionPending(true))
apiMethod(token, payload)
.then(() => {
dispatch(this.toggleField(ids, optionName, optionValue))
dispatch(this.asyncActionPending(false))
})
.catch((error) => {
dispatch(this.asyncActionPending(false))
dispatch(addAlert(error))
})
})
}
}
_fixForDeleteFromLastPage (dispatch, getState, ids) {
const tableState = this.getTableState(getState()).toJS()
const totalPages = Math.ceil(tableState.totalCount / tableState.limit)
if (tableState.page === totalPages && totalPages !== 1) { //we are on the last page
const itemsOnLastPage = tableState.totalCount % tableState.limit
const itemsToDelete = Object.keys(ids).length
if (itemsOnLastPage === itemsToDelete) { //and we are deleting everything
const newPage = tableState.page - 1
dispatch(this.setTableParams({page: newPage}))
}
}
}
deleteItems = (ids, {token, fulfilled, getState, dispatch}) => {
return this.api
.deleteItems(token, {ids})
.then(() => {
this._fixForDeleteFromLastPage(dispatch, getState, ids)
const payload = this._getLoadPayload(getState)
this.updateRestrictionsAfterDelete && dispatch(getRestrictions())
return this.api
.getItems(token, payload)
.then((response) => {
fulfilled(this.getDataFromResponse(response))
})
})
};
_getLoadPayload (getState) {
const state = getState()
const tableState = this.getTableState(state).toJS()
return this.getLoadTableRequestPayload(tableState)
}
getLoadTableRequestPayload (tableState) {
const payload = {
page: tableState.page,
limit: tableState.limit,
sortField: tableState.sortField,
sortDirection: tableState.sortDirection
}
if (tableState.filter) {
payload.filter = tableState.filter
}
return payload
}
getDataFromResponse (response) {
return response['notifications']
}
getItems = (token, payload) => {
return this.api.getItems(token, payload)
}
loadTable = (params, {dispatch, getState, token, fulfilled}) => {
if (params) {
dispatch(this.setTableParams(params))
}
const payload = this._getLoadPayload(getState)
return this.getItems(token, payload)
.then((response) => {
fulfilled(this.getDataFromResponse(response))
})
};
defineActions () {
const selectTableRow = this.createAction(SELECT_TABLE_ROW, (itemId) => itemId)
const selectTableAllRows = this.createAction(SELECT_TABLE_ALL_ROWS)
const confirmDelete = this.createAction(CONFIRM_DELETE)
const cancelDelete = this.createAction(CANCEL_DELETE)
const changeTableFiltered = this.createAction(CHANGE_FILTERED)
const setTableParams = this.createAction(SET_TABLE_PARAMS, (params) => params)
this.setTableParams = setTableParams
const loadTable = this.thunkAction(LOAD_TABLE, this.loadTable, true)
const deleteItems = this.thunkAction(DELETE_ITEMS, this.deleteItems, true)
this.asyncActionPending = this.createAction(`${ASYNC_ACTION} pending`, (value) => ({isPending: value}))
this.toggleField = this.createAction(TOGGLE_FIELD, (ids, fieldName, fieldValue) => ({ids, fieldName, fieldValue}))
const toggleActive = this.asyncToggleFieldAction(this.api.activateItems, 'active')
const togglePublish = this.asyncToggleFieldAction(this.api.publishItems, 'published')
return {
loadTable,
selectTableRow,
selectTableAllRows,
setTableParams,
confirmDelete,
cancelDelete,
changeTableFiltered,
toggleActive,
togglePublish,
deleteItems
}
}
getInitialState () {
return {
page: 1,
limit: 10,
sortField: 'name',
sortDirection: 'asc',
data: [],
count: 0,
totalCount: 0,
isLoading: false,
isAllSelected: false,
selectedIds: [],
idsToDelete: {},
isDeletePopupVisible: false
}
}
/** REDUCERS **/
onSelectTableRow (state, {payload: itemId}) {
let selectedIds = state.get('selectedIds')
const isSelected = selectedIds.includes(itemId)
if (isSelected) {
selectedIds = selectedIds.filter(id => id !== itemId)
}
else {
selectedIds = selectedIds.push(itemId)
}
return state.merge({
'selectedIds': selectedIds,
'isAllSelected': false
})
}
onSelectAllTableRows (state) {
const isAllSelected = state.get('isAllSelected')
if (isAllSelected) { //then deselect all
return state.merge({
isAllSelected: false,
selectedIds: []
})
}
else { //select all currently loaded data
const selectedIds = state.get('data').map(item => item.get('id'))
return state.merge({
isAllSelected: true,
selectedIds
})
}
}
onLoadFulfilled (state, {payload: response}) {
return state.merge({
data: response.data,
count: response.count,
totalCount: response.totalCount
})
}
onDeleteItemsFulfilled (state, {payload: response}) {
return state.merge({
data: response.data,
count: response.count,
totalCount: response.totalCount,
selectedIds: []
})
};
onConfirmDelete (state, {payload: ids}) {
return state.merge({
isDeletePopupVisible: true,
idsToDelete: ids
})
}
onCancelDelete (state) {
return state.merge({
isDeletePopupVisible: false,
idsToDelete: {}
})
}
onAsyncActionPending (state, {payload: {isPending}}) {
return state.merge({
isLoading: isPending,
isDeletePopupVisible: false //dirty hack, need to think 'bout it
})
}
onToggleField (state, {payload: {ids, fieldName, fieldValue}}) {
let tableData = state.get('data')
const newValues = {[fieldName]: fieldValue}
tableData = tableData.map((item) => (ids.includes(item.get('id'))) ? item.merge(newValues) : item)
return state.set('data', tableData)
}
defineReducers () {
return {
[SELECT_TABLE_ROW]: this.onSelectTableRow,
[SELECT_TABLE_ALL_ROWS]: this.onSelectAllTableRows,
[SET_TABLE_PARAMS]: this.mergeReducer(),
[`${LOAD_TABLE} pending`]: this.thunkPendingReducer('isLoading'),
[`${LOAD_TABLE} fulfilled`]: this.onLoadFulfilled,
[`${ASYNC_ACTION} pending`]: this.onAsyncActionPending,
[`${DELETE_ITEMS} pending`]: this.onAsyncActionPending,
[`${DELETE_ITEMS} fulfilled`]: this.onDeleteItemsFulfilled,
[TOGGLE_FIELD]: this.onToggleField,
[CONFIRM_DELETE]: this.onConfirmDelete,
[CANCEL_DELETE]: this.onCancelDelete,
[CHANGE_FILTERED]: this.setReducer('filtered')
}
}
}
@@ -0,0 +1,34 @@
import GenericTable from './genericTable'
import * as api from '../../../../../api/groupsApi'
class GroupsTable extends GenericTable {
constructor () {
super(api)
}
getNamespace () {
return '[Groups table]'
}
getInitialState () {
const state = super.getInitialState()
return {
...state,
filter: ''
}
}
getDataFromResponse (response) {
return response['groups']
}
getTableState (state) {
return state.getIn(['appState', 'share', 'tables', 'groups'])
}
}
const instance = new GroupsTable()
instance.init()
export default instance
@@ -0,0 +1,30 @@
import GenericTable from './genericTable'
import * as api from '../../../../../api/notificationsApi'
class MyEmailsTable extends GenericTable {
constructor () {
super(api)
this.updateRestrictionsAfterDelete = true
}
getNamespace () {
return '[My Emails]'
}
getTableState (state) {
return state.getIn(['appState', 'share', 'tables', 'myEmails'])
}
getLoadTableRequestPayload (tableState) {
return {
...super.getLoadTableRequestPayload(tableState),
onlyPublished: false
}
}
}
const instance = new MyEmailsTable()
instance.init()
export default instance
@@ -0,0 +1,38 @@
import GenericTableModule from './genericTable'
import * as api from '../../../../../api/notificationsApi'
class PublishedEmailsTable extends GenericTableModule {
constructor () {
super(api)
this.updateRestrictionsAfterDelete = true
}
getNamespace () {
return '[Published Emails]'
}
getTableState (state) {
return state.getIn(['appState', 'share', 'tables', 'publishedEmails'])
}
getLoadTableRequestPayload (tableState) {
return {
...super.getLoadTableRequestPayload(tableState),
onlyPublished: true
}
}
defineActions () {
const toggleSubscribe = this.asyncToggleFieldAction(this.api.subscribeItems, 'subscribed')
return {
...super.defineActions(),
toggleSubscribe
}
}
}
const instance = new PublishedEmailsTable()
instance.init()
export default instance
@@ -0,0 +1,34 @@
import * as api from '../../../../../../api/receiversApi'
import GenericTableModule from '../genericTable'
class EmailHistory extends GenericTableModule {
constructor () {
super(api)
}
getNamespace () {
return '[Email history]'
}
getTableState (state) {
return state.getIn(['appState', 'share', 'tables', 'receiverForm', 'emailHistory'])
}
loadTable = (params, receiver, {dispatch, getState, token, fulfilled}) => {
if (params) {
dispatch(this.setTableParams(params))
}
const payload = this._getLoadPayload(getState)
return this.api
.getEmailHistory(token, payload, receiver.id)
.then((response) => {
fulfilled(response)
})
};
}
const instance = new EmailHistory()
instance.init()
export default instance
@@ -0,0 +1,84 @@
import {fromJS} from 'immutable'
import GenericTableModule from '../genericTable'
const TOGGLE_SUBSCRIBED = 'Toggle subscribed'
const TOGGLE_ENROLLED = 'Toggle enrolled'
export default class ReceiverFormTable extends GenericTableModule {
getDataFromResponse (response) {
//implement in subclasses
}
getLoadTableRequestPayload (tableState, receiver) {
let payload = super.getLoadTableRequestPayload(tableState)
if (tableState.filter) {
payload.filter = tableState.filter
}
const statusFilter = tableState.statusFilter
if (statusFilter) {
payload.statusFilter = tableState.statusFilter
}
return payload
}
_getLoadPayload (getState, receiver) {
const tableState = this.getTableState(getState()).toJS()
return this.getLoadTableRequestPayload(tableState, receiver)
}
loadTable = (params, receiver, {dispatch, getState, token, fulfilled}) => {
if (params) {
dispatch(this.setTableParams(params))
}
const payload = this._getLoadPayload(getState, receiver) // <-- difference from genericTable
return this.api
.getItems(token, payload)
.then((response) => {
fulfilled(this.getDataFromResponse(response, receiver))
})
};
addDataColumn (data, fieldName, ids = []) {
data.forEach((item) => {
item[fieldName] = ids.includes(item.id)
})
};
toggleDataField (actionName, fieldName) {
return this.createHandler(
actionName,
(itemId, turnOn) => ({itemId, turnOn}),
(state, {payload: {itemId, turnOn}}) => {
const tableData = state.get('data').toJS()
tableData.forEach((item) => {
if (item.id === itemId) {
item[fieldName] = turnOn
}
})
return state.set('data', fromJS(tableData))
}
)
}
defineActions () {
const actions = super.defineActions()
const toggleSubscribed = this.toggleDataField(TOGGLE_SUBSCRIBED, 'subscribed')
const toggleEnrolled = this.toggleDataField(TOGGLE_ENROLLED, 'enrolled')
return {
...actions,
toggleSubscribed,
toggleEnrolled
}
}
getInitialState () {
return {
...super.getInitialState(),
filter: '',
statusFilter: 'all'
}
}
}
@@ -0,0 +1,38 @@
import ReceiverFormTable from './receiverFormTable'
import * as api from '../../../../../../api/groupsApi'
///api/v1/recipients/groups with recipientId
class ReceiverGroupsTable extends ReceiverFormTable {
constructor () {
super(api)
}
getNamespace () {
return '[Recipient form groups table]'
}
getTableState (state) {
return state.getIn(['appState', 'share', 'tables', 'receiverForm', 'groups'])
}
getLoadTableRequestPayload (tableState, receiver) {
let payload = super.getLoadTableRequestPayload(tableState)
if (receiver) {
payload.recipientId = receiver.id
}
return payload
}
getDataFromResponse (response, receiver) {
const data = response.groups
this.addDataColumn(data.data, 'enrolled', receiver.groups)
return data
}
}
const instance = new ReceiverGroupsTable()
instance.init()
export default instance
@@ -0,0 +1,37 @@
import ReceiverFormTable from './receiverFormTable'
import * as api from '../../../../../../api/recipientsApi'
///api/v1/recipients with groupId
class ReceiverRecipientsTable extends ReceiverFormTable {
constructor () {
super(api)
}
getNamespace () {
return '[Group form recipients table]'
}
getTableState (state) {
return state.getIn(['appState', 'share', 'tables', 'receiverForm', 'recipients'])
}
getLoadTableRequestPayload (tableState, receiver) {
let payload = super.getLoadTableRequestPayload(tableState)
if (receiver) {
payload.groupId = receiver.id
}
return payload
}
getDataFromResponse (response, receiver) {
const data = response['recipients']
this.addDataColumn(data.data, 'enrolled', receiver.recipients)
return data
}
}
const instance = new ReceiverRecipientsTable()
instance.init()
export default instance
@@ -0,0 +1,36 @@
import ReceiverFormTable from './receiverFormTable'
import * as api from '../../../../../../api/notificationsApi'
class ReceiverSubscriptionsTable extends ReceiverFormTable {
constructor () {
super(api)
}
getNamespace () {
return '[Receiver form subscriptions table]'
}
getTableState (state) {
return state.getIn(['appState', 'share', 'tables', 'receiverForm', 'subscriptions'])
}
getLoadTableRequestPayload (tableState, receiver) {
let payload = super.getLoadTableRequestPayload(tableState)
if (receiver) {
payload.entityId = receiver.id
}
return payload
}
getDataFromResponse (response, receiver) {
const data = response.notifications
this.addDataColumn(data.data, 'subscribed', receiver.subscriptions)
return data
}
}
const instance = new ReceiverSubscriptionsTable()
instance.init()
export default instance
@@ -0,0 +1,34 @@
import GenericTable from './genericTable'
import * as api from '../../../../../api/recipientsApi'
class RecipientsTable extends GenericTable {
constructor () {
super(api)
}
getNamespace () {
return '[Recipients table]'
}
getInitialState () {
const state = super.getInitialState()
return {
...state,
filter: ''
}
}
getTableState (state) {
return state.getIn(['appState', 'share', 'tables', 'recipients'])
}
getDataFromResponse (response) {
return response['recipients']
}
}
const instance = new RecipientsTable()
instance.init()
export default instance