Skip to content

Commit

Permalink
Merge pull request #77 from chekjun/P2Fancy-cg
Browse files Browse the repository at this point in the history
Fixes for P2Fancy
  • Loading branch information
milidieh authored Jun 18, 2021
2 parents 433ab6a + 672280d commit defcc83
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 340 deletions.
91 changes: 9 additions & 82 deletions src/commons/githubAssessments/GitHubMissionDataUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,91 +265,18 @@ export async function getContentAsString(
* @param metadataString The file contents of the '.metadata' file of a mission repository
*/
function convertMetadataStringToMissionMetadata(metadataString: string) {
const missionMetadata: MissionMetadata = {
coverImage: '',
type: '',
id: '',
title: '',
sourceVersion: 1,
dueDate: new Date(8640000000000000),
reading: '',
webSummary: ''
};
const stringPropsToExtract = ['coverImage', 'type', 'id', 'title', 'reading', 'webSummary'];
const numPropsToExtract = ['sourceVersion'];
const datePropsToExtract = ['dueDate'];

const retVal = parseMetadataProperties<MissionMetadata>(
missionMetadata,
stringPropsToExtract,
numPropsToExtract,
datePropsToExtract,
metadataString
);

return retVal;
try {
return JSON.parse(metadataString) as MissionMetadata;
} catch (err) {
console.error(err);
return {
sourceVersion: 4
} as MissionMetadata;
}
}

function convertMissionMetadataToMetadataString(missionMetadata: MissionMetadata) {
const properties: string[] = [
'title',
'coverImage',
'webSummary',
'dueDate',
'type',
'id',
'sourceVersion',
'reading'
];
const propertyValuePairs = properties.map(property => property + '=' + missionMetadata[property]);
return propertyValuePairs.join('\n');
}

/**
* Converts the contents of a '.metadata' file into an object of type R.
*
* @param propertyContainer The object of which properties will be set
* @param stringProps An array containing the names of properties with string values
* @param numProps An array containing the names of properties with numerical values
* @param dateProps An array containing the names of properties with date values
* @param metadataString The content of the '.metadata' file to be parsed
*/
export function parseMetadataProperties<R>(
propertyContainer: R,
stringProps: string[],
numProps: string[],
dateProps: string[],
metadataString: string
) {
const lines = metadataString.replace(/\r/g, '').split(/\n/);

lines.forEach(line => {
for (let i = 0; i < stringProps.length; i++) {
const propName = stringProps[i];
if (line.startsWith(propName)) {
propertyContainer[propName] = line.substr(propName.length + 1);
return;
}
}

for (let i = 0; i < numProps.length; i++) {
const propName = numProps[i];
if (line.startsWith(propName)) {
propertyContainer[propName] = parseInt(line.substr(propName.length + 1), 10);
return;
}
}

for (let i = 0; i < dateProps.length; i++) {
const propName = dateProps[i];
if (line.startsWith(propName)) {
propertyContainer[propName] = new Date(line.substr(propName.length + 1));
return;
}
}
});

return propertyContainer;
return jsonStringify(missionMetadata);
}

/**
Expand Down
8 changes: 0 additions & 8 deletions src/commons/githubAssessments/GitHubMissionTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,7 @@ export type TaskData = {
* An code representation of a GitHub-hosted mission's '.metadata' file.
*/
export type MissionMetadata = {
coverImage: string;
type: string;
id: string;
title: string;
sourceVersion: number;
dueDate: Date;

reading: string;
webSummary: string;
};

/**
Expand Down
62 changes: 5 additions & 57 deletions src/commons/githubAssessments/__tests__/GitHubMissionDataUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Octokit } from '@octokit/rest';

import { IMCQQuestion } from '../../assessment/AssessmentTypes';
import * as GitHubMissionDataUtils from '../GitHubMissionDataUtils';
import { MissionMetadata, MissionRepoData } from '../GitHubMissionTypes';
import { MissionRepoData } from '../GitHubMissionTypes';

test('getContentAsString correctly gets content and translates from Base64 to utf-8', async () => {
const octokit = new Octokit();
Expand All @@ -23,40 +23,6 @@ test('getContentAsString correctly gets content and translates from Base64 to ut
expect(content).toBe('Hello World!');
});

test('parseMetadataProperties correctly discovers properties', () => {
const missionMetadata = Object.assign({}, dummyMissionMetadata);
const stringPropsToExtract = ['coverImage', 'type', 'id', 'title', 'reading', 'webSummary'];
const numPropsToExtract = ['sourceVersion'];
const datePropsToExtract = ['dueDate'];

const metadataString =
'coverImage=www.somelink.com\n' +
'type=Mission\n' +
'id=M3\n' +
'title=Dummy Mission\n' +
'reading=Textbook Pages 1 to 234763\n' +
'dueDate=December 17, 1995 03:24:00\n' +
'webSummary=no\n' +
'sourceVersion=3';

const retVal = GitHubMissionDataUtils.parseMetadataProperties<MissionMetadata>(
missionMetadata,
stringPropsToExtract,
numPropsToExtract,
datePropsToExtract,
metadataString
);

expect(retVal.coverImage).toBe('www.somelink.com');
expect(retVal.type).toBe('Mission');
expect(retVal.id).toBe('M3');
expect(retVal.title).toBe('Dummy Mission');
expect(retVal.reading).toBe('Textbook Pages 1 to 234763');
expect(retVal.webSummary).toBe('no');
expect(retVal.sourceVersion).toBe(3);
expect(retVal.dueDate).toStrictEqual(new Date('December 17, 1995 03:24:00'));
});

test('getMissionData works properly', async () => {
const missionRepoData: MissionRepoData = {
repoOwner: 'Pain',
Expand All @@ -79,13 +45,9 @@ test('getMissionData works properly', async () => {
// Metadata String
const contentResponse = generateGetContentResponse();
(contentResponse.data as any).content = Buffer.from(
'coverImage=www.somelink.com\n' +
'type=Mission\n' +
'id=M3\n' +
'title=Dummy Mission\n' +
'reading=Textbook Pages 1 to 234763\n' +
'webSummary=no\n' +
'sourceVersion=3',
`{
"sourceVersion": 3
}`,
'utf-8'
).toString('base64');
return contentResponse;
Expand Down Expand Up @@ -183,13 +145,6 @@ test('getMissionData works properly', async () => {
expect(missionData.missionRepoData.repoName).toBe('Peko');

expect(missionData.missionBriefing).toBe('Briefing Content');

expect(missionData.missionMetadata.coverImage).toBe('www.somelink.com');
expect(missionData.missionMetadata.type).toBe('Mission');
expect(missionData.missionMetadata.id).toBe('M3');
expect(missionData.missionMetadata.title).toBe('Dummy Mission');
expect(missionData.missionMetadata.reading).toBe('Textbook Pages 1 to 234763');
expect(missionData.missionMetadata.webSummary).toBe('no');
expect(missionData.missionMetadata.sourceVersion).toBe(3);

expect(missionData.tasksData.length).toBe(2);
Expand Down Expand Up @@ -932,14 +887,7 @@ function generateGetContentResponse() {
}

const dummyMissionMetadata = {
coverImage: 'www.eh',
type: 'mission',
id: 'M2',
title: 'Dummy',
sourceVersion: 1,
dueDate: new Date('December 17, 1996 03:24:00'),
reading: 'none',
webSummary: 'no'
sourceVersion: 1
};

const defaultMissionMetadata = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { InputGroup, Label } from '@blueprintjs/core';
import { DatePicker } from '@blueprintjs/datetime';
import { Label } from '@blueprintjs/core';
import { Variant } from 'js-slang/dist/types';
import React from 'react';

Expand All @@ -14,50 +13,20 @@ export type SideContentMissionEditorProps = {
};

const SideContentMissionEditor: React.FC<SideContentMissionEditorProps> = props => {
const datePicker =
props.missionMetadata.dueDate.getFullYear() > new Date().getFullYear() ? (
<DatePicker onChange={handleDateChange} />
) : (
<DatePicker onChange={handleDateChange} defaultValue={props.missionMetadata.dueDate} />
);

return (
<div className="SideContentMissionEditor">
<div className="SideContentMissionEditorRow">
<div className="SideContentMissionEditorLabelColumn">
<Label>Title</Label>
<Label>Cover Image Link</Label>
<Label>Summary</Label>
<Label>Type</Label>
<Label>ID</Label>
<Label>Source Version</Label>
<Label>Reading</Label>
<Label>Due Date</Label>
</div>
<div className="SideContentMissionEditorOptionColumn">
<InputGroup
defaultValue={props.missionMetadata.title}
onChange={handleChangeMissionTitle}
/>
<InputGroup
defaultValue={props.missionMetadata.coverImage}
onChange={handleChangeCoverImageLink}
/>
<InputGroup
defaultValue={props.missionMetadata.webSummary}
onChange={handleChangeMissionSummary}
/>
<InputGroup defaultValue={props.missionMetadata.type} onChange={handleChangeType} />
<InputGroup defaultValue={props.missionMetadata.id} onChange={handleChangeMissionId} />
<ControlBarChapterSelect
sourceChapter={props.missionMetadata.sourceVersion}
sourceVariant={Constants.defaultSourceVariant as Variant}
key="chapter"
disabled={false}
handleChapterSelect={handleChapterSelect}
/>
<InputGroup defaultValue={props.missionMetadata.reading} onChange={handleChangeReading} />
{datePicker}
</div>
</div>
</div>
Expand All @@ -69,37 +38,9 @@ const SideContentMissionEditor: React.FC<SideContentMissionEditorProps> = props
props.setMissionMetadata(newMetadata);
}

function handleChangeMissionTitle(event: any) {
setMissionMetadataWrapper<string>('title', event.target.value);
}

function handleChangeCoverImageLink(event: any) {
setMissionMetadataWrapper<string>('coverImage', event.target.value);
}

function handleChangeMissionSummary(event: any) {
setMissionMetadataWrapper<string>('webSummary', event.target.value);
}

function handleChapterSelect(i: SourceLanguage, e?: React.SyntheticEvent<HTMLElement>) {
setMissionMetadataWrapper<number>('sourceVersion', i.chapter);
}

function handleChangeMissionId(event: any) {
setMissionMetadataWrapper<string>('id', event.target.value);
}

function handleChangeReading(event: any) {
setMissionMetadataWrapper<string>('reading', event.target.value);
}

function handleChangeType(event: any) {
setMissionMetadataWrapper<string>('type', event.target.value);
}

function handleDateChange(date: Date) {
setMissionMetadataWrapper<Date>('dueDate', date);
}
};

export default SideContentMissionEditor;

This file was deleted.

9 changes: 1 addition & 8 deletions src/pages/githubAssessments/GitHubAssessmentDefaultValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,7 @@ If you need a more detailed cheatsheet, please click [here](https://www.markdown
export const defaultStarterCode = '// Your program here!\n';

export const defaultMissionMetadata = {
coverImage: '',
type: '',
id: '',
title: '',
sourceVersion: 1,
dueDate: new Date(8640000000000000),
reading: '',
webSummary: ''
sourceVersion: 1
} as MissionMetadata;

export const defaultTask = {
Expand Down
Loading

0 comments on commit defcc83

Please sign in to comment.