Skip to content

Commit

Permalink
Merge branch 'feat/messaging' into working
Browse files Browse the repository at this point in the history
  • Loading branch information
Truiteseche committed Sep 8, 2024
2 parents 390419f + ad11423 commit 739d177
Show file tree
Hide file tree
Showing 12 changed files with 527 additions and 49 deletions.
4 changes: 4 additions & 0 deletions public/images/attachment-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
162 changes: 134 additions & 28 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { areOccurenciesEqual, createUserLists, encrypt, decrypt, getBrowser } fr
import { getCurrentSchoolYear } from "./utils/date";
import { getProxiedURL } from "./utils/requests";
import EdpuLogo from "./components/graphics/EdpuLogo";
import { add } from "date-fns";

// CODE-SPLITTING - DYNAMIC IMPORTS
const Lab = lazy(() => import("./components/app/CoreApp").then((module) => { return { default: module.Lab } }));
Expand Down Expand Up @@ -72,7 +71,7 @@ function consoleLogEDPLogo() {
consoleLogEDPLogo();

const currentEDPVersion = "0.3.1";
const apiVersion = "4.53.4";
const apiVersion = "4.60.4";

// secret webhooks
const carpeConviviale = "CARPE_CONVIVIALE_WEBHOOK_URL";
Expand Down Expand Up @@ -1140,6 +1139,40 @@ export default function App() {
return sortedHomeworks
}


function sortMessages(messages) {
const sortedMessages = messages.messages.received.map((message) => { console.log("files:", message.files); return {
date: message.date,
files: structuredClone(message.files)?.map((file) => new File(file.id, file.type, file.libelle)),
from: message.from,
id: message.id,
read: message.read,
subject: message.subject,
content: null,
// ...
}});

return sortedMessages;
}

function sortMessageContent(messageContent) {
if (!messageContent) {
return;
}
const oldSortedMessages = useUserData("sortedMessages").get();
const targetMessageIdx = oldSortedMessages.findIndex((item) => item.id === messageContent.id);
oldSortedMessages[targetMessageIdx].read = true;
oldSortedMessages[targetMessageIdx].files = messageContent.files.map((file) => new File(file.id, file.type, file.libelle));
oldSortedMessages[targetMessageIdx].content = {
id: messageContent.id,
subject: messageContent.subject,
date: messageContent.subject,
content: messageContent.content
// ...
};
useUserData("sortedMessages").set(oldSortedMessages);
}

function sortSchoolLife(schoolLife, activeAccount) {
const sortedSchoolLife = {
delays: [],
Expand Down Expand Up @@ -1283,6 +1316,7 @@ export default function App() {
})
})
.then((response) => {
console.log(".then ~ response:", response)
// GESTION DATA
let statusCode = response.code;
if (statusCode === 200) {
Expand Down Expand Up @@ -1317,6 +1351,7 @@ export default function App() {
accountType: "P",
lastConnection: accounts.lastConnexion,
id: account.id,
familyId: accounts.id,
firstName: account.prenom,
lastName: account.nom,
email: email,
Expand Down Expand Up @@ -1363,9 +1398,6 @@ export default function App() {
if (error.name !== 'AbortError') {
messages.submitButtonText = "Échec de la connexion";
messages.submitErrorMessage = "Error: " + error.message;
if (error.message === "Unexpected token 'P', \"Proxy error\" is not valid JSON") {
setProxyError(true);
}
}
})
.finally(() => {
Expand Down Expand Up @@ -1419,11 +1451,6 @@ export default function App() {
}
setTokenState((old) => (response?.token || old));
})
.catch((error) => {
if (error.message === "Unexpected token 'P', \"Proxy error\" is not valid JSON") {
setProxyError(true);
}
})
.finally(() => {
abortControllers.current.splice(abortControllers.current.indexOf(controller), 1);
})
Expand Down Expand Up @@ -1472,11 +1499,6 @@ export default function App() {
}
setTokenState((old) => (response?.token || old));
})
.catch((error) => {
if (error.message === "Unexpected token 'P', \"Proxy error\" is not valid JSON") {
setProxyError(true);
}
})
.finally(() => {
abortControllers.current.splice(abortControllers.current.indexOf(controller), 1);
})
Expand Down Expand Up @@ -1536,11 +1558,6 @@ export default function App() {
}
setTokenState((old) => (response?.token || old));
})
.catch((error) => {
if (error.message === "Unexpected token 'P', \"Proxy error\" is not valid JSON") {
setProxyError(true);
}
})
.finally(() => {
abortControllers.current.splice(abortControllers.current.indexOf(controller), 1);
})
Expand Down Expand Up @@ -1602,10 +1619,6 @@ export default function App() {
requireLogin();
}
setTokenState(old => responseData?.token || old);
} catch (error) {
if (error.message === "Unexpected token 'P', \"Proxy error\" is not valid JSON") {
setProxyError(true);
}
} finally {
abortControllers.current.splice(abortControllers.current.indexOf(controller), 1);
}
Expand Down Expand Up @@ -1650,16 +1663,109 @@ export default function App() {
}
setTokenState((old) => (response?.token || old));
})
.catch((error) => {
if (error.message === "Unexpected token 'P', \"Proxy error\" is not valid JSON") {
setProxyError(true);
.finally(() => {
abortControllers.current.splice(abortControllers.current.indexOf(controller), 1);
})
}


async function fetchMessages(controller = (new AbortController())) {
abortControllers.current.push(controller);
const userId = activeAccount;
const data = {
anneeMessages: getUserSettingValue("isSchoolYearEnabled") ? getUserSettingValue("schoolYear").join("-") : getCurrentSchoolYear().join("-"),
}
fetch(
getProxiedURL(`https://api.ecoledirecte.com/v3/${accountsListState[userId].accountType === "E" ? "eleves/" + accountsListState[userId].id : "familles/" + accountsListState[userId].familyId}/messages.awp?force=false&typeRecuperation=received&idClasseur=0&orderBy=date&order=desc&query=&onlyRead=&page=0&itemsPerPage=100&getAll=0&verbe=get&v=${apiVersion}`, true),
{
method: "POST",
headers: {
"x-token": tokenState
},
body: `data=${JSON.stringify(data)}`,
signal: controller.signal,
referrerPolicy: "no-referrer",
},
)
.then((response) => response.json())
.then((response) => {
console.log(".then ~ response:", response)
let code;
if (accountsListState[activeAccount].firstName === "Guest") {
code = 49969;
} else {
code = response.code;
}
if (code === 200) {
changeUserData("sortedMessages", sortMessages(response.data));
} else if (code === 520 || code === 525) {
// token invalide
requireLogin();
} else if (code === 49969) {
// TODO: add data/messages.json for guest user
// import("./data/messages.json").then((module) => {
// changeUserData("sortedMessages", sortMessages(module.data));;
// })
}
setTokenState((old) => (response?.token || old));
})
.finally(() => {
abortControllers.current.splice(abortControllers.current.indexOf(controller), 1);
})
}

async function fetchMessageContent(id, controller) {
const oldSortedMessages = useUserData("sortedMessages").get();
if (oldSortedMessages) {
const targetMessageIdx = oldSortedMessages.findIndex((item) => item.id === id);
if (oldSortedMessages[targetMessageIdx].content !== null) {
return;
}
}
abortControllers.current.push(controller);
const userId = activeAccount;
const data = {
anneeMessages: getUserSettingValue("isSchoolYearEnabled") ? getUserSettingValue("schoolYear").join("-") : getCurrentSchoolYear().join("-"),
}
fetch(
getProxiedURL(`https://api.ecoledirecte.com/v3/${accountsListState[userId].accountType === "E" ? "eleves/" + accountsListState[userId].id : "familles/" + accountsListState[userId].familyId}/messages/${id}.awp?verbe=get&mode=destinataire&v=${apiVersion}`, true),
{
method: "POST",
headers: {
"x-token": tokenState
},
body: `data=${JSON.stringify(data)}`,
signal: controller.signal,
referrerPolicy: "no-referrer",
},
)
.then((response) => response.json())
.then((response) => {
let code;
if (accountsListState[activeAccount].firstName === "Guest") {
code = 49969;
} else {
code = response.code;
}
if (code === 200) {
sortMessageContent(response.data)
} else if (code === 520 || code === 525) {
// token invalide
requireLogin();
} else if (code === 49969) {
// TODO: add data/messages.json for guest user
// import("./data/messages.json").then((module) => {
// sortMessageContent(module.data)
// })
}
setTokenState((old) => (response?.token || old));
})
.finally(() => {
abortControllers.current.splice(abortControllers.current.indexOf(controller), 1);
})
}


async function fetchSchoolLife(controller = (new AbortController())) {
abortControllers.current.push(controller);
const data = {
Expand Down Expand Up @@ -2062,7 +2168,7 @@ export default function App() {
path: "messaging"
},
{
element: <Messaging />,
element: <Messaging isLoggedIn={isLoggedIn} activeAccount={activeAccount} fetchMessages={fetchMessages} fetchMessageContent={fetchMessageContent} />,
path: ":userId/messaging"
},
],
Expand Down
91 changes: 91 additions & 0 deletions src/components/app/Messaging/Inbox.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@


#messaging .inbox-window-header {
height: 50px;
border-radius: 15px 15px 0 0;
box-shadow: none;
border-bottom: 1px solid rgba(var(--text-color-alt), .5);
position: relative;
}

#inbox {
height: 100%;
}

#inbox .inbox-search-input {
background-color: rgb(var(--background-color-0), .5);
border: none;
background-color: none;
border-radius: 0;
}
#inbox .inbox-search-input .text-input-container {
border-radius: 0;
border: none;
background-color: none;
}
#inbox .inbox-search-input .text-input-container:is(:hover, :focus-within) {
background-color: rgb(var(--background-color-0), rgb(var(--text-color-alt), .5));
}

