Skip to content

Commit

Permalink
Can add friends
Browse files Browse the repository at this point in the history
  • Loading branch information
zimengzhou1 committed Oct 31, 2022
1 parent 7e180b9 commit d3cacbe
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 6 deletions.
1 change: 0 additions & 1 deletion src/components/OneLineForm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as React from "react";
import Typography from "@mui/material/Typography";
import Grid from "@mui/material/Grid";
import TextField from "@mui/material/TextField";
import Box from "@mui/material/Box";
Expand Down
14 changes: 12 additions & 2 deletions src/components/PeopleDrawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function PeopleDrawer({
invalidParticipants,
selectedParticipants,
setSelectedParticipants,
getFriends,
}) {
const [invalidToggle, setInvalidToggle] = useState(false);

Expand All @@ -31,14 +32,23 @@ function PeopleDrawer({

return (
<>
<Box m="auto">
<Box m="auto" align="center">
<Button
variant="outlined"
onClick={() => {
getContacts();
}}
>
Retrieve contacts
Retrieve contacts (hardcode)
</Button>
<Button
sx={{ mt: 1 }}
variant="outlined"
onClick={() => {
getFriends();
}}
>
Retrieve friends
</Button>
</Box>
<Box sx={{ pl: 2 }}>
Expand Down
66 changes: 64 additions & 2 deletions src/components/Schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import {
fetchParticipantWebIdData,
} from "../utils/participantsHelper";
import { createAvailabilityEvents } from "../utils/calendarHelper";
import OneLineForm from "./OneLineForm";
import OneLineForm from "./ScheduleForm";
import { useSnackbar } from "notistack";
import {
downloadSelectedAvailability,
downloadSelectedVacation,
} from "../utils/calendarHelper";
import { getRDFasJson } from "../utils/fetchHelper";

const participants = {
dummy1: {
Expand Down Expand Up @@ -149,6 +150,28 @@ export default function Schedule() {
}
};

const addFriend = async (friendWebId, webID) => {
const response = await solidFetch(webID, {
method: "PATCH",
headers: {
"Content-Type": "application/sparql-update",
},
body: `
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
INSERT DATA {
<#me> foaf:knows "${friendWebId}".
}`,
});
if (response.status >= 200 && response.status <= 300) {
enqueueSnackbar("Success!", { variant: "success" });
} else {
enqueueSnackbar("Something went wrong when adding friend...", {
variant: "error",
});
}
console.log(response);
};

const fetchFriend = async (webId) => {
participants[webId] = {};

Expand All @@ -174,6 +197,41 @@ export default function Schedule() {
});
};

const fetchFriends = async () => {
participants = {};
setValidParticipants([]);
setInvalidParticipants([]);
const frame = {
"@context": {
knows: "http://xmlns.com/foaf/0.1/knows",
},
"@id": session.info.webId,
};

const result = await getRDFasJson(session.info.webId, frame, solidFetch);
let friendList = result["knows"];
if (friendList === undefined) {
return;
} else if (!Array.isArray(friendList)) {
friendList = [friendList];
}
console.log(friendList);
console.info("All participants' WebIDs fetched (without data).");

friendList.forEach((id) => {
participants[id] = {};
});

await fetchParticipantWebIdData(
participants,
solidFetch,
setValidParticipants,
setInvalidParticipants
);
console.log("All participants' WebIDs fetched (with data).");
console.log(participants);
};

return (
<>
{session.info.isLoggedIn && (
Expand Down Expand Up @@ -207,9 +265,12 @@ export default function Schedule() {
<OneLineForm
id="inputWebId"
label="Input webId of friend"
trigger={(webId) => fetchFriend(webId)}
trigger={async (webId) => await fetchFriend(webId)}
required={false}
buttonText="Fetch"
addFriend={async (friendWebId) =>
await addFriend(friendWebId, session.info.webId)
}
/>
<Box sx={{ height: "60%" }}>
<CustomCalendar
Expand Down Expand Up @@ -239,6 +300,7 @@ export default function Schedule() {
contactUrl
);
}}
getFriends={fetchFriends}
validParticipants={validParticipants}
invalidParticipants={invalidParticipants}
selectedParticipants={selectedParticipants}
Expand Down
57 changes: 57 additions & 0 deletions src/components/ScheduleForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import * as React from "react";
import Grid from "@mui/material/Grid";
import TextField from "@mui/material/TextField";
import Box from "@mui/material/Box";
import Button from "@mui/material/Button";
import { useState } from "react";

export default function OneLineForm({
id,
label,
trigger,
required,
buttonText,
addFriend,
}) {
const handleSubmit = async (event) => {
event.preventDefault();
const data = new FormData(event.currentTarget);
trigger(data.get(id));
};

const [val, setVal] = useState("");

return (
<>
<Box component="form" onSubmit={handleSubmit}>
<Grid container spacing={3}>
<Grid item xs={12} md={12}>
<TextField
required={required}
id={id}
name={id}
label={label}
onChange={(e) => {
setVal(e.currentTarget.value);
}}
fullWidth
variant="standard"
/>
<Button type="submit" variant="contained" sx={{ mt: 3, mb: 2 }}>
{buttonText}
</Button>
<Button
onClick={() => {
addFriend(val);
}}
variant="contained"
sx={{ mt: 3, mb: 2, ml: 3 }}
>
{"Add Friend"}
</Button>
</Grid>
</Grid>
</Box>
</>
);
}
2 changes: 1 addition & 1 deletion src/utils/participantsHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export async function fetchParticipantWebIdData(
for (let i = 0; i < webids.length; i++) {
const id = webids[i];

if (id.startsWith("http")) {
if (id != "dummy1" || id != "dummy2") {
try {
const frame = {
"@context": {
Expand Down

0 comments on commit d3cacbe

Please sign in to comment.