Skip to content

Commit

Permalink
Implement shared snapshot view link #1703
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalbg committed Nov 28, 2024
1 parent 5077c8f commit 2956841
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 25 deletions.
9 changes: 9 additions & 0 deletions client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import Logout from "./components/Authorization/Logout";
import { getConfigs } from "./helpers/Config";

const calculationPath = "/calculation/:page/:projectId?/*";
const sharedProjectPath = "/projects/:projectId?";

const App = () => {
const contentContainerRef = useRef();
Expand Down Expand Up @@ -68,6 +69,14 @@ const App = () => {
</RequireAuth>
}
/>
<Route
path={sharedProjectPath}
element={
<TdmCalculationContainer
contentContainerRef={contentContainerRef}
/>
}
/>
<Route
path={calculationPath}
element={
Expand Down
39 changes: 27 additions & 12 deletions client/src/components/ProjectWizard/TdmCalculationContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export function TdmCalculationContainer({ contentContainerRef }) {
const [rules, setRules] = useState([]);

const [project, setProject] = useState({});
const [shareView, setShareView] = useState(false);

const toast = useToast();

Expand All @@ -76,19 +77,32 @@ export function TdmCalculationContainer({ contentContainerRef }) {
let projectResponse = null;
let inputs = {};
if (Number(projectId) > 0 && account?.id) {
projectResponse = await projectService.getById(projectId);

// setLoginId(projectResponse.data.loginId);
// setDateModified(formatDatetime(projectResponse.data.dateModified));
// setDateSnapshotted(
// formatDatetime(projectResponse.data?.dateSnapshotted)
// );
// setDateSubmitted(formatDatetime(projectResponse.data?.dateSubmitted));
setProject(projectResponse.data);
if (location.pathname.split("/")[1] == "projects") {
projectResponse = await projectService.getByIdWithEmail(projectId);

inputs = JSON.parse(projectResponse.data.formInputs);
setStrategiesInitialized(true);
if (projectResponse) {
setShareView(true);
} else {
navigate("/unauthorized");
}
} else {
projectResponse = await projectService.getById(projectId);

// setLoginId(projectResponse.data.loginId);
// setDateModified(formatDatetime(projectResponse.data.dateModified));
// setDateSnapshotted(
// formatDatetime(projectResponse.data?.dateSnapshotted)
// );
// setDateSubmitted(formatDatetime(projectResponse.data?.dateSubmitted));
setShareView(false);
}
if (projectResponse) {
setProject(projectResponse);
inputs = JSON.parse(projectResponse.data.formInputs);
setStrategiesInitialized(true);
}
} else {
setShareView(false);
setStrategiesInitialized(false);
}
engine.run(inputs, resultRuleCodes);
Expand All @@ -104,7 +118,7 @@ export function TdmCalculationContainer({ contentContainerRef }) {
// const redirect = account.id ? "/projects" : "/login";
// navigate(redirect);
}
}, [engine, projectId, account, setRules, setProject]);
}, [engine, projectId, account, location, navigate, setRules, setProject]);

// Initialize the engine with saved project data, as appropriate.
// Should run only when projectId changes.
Expand Down Expand Up @@ -543,6 +557,7 @@ export function TdmCalculationContainer({ contentContainerRef }) {
inapplicableStrategiesModal={inapplicableStrategiesModal}
closeStrategiesModal={closeStrategiesModal}
project={project}
shareView={shareView}
/>
);
}
Expand Down
26 changes: 16 additions & 10 deletions client/src/components/ProjectWizard/TdmCalculationWizard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,16 @@ const TdmCalculationWizard = props => {
contentContainerRef,
inapplicableStrategiesModal,
closeStrategiesModal,
project
project,
shareView
} = props;
const classes = useStyles();
const context = useContext(ToastContext);
const userContext = useContext(UserContext);
const account = userContext ? userContext.account : null;
const params = useParams();
const navigate = useNavigate();
const page = Number(params.page || 1);
const page = Number(shareView ? 5 : params.page || 1);
const projectId = Number(params.projectId);
const { pathname } = useLocation();
const [ainInputError, setAINInputError] = useState("");
Expand Down Expand Up @@ -104,10 +105,13 @@ const TdmCalculationWizard = props => {
!projectId)
);
};

return formIsDirty && !isSameProject(currentLocation, nextLocation);
return (
!shareView &&
formIsDirty &&
!isSameProject(currentLocation, nextLocation)
);
},
[formIsDirty, projectId]
[formIsDirty, projectId, shareView]
);
const blocker = useBlocker(shouldBlock);

