Skip to content

Commit

Permalink
fix: default value for dates (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ad96el authored Jun 28, 2024
1 parent 63f406f commit c66b53c
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 33 deletions.
7 changes: 3 additions & 4 deletions frontend/src/api/credential.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getBackendUrl } from '../utils/utils';
import { getBackendUrl } from '../utils/utils'
import { getAxiosClient } from './dataProvider'
import { InjectedWindowProvider } from '@kiltprotocol/kilt-extension-api'

Expand All @@ -7,10 +7,9 @@ export async function fetchCredential(extension: InjectedWindowProvider, session

const client = await getAxiosClient()

const credentialUrl = `${apiUrl}/credential`;

const getTermsResponse = await client.post(`${credentialUrl}/terms/${sessionId}/${attestationId}`, sessionId);
const credentialUrl = `${apiUrl}/credential`

const getTermsResponse = await client.post(`${credentialUrl}/terms/${sessionId}/${attestationId}`, sessionId)

const getCredentialRequestFromExtension = await new Promise((resolve, reject) => {
try {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/api/dataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ async function httpClient(url: string, options: { [key: string]: any } = {}) {
return fetchUtils.fetchJson(url, options)
}

const apiUrl = getBackendUrl();
const apiUrl = getBackendUrl()
export const dataProvider = simpleRestProvider(apiUrl, httpClient)
24 changes: 11 additions & 13 deletions frontend/src/api/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import axios from "axios";

import axios from 'axios'

export async function getEndpoints(): Promise<Array<string>> {
const origin = window.location.origin
const backendUrl = `${origin}/api/v1`
const endpointUrl = `${backendUrl}/endpoints`
const response = await axios.get<Array<string>>(endpointUrl)

const origin = window.location.origin;
const backendUrl = `${origin}/api/v1`
const endpointUrl = `${backendUrl}/endpoints`;
const response = await axios.get<Array<string>>(endpointUrl)

if (response.status !== 200) {
throw new Error("Could not fetch endpoints")
}
if (response.status !== 200) {
throw new Error('Could not fetch endpoints')
}

const endpoints = response.data;
endpoints.push(backendUrl)
const endpoints = response.data
endpoints.push(backendUrl)

return endpoints
return endpoints
}
4 changes: 2 additions & 2 deletions frontend/src/api/session.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getBackendUrl } from '../utils/utils';
import { getBackendUrl } from '../utils/utils'
import { getAxiosClient } from './dataProvider'

import { InjectedWindowProvider, PubSubSessionV1, PubSubSessionV2 } from '@kiltprotocol/kilt-extension-api'
Expand All @@ -9,7 +9,7 @@ export async function getSession(provider: InjectedWindowProvider): Promise<PubS
}

const apiUrl = getBackendUrl()
const challengeUrl = `${apiUrl}/challenge`;
const challengeUrl = `${apiUrl}/challenge`

const client = await getAxiosClient()

Expand Down
22 changes: 18 additions & 4 deletions frontend/src/components/AttestationAdd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@ import TextField from '@mui/material/TextField'
import { ICType, IClaimContents, Claim, DidUri, Credential as KiltCredential } from '@kiltprotocol/sdk-js'
import { useState } from 'react'
import ReactJson, { InteractionProps } from 'react-json-view'

import { fetchCType } from '../utils/utils'

function getFormattedDate(): string {
const date = new Date()
const day = date.getDate().toString().padStart(2, '0')
const month = (date.getMonth() + 1).toString().padStart(2, '0')
const year = date.getFullYear().toString()
return `${year}-${month}-${day}`
}

//TODO:fix "It currently only works with ctypes with a height of 1."
function getDefaultEntryForType({ type }: { type: string }) {
function getDefaultEntryForType({ type, format }: { type: string; format?: string }) {
if (format === 'date' && type === 'string') {
return getFormattedDate()
}
if (type === 'string') {
return ''
}
Expand Down Expand Up @@ -37,7 +49,7 @@ export default function AttestationCreate() {
const ctypeDetails = await fetchCType(fmtCtype as unknown as `kilt:ctype:0x${string}`)
const claimContent: Record<string, any> = {}
Object.entries(ctypeDetails.cType.properties).map(
([key, type]) => (claimContent[key] = getDefaultEntryForType(type as { type: string })),
([key, type]) => (claimContent[key] = getDefaultEntryForType(type as { type: string; format?: string })),
)
setCtypeDetails(ctypeDetails.cType)
setClaimContent(claimContent)
Expand Down Expand Up @@ -72,15 +84,17 @@ export default function AttestationCreate() {
return KiltCredential.fromClaim(claim)
} catch (e) {
console.error(e)
notify('Ctype Verification failed')
notify('Ctype Verification failed', { type: 'error' })

throw e
}
}

//Elements
const CustomToolBar = () => {
return (
<Toolbar>
<SaveButton alwaysEnable={claimer !== '' && claimContent !== undefined} label="Save" />
<SaveButton alwaysEnable={claimer !== '' && claimContent !== undefined} label="Save" type="button" />
</Toolbar>
)
}
Expand Down
18 changes: 9 additions & 9 deletions frontend/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ export function isUserAdmin() {
}

export function storeEndpoints(endpoints: Array<string>) {
const authorizeUrl = endpoints[0];
const kiltEndpoint = endpoints[1];
const backendUrl = endpoints[2];
const authorizeUrl = endpoints[0]
const kiltEndpoint = endpoints[1]
const backendUrl = endpoints[2]

localStorage.setItem("authorizeUrl", authorizeUrl);
localStorage.setItem("kiltEndpoint", kiltEndpoint);
localStorage.setItem("backendUrl", backendUrl);
localStorage.setItem('authorizeUrl', authorizeUrl)
localStorage.setItem('kiltEndpoint', kiltEndpoint)
localStorage.setItem('backendUrl', backendUrl)
}

export function getBackendUrl(): string {
return localStorage.getItem("backendUrl") || ""
return localStorage.getItem('backendUrl') || ''
}

export function getKiltEndpoint(): string {
return localStorage.getItem("kiltEndpoint") || ""
return localStorage.getItem('kiltEndpoint') || ''
}

export function getAuthorizeUrl(): string {
return localStorage.getItem("authorizeUrl") || ""
return localStorage.getItem('authorizeUrl') || ''
}

0 comments on commit c66b53c

Please sign in to comment.