Commit f83bb449 authored by Geovanny's avatar Geovanny

House request system done

parent 5356bdd6
......@@ -38,6 +38,20 @@ router.get('/all', async (context, next) => {
}
});
authRouter.get('/has-house', async (context, next) => {
try{
if(context.user.house_id){
context.response.body = true;
}else{
context.response.body = false;
}
context.response.status = 200;
}catch(error){
console.log(error);
context.throw(400, 'ERROR');
}
})
authRouter.post('/request', async (context, next) => {
if(context.user.house_id){
context.throw(400, 'Already in a house')
......
......@@ -6,17 +6,54 @@ class HousesController{
this.view = view;
this.sync = new Sync();
this.houses = [];
this.selectedIndex = undefined;
this.getHouses();
this.view.addEventListener("house_select", (event) => this.selectedIndex = event.detail);
this.view.addEventListener("request_attemp", () => this.attempRequest());
this.checkHouse();
setInterval(async () => this.checkHouse(), 5000)
}
async attempRequest(){
if(this.selectedIndex === undefined){
alert("No house selected");
return;
}
try{
await this.sync.requestHouse(this.houses[this.selectedIndex].id);
alert('Attemp Successfully Sent');
}catch(error){
console.log(error);
alert("Attemp failed");
}
}
async checkHouse(){
try{
const has_house = await this.sync.hasHouse();
if(has_house){
this.view.hideJoin();
}else{
this.view.showJoin();
}
}catch(error){
console.log(error);
}
}
async getHouses(){
this.houses = [];
try{
const houses = await this.sync.getHouses();
this.view.drawTable(houses);
this.houses = await this.sync.getHouses();
}catch(error){
console.log(error);
alert('Failed to retrieve houses');
}
this.view.drawTable(this.houses);
}
}
......
......@@ -16,8 +16,8 @@
<script src="/navbar/navbar.js"></script>
<content-body>
<table-wrapper class="house_table">
<div class="tab">
<button class="join_house">Join House</button>
<div class="tab" style="display:none">
<button class="request_house">Send House Request</button>
</div>
<table id="data_table" class="display"></table>
</table-wrapper>
......
......@@ -2,11 +2,42 @@ class Sync{
async getHouses(){
const houses_response = await fetch('/api/house/all');
if(!houses_response.ok){
throw Error('Failed to get units')
}
const houses = await houses_response.json();
return houses;
}
async hasHouse(){
const response = await fetch('/api/house/has-house');
if(response.status === 401){
return false;
}
if(!response.ok){
throw Error('Failed to get units')
}
const has_house = await response.json();
return has_house;
}
async requestHouse(house_id){
const response = await fetch('/api/house/request', {
method: "POST",
body: JSON.stringify({house_id: house_id}),
headers: {
'Content-Type': 'application/json'
}
});
if(!response.ok){
throw Error('Failed to Send Request')
}
}
}
export default Sync;
\ No newline at end of file
......@@ -13,9 +13,23 @@ class HousesView extends EventTarget{
constructor(){
super();
this.join_tab = document.querySelector('.tab');
this.join_button = this.join_tab.querySelector('.join_button');
this.houses_table_view = new TableView($('table-wrapper'), house_columns);
this.request_tab = document.querySelector('.tab');
this.request_button = this.request_tab.querySelector('.request_house');
this.houses_table_view = new TableView($('table-wrapper'), house_columns);
this.houses_table_view.addEventListener("row_click", (event) =>{
this.dispatchEvent(new CustomEvent("house_select", {detail: event.detail}));
});
this.request_button.addEventListener("click", (event) => {
this.dispatchEvent(new CustomEvent("request_attemp"));
});
}
hideJoin(){
this.request_tab.style.display = "none";
}
showJoin(){
this.request_tab.style.display = "";
}
drawTable(data){
......
......@@ -28,6 +28,9 @@ class Sync{
'Content-Type': 'application/json'
}
});
if(!response.ok){
throw Error('Failed to get units')
}
}
async modifyUnit(data){
......@@ -38,12 +41,18 @@ class Sync{
'Content-Type': 'application/json'
}
});
if(!response.ok){
throw Error('Failed to get units')
}
}
async deleteUnit(id){
const response = await fetch(`/api/user/unit/${id}`, {
method: "DELETE",
});
if(!response.ok){
throw Error('Failed to get units')
}
}
}
......
......@@ -6,7 +6,7 @@
max-width: 1000px;
margin-left: -500px;
.join_house{
.request_house{
float: right;
margin: 5px;
color: whitesmoke;
......@@ -14,4 +14,19 @@
background-color: grey;
border-radius: 3px;
}
}
\ No newline at end of file
.dataTable{
tr.even.active{
background-color: rgb(151, 151, 151);
td.sorting_1{
background-color: rgb(151, 151, 151);
}
}
tr.odd.active{
background-color: rgb(151, 151, 151);
td.sorting_1{
background-color: rgb(151, 151, 151);
}
}
}
}
......@@ -6,7 +6,7 @@
max-width: 1000px;
margin-left: -500px;
}
.house_table .join_house {
.house_table .request_house {
float: right;
margin: 5px;
color: whitesmoke;
......@@ -14,5 +14,17 @@
background-color: grey;
border-radius: 3px;
}
.house_table .dataTable tr.even.active {
background-color: #979797;
}
.house_table .dataTable tr.even.active td.sorting_1 {
background-color: #979797;
}
.house_table .dataTable tr.odd.active {
background-color: #979797;
}
.house_table .dataTable tr.odd.active td.sorting_1 {
background-color: #979797;
}
/*# sourceMappingURL=house.css.map */
{"version":3,"sourceRoot":"","sources":["../sass/house.scss"],"names":[],"mappings":"AAAA;EACI;EACA;EACA;EACA;EACA;EACA;;AAEA;EACI;EACA;EACA;EACA;EACA;EACA","file":"house.css"}
\ No newline at end of file
{"version":3,"sourceRoot":"","sources":["../sass/house.scss"],"names":[],"mappings":"AAAA;EACI;EACA;EACA;EACA;EACA;EACA;;AAEA;EACI;EACA;EACA;EACA;EACA;EACA;;AAGA;EACI;;AACA;EACI;;AAGR;EACI;;AACA;EACI","file":"house.css"}
\ No newline at end of file
......@@ -360,7 +360,7 @@ unit-detail {
max-width: 1000px;
margin-left: -500px;
}
.house_table .join_house {
.house_table .request_house {
float: right;
margin: 5px;
color: whitesmoke;
......@@ -368,6 +368,18 @@ unit-detail {
background-color: grey;
border-radius: 3px;
}
.house_table .dataTable tr.even.active {
background-color: #979797;
}
.house_table .dataTable tr.even.active td.sorting_1 {
background-color: #979797;
}
.house_table .dataTable tr.odd.active {
background-color: #979797;
}
.house_table .dataTable tr.odd.active td.sorting_1 {
background-color: #979797;
}
body {
font-family: Roboto;
......
{"version":3,"sourceRoot":"","sources":["../fontawesome/font_awesome.scss","../sass/navbar.scss","../sass/table.scss","../sass/login.scss","../sass/unit.scss","../sass/house.scss","../sass/main.scss"],"names":[],"mappings":"AAAA;EACI;EACA;EACA;EACA;EACA;;AAGF;EACE;EACA;EACA;EACA;EACA;;AAGF;EACE;EACA;EACA;EACA;EACA;;AAmBF;EACI;IAAK;;EACP;IAAO;;;AC1CX;EACI;EACA;EACA;EACA,eACI;EAGJ;EACA;;;AAGJ;EAEI;EACA;EACA;;AACA;EACI;EACA;EAEA;EACA;;;AAIR;EACI;EACA;;;AAGJ;EACI;EACA;EACA;EACA;EACA;EACA;;;AAGJ;EACI;EACA;EACA;EACA;EACA;EACA;;AACA;EACI;EACA;EACA;EACA;EACA;;;AAGR;EACI;;;AAGJ;EACI;EACA;EACA;EACA;EACA;EACA;EACA;;AACA;EACI;EACA;;AAEJ;EACI;;;AAKJ;EACI;;;AAIR;EACI;EACA;EACA;;AACA;EACI;EACA;;;AAIR;EACI;;;AAKA;EACI;;AAEJ;EACI;;;ACtGR;EACI;;;AAEJ;EACI;;AAEA;EACI;;;AAKR;EACI;;AACA;EACI;;;AAGR;EACI;;;AAGJ;EACI;EACA;EACA;;;AAIJ;EACI;EACA;EAEA,eACI;;;AClCR;EACI;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;;AAGJ;EACI;EAEA;EACA;EACA;;AACA;EACI;EACA;;;AAIN;AACF;EACI;EAEA;EACA;EACA;EACA;EACA;;;AAGF;AACF;EACI;;;AAGF;AACF;EACI;;;AAGF;AACF;EACI;EACA;EACA;EACA;EACA;;;AAGJ;EACI;AAA0B;;;AAG9B;AACA;EACI;IAAM;;EACN;IAAI;;;AAGN;EACE;EACA;;AAEF;EACE;;AAEF;EACE;EACA;EACA;EACA;EACA;EACA;;AAEF;EACE;EACA;EACA;EACA;;;ACnFJ;EACI;EACA;EACA;;;AAGJ;EACI;EACA;;;AAGJ;EACI;EACA;;;AAEJ;EACI;EACA;EACA;;;AAGJ;EACI,eACI;EAGJ;EACA;;;AAGJ;EACI;EACA;EACA;EACA;EACA;EACA;;;AAIJ;EACI;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;;AAGJ;EACI;EACA;EACA;EACA;;;AAEJ;EACI;EACA;EACA;;;AAEJ;EACI;EACA;;AACA;EACI;EACA;;;AAGR;EACI;;;AAEJ;EACI;;AACA;EACI;EACA;EACA;EACA;EACA;EACA;EACA;;;AAGR;EACI;EACA;;;AAEJ;EACI;EACA;;;AAEJ;EACI;EACA;;;ACnGJ;EACI;EACA;EACA;EACA;EACA;EACA;;AAEA;EACI;EACA;EACA;EACA;EACA;EACA;;;ACPR;EACI;EACA;EACA;EACA;EACA,eACI;;;AAKR;EACI;EACA;;;AAGJ;EACI;EACA;;AACA;EACI","file":"main.css"}
\ No newline at end of file
{"version":3,"sourceRoot":"","sources":["../fontawesome/font_awesome.scss","../sass/navbar.scss","../sass/table.scss","../sass/login.scss","../sass/unit.scss","../sass/house.scss","../sass/main.scss"],"names":[],"mappings":"AAAA;EACI;EACA;EACA;EACA;EACA;;AAGF;EACE;EACA;EACA;EACA;EACA;;AAGF;EACE;EACA;EACA;EACA;EACA;;AAmBF;EACI;IAAK;;EACP;IAAO;;;AC1CX;EACI;EACA;EACA;EACA,eACI;EAGJ;EACA;;;AAGJ;EAEI;EACA;EACA;;AACA;EACI;EACA;EAEA;EACA;;;AAIR;EACI;EACA;;;AAGJ;EACI;EACA;EACA;EACA;EACA;EACA;;;AAGJ;EACI;EACA;EACA;EACA;EACA;EACA;;AACA;EACI;EACA;EACA;EACA;EACA;;;AAGR;EACI;;;AAGJ;EACI;EACA;EACA;EACA;EACA;EACA;EACA;;AACA;EACI;EACA;;AAEJ;EACI;;;AAKJ;EACI;;;AAIR;EACI;EACA;EACA;;AACA;EACI;EACA;;;AAIR;EACI;;;AAKA;EACI;;AAEJ;EACI;;;ACtGR;EACI;;;AAEJ;EACI;;AAEA;EACI;;;AAKR;EACI;;AACA;EACI;;;AAGR;EACI;;;AAGJ;EACI;EACA;EACA;;;AAIJ;EACI;EACA;EAEA,eACI;;;AClCR;EACI;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;;AAGJ;EACI;EAEA;EACA;EACA;;AACA;EACI;EACA;;;AAIN;AACF;EACI;EAEA;EACA;EACA;EACA;EACA;;;AAGF;AACF;EACI;;;AAGF;AACF;EACI;;;AAGF;AACF;EACI;EACA;EACA;EACA;EACA;;;AAGJ;EACI;AAA0B;;;AAG9B;AACA;EACI;IAAM;;EACN;IAAI;;;AAGN;EACE;EACA;;AAEF;EACE;;AAEF;EACE;EACA;EACA;EACA;EACA;EACA;;AAEF;EACE;EACA;EACA;EACA;;;ACnFJ;EACI;EACA;EACA;;;AAGJ;EACI;EACA;;;AAGJ;EACI;EACA;;;AAEJ;EACI;EACA;EACA;;;AAGJ;EACI,eACI;EAGJ;EACA;;;AAGJ;EACI;EACA;EACA;EACA;EACA;EACA;;;AAIJ;EACI;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;;AAGJ;EACI;EACA;EACA;EACA;;;AAEJ;EACI;EACA;EACA;;;AAEJ;EACI;EACA;;AACA;EACI;EACA;;;AAGR;EACI;;;AAEJ;EACI;;AACA;EACI;EACA;EACA;EACA;EACA;EACA;EACA;;;AAGR;EACI;EACA;;;AAEJ;EACI;EACA;;;AAEJ;EACI;EACA;;;ACnGJ;EACI;EACA;EACA;EACA;EACA;EACA;;AAEA;EACI;EACA;EACA;EACA;EACA;EACA;;AAGA;EACI;;AACA;EACI;;AAGR;EACI;;AACA;EACI;;;ACnBhB;EACI;EACA;EACA;EACA;EACA,eACI;;;AAKR;EACI;EACA;;;AAGJ;EACI;EACA;;AACA;EACI","file":"main.css"}
\ No newline at end of file
......@@ -4,29 +4,30 @@ class TableView extends EventTarget{
super();
if(!element)
{
if(!element){
throw Error('No element passed');
}
if(!columns){
throw Error('Columns are needed');
}
this.element = element;
this.$element = element;
this.columns = columns;
this.table = element.find('#data_table');
this.datatable = this.table.DataTable({
this.$table = element.find('#data_table');
this.datatable = this.$table.DataTable({
columns: this.columns
});
if(overflow){
this.$overflow_div = $("<div></div>").addClass("overflow_table");
this.element.find('#data_table_filter').after(this.$overflow_div);
this.table.detach();
this.$overflow_div.append(this.table);
this.$element.find('#data_table_filter').after(this.$overflow_div);
this.$table.detach();
this.$overflow_div.append(this.$table);
}
const datatable = this.datatable;
const view = this;
this.table.children("#units_table tbody").on("click", "tr", function(){
this.$table.children("tbody").on("click", "tr", function(){
const index = datatable.row(this).index();
view.$table.children("tbody").children().removeClass("active");
$(datatable.row(this).node()).addClass("active");
view.dispatchEvent(new CustomEvent("row_click", {detail: index}));
})
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment