Skip to content

Commit 53fd3dd

Browse files
authored
Merge pull request #102 from distributeaid/98-carry-information-forward
Carry information forward
2 parents 4dba164 + cfaecda commit 53fd3dd

File tree

4 files changed

+93
-13
lines changed

4 files changed

+93
-13
lines changed

backend/consts.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
REQUIRED_PAGES = {"Demographics"}
2+
3+
4+
ORG_NAME = "orgname"

backend/logic/manipulate_site_info.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
11
import logging
22

3-
from backend.models import db, Site, SiteQuestion, Organization, OrganizationQuestion
3+
from backend.models import (
4+
db,
5+
Site,
6+
SiteQuestion,
7+
Organization,
8+
OrganizationQuestion,
9+
OrganizationQuestionResponse,
10+
)
11+
from backend.consts import ORG_NAME
12+
13+
14+
def update_org_and_responses(organization, name):
15+
if name:
16+
org_question = OrganizationQuestion.query.filter_by(slug=ORG_NAME).first()
17+
existing = OrganizationQuestionResponse.query.filter_by(
18+
organization_id=organization.id,
19+
question_id=org_question.id,
20+
).first()
21+
if existing:
22+
existing.value = name
23+
db.session.add(existing)
24+
else:
25+
new_response = OrganizationQuestionResponse(
26+
organization_id=organization.id, question_id=org_question.id, value=name
27+
)
28+
db.session.add(new_response)
29+
organization.question_responses.append(new_response)
30+
db.session.commit()
431

532

633
def create_or_update_site_from_responses(user, responses_data):

backend/routes.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from backend.logic.manipulate_site_info import (
3838
create_or_update_site_from_responses,
3939
create_or_update_org_from_responses,
40+
update_org_and_responses,
4041
)
4142

4243
api_bp = Blueprint("api", __name__)
@@ -224,9 +225,9 @@ def register():
224225
org_name = data.get("orgName")
225226
password = data.get("password")
226227

227-
site = Organization.query.filter_by(name=org_name).first()
228+
org = Organization.query.filter_by(name=org_name).first()
228229
# there shouldn't be an existing site with the same name
229-
if site:
230+
if org:
230231
return (
231232
jsonify(
232233
{
@@ -246,6 +247,8 @@ def register():
246247
org = Organization(name=org_name)
247248
db.session.add(org)
248249
db.session.commit()
250+
org = Organization.query.filter_by(name=org_name).first()
251+
update_org_and_responses(org, org_name)
249252

250253
# Hash the password before storing it
251254
hashed_password = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt())

src/app/create-account/page.tsx

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,92 @@
11
"use client";
2-
import { useState } from "react";
3-
import { useRouter } from "next/navigation";
2+
3+
import { useState, useEffect } from "react";
4+
import { useRouter, useSearchParams } from "next/navigation";
5+
import { signIn } from "next-auth/react";
46
import ActionButton from "@/components/ui/ActionButton";
57
import ErrorAlert from "@/components/ui/ErrorAlert";
8+
import { colors } from "@/styles/colors";
69

710
export default function CreateAccountPage() {
8-
const [email, setEmail] = useState("");
11+
const router = useRouter();
12+
const searchParams = useSearchParams();
13+
14+
const initialEmail = searchParams.get("email") || "";
15+
const [email, setEmail] = useState(initialEmail);
916
const [password, setPassword] = useState("");
1017
const [orgName, setOrgName] = useState("");
1118
const [error, setError] = useState<string | null>(null);
12-
const router = useRouter();
19+
const [loading, setLoading] = useState(false);
20+
21+
useEffect(() => {
22+
if (initialEmail && !orgName) {
23+
const namePart = initialEmail.split("@")[0];
24+
const formatted = namePart
25+
.replace(/[.\-_]/g, " ")
26+
.replace(/\b\w/g, (c) => c.toUpperCase());
27+
setOrgName(formatted);
28+
}
29+
}, [initialEmail, orgName]);
1330

1431
const handleSubmit = async (e: React.FormEvent) => {
1532
e.preventDefault();
1633
setError(null);
1734

18-
// front-end validation
1935
if (!email || !password || !orgName) {
2036
setError("Please fill out all fields.");
2137
return;
2238
}
2339

2440
try {
41+
setLoading(true);
42+
2543
const res = await fetch(`/flask-api/register`, {
2644
method: "POST",
2745
headers: { "Content-Type": "application/json" },
2846
body: JSON.stringify({ orgName, email, password }),
2947
});
48+
3049
const data = await res.json();
3150

3251
if (res.ok) {
33-
router.push("/login");
52+
// After successful registration, automatically sign them in
53+
const signInResult = await signIn("credentials", {
54+
redirect: false,
55+
email,
56+
password,
57+
});
58+
59+
if (signInResult?.ok) {
60+
router.push("/");
61+
} else {
62+
setError(
63+
"Account created, but failed to log in. Please log in manually.",
64+
);
65+
router.push("/login"); // Fallback
66+
}
3467
} else {
3568
setError(data.error || "Registration failed. Please try again.");
3669
}
3770
} catch (err) {
3871
console.error("Registration error:", err);
3972
setError("An unexpected error occurred. Please try again.");
73+
} finally {
74+
setLoading(false);
4075
}
4176
};
4277

4378
return (
44-
<div className="flex justify-center items-center h-screen bg-gray-100">
79+
<div
80+
className="flex justify-center items-center h-screen"
81+
style={{ backgroundColor: colors.secondary.base }}
82+
>
4583
<form
4684
onSubmit={handleSubmit}
4785
className="bg-white p-8 rounded-lg shadow-md w-96 space-y-6"
4886
>
49-
<h2 className="text-2xl font-bold text-center">Create Account</h2>
87+
<h2 className="text-2xl font-bold text-center text-blue-900">
88+
Create Account
89+
</h2>
5090

5191
<div>
5292
<label className="block text-gray-700 mb-1">Organization Name</label>
@@ -55,6 +95,7 @@ export default function CreateAccountPage() {
5595
value={orgName}
5696
onChange={(e) => setOrgName(e.target.value)}
5797
className="w-full px-3 py-2 border rounded-lg"
98+
placeholder="Your Organization"
5899
required
59100
/>
60101
</div>
@@ -66,6 +107,7 @@ export default function CreateAccountPage() {
66107
value={email}
67108
onChange={(e) => setEmail(e.target.value)}
68109
className="w-full px-3 py-2 border rounded-lg"
110+
placeholder="[email protected]"
69111
required
70112
/>
71113
</div>
@@ -77,14 +119,19 @@ export default function CreateAccountPage() {
77119
value={password}
78120
onChange={(e) => setPassword(e.target.value)}
79121
className="w-full px-3 py-2 border rounded-lg"
122+
placeholder="Create a password"
80123
required
81124
/>
82125
</div>
83126

84-
{/* error message */}
85127
{error && <ErrorAlert message={error} />}
86128

87-
<ActionButton type="submit" label="Create Account" variant="primary" />
129+
<ActionButton
130+
type="submit"
131+
label={loading ? "Creating..." : "Create Account"}
132+
variant="primary"
133+
disabled={loading}
134+
/>
88135
</form>
89136
</div>
90137
);

0 commit comments

Comments
 (0)