Skip to content

Commit

Permalink
feat: add delete project unit
Browse files Browse the repository at this point in the history
  • Loading branch information
danielstefanequilobe committed Feb 4, 2022
1 parent ff123ca commit e067563
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 26 deletions.
74 changes: 50 additions & 24 deletions src/components/blocks/APIDataTable.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import _ from 'lodash';
import React, { useState, useEffect } from 'react';
import { useSelector } from 'react-redux';
import { useSelector, useDispatch } from 'react-redux';
import { useIntl } from 'react-intl';
import styled, { withTheme, css } from 'styled-components';

import { TableCellHeaderText, TableCellText } from '../typography';
import { convertPascalCaseToSentenceCase } from '../../utils/stringUtils';
import { TableDrawer, APIPagination, Message } from '.';
import { EllipsisMenuIcon, BasicMenu } from '..';
import { BasicMenu } from '..';
import { useWindowSize } from '../hooks/useWindowSize';
import { EditUnitsForm, EditProjectsForm, SplitUnitForm } from '..';

import {
EditUnitsForm,
EditProjectsForm,
SplitUnitForm,
} from '..';
deleteProject,
deleteUnit,
} from '../../store/actions/climateWarehouseActions';

const Table = styled('table')`
box-sizing: border-box;
Expand Down Expand Up @@ -115,12 +116,13 @@ const APIDataTable = withTheme(({ headings, data, actions }) => {
const [getRecord, setRecord] = useState(null);
const [editRecord, setEditRecord] = useState(null);
const [unitToBeSplit, setUnitToBeSplit] = useState(null);
const { theme, notification} = useSelector(state => state.app);
const { theme, notification } = useSelector(state => state.app);
const climateWarehouseStore = useSelector(state => state.climateWarehouse);
const ref = React.useRef(null);
const [height, setHeight] = React.useState(0);
const windowSize = useWindowSize();
const intl = useIntl();
const dispatch = useDispatch();

useEffect(() => {
setHeight(windowSize.height - ref.current.getBoundingClientRect().top - 20);
Expand All @@ -138,7 +140,8 @@ const APIDataTable = withTheme(({ headings, data, actions }) => {
start={index === 0}
end={!actions && index === headings.length - 1}
selectedTheme={theme}
key={index}>
key={index}
>
<TableCellHeaderText>
{heading === 'orgUid' && 'Organization'}
{heading !== 'orgUid' &&
Expand All @@ -151,7 +154,8 @@ const APIDataTable = withTheme(({ headings, data, actions }) => {
start={false}
end={true}
selectedTheme={theme}
key={'action'}></Th>
key={'action'}
></Th>
)}
</tr>
</THead>
Expand All @@ -164,15 +168,17 @@ const APIDataTable = withTheme(({ headings, data, actions }) => {
onClick={() => setRecord(record)}
selectedTheme={theme}
columnId={key}
key={index}>
key={index}
>
<TableCellText
tooltip={
record[key] &&
`${_.get(
climateWarehouseStore,
`organizations[${record[key]}].name`,
)}: ${record[key].toString()}`
}>
}
>
{key === 'orgUid' &&
climateWarehouseStore.organizations[
record[key]
Expand All @@ -193,9 +199,7 @@ const APIDataTable = withTheme(({ headings, data, actions }) => {
</Td>
))}
{actions === 'Units' && (
<Td
style={{ cursor: 'pointer' }}
selectedTheme={theme}>
<Td style={{ cursor: 'pointer' }} selectedTheme={theme}>
<BasicMenu
options={[
{
Expand All @@ -212,18 +216,40 @@ const APIDataTable = withTheme(({ headings, data, actions }) => {
}),
action: () => setUnitToBeSplit(record),
},
{
label: intl.formatMessage({
id: 'delete-unit',
}),
action: () =>
dispatch(deleteUnit(record.warehouseUnitId)),
},
]}
/>
</Td>
)}
{actions === 'Projects' && (
<Td
style={{ cursor: 'pointer' }}
onClick={() => {
setEditRecord(record);
}}
selectedTheme={theme}>
<EllipsisMenuIcon />
<Td style={{ cursor: 'pointer' }} selectedTheme={theme}>
<BasicMenu
options={[
{
label: intl.formatMessage({
id: 'edit-project',
}),
action: () => {
setEditRecord(record);
},
},
{
label: intl.formatMessage({
id: 'delete-project',
}),
action: () =>
dispatch(
deleteProject(record.warehouseProjectId),
),
},
]}
/>
</Td>
)}
</Tr>
Expand Down Expand Up @@ -262,9 +288,9 @@ const APIDataTable = withTheme(({ headings, data, actions }) => {
record={unitToBeSplit}
/>
)}
{
notification && <Message type={notification.type} id={notification.id} />
}
{notification && (
<Message type={notification.type} id={notification.id} />
)}
</>
);
});
Expand Down
90 changes: 90 additions & 0 deletions src/store/actions/climateWarehouseActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,96 @@ export const deleteStagingData = uuid => {
};
};

export const deleteUnit = warehouseUnitId => {
return async dispatch => {
try {
dispatch(activateProgressIndicator);

const url = `${constants.API_HOST}/units`;
const payload = {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ warehouseUnitId }),
};

const response = await fetch(url, payload);

if (response.ok) {
dispatch(
setNotificationMessage(
NotificationMessageTypeEnum.success,
'unit-deleted',
),
);
dispatch(getStagingData({ useMockedResponse: false }));
} else {
dispatch(
setNotificationMessage(
NotificationMessageTypeEnum.error,
'unit-could-not-be-deleted',
),
);
}
} catch {
dispatch(
setNotificationMessage(
NotificationMessageTypeEnum.error,
'unit-could-not-be-deleted',
),
);
} finally {
dispatch(deactivateProgressIndicator);
}
};
};

export const deleteProject = warehouseProjectId => {
return async dispatch => {
try {
dispatch(activateProgressIndicator);

const url = `${constants.API_HOST}/projects`;
const payload = {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ warehouseProjectId }),
};

const response = await fetch(url, payload);

if (response.ok) {
dispatch(
setNotificationMessage(
NotificationMessageTypeEnum.success,
'project-deleted',
),
);
dispatch(getStagingData({ useMockedResponse: false }));
} else {
dispatch(
setNotificationMessage(
NotificationMessageTypeEnum.error,
'project-could-not-be-deleted',
),
);
}
} catch {
dispatch(
setNotificationMessage(
NotificationMessageTypeEnum.error,
'project-could-not-be-deleted',
),
);
} finally {
dispatch(deactivateProgressIndicator);
}
};
};

export const postNewProject = data => {
return async dispatch => {
try {
Expand Down
4 changes: 4 additions & 0 deletions src/translations/tokens/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@
"add-project": "Add Project",
"update-project": "Update Project",
"delete-unit": "Delete Unit",
"unit-deleted": "The Unit was successfully deleted.",
"unit-could-not-be-deleted": "The Unit could not be deleted.",
"project-deleted": "The Project was successfully deleted.",
"project-could-not-be-deleted": "The Project could not be deleted.",
"add-unit": "Add Unit",
"update-unit": "Update Unit",
"split-unit": "Split Unit",
Expand Down
6 changes: 5 additions & 1 deletion src/translations/tokens/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,9 @@
"labels": "Etiquetas",
"issuances": "Emisiones",
"network-error": "Error de red",
"there-is-a-connection-error": "Hay un error de conexión. El Almacén Climático es inaccesible."
"there-is-a-connection-error": "Hay un error de conexión. El Almacén Climático es inaccesible.",
"unit-deleted": "La unidad se eliminó con éxito.",
"unit-could-not-be-deleted": "No se pudo eliminar la Unidad.",
"project-deleted": "El proyecto se eliminó con éxito.",
"project-could-not-be-deleted": "No se pudo eliminar el Proyecto."
}
6 changes: 5 additions & 1 deletion src/translations/tokens/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,9 @@
"labels": "ラベル",
"issuances": "発行",
"network-error": "ネットワークエラー",
"there-is-a-connection-error": "接続エラーが発生しました。 ClimateWarehouseにアクセスできません。"
"there-is-a-connection-error": "接続エラーが発生しました。 ClimateWarehouseにアクセスできません。",
"unit-deleted": "ユニットは正常に削除されました。",
"unit-could-not-be-deleted": "ユニットを削除できませんでした。",
"project-deleted": "プロジェクトは正常に削除されました。",
"project-could-not-be-deleted": "プロジェクトを削除できませんでした。"
}

0 comments on commit e067563

Please sign in to comment.