Skip to content

Commit

Permalink
Merge pull request #10 from vincevd1/remove_button
Browse files Browse the repository at this point in the history
added remove button
  • Loading branch information
vincevd1 authored May 23, 2024
2 parents 27d51a9 + 49a1ff9 commit 8843193
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 5 deletions.
8 changes: 8 additions & 0 deletions client/src/components/anytimer.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@
margin-bottom: 1em;
}

.delete-button {
background-color: var(--cancel-red);
}

.delete-button:hover {
background-color: var(--cancel-red-highlight);
}

@media (max-width: 1100px) {
.anytimer, .anytimer-info {
flex-direction: column;
Expand Down
48 changes: 48 additions & 0 deletions client/src/components/anytimer.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,24 @@ export default function AnyTimerComponent({ AnyTimer, direction, type, state, re
});
}

function deleteAny() {
APIService.delete(
APIBase.BACKEND,
`/api/anytimers/confirmed/${AnyTimer.id}/delete/
`
).then(() => {
if (amount == 1) {
remove(AnyTimer.id)
} else {
setAmount(amount - 1);
}
notifications.notify("Successfully deleted anytimer!");
})
.catch(() => {
notifications.notify("Something went wrong while trying to delete anytimer")
});
}

async function postCompleteAny(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
// THIS TOOK ME 3 HOURS TO FIGURE OUT AND IT DOES NOT EVEN LOOK THAT COMPLICATED
Expand Down Expand Up @@ -211,6 +229,36 @@ export default function AnyTimerComponent({ AnyTimer, direction, type, state, re
</button>
)
}
{
type == 'confirmed' && direction == 'outgoing' && state == "unused" && (
<Popup
title="Are you sure you want to delete this anytimer?"
button={
<button className="anytimer-button delete-button">
DELETE
</button>
}>
{
close =>
<>
<div className="button-wrapper">
<button className="confirm-button confirmation-button" onClick={
() => {
close();
deleteAny();
}
}>
CONFIRM
</button>
<button className='cancel-button confirmation-button' onClick={close}>
CANCEL
</button>
</div>
</>
}
</Popup>
)
}
{
type == 'confirmed' && direction == 'incoming' && state == "used" && (
<Popup title={"Complete anytimer from " + AnyTimer.owner_name} button={
Expand Down
27 changes: 22 additions & 5 deletions client/src/services/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class _APIService {
const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const values = new Uint32Array(48);
window.crypto.getRandomValues(values);

let code_challenge = '';
for (let i = 0; i < 48; i++) {
code_challenge += charset[values[i] % charset.length];
Expand All @@ -60,13 +60,13 @@ class _APIService {
const form: FormData = new FormData();
const code_challenge = localStorage.getItem('code_challenge');

if(code_challenge) {
if (code_challenge) {
form.append('grant_type', 'authorization_code')
form.append('client_id', this.client_id)
form.append('code', code)
form.append('code_verifier', code_challenge)
form.append('redirect_uri', this.redirect_uri)

axios.postForm<Credentials>(`${this.concrexit_uri}${this.token_path}`, form)
.then(res => {
localStorage.removeItem('code_challenge')
Expand All @@ -87,7 +87,7 @@ class _APIService {
form.append('grant_type', 'refresh_token')
form.append('client_id', this.client_id)
form.append('refresh_token', refresh_token)

axios.postForm<Credentials>(`${this.concrexit_uri}${this.token_path}`, form)
.then(res => {
resolve(res.data)
Expand All @@ -102,7 +102,7 @@ class _APIService {
return new Promise((resolve, reject) => {
// console.info("GET")
const base_uri = base == APIBase.CONCREXIT ? this.concrexit_uri : this.backend_uri;

axios.get(`${base_uri}${path}`, {
headers: {
'Authorization': `Bearer ${User.getAccessToken}`,
Expand Down Expand Up @@ -131,6 +131,23 @@ class _APIService {
})
})
}

delete<T>(base: APIBase, path: string): Promise<T> {
return new Promise((resolve, reject) => {
// console.info("DELETE")
const base_uri = base == APIBase.CONCREXIT ? this.concrexit_uri : this.backend_uri;

axios.delete(`${base_uri}${path}`, {
headers: {
'Authorization': `Bearer ${User.getAccessToken}`,
},
}).then(res => {
resolve(res.data)
}).catch(err => {
reject(err);
})
})
}
}

const APIService = new _APIService(
Expand Down
5 changes: 5 additions & 0 deletions server/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
views.use_anytimer,
name="use_anytimer",
),
path(
"anytimers/confirmed/<anytimer_id>/delete/",
views.remove_anytimer,
name="remove_anytimer",
),
path("proofs/<anytimer_id>/", views.fetch_proof, name="fetch_proof"),
path("proofs/<anytimer_id>/file/", views.proof_sendfile, name="proof_sendfile"),
]
9 changes: 9 additions & 0 deletions server/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ def revoke_request(request, request_id):
anytimerrequest.delete()
return Response(status=200)

@api_view(["DELETE"])
def remove_anytimer(request, anytimer_id):
anytimer = AnyTimer.objects.get(owner_id=request.user_id, id=anytimer_id)
if anytimer.amount > 1:
anytimer.amount -= 1
anytimer.save()
else:
anytimer.delete()
return Response(status=200)

@api_view(["GET"])
def fetch_requests(request, direction):
Expand Down

0 comments on commit 8843193

Please sign in to comment.