Skip to content

Commit

Permalink
CLDR-16499 CLA: refactor client code out per code review
Browse files Browse the repository at this point in the history
  • Loading branch information
srl295 committed May 30, 2024
1 parent 2ea59c6 commit 3c6b88c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 38 deletions.
58 changes: 58 additions & 0 deletions tools/cldr-apps/js/src/esm/cldrCla.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import * as cldrClient from "../esm/cldrClient.mjs";
import * as cldrNotify from "../esm/cldrNotify.mjs";
/**
* see ClaSignature.java
* @typedef {Object} ClaSignature
* @property {boolean} corporate true if a corporate signature
* @property {string} email
* @property {string} employer
* @property {string} name
* @property {boolean} unauthorized true if cla load failed
* @property {boolean} readonly true if cla may not be modified
* @property {boolean} signed true if signed, always true when returned from getCla()
*/

/** @return {ClaSignature} signed cla if present otherwise null if not accessible */
export async function getCla() {
try {
const client = await cldrClient.getClient();
const { body } = await client.apis.user.getCla();
return body;
} catch (e) {
if (e.statusCode === 401) {
return { unauthorized: true };
} else if (e.statusCode === 404) {
return { signed: false };
} else {
cldrNotify.exception(e, `trying to load CLA`);
throw e;
}
}
}

/**
* Attempt to sign.
* @throws {statusCode: 423} if the CLA may not be modified
* @throws {statusCode: 406} if there is an imput validation error
* @param {ClaSignature} cla
* @returns nothing if successful
*/
export async function signCla(cla) {
const client = await cldrClient.getClient();
const result = await client.apis.user.signCla(
{},
{
requestBody: cla,
}
);
}

/**
* Attempt to revoke.
* @throws {statusCode: 423} if the CLA may not be modified
* @throws {statusCode: 404} if the CLA was never signed
*/
export async function revokeCla() {
const client = await cldrClient.getClient();
const result = await client.apis.user.revokeCla();
}
3 changes: 2 additions & 1 deletion tools/cldr-apps/js/src/views/MainMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<li>
<ul>
<li><a href="#account///">Account Settings</a></li>
<!-- <li><a href="#cla///">Sign CLA</a></li> -->
<li v-if="showClaMenu"><a href="#cla///">Sign CLA</a></li>
</ul>
</li>
<li v-if="!isAdmin && !accountLocked">
Expand Down Expand Up @@ -139,6 +139,7 @@ export default {
recentActivityUrl: null,
uploadXmlUrl: null,
userId: 0,
showClaMenu: false, // off by default, see CLDR-16499
};
},
Expand Down
55 changes: 18 additions & 37 deletions tools/cldr-apps/js/src/views/SignCla.vue
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@
</template>

<script setup>
import * as cldrStatus from "../esm/cldrStatus.mjs";
import * as cldrClient from "../esm/cldrClient.mjs";
import * as cldrCla from "../esm/cldrCla.mjs";
import { marked } from "../esm/cldrMarked.mjs";
import * as cldrNotify from "../esm/cldrNotify.mjs";
import * as cldrStatus from "../esm/cldrStatus.mjs";
import { ref } from "vue";
import { marked } from "../esm/cldrMarked.mjs";
import claMd from "../md/cla.md";
const claHtml = marked(claMd);
Expand All @@ -121,27 +121,15 @@ const radioStyle = {
async function loadData() {
if (!user) return;
loading.value = true;
const client = await cldrClient.getClient();
try {
const { body } = await client.apis.user.getCla();
const { corporate, email, employer, name, readonly, signed } = body;
readonlyCla.value = readonly;
userName.value = name;
userEmail.value = email;
userEmployer.value = employer;
userSign.value = corporate ? 1 : 2;
} catch (e) {
loading.value = false;
if (e.statusCode === 401) {
return; // unauthorized, nothing to do
} else if (e.statusCode === 404) {
// not signed
return;
} else {
throw e;
}
}
const { corporate, email, employer, name, readonly, unauthorized, signed } =
await cldrCla.getCla();
loading.value = false;
if (unauthorized || !signed) return;
readonlyCla.value = readonly;
userName.value = name;
userEmail.value = email;
userEmployer.value = employer;
userSign.value = corporate ? 1 : 2;
}
// load the existing signing data
Expand All @@ -153,18 +141,12 @@ loadData().then(
async function sign() {
loading.value = true;
try {
const client = await cldrClient.getClient();
const result = await client.apis.user.signCla(
{},
{
requestBody: {
email: userEmail.value, // unwrap refs
name: userName.value,
employer: userEmployer.value,
corporate: userSign == 1,
},
}
);
await cldrCla.signCla({
email: userEmail.value, // unwrap refs
name: userName.value,
employer: userEmployer.value,
corporate: userSign == 1,
});
user.claSigned = true; // update global user obj
needCla.value = false;
cldrNotify.open(
Expand Down Expand Up @@ -196,8 +178,7 @@ async function sign() {
async function revoke() {
loading.value = true;
try {
const client = await cldrClient.getClient();
const result = await client.apis.user.revokeCla();
await cldrCla.revokeCla();
needCla.value = true;
user.claSigned = false; // update global user obj
cldrNotify.open(`CLA Revoked`, `The CLA has been revoked.`);
Expand Down

0 comments on commit 3c6b88c

Please sign in to comment.