Fix the frontend API calls and implement logins on frontend #7
@@ -7,6 +7,7 @@ const styles = StatsStyling;
|
||||
const API_BASE_URL = "http://localhost:5000";
|
||||
|
||||
const UploadPage = () => {
|
||||
const [datasetName, setDatasetName] = useState("");
|
||||
const [postFile, setPostFile] = useState<File | null>(null);
|
||||
const [topicBucketFile, setTopicBucketFile] = useState<File | null>(null);
|
||||
const [returnMessage, setReturnMessage] = useState("");
|
||||
@@ -15,6 +16,14 @@ const UploadPage = () => {
|
||||
const navigate = useNavigate();
|
||||
|
||||
const uploadFiles = async () => {
|
||||
const normalizedDatasetName = datasetName.trim();
|
||||
|
||||
if (!normalizedDatasetName) {
|
||||
setHasError(true);
|
||||
setReturnMessage("Please add a dataset name before continuing.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!postFile || !topicBucketFile) {
|
||||
setHasError(true);
|
||||
setReturnMessage("Please upload both files before continuing.");
|
||||
@@ -22,6 +31,7 @@ const UploadPage = () => {
|
||||
}
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append("name", normalizedDatasetName);
|
||||
formData.append("posts", postFile);
|
||||
formData.append("topics", topicBucketFile);
|
||||
|
||||
@@ -63,7 +73,7 @@ const UploadPage = () => {
|
||||
<div>
|
||||
<h1 style={{ margin: 0, color: "#111827", fontSize: 28 }}>Upload Dataset</h1>
|
||||
<p style={{ margin: "8px 0 0", color: "#6b7280", fontSize: 14 }}>
|
||||
Add your posts and topic map files to generate fresh analytics.
|
||||
Name your dataset, then upload posts and topic map files to generate analytics.
|
||||
</p>
|
||||
</div>
|
||||
<button
|
||||
@@ -83,11 +93,23 @@ const UploadPage = () => {
|
||||
gridTemplateColumns: "repeat(auto-fit, minmax(280px, 1fr))",
|
||||
}}
|
||||
>
|
||||
<div style={{ ...styles.card, gridColumn: "auto" }}>
|
||||
<h2 style={{ ...styles.sectionTitle, color: "#111827" }}>Dataset Name</h2>
|
||||
<p style={styles.sectionSubtitle}>Use a clear label so you can identify this upload later.</p>
|
||||
<input
|
||||
style={{ ...styles.input, width: "100%", maxWidth: "100%", boxSizing: "border-box" }}
|
||||
type="text"
|
||||
placeholder="Example: Cork Discussions - Jan 2026"
|
||||
value={datasetName}
|
||||
onChange={(event) => setDatasetName(event.target.value)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div style={{ ...styles.card, gridColumn: "auto" }}>
|
||||
<h2 style={{ ...styles.sectionTitle, color: "#111827" }}>Posts File (.jsonl)</h2>
|
||||
<p style={styles.sectionSubtitle}>Upload the raw post records export.</p>
|
||||
<input
|
||||
style={{ ...styles.input, width: "80%", maxWidth: "100%" }}
|
||||
style={{ ...styles.input, width: "100%", maxWidth: "100%", boxSizing: "border-box" }}
|
||||
type="file"
|
||||
accept=".jsonl"
|
||||
onChange={(event) => setPostFile(event.target.files?.[0] ?? null)}
|
||||
@@ -101,7 +123,7 @@ const UploadPage = () => {
|
||||
<h2 style={{ ...styles.sectionTitle, color: "#111827" }}>Topics File (.json)</h2>
|
||||
<p style={styles.sectionSubtitle}>Upload your topic bucket mapping file.</p>
|
||||
<input
|
||||
style={{ ...styles.input, width: "80%", maxWidth: "100%" }}
|
||||
style={{ ...styles.input, width: "100%", maxWidth: "100%", boxSizing: "border-box" }}
|
||||
type="file"
|
||||
accept=".json"
|
||||
onChange={(event) => setTopicBucketFile(event.target.files?.[0] ?? null)}
|
||||
|
||||
@@ -45,6 +45,7 @@ auth_manager = AuthManager(db, bcrypt)
|
||||
dataset_manager = DatasetManager(db)
|
||||
stat_gen = StatGen()
|
||||
|
||||
|
||||
@app.route("/register", methods=["POST"])
|
||||
def register_user():
|
||||
data = request.get_json()
|
||||
@@ -113,12 +114,15 @@ def get_user_datasets():
|
||||
@app.route("/upload", methods=["POST"])
|
||||
@jwt_required()
|
||||
def upload_data():
|
||||
if "posts" not in request.files or "topics" not in request.files or "name" not in request.form:
|
||||
if "posts" not in request.files or "topics" not in request.files:
|
||||
return jsonify({"error": "Missing required files or form data"}), 400
|
||||
|
||||
post_file = request.files["posts"]
|
||||
topic_file = request.files["topics"]
|
||||
dataset_name = request.form["name"]
|
||||
dataset_name = (request.form.get("name") or "").strip()
|
||||
|
||||
if not dataset_name:
|
||||
return jsonify({"error": "Missing required dataset name"}), 400
|
||||
|
||||
if post_file.filename == "" or topic_file.filename == "":
|
||||
return jsonify({"error": "Empty filename"}), 400
|
||||
@@ -137,17 +141,13 @@ def upload_data():
|
||||
topics = json.load(topic_file)
|
||||
dataset_id = dataset_manager.save_dataset_info(current_user, dataset_name, topics)
|
||||
|
||||
process_dataset.delay(
|
||||
dataset_id,
|
||||
posts_df.to_dict(orient="records"),
|
||||
topics
|
||||
)
|
||||
process_dataset.delay(dataset_id, posts_df.to_dict(orient="records"), topics)
|
||||
|
||||
return jsonify(
|
||||
{
|
||||
"message": "Dataset queued for processing",
|
||||
"dataset_id": dataset_id,
|
||||
"status": "processing"
|
||||
"status": "processing",
|
||||
}
|
||||
), 202
|
||||
except ValueError as e:
|
||||
@@ -160,7 +160,7 @@ def upload_data():
|
||||
def get_dataset(dataset_id):
|
||||
try:
|
||||
user_id = int(get_jwt_identity())
|
||||
|
||||
|
||||
if not dataset_manager.authorize_user_dataset(dataset_id, user_id):
|
||||
raise NotAuthorisedException("This user is not authorised to access this dataset")
|
||||
|
||||
@@ -181,7 +181,7 @@ def get_dataset(dataset_id):
|
||||
def get_dataset_status(dataset_id):
|
||||
try:
|
||||
user_id = int(get_jwt_identity())
|
||||
|
||||
|
||||
if not dataset_manager.authorize_user_dataset(dataset_id, user_id):
|
||||
raise NotAuthorisedException("This user is not authorised to access this dataset")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user