Expand Down Expand Up @@ -190,7 +194,7 @@ const TdmCalculationWizard = props => {
const setDisplaySaveButton = () => {
const loggedIn = !!account && !!account.id;
const setDisplayed = loggedIn;
return setDisplayed;
return !shareView && setDisplayed;
};

const setDisplayPrintButton = () => {
Expand All @@ -201,7 +205,7 @@ const TdmCalculationWizard = props => {
};

const setDisplaySubmitButton = () => {
if (page === 5) {
if (page === 5 && !shareView) {
return true;
}
return false;
Expand Down Expand Up @@ -298,8 +302,8 @@ const TdmCalculationWizard = props => {
rules={rules}
account={account}
projectId={projectId}
loginId={loginId}
onSave={onSave}
loginId={!shareView ? loginId : account?.id}
onSave={!shareView ? onSave : null}
dateModified={project.dateModified}
/>
);
Expand Down Expand Up @@ -338,6 +342,7 @@ const TdmCalculationWizard = props => {
setDisplaySubmitButton={setDisplaySubmitButton}
onSave={onSave}
project={project}
shareView={shareView}
/>
</ContentContainer>
</div>
Expand Down Expand Up @@ -387,7 +392,8 @@ TdmCalculationWizard.propTypes = {
// dateSubmitted: PropTypes.string,
inapplicableStrategiesModal: PropTypes.bool,
closeStrategiesModal: PropTypes.func,
project: PropTypes.any
project: PropTypes.any,
shareView: PropTypes.bool
};

export default TdmCalculationWizard;
8 changes: 5 additions & 3 deletions client/src/components/ProjectWizard/WizardFooter.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ const WizardFooter = ({
setDisplayPrintButton,
setDisplaySubmitButton,
onSave,
project
project,
shareView
}) => {
const classes = useStyles();
const componentRef = useRef();
Expand All @@ -76,7 +77,7 @@ const WizardFooter = ({
navDirection="previous"
color="colorPrimary"
isVisible={page !== 1}
isDisabled={Number(page) === 1}
isDisabled={shareView || Number(page) === 1}
onClick={() => {
onPageChange(Number(page) - 1);
}}
Expand Down Expand Up @@ -174,7 +175,8 @@ WizardFooter.propTypes = {
setDisplaySubmitButton: PropTypes.any,
onSave: PropTypes.any,
onDownload: PropTypes.any,
project: PropTypes.any
project: PropTypes.any,
shareView: PropTypes.bool
};

export default WizardFooter;
4 changes: 4 additions & 0 deletions client/src/services/project.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export function getById(id) {
return axios.get(`${baseUrl}/${id}`);
}

export function getByIdWithEmail(id) {
return axios.get(`${baseUrl}/projectShare/${id}`);
}

export function post(project) {
return axios.post(baseUrl, project);
}
Expand Down
25 changes: 25 additions & 0 deletions server/app/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@ const getById = async (req, res) => {
}
};

const getByIdWithEmail = async (req, res) => {
try {
const project = await projectService.getByIdWithEmail(
req.params.id,
req.user.email
);
if (!project) {
res
.status(404)
.send(
"project " +
req.params.id +
" not shared with " +
req.user.email +
" or does not exist."
);
return;
}
res.status(200).json(project);
} catch (err) {
res.status(500).send(err);
}
};

const post = async (req, res) => {
try {
if (req.body.loginId !== req.user.id) {
Expand Down Expand Up @@ -229,6 +253,7 @@ const updateTotals = async (req, res) => {
module.exports = {
getAll,
getById,
getByIdWithEmail,
post: [validate({ body: projectSchema }), post, validationErrorMiddleware],
put,
del,
Expand Down
5 changes: 5 additions & 0 deletions server/app/routes/project.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ router.get(
);
router.get("/", jwtSession.validateUser, projectController.getAll);
router.get("/:id", jwtSession.validateUser, projectController.getById);
router.get(
"/projectShare/:id/",
jwtSession.validateUser,
projectController.getByIdWithEmail
);
router.post("/", jwtSession.validateUser, projectController.post);
router.put("/hide", jwtSession.validateUser, projectController.hide);
router.put("/trash", jwtSession.validateUser, projectController.trash);
Expand Down
18 changes: 18 additions & 0 deletions server/app/services/project.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ const getById = async (loginId, id) => {
}
};

const getByIdWithEmail = async (id, email) => {
try {
await poolConnect;
const request = pool.request();
request.input("email", mssql.NVarChar, email);
request.input("Id", mssql.Int, id);
const response = await request.execute("Project_SelectByIdWithSharedEmail");
if (response.recordset && response.recordset.length > 0) {
return response.recordset[0];
} else {
return null;
}
} catch (err) {
return Promise.reject(err);
}
};

const post = async item => {
try {
await poolConnect;
Expand Down Expand Up @@ -264,6 +281,7 @@ const updateTotals = async (
module.exports = {
getAll,
getById,
getByIdWithEmail,
post,
put,
del,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
CREATE proc [dbo].[Project_SelectByIdWithSharedEmail]
@email NVARCHAR(100),
@id INT
AS
BEGIN
SELECT
p.id,
p.name,
p.address,
p.formInputs,
p.loginId,
p.calculationId,
p.dateCreated,
p.dateModified,
p.description,
p.droId,
p.adminNotes,
p.dateModifiedAdmin,
p.dateHidden,
p.dateTrashed,
p.dateSnapshotted,
p.dateSubmitted
FROM Project p
RIGHT JOIN ProjectShare ps ON ps.projectId = p.id
WHERE ps.email = @email AND ps.projectid = @id;
END
GO

0 comments on commit 2956841

Please sign in to comment.