Skip to content

Commit

Permalink
Conver Organization Compoonents to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
SmitGala committed Feb 7, 2025
1 parent d3791bd commit bcb5bec
Show file tree
Hide file tree
Showing 10 changed files with 415 additions and 209 deletions.
68 changes: 0 additions & 68 deletions app/components/github-account/index.js

This file was deleted.

118 changes: 118 additions & 0 deletions app/components/github-account/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { task } from 'ember-concurrency';
import type IntlService from 'ember-intl/services/intl';

import triggerAnalytics from 'irene/utils/trigger-analytics';
import ENV from 'irene/config/environment';
import type IreneAjaxService from 'irene/services/ajax';
import type OrganizationService from 'irene/services/organization';
import type { AjaxError } from 'irene/services/ajax';
import type UserModel from 'irene/models/user';

type IntegratedUserType = {
avatar_url: string;
created_on: string;
html_url: string;
login: string;
name: string | null;
updated_on: string;
};

type GithubRedirectResponse = {
url: string;
};

export interface GithubAccountSignature {
Args: {
integratedUser: IntegratedUserType | null;
reconnect: boolean;
user: UserModel;
};
Blocks: {
default: [];
};
}

export default class GithubAccountComponent extends Component<GithubAccountSignature> {
@service declare intl: IntlService;
@service declare ajax: IreneAjaxService;
@service('notifications') declare notify: NotificationService;
@service declare organization: OrganizationService;

@tracked showRevokeGithubConfirmBox = false;
@tracked integratedUser: IntegratedUserType | null = null;

tGithubWillBeRevoked: string;
tGithubErrorIntegration: string;

constructor(owner: unknown, args: GithubAccountSignature['Args']) {
super(owner, args);

this.tGithubWillBeRevoked = this.intl.t('githubWillBeRevoked');
this.tGithubErrorIntegration = this.intl.t('githubErrorIntegration');

this.integratedUser = this.args.integratedUser;
}

redirectAPI = task(async () => {
return await this.ajax.request<GithubRedirectResponse>(
`/api/organizations/${this.organization.selected?.id}/github/redirect`
);
});

integrateGithub = task(async () => {
try {
triggerAnalytics(
'feature',
ENV.csb['integrateGithub'] as CsbAnalyticsFeatureData
);

const data = await this.redirectAPI.perform();

window.location.href = data.url;
} catch (error) {
this.notify.error(this.tGithubErrorIntegration);
}
});

removeIntegrationUri = task(async () => {
return await this.ajax.delete(
`/api/organizations/${this.organization.selected?.id}/github`
);
});

removeIntegration = task(async () => {
try {
await this.removeIntegrationUri.perform();

this.notify.success(this.tGithubWillBeRevoked);

this.closeRevokeGithubConfirmBox();

this.integratedUser = null;
} catch (e) {
const err = e as AjaxError;

this.notify.error(err.payload.detail);
}
});

@action
openRevokeGithubConfirmBox() {
this.showRevokeGithubConfirmBox = true;
}

@action
closeRevokeGithubConfirmBox() {
this.showRevokeGithubConfirmBox = false;
}
}

declare module '@glint/environment-ember-loose/registry' {
export default interface Registry {
GithubAccount: typeof GithubAccountComponent;
}
}
Original file line number Diff line number Diff line change
@@ -1,62 +1,91 @@
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import ENV from 'irene/config/environment';
import triggerAnalytics from 'irene/utils/trigger-analytics';
import { task } from 'ember-concurrency';
import lookupValidator from 'ember-changeset-validations';
import Changeset from 'ember-changeset';
import JIRAValidation from '../../validations/jiraintegrate';
import { tracked } from '@glimmer/tracking';
import { Changeset } from 'ember-changeset';
import type { BufferedChangeset } from 'ember-changeset/types';
import type IntlService from 'ember-intl/services/intl';

import ENV from 'irene/config/environment';
import triggerAnalytics from 'irene/utils/trigger-analytics';
import JIRAValidation from 'irene/validations/jiraintegrate';
import type IreneAjaxService from 'irene/services/ajax';
import type OrganizationService from 'irene/services/organization';
import type UserModel from 'irene/models/user';
import type { AjaxError } from 'irene/services/ajax';

