Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/user favorite tags #266

Merged
merged 5 commits into from
Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added ability to set and use favorite tags
  • Loading branch information
chadweimer committed Jan 31, 2021
commit 745c12e480fe80ab187f12d195c7ec6c62ebcec5
15 changes: 8 additions & 7 deletions static/src/components/tag-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {customElement, property } from '@polymer/decorators';
import { IronAjaxElement } from '@polymer/iron-ajax/iron-ajax.js';
import { PaperTagsInput } from '@cwmr/paper-tags-input/paper-tags-input.js';
import { GompBaseElement } from '../common/gomp-base-element.js';
import { UserSettings } from '../models/models.js';
import '@polymer/iron-ajax/iron-ajax.js';
import '@polymer/iron-icon/iron-icon.js';
import '@polymer/iron-icons/iron-icons.js';
Expand Down Expand Up @@ -52,7 +53,7 @@ export class TagInput extends GompBaseElement {
<input type="hidden" slot="input">
</paper-input-container>

<iron-ajax bubbles="" id="getSuggestedTagsAjax" url="/api/v1/tags" params="{&quot;sort&quot;: &quot;frequency&quot;, &quot;dir&quot;: &quot;desc&quot;, &quot;count&quot;: 12}" on-request="handleGetSuggestedTagsRequest" on-response="handleGetSuggestedTagsResponse"></iron-ajax>
<iron-ajax bubbles="" id="getSettingsAjax" url="/api/v1/users/current/settings" on-request="handleGetSettingsRequest" on-response="handleGetSettingsResponse"></iron-ajax>
`;
}

Expand All @@ -61,15 +62,15 @@ export class TagInput extends GompBaseElement {

protected suggestedTags: string[] = [];

private get getSuggestedTagsAjax(): IronAjaxElement {
return this.$.getSuggestedTagsAjax as IronAjaxElement;
private get getSettingsAjax(): IronAjaxElement {
return this.$.getSettingsAjax as IronAjaxElement;
}
private get tagsElement(): PaperTagsInput {
return this.$.tags as PaperTagsInput;
}

public refresh() {
this.getSuggestedTagsAjax.generateRequest();
this.getSettingsAjax.generateRequest();
}

protected onSuggestedTagClicked(e: {model: {item: string}}) {
Expand All @@ -81,10 +82,10 @@ export class TagInput extends GompBaseElement {
this.splice('suggestedTags', suggestedTagIndex, 1);
}
}
protected handleGetSuggestedTagsRequest() {
protected handleGetSettingsRequest() {
this.suggestedTags = [];
}
protected handleGetSuggestedTagsResponse(e: CustomEvent<{response: string[]}>) {
this.suggestedTags = e.detail.response;
protected handleGetSettingsResponse(e: CustomEvent<{response: UserSettings}>) {
this.suggestedTags = e.detail.response.favoriteTags;
}
}
3 changes: 2 additions & 1 deletion static/src/models/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export interface User {
export interface UserSettings {
userId: string;
homeTitle: string;
homeImageUrl: string;
homeImageUrl: string;
chadweimer marked this conversation as resolved.
Show resolved Hide resolved
favoriteTags: string[];
}

interface RecipeBase {
Expand Down
124 changes: 68 additions & 56 deletions static/src/settings-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import '@polymer/paper-fab/paper-fab.js';
import '@polymer/paper-input/paper-input.js';
import '@polymer/paper-spinner/paper-spinner.js';
import '@cwmr/paper-password-input/paper-password-input.js';
import '@cwmr/paper-tags-input/paper-tags-input.js';
import './shared-styles.js';

@customElement('settings-view')
Expand Down Expand Up @@ -63,55 +64,59 @@ export class SettingsView extends GompBaseElement {
}
@media screen and (max-width: 600px) {
}
</style>
<div class="container">
<paper-card>
<div class="card-content">
<h3>Security Settings</h3>
<paper-input label="Username" value="[[currentUser.username]]" always-float-label="" disabled=""></paper-input>
<paper-input label="Access Level" value="[[currentUser.accessLevel]]" always-float-label="" disabled=""></paper-input>
<paper-password-input label="Current Password" value="{{currentPassword}}" always-float-label=""></paper-password-input>
<paper-password-input label="New Password" value="{{newPassword}}" always-float-label=""></paper-password-input>
<paper-password-input label="Confirm Password" value="{{repeatPassword}}" always-float-label=""></paper-password-input>
</div>
<div class="card-actions">
<paper-button on-click="onUpdatePasswordClicked">
<iron-icon icon="icons:lock-outline"></iron-icon>
<span>Update Password</span>
<paper-button>
</div>
</paper-card>
</div>
<div class="container">
<paper-card>
<div class="card-content">
<h3>Home Settings</h3>
<paper-input label="Title" always-float-label="" value="{{userSettings.homeTitle}}">
<paper-icon-button slot="suffix" icon="icons:save" on-click="onSaveButtonClicked"></paper-icon-button>
</paper-input>
<form id="homeImageForm" enctype="multipart/form-data">
<paper-input-container always-float-label="">
<label slot="label">Image</label>
<iron-input slot="input">
<input id="homeImageFile" name="file_content" type="file" accept=".jpg,.jpeg,.png">
</iron-input>
<paper-icon-button slot="suffix" icon="icons:file-upload" on-click="onUploadButtonClicked"></paper-icon-button>
</paper-input-container>
</form>
<img alt="Home Image" src="[[userSettings.homeImageUrl]]" class="responsive" hidden\$="[[!userSettings.homeImageUrl]]">
</div>
</paper-card>
</div>
<paper-dialog id="uploadingDialog" with-backdrop="">
<h3><paper-spinner active=""></paper-spinner>Uploading</h3>
</paper-dialog>

<a href="/create"><paper-fab icon="icons:add" class="green"></paper-fab></a>

<iron-ajax bubbles="" id="putPasswordAjax" url="/api/v1/users/current/password" method="PUT" on-response="handlePutPasswordResponse" on-error="handlePutPasswordError"></iron-ajax>
<iron-ajax bubbles="" id="getSettingsAjax" url="/api/v1/users/current/settings" on-response="handleGetSettingsResponse"></iron-ajax>
<iron-ajax bubbles="" id="putSettingsAjax" url="/api/v1/users/current/settings" method="PUT" on-response="handlePutSettingsResponse" on-error="handlePutSettingsError"></iron-ajax>
<iron-ajax bubbles="" id="postImageAjax" url="/api/v1/uploads" method="POST" on-request="handlePostImageRequest" on-response="handlePostImageResponse" on-error="handlePostImageError"></iron-ajax>
</style>
<div class="container">
<paper-card>
<div class="card-content">
<h3>Security Settings</h3>
<paper-input label="Username" value="[[currentUser.username]]" always-float-label="" disabled=""></paper-input>
<paper-input label="Access Level" value="[[currentUser.accessLevel]]" always-float-label="" disabled=""></paper-input>
<paper-password-input label="Current Password" value="{{currentPassword}}" always-float-label=""></paper-password-input>
<paper-password-input label="New Password" value="{{newPassword}}" always-float-label=""></paper-password-input>
<paper-password-input label="Confirm Password" value="{{repeatPassword}}" always-float-label=""></paper-password-input>
</div>
<div class="card-actions">
<paper-button on-click="onUpdatePasswordClicked">
<iron-icon icon="icons:lock-outline"></iron-icon>
<span>Update Password</span>
<paper-button>
</div>
</paper-card>
</div>
<div class="container">
<paper-card>
<div class="card-content">
<h3>Settings</h3>
<paper-tags-input id="tags" label="Favorite Tags" tags="{{userSettings.favoriteTags}}"></paper-tags-input>
<paper-input label="Home Title" always-float-label="" value="{{userSettings.homeTitle}}"></paper-input>
<form id="homeImageForm" enctype="multipart/form-data">
<paper-input-container always-float-label="">
<label slot="label">Home Image</label>
<iron-input slot="input">
<input id="homeImageFile" name="file_content" type="file" accept=".jpg,.jpeg,.png">
</iron-input>
</paper-input-container>
</form>
<img alt="Home Image" src="[[userSettings.homeImageUrl]]" class="responsive" hidden\$="[[!userSettings.homeImageUrl]]">
</div>
<div class="card-actions">
<paper-button on-click="onSaveButtonClicked">
<iron-icon icon="icons:save"></iron-icon>
<span>Save</span>
<paper-button>
</div>
</paper-card>
</div>
<paper-dialog id="uploadingDialog" with-backdrop="">
<h3><paper-spinner active=""></paper-spinner>Uploading</h3>
</paper-dialog>

<a href="/create"><paper-fab icon="icons:add" class="green"></paper-fab></a>

<iron-ajax bubbles="" id="putPasswordAjax" url="/api/v1/users/current/password" method="PUT" on-response="handlePutPasswordResponse" on-error="handlePutPasswordError"></iron-ajax>
<iron-ajax bubbles="" id="getSettingsAjax" url="/api/v1/users/current/settings" on-response="handleGetSettingsResponse"></iron-ajax>
<iron-ajax bubbles="" id="putSettingsAjax" url="/api/v1/users/current/settings" method="PUT" on-response="handlePutSettingsResponse" on-error="handlePutSettingsError"></iron-ajax>
<iron-ajax bubbles="" id="postImageAjax" url="/api/v1/uploads" method="POST" on-request="handlePostImageRequest" on-response="handlePostImageResponse" on-error="handlePostImageError"></iron-ajax>
`;
}