#inbox .messages-container {
height: 100%;
}

#inbox .messages-container ul {
list-style-type: none;
}

#inbox .message-container {
position: relative;
text-align: left;
padding: 10px;
border-left: 4px solid rgb(var(--text-color-alt), .5);
border-bottom: 1px solid rgb(var(--text-color-alt), .5);
opacity: .6;
outline: none;
cursor: pointer;
}

#inbox .message-container:last-child {
border-bottom: none;
}

#inbox .message-container[data-read=false] {
border-left: 4px solid rgb(var(--text-color-alt));
padding-left: 6px;
opacity: 1;
}

#inbox .message-container:is(:hover, :focus-visible) {
background-color: rgba(var(--background-color-0), .2);
}
#inbox .message-container.selected {
opacity: .8;
background-color: rgba(var(--background-color-focus));
}

#inbox .message-container :is(.message-author, .message-date) {
color: rgb(var(--text-color-alt))
}

#inbox .message-container .message-subject {
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
width: 100%;
}
#inbox .message-container .attachment-icon {
height: 16px;
transform: scale(1.3);
}

:fullscreen #inbox ul {
display: flex;
flex-flow: row wrap;
}
:fullscreen .message-container {
flex-basis: 25%;
}
:fullscreen #inbox .message-container:last-child {
border-bottom: none;
}
Loading

0 comments on commit 739d177

Please sign in to comment.