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

feat(soffit-form): submit form #693

Merged
merged 26 commits into from
Jan 22, 2023
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
00e51eb
feat(soffit-pwa): page form
njfamirm Jan 19, 2023
71e788c
feat(soffit-pwa): lottery code registration form
njfamirm Jan 19, 2023
767bda7
feat(soffit-pwa): button style
njfamirm Jan 19, 2023
6ca313b
feat(soffit-pwa): activityType on page form
njfamirm Jan 19, 2023
f278802
feat(soffit-pwa): improve form styles
njfamirm Jan 19, 2023
4bc52af
fix(soffit-pwa): page form styles
njfamirm Jan 20, 2023
dc20082
feat(soffit-form): submit form
njfamirm Jan 20, 2023
aa3b294
feat(soffit-form): load api server info from config file
njfamirm Jan 20, 2023
40ce349
refactor(soffit-pwa): send form to server
njfamirm Jan 22, 2023
e99f2e5
refactor(soffit-pwa): remove process from config!
njfamirm Jan 22, 2023
af3acd7
refactor(soffit-pwa): renderRoot for select input
njfamirm Jan 22, 2023
f0d61f8
fix(soffit-pwa): app config and temp service worker
alimd Jan 22, 2023
4894547
fix(pwa): add blank service-worker.js for debug
alimd Jan 22, 2023
e78fc58
feat(soffit-pwa): load config
alimd Jan 22, 2023
405a0e7
fix(text-field): elevation bug
alimd Jan 22, 2023
61903f8
feat(pwa/esbuild): pretty mode
alimd Jan 22, 2023
3d0f8d5
feat(soffit-pwa): componentize fieldset
njfamirm Jan 22, 2023
de06e45
feat(ui-kit/text-field): value prop
alimd Jan 22, 2023
60413d1
fix(soffit-pwa): remove submit at firstUpdated
njfamirm Jan 22, 2023
be84c63
refactor(soffit-pwa): radio-group
alimd Jan 22, 2023
fdbc609
feat(soffit-pwa): radio-group value
alimd Jan 22, 2023
5723c6a
feat(surface,icon-box): disabled animation
alimd Jan 22, 2023
37d9803
feat(fields): name property
alimd Jan 22, 2023
1a81dcc
feat(soffit-pwa): form data gathering and submit process
alimd Jan 22, 2023
9294c58
fix(soffit-pwa): build issue
alimd Jan 22, 2023
f050d6d
feat(soffit/lottery-box): success color
alimd Jan 22, 2023
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Desktop.ini
*.d.ts
*.js
!*.config.js
!**/res/*.js
dist
build

Expand Down
4 changes: 3 additions & 1 deletion ui/demo-pwa/esbuild.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ const srcFilename = 'alwatr-pwa';
const cleanMode = process.argv.includes('--clean');
const watchMode = process.argv.includes('--watch');
const debugMode = process.argv.includes('--debug');
const prettyMode = process.argv.includes('--pretty');

logger.logOther(banner);

logger.logProperty('cleanMode', cleanMode);
logger.logProperty('watchMode', watchMode);
logger.logProperty('debugMode', debugMode);
logger.logProperty('prettyMode', prettyMode);

if (cleanMode) {
logger.logMethod('cleanDist');
Expand All @@ -44,7 +46,7 @@ const esbuildContext = await esbuild.context({
format: 'esm',
conditions: debugMode ? ['development'] : undefined,

minify: true,
minify: !prettyMode,
treeShaking: true,
sourcemap: true,
sourcesContent: debugMode,
Expand Down
1 change: 1 addition & 0 deletions ui/demo-pwa/res/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Service worker not build in development mode.')
4 changes: 2 additions & 2 deletions ui/ui-kit/src/button/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import {AlwatrSurface} from '../card/surface.js';

declare global {
interface HTMLElementTagNameMap {
'alwatr-button': AlwatrTextField;
'alwatr-button': AlwatrButton;
}
}

/**
* Alwatr outlined text field.
*/
@customElement('alwatr-button')
export class AlwatrTextField extends AlwatrSurface {
export class AlwatrButton extends AlwatrSurface {
static override styles = [
AlwatrSurface.styles,
css`
Expand Down
2 changes: 1 addition & 1 deletion ui/ui-kit/src/card/icon-box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class AlwatrIconBox extends AlwatrSurface {
:host {
display: block;
padding: 0;
transition-property: color, background-color;
transition-property: color, background-color, opacity;
transition-duration: var(--sys-motion-duration-small);
transition-timing-function: var(--sys-motion-easing-linear);
font-family: var(--sys-typescale-body-small-font-family-name);
Expand Down
1 change: 1 addition & 0 deletions ui/ui-kit/src/card/surface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export class AlwatrSurface extends AlwatrDummyElement {
border-radius: var(--sys-radius-medium);
overflow: hidden;
overflow: clip;
transition: opacity var(--sys-motion-duration-small) var(--sys-motion-easing-linear);
}

:host([outlined]) {
Expand Down
18 changes: 16 additions & 2 deletions ui/ui-kit/src/text-field/text-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export class AlwatrTextField extends AlwatrSurface {
background-color: transparent;
}

:host([stated][outlined]) {
box-shadow: none;
:host([stated][outlined]:hover) {
--_surface-elevation: var(--sys-surface-elevation-0);
}

input {
Expand Down Expand Up @@ -84,6 +84,8 @@ export class AlwatrTextField extends AlwatrSurface {
`,
];

name = this.getAttribute('name') ?? 'unknown';

@property({type: String})
type: InputType = 'text';

Expand All @@ -92,6 +94,18 @@ export class AlwatrTextField extends AlwatrSurface {

inputElement: HTMLInputElement | null = null;

get value(): string {
return this.inputElement?.value ?? '';
}
set value(val: string) {
if (this.inputElement != null) {
this.inputElement.value = val;
}
else {
this.updateComplete.then(() => {this.value = val;});
}
}

override render(): unknown {
this._logger.logMethod('render');
return html`<input type=${this.type} placeholder=${ifDefined(this.placeholder)}></input>`;
Expand Down
1 change: 1 addition & 0 deletions uniquely/soffit-pwa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"devDependencies": {
"@alwatr/element": "^0.28.0",
"@alwatr/ui-kit": "^0.28.0",
"@alwatr/fetch": "^0.28.0",
"@web/dev-server": "^0.1.35",
"@webcomponents/webcomponentsjs": "^2.7.0",
"esbuild": "^0.17.3",
Expand Down
4 changes: 4 additions & 0 deletions uniquely/soffit-pwa/res/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
window.appConfig = {
api: 'http://localhost:8000',
token: 'YOUR_SECRET_TOKEN',
};
1 change: 1 addition & 0 deletions uniquely/soffit-pwa/res/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<title>بازرگانی سافیت</title>

<link rel="stylesheet" href="alwatr-pwa.css" />
<script type="module" src="config.js"></script>
<script type="module" src="alwatr-pwa.js"></script>

<meta name="color-scheme" content="light dark" />
Expand Down
1 change: 1 addition & 0 deletions uniquely/soffit-pwa/res/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Service worker not build in development mode.')
43 changes: 34 additions & 9 deletions uniquely/soffit-pwa/src/lottery-box.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {customElement, AlwatrSmartElement, css, html, property} from '@alwatr/element';
import {customElement, AlwatrSmartElement, css, html, state} from '@alwatr/element';

import '@alwatr/ui-kit/card/icon-box.js';
import './lottery-form.js';
Expand Down Expand Up @@ -26,21 +26,46 @@ export class AlwatrLotteryBox extends AlwatrSmartElement {
}
`;

@property({type: Boolean})
@state()
expanded = false;

@state()
submitted = false;

override render(): unknown {
super.render();
return html`
<alwatr-icon-box .content=${_lotteryContent} elevated="3" ?stated=${!this.expanded} @click=${this._click}>
${this.expanded
? html`<alwatr-lottery-form></alwatr-lottery-form>`
: html`فرم شرکت در قرعه‌کشی میدکس`}
</alwatr-icon-box>
<alwatr-icon-box
.content=${_lotteryContent}
elevated="3"
?stated=${!this.expanded}
?highlight=${!this.expanded && !this.submitted}
@click=${this._click}
>${this._boxContentTemplate()}</alwatr-icon-box>
`;
}

_click(): void {
if (!this.expanded) this.expanded = true;

private _boxContentTemplate(): unknown {
if (this.expanded) {
return html`<alwatr-lottery-form @form-submitted=${this._formSubmitted}></alwatr-lottery-form>`;
}
else if (this.submitted) {
return html`اطلاعات شما با موفقیت ذخیره شد.`;
}
else {
return html`فرم شرکت در قرعه‌کشی میدکس`;
}
}

private _click(): void {
this._logger.logMethod('_click');
if (!this.expanded && !this.submitted) this.expanded = true;
}

private _formSubmitted(): void {
this._logger.logMethod('_formSubmitted');
this.expanded = false;
this.submitted = true;
}
}
69 changes: 48 additions & 21 deletions uniquely/soffit-pwa/src/lottery-form.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import {customElement, AlwatrSmartElement, css, html, map} from '@alwatr/element';
import {customElement, AlwatrSmartElement, css, html, property} from '@alwatr/element';
// import {fetch} from '@alwatr/fetch';

// import {config} from './tech-dep/config.js';

import type {AlwatrTextField} from '@alwatr/ui-kit/text-field/text-field.js';

import '@alwatr/ui-kit/text-field/text-field.js';
import '@alwatr/ui-kit/button/button.js';
import './tech-dep/radio-group.js';

declare global {
interface HTMLElementTagNameMap {
'alwatr-lottery-form': AlwatrLotteryForm;
}
}

const _activityType = ['پخش کننده تایل', 'نصاب تایل', 'فروشنده و مغازه‌دار', 'پیمانکار', 'سازنده', 'سایر'];

/**
* Soffit lottery form element
*/
Expand All @@ -18,6 +23,12 @@ export class AlwatrLotteryForm extends AlwatrSmartElement {
static override styles = css`
:host {
display: block;
transition: opacity var(--sys-motion-duration-small) var(--sys-motion-easing-linear);
}

:host([disabled]) {
pointer-events: none;
opacity: var(--sys-surface-disabled-opacity);
}

alwatr-text-field {
Expand Down Expand Up @@ -72,48 +83,64 @@ export class AlwatrLotteryForm extends AlwatrSmartElement {
}
`;

@property({type: Boolean, reflect: true})
disabled = false;

async submit(): Promise<void> {
const bodyJson = this.getFormData();
this._logger.logMethodArgs('_submit', bodyJson);
this.disabled = true;
await new Promise((resolve) => setTimeout(resolve, 3_000));
this.disabled = false;
this.dispatchEvent(new CustomEvent('form-submitted'));
// return fetch({
// url: config.api + '/',
// token: config.token,
// method: 'PUT',
// bodyJson,
// });
}

getFormData(): Record<string, unknown> {
this._logger.logMethod('_getInputData');
const data: Record<string, string> = {};
for (const inputElement of this.renderRoot.querySelectorAll<AlwatrTextField>(
'alwatr-text-field,alwatr-radio-group',
)) {
data[inputElement.name] = inputElement.value;
}
return data;
}

override render(): unknown {
super.render();
return html`
<alwatr-text-field
id="code"
name="code"
type="text"
outlined
active-outline
stated
placeholder="شماره قرعه‌کشی"
></alwatr-text-field>
<alwatr-text-field
id="name"
name="name"
type="text"
outlined
active-outline
stated
placeholder="نام و نام‌خانوادگی"
></alwatr-text-field>
<alwatr-text-field
id="phone"
name="phone"
type="tel"
outlined
active-outline
stated
placeholder="شماره موبایل"
></alwatr-text-field>
<fieldset>
<legend>نوع فعالیت:</legend>
${this._radioButtonTemplate()}
</fieldset>
<alwatr-button outlined>ارسال فرم</alwatr-button>
<alwatr-radio-group></alwatr-radio-group>
<alwatr-button outlined @click=${this.submit}>ارسال فرم</alwatr-button>
`;
}

protected _radioButtonTemplate(): unknown {
return map(_activityType, (activity, index) => {
const id = 'activityType' + index;
return html`<div>
<input type="radio" id=${id} name="activity" value="${activity}" />
<label for=${id}>${activity}</label>
</div>`;
});
}
}
20 changes: 20 additions & 0 deletions uniquely/soffit-pwa/src/tech-dep/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
declare global {
// eslint-disable-next-line no-var
var appConfig: Record<string, string | number | boolean | undefined> | undefined;

Check failure

Code scanning / ESLint

Require `let` or `const` instead of `var`

Unexpected var, use let or const instead.
}

const debugConfig = localStorage.getItem('DEBUG_CONFIG');
if (debugConfig != null) {
globalThis.appConfig = JSON.parse(debugConfig);
}

function getConfKey<T extends string | number | boolean>(key: string): T {
const val = globalThis.appConfig?.[key];
if (val == null) throw new Error('invalid_app_config', {cause: {key, val}});
return val as T;
}

export const config = {
api: getConfKey<string>('api'),
token: getConfKey<string>('token'),
};
Loading