type ChangesetBufferProps = BufferedChangeset & {
username: string;
password: string;
host: string;
};

interface JiraCheckResponse {
host: string;
username: string;
}

export interface JiraAccountSignature {
Args: {
user: UserModel;
};
}

export default class JiraAccountComponent extends Component {
@service intl;
@service ajax;
@service organization;
@service('notifications') notify;
export default class JiraAccountComponent extends Component<JiraAccountSignature> {
@service declare intl: IntlService;
@service declare ajax: IreneAjaxService;
@service declare organization: OrganizationService;
@service('notifications') declare notify: NotificationService;

user = null;
user: null = null;
changeset: ChangesetBufferProps;

@tracked jiraHost = '';
@tracked jiraUsername = '';
@tracked jiraPassword = '';
@tracked jiraPOJO = {};
@tracked jiraPOJO: Record<string, unknown> = {};

@tracked isRevokingJIRA = false;
@tracked isIntegratingJIRA = false;

tInValidCredentials = this.intl.t('tInValidCredentials');
tJiraIntegrated = this.intl.t('jiraIntegrated');
tJiraWillBeRevoked = this.intl.t('jiraWillBeRevoked');
tPleaseEnterAllDetails = this.intl.t('pleaseEnterAllDetails');
tPleaseTryAgain = this.intl.t('pleaseTryAgain');
tInValidCredentials: string;
tJiraIntegrated: string;
tJiraWillBeRevoked: string;
tPleaseEnterAllDetails: string;
tPleaseTryAgain: string;

@tracked isJIRAConnected = false;
@tracked connectedHost = '';
@tracked connectedUsername = '';
@tracked showRevokeJIRAConfirmBox = false;

constructor() {
super(...arguments);
constructor(owner: unknown, args: JiraAccountSignature['Args']) {
super(owner, args);

this.tInValidCredentials = this.intl.t('tInValidCredentials');
this.tJiraIntegrated = this.intl.t('jiraIntegrated');
this.tJiraWillBeRevoked = this.intl.t('jiraWillBeRevoked');
this.tPleaseEnterAllDetails = this.intl.t('pleaseEnterAllDetails');
this.tPleaseTryAgain = this.intl.t('pleaseTryAgain');

const jiraPOJO = this.jiraPOJO;

const changeset = new Changeset(
this.changeset = Changeset(
jiraPOJO,
lookupValidator(JIRAValidation),
JIRAValidation
);

this.changeset = changeset;
) as ChangesetBufferProps;

this.checkJIRA.perform();
}

get baseURL() {
return [
'/api/organizations',
this.organization.selected.id,
ENV.endpoints.integrateJira,
this.organization.selected?.id,
ENV.endpoints['integrateJira'],
].join('/');
}

Expand All @@ -66,13 +95,15 @@ export default class JiraAccountComponent extends Component {

checkJIRA = task(async () => {
try {
const data = await this.ajax.request(this.baseURL);
const data = await this.ajax.request<JiraCheckResponse>(this.baseURL);

this.isJIRAConnected = true;
this.connectedHost = data.host;
this.connectedUsername = data.username;
} catch (error) {
if (error.status == 404) {
} catch (err) {
const error = err as AjaxError;

if (error.status === 404) {
this.isJIRAConnected = false;
}
}
Expand Down Expand Up @@ -115,8 +146,14 @@ export default class JiraAccountComponent extends Component {
this.checkJIRA.perform();

this.notify.success(this.tJiraIntegrated);
triggerAnalytics('feature', ENV.csb.integrateJIRA);
} catch (error) {

triggerAnalytics(
'feature',
ENV.csb['integrateJIRA'] as CsbAnalyticsFeatureData
);
} catch (err) {
const error = err as AjaxError;

if (error.payload) {
if (error.payload.host) {
this.notify.error(error.payload.host[0], ENV.notifications);
Expand All @@ -139,3 +176,9 @@ export default class JiraAccountComponent extends Component {
this.showRevokeJIRAConfirmBox = false;
}
}

declare module '@glint/environment-ember-loose/registry' {
export default interface Registry {
JiraAccount: typeof JiraAccountComponent;
}
}
Loading

0 comments on commit bcb5bec

Please sign in to comment.