-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProjectDetails.jsx
137 lines (126 loc) · 3.68 KB
/
ProjectDetails.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import { getProject, createProject, updateProject, deleteProject } from './solanaUtils';
const INITIAL_PROJECT_DETAILS = {
projectId: '',
projectName: '',
projectDescription: '',
startDate: new Date().toISOString().substring(0, 10),
endDate: new Date().toISOString().substring(0, 10),
targetFund: 0,
currentFund: 0,
};
const ProjectDetails = ({ projectId }) => {
const [projectDetails, setProjectDetails] = useState(INITIAL_PROJECT_DETAILS);
const [errorMessage, setErrorMessage] = useState('');
useEffect(() => {
if (projectId) {
getProjectDetails(projectId);
}
}, [projectId]);
const getProjectDetails = async (id) => {
try {
const details = await getProject(id);
setProjectDetails({
...details,
startDate: details.startDate.substring(0, 10),
endDate: details.endDate.substring(0, 10),
});
} catch (error) {
console.error('Failed to fetch project details:', error);
setErrorMessage('Failed to fetch project details. Please try again later.');
}
};
const handleInputChange = (e) => {
const { name, value } = e.target;
setProjectDetails((prevDetails) => ({
...prevDetails,
[name]: value,
}));
};
const resetProjectDetails = () => {
setProjectDetails(INITIAL_PROJECT_DETAILS);
};
const handleSave = async () => {
const action = projectDetails.projectId ? updateProject : createProject;
try {
await action(projectDetails);
alert('Project saved successfully!');
resetProjectDetails();
} catch (error) {
console.error('Failed to save project:', error);
setErrorMessage('Failed to save project. Please try again later.');
}
};
const handleDelete = async () => {
try {
await deleteProject(projectDetails.projectId);
alert('Project deleted successfully!');
resetProjectDetails();
} catch (error) {
console.error('Failed to delete project:', error);
setErrorMessage('Failed to delete project. Please try again later.');
}
};
return (
<div>
{errorMessage && <p className="error-message">{errorMessage}</p>}
<form onSubmit={(e) => e.preventDefault()}>
<label>
Project Name:
<input
type="text"
name="projectName"
value={projectDetails.projectName}
onChange={handleInputChange}
/>
</label>
<label>
Description:
<textarea
name="projectDescription"
value={projectDetails.projectDescription}
onChange={handleInputChange}
/>
</label>
<label>
Start Date:
<input
type="date"
name="startDate"
value={projectDetails.startDate}
onChange={handleInputChange}
/>
</label>
<label>
End Date:
<input
type="date"
name="endDate"
value={projectDetails.endDate}
onChange={handleInputChange}
/>
</label>
<label>
Target Fund:
<input
type="number"
name="targetFund"
value={projectDetails.targetFund}
onChange={handleInputChange}
/>
</label>
<button type="button" onClick={handleSave}>Save Project</button>
{projectDetails.projectId && (
<button type="button" onClick={handleDelete}>
Delete Project
</button>
)}
</form>
</div>
);
};
ProjectDetails.propTypes = {
projectId: PropTypes.string,
};
export default ProjectDetails;