Expand Down Expand Up @@ -167,12 +172,14 @@ export class SettingsView extends GompBaseElement {
this.putPasswordAjax.generateRequest();
}
protected onSaveButtonClicked() {
this.putSettingsAjax.body = JSON.stringify(this.userSettings) as any;
this.putSettingsAjax.generateRequest();
}
protected onUploadButtonClicked() {
this.postImageAjax.body = new FormData(this.homeImageForm);
this.postImageAjax.generateRequest();
// If there's no image to upload, go directly to saving
if (!this.homeImageFile.value) {
this.saveSettings();
} else {
// We start by uploading the image, after which the rest of the settings will be saved
this.postImageAjax.body = new FormData(this.homeImageForm);
this.postImageAjax.generateRequest();
}
}

protected isActiveChanged(isActive: boolean) {
Expand Down Expand Up @@ -212,13 +219,18 @@ export class SettingsView extends GompBaseElement {

const location = req.xhr.getResponseHeader('Location');
this.userSettings.homeImageUrl = location;
this.onSaveButtonClicked();
this.saveSettings();
}
protected handlePostImageError() {
this.uploadingDialog.close();
this.showToast('Upload failed!');
}

private saveSettings() {
this.putSettingsAjax.body = JSON.stringify(this.userSettings) as any;
this.putSettingsAjax.generateRequest();
}

protected refresh() {
this.getSettingsAjax.generateRequest();
}
Expand Down