Populating the user form
This commit is contained in:
		| @ -20,6 +20,7 @@ type templateData struct { | |||||||
| 	API             string | 	API             string | ||||||
| 	CSSVersion      string | 	CSSVersion      string | ||||||
| 	IsAuthenticated int | 	IsAuthenticated int | ||||||
|  | 	UserID          int | ||||||
| 	StripeSecretKey string | 	StripeSecretKey string | ||||||
| 	StripePubKey    string | 	StripePubKey    string | ||||||
| } | } | ||||||
| @ -43,6 +44,7 @@ func (app *application) addDefaultData(td *templateData, r *http.Request) *templ | |||||||
|  |  | ||||||
| 	if app.Session.Exists(r.Context(), "userID") { | 	if app.Session.Exists(r.Context(), "userID") { | ||||||
| 		td.IsAuthenticated = 1 | 		td.IsAuthenticated = 1 | ||||||
|  | 		td.UserID = app.Session.GetInt(r.Context(), "userID") | ||||||
| 	} else { | 	} else { | ||||||
| 		td.IsAuthenticated = 0 | 		td.IsAuthenticated = 0 | ||||||
| 	} | 	} | ||||||
|  | |||||||
| @ -16,6 +16,7 @@ Admin User | |||||||
|         <label for="first_name" class="form-label">First Name</label> |         <label for="first_name" class="form-label">First Name</label> | ||||||
|         <input type="text" |         <input type="text" | ||||||
|                name="first_name" |                name="first_name" | ||||||
|  |                id="first_name" | ||||||
|                class="form-control" |                class="form-control" | ||||||
|                required="" |                required="" | ||||||
|                autocomplete="first_name-new"> |                autocomplete="first_name-new"> | ||||||
| @ -24,6 +25,7 @@ Admin User | |||||||
|         <label for="last_name" class="form-label">Last Name</label> |         <label for="last_name" class="form-label">Last Name</label> | ||||||
|         <input type="text" |         <input type="text" | ||||||
|                name="last_name" |                name="last_name" | ||||||
|  |                id="last_name" | ||||||
|                class="form-control" |                class="form-control" | ||||||
|                required="" |                required="" | ||||||
|                autocomplete="last_name-new"> |                autocomplete="last_name-new"> | ||||||
| @ -32,6 +34,7 @@ Admin User | |||||||
|         <label for="email" class="form-label">Email</label> |         <label for="email" class="form-label">Email</label> | ||||||
|         <input type="text" |         <input type="text" | ||||||
|                name="email" |                name="email" | ||||||
|  |                id="email" | ||||||
|                class="form-control" |                class="form-control" | ||||||
|                required="" |                required="" | ||||||
|                autocomplete="email-new"> |                autocomplete="email-new"> | ||||||
| @ -40,6 +43,7 @@ Admin User | |||||||
|         <label for="password" class="form-label">Password</label> |         <label for="password" class="form-label">Password</label> | ||||||
|         <input type="text" |         <input type="text" | ||||||
|                name="password" |                name="password" | ||||||
|  |                id="password" | ||||||
|                class="form-control" |                class="form-control" | ||||||
|                autocomplete="password-new"> |                autocomplete="password-new"> | ||||||
|     </div> |     </div> | ||||||
| @ -47,6 +51,7 @@ Admin User | |||||||
|         <label for="verify_password" class="form-label">Verify Password</label> |         <label for="verify_password" class="form-label">Verify Password</label> | ||||||
|         <input type="text" |         <input type="text" | ||||||
|                name="verify_password" |                name="verify_password" | ||||||
|  |                id="verify_password" | ||||||
|                class="form-control" |                class="form-control" | ||||||
|                autocomplete="veryfy_password-new"> |                autocomplete="veryfy_password-new"> | ||||||
|     </div> |     </div> | ||||||
| @ -56,14 +61,15 @@ Admin User | |||||||
|         <a href="javascript:void(0)" class="btn btn-warning" id="cancelBtn">Cancel</a> |         <a href="javascript:void(0)" class="btn btn-warning" id="cancelBtn">Cancel</a> | ||||||
|     </div> |     </div> | ||||||
|     <div class="float-end"> |     <div class="float-end"> | ||||||
|         <a href="javascript:void(0)" class="btn btn-danger" id="deleteBtn">Delete</a> |         <a href="javascript:void(0)" class="btn btn-danger d-none" id="deleteBtn">Delete</a> | ||||||
|     </div> |     </div> | ||||||
|     <div class="clearfix"></div> |     <div class="clearfix"></div> | ||||||
| </form> | </form> | ||||||
| {{ end }} | {{ end }} | ||||||
| {{ define "js" }} | {{ define "js" }} | ||||||
|  | <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script> | ||||||
| <script type="module"> | <script type="module"> | ||||||
| import {showUser} from "/static/js/users.js" | import {showUser} from "/static/js/users.js" | ||||||
| showUser({{.API}}); | showUser({{.API}}, {{.UserID}}); | ||||||
| </script> | </script> | ||||||
| {{ end }} | {{ end }} | ||||||
|  | |||||||
| @ -34,10 +34,18 @@ export function showUsers(api) { | |||||||
|         }); |         }); | ||||||
| } | } | ||||||
|  |  | ||||||
| export function showUser(api) { | export function showUser(api, userID) { | ||||||
|     const token = localStorage.getItem("token"); |     const token = localStorage.getItem("token"); | ||||||
|     let id = window.location.pathname.split("/").pop(); |     let id = window.location.pathname.split("/").pop(); | ||||||
|  |     let delBtn = document.getElementById("deleteBtn"); | ||||||
|  |  | ||||||
|  |     if (id === "0") { | ||||||
|  |         return | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     if (userID !== parseInt(id, 10)) { | ||||||
|  |         delBtn.classList.remove("d-none") | ||||||
|  |     } | ||||||
|     const requestOptions = { |     const requestOptions = { | ||||||
|         method: 'post', |         method: 'post', | ||||||
|         headers: { |         headers: { | ||||||
| @ -51,6 +59,23 @@ export function showUser(api) { | |||||||
|         .then(response => response.json()) |         .then(response => response.json()) | ||||||
|         .then(function (data) { |         .then(function (data) { | ||||||
|             console.log(data); |             console.log(data); | ||||||
|  |             document.getElementById("first_name").value = data.first_name; | ||||||
|  |             document.getElementById("last_name").value = data.last_name; | ||||||
|  |             document.getElementById("email").value = data.email; | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|  |     delBtn.addEventListener("click", function () { | ||||||
|  |         Swal.fire({ | ||||||
|  |             title: "Are you sure?", | ||||||
|  |             text: "You won't be able to undo this!", | ||||||
|  |             icon: "warning", | ||||||
|  |             showCancelButton: true, | ||||||
|  |             confirmButtonColor: "#3085d6", | ||||||
|  |             cancelButtonColor: "#d33", | ||||||
|  |             confirmButtonText: 'Delete User', | ||||||
|  |         }).then((result) => { | ||||||
|  |             console.log("would delete user id", id); | ||||||
|  |         }); | ||||||
|  |     }) | ||||||
|  |  | ||||||
| } | } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user