-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
➡️ merge pull request #55 from devsoc-unsw/feature/joinSocietyFrontend
feature/join society frontend, also improves register ux
- Loading branch information
Showing
7 changed files
with
182 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...rc/Settings/SettingsPage/SocietyManagementPage/SearchSocieties/SearchSocieties.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.societiesList { | ||
list-style-type: none; | ||
} | ||
|
||
.societiesList li { | ||
display: flex; | ||
align-items: center; | ||
justify-content: space-between; | ||
padding: 32px; | ||
border-radius: 32px; | ||
} |
124 changes: 124 additions & 0 deletions
124
frontend/src/Settings/SettingsPage/SocietyManagementPage/SearchSocieties/SearchSocieties.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { UserGroupIcon } from "@heroicons/react/24/outline"; | ||
import Button from "../../../../Button/Button"; | ||
import { ButtonIcons, ButtonVariants } from "../../../../Button/ButtonTypes"; | ||
import { TextInput, TextOptions } from "../../../../TextInput/TextInput"; | ||
import { SettingsPage } from "../../SettingsPage"; | ||
import { useContext, useEffect, useState } from "react"; | ||
import { Society, UserContext } from "../../../../UserContext/UserContext"; | ||
import classes from "./SearchSocieties.module.css"; | ||
|
||
export const SearchSocietiesPage = () => { | ||
const [societyName, setSocietyName] = useState(""); | ||
const [foundSocieties, setFoundSocieties] = useState([]); | ||
const [error, setError] = useState(""); | ||
const { societies, setSocieties } = useContext(UserContext); | ||
useEffect(() => { | ||
searchSocieties(""); | ||
}, []); | ||
|
||
const searchSocieties = async (societyName: string) => { | ||
const allSocieties = await fetch("http://localhost:5180/societies", { | ||
method: "GET", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
credentials: "include", | ||
}); | ||
|
||
const societiesJson = await allSocieties.json(); | ||
|
||
const userSocieties = await fetch("http://localhost:5180/user/societies", { | ||
method: "GET", | ||
credentials: "include", | ||
}); | ||
|
||
const userSocietiesJson = await userSocieties.json(); | ||
setSocieties?.(userSocietiesJson); | ||
|
||
if (allSocieties.ok) { | ||
setError(""); | ||
setFoundSocieties(societiesJson); | ||
if (societyName) { | ||
setFoundSocieties( | ||
societiesJson.filter((society: Society) => | ||
society.name.toLowerCase().includes(societyName.toLowerCase()) | ||
) | ||
); | ||
} | ||
} else { | ||
setError("Failed to fetch all societies."); | ||
} | ||
}; | ||
|
||
const joinSociety = async (society: Society) => { | ||
const res = await fetch("http://localhost:5180/user/society/join", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
credentials: "include", | ||
body: JSON.stringify({ societyId: society.id }), | ||
}); | ||
|
||
if (res.ok) { | ||
setSocieties?.({ | ||
joined: [...(societies?.joined ?? []), society], | ||
administering: societies?.administering ?? [], | ||
}); | ||
} else { | ||
alert("Failed to join society"); | ||
} | ||
}; | ||
|
||
return ( | ||
<SettingsPage | ||
title="Join a society" | ||
pageAbovePath="/settings/societies" | ||
headerButtons={[ | ||
<TextInput | ||
placeholder="Society name" | ||
name="societyName" | ||
type={TextOptions.Text} | ||
onChange={(name) => setSocietyName(name)} | ||
icon={<UserGroupIcon />} | ||
error={error !== ""} | ||
noMargin | ||
/>, | ||
<Button | ||
variant={ButtonVariants.Primary} | ||
icon={ButtonIcons.Search} | ||
type="button" | ||
onClick={() => searchSocieties(societyName)} | ||
/>, | ||
]} | ||
> | ||
<ul className={classes.societiesList}> | ||
{foundSocieties.map( | ||
(society: Society) => | ||
!societies?.administering.some( | ||
(administeredSociety) => administeredSociety.name === society.name | ||
) && ( | ||
<li key={society.id}> | ||
<h2>{society.name}</h2> | ||
{!societies?.joined.some( | ||
(joinedSociety) => joinedSociety.name === society.name | ||
) ? ( | ||
<Button | ||
variant={ButtonVariants.Secondary} | ||
icon={ButtonIcons.Plus} | ||
type="button" | ||
onClick={() => { | ||
joinSociety(society); | ||
}} | ||
/> | ||
) : ( | ||
<p>Already joined</p> | ||
)} | ||
</li> | ||
) | ||
)} | ||
</ul> | ||
{error && <p>{error}</p>} | ||
</SettingsPage> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters