Skip to content

Ab#106897 refactor question types #1172

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

Draft
wants to merge 1 commit into
base: alpha
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 7 additions & 1 deletion src/schema/types/metadata.type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { questionType } from '@services/form.service';
import { Form, Resource } from '@models';
import {
GraphQLObjectType,
Expand Down Expand Up @@ -67,7 +68,12 @@ export const FieldMetaDataType = new GraphQLObjectType({
});
}
if (
['radiogroup', 'dropdown', 'checkbox', 'tagbox'].includes(parent.type)
[
questionType.RADIO_GROUP,
questionType.DROPDOWN,
questionType.CHECKBOX,
questionType.TAGBOX,
].includes(parent.type)
) {
if (parent._field?.choicesByUrl || parent._field?.choicesByGraphQL) {
return getFullChoices(parent._field, context);
Expand Down
62 changes: 57 additions & 5 deletions src/services/form.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,57 @@
import * as Survey from 'survey-knockout';

/** types for the surveyjs question types */
export enum questionType {
BOOLEAN = 'boolean',
CHECKBOX = 'checkbox',
COMMENT = 'comment',
EDITOR = 'editor',
EXPRESSION = 'expression',
DROPDOWN = 'dropdown',
FILE = 'file',
GEOSPATIAL = 'geospatial',
HTML = 'html',
MATRIX = 'matrix',
MATRIX_DROPDOWN = 'matrixdropdown',
MATRIX_DYNAMIC = 'matrixdynamic',
MULTIPLE_TEXT = 'multipletext',
OWNER = 'owner',
PANEL = 'panel',
PANEL_DYNAMIC = 'paneldynamic',
RADIO_GROUP = 'radiogroup',
RESOURCES = 'resources',
RESOURCE = 'resource',
SELECT = 'select',
TAGBOX = 'tagbox',
TEXT = 'text',
USERS = 'users',
}

/** Input type for text questions */
export enum inputType {
COLOR = 'color',
DATE = 'date',
DATETIME_LOCAL = 'datetime-local',
DATETIME = 'datetime',
DECIMAL = 'decimal',
EMAIL = 'email',
NUMBER = 'number',
NUMERIC = 'numeric',
TIME = 'time',
TEL = 'tel',
TEXT = 'text',
URL = 'url',
}

/** Input type for text questions */
export enum displayStyle {
DATE = 'date',
DECIMAL = 'decimal',
CURRENCY = 'currency',
PERCENT = 'percent',
NUMBER = 'number',
}

/**
* Form service, for SurveyJS validation.
*/
Expand Down Expand Up @@ -32,7 +84,7 @@ export class FormService {
title: 'Resource',
questionJSON: {
name: 'resource',
type: 'dropdown',
type: questionType.DROPDOWN,
choicesOrder: 'asc',
choices: [] as any[],
},
Expand All @@ -57,7 +109,7 @@ export class FormService {
title: 'Resources',
questionJSON: {
name: 'resources',
type: 'tagbox',
type: questionType.TAGBOX,
choicesOrder: 'asc',
choices: [] as any[],
},
Expand All @@ -82,7 +134,7 @@ export class FormService {
title: 'Owner',
questionJSON: {
name: 'owner',
type: 'tagbox',
type: questionType.TAGBOX,
choicesOrder: 'asc',
choices: [] as any[],
},
Expand All @@ -107,7 +159,7 @@ export class FormService {
title: 'Users',
questionJSON: {
name: 'users',
type: 'tagbox',
type: questionType.TAGBOX,
choicesOrder: 'asc',
choices: [] as any[],
},
Expand All @@ -132,7 +184,7 @@ export class FormService {
title: 'Geospatial',
questionJSON: {
name: 'geospatial',
type: 'text',
type: questionType.TEXT,
},
onInit: () => {},
onCreated: () => {},
Expand Down
17 changes: 9 additions & 8 deletions src/utils/files/getColumns.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { questionType } from '@services/form.service';
import { getChoices } from '../proxy/getChoices';

/** Default record fields */
Expand All @@ -19,8 +20,8 @@ export const getColumns = async (
const columns = [];
for (const field of fields) {
switch (field.type) {
case 'checkbox':
case 'tagbox': {
case questionType.CHECKBOX:
case questionType.TAGBOX: {
if (field.choices && Array.isArray(field.choices) && template) {
for (const item of field.choices) {
const name = `${field.name}.${item.value}`;
Expand Down Expand Up @@ -66,7 +67,7 @@ export const getColumns = async (
}
break;
}
case 'multipletext': {
case questionType.MULTIPLE_TEXT: {
for (const item of field.items) {
const name = `${field.name}.${item.name}`;
columns.push({
Expand All @@ -79,7 +80,7 @@ export const getColumns = async (
}
break;
}
case 'matrix': {
case questionType.MATRIX: {
for (const row of field.rows) {
const name = `${field.name}.${row.name}`;
columns.push({
Expand All @@ -97,7 +98,7 @@ export const getColumns = async (
}
break;
}
case 'matrixdropdown': {
case questionType.MATRIX_DROPDOWN: {
for (const row of field.rows) {
for (const column of field.columns) {
const name = `${field.name}.${row.name}.${column.name}`;
Expand All @@ -113,7 +114,7 @@ export const getColumns = async (
}
break;
}
case 'matrixdynamic': {
case questionType.MATRIX_DYNAMIC: {
for (const column of field.columns) {
const name = `${field.name}.0.${column.name}`;
columns.push({
Expand All @@ -123,8 +124,8 @@ export const getColumns = async (
}
break;
}
case 'dropdown':
case 'radiogroup': {
case questionType.DROPDOWN:
case questionType.RADIO_GROUP: {
const name = `${field.name}`;
if (field.choices && Array.isArray(field.choices) && template) {
const options = field.choices.map((x) => x.value);
Expand Down
23 changes: 12 additions & 11 deletions src/utils/files/getRows.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { inputType, questionType } from '@services/form.service';
import get from 'lodash/get';
import set from 'lodash/set';
import { getText } from '../form/getDisplayText';
Expand All @@ -19,8 +20,8 @@ export const getRows = async (
const data = record.data;
for (const column of columns) {
switch (column.type) {
case 'checkbox':
case 'tagbox': {
case questionType.CHECKBOX:
case questionType.TAGBOX: {
if (column.value) {
const value = data[column.field]?.includes(column.value) ? 1 : 0;
set(row, column.name, value);
Expand All @@ -42,7 +43,7 @@ export const getRows = async (
}
break;
}
case 'dropdown': {
case questionType.DROPDOWN: {
let value: any = get(data, column.field);
const choices = column.meta.field.choices || [];
if (choices.length > 0) {
Expand All @@ -55,14 +56,14 @@ export const getRows = async (
set(row, column.name, Array.isArray(value) ? value.join(',') : value);
break;
}
case 'multipletext':
case 'matrix':
case 'matrixdropdown': {
case questionType.MULTIPLE_TEXT:
case questionType.MATRIX:
case questionType.MATRIX_DROPDOWN: {
const value = get(data, column.name);
set(row, column.name, value);
break;
}
case 'resources': {
case questionType.RESOURCES: {
const value = get(data, column.field) || [];
if (value.length > 0) {
set(
Expand All @@ -75,7 +76,7 @@ export const getRows = async (
}
break;
}
case 'date': {
case inputType.DATE: {
const value = get(data, column.field);
if (value) {
const date = new Date(value);
Expand All @@ -85,8 +86,8 @@ export const getRows = async (
}
break;
}
case 'datetime':
case 'datetime-local': {
case inputType.DATETIME:
case inputType.DATETIME_LOCAL: {
const value = column.default
? get(record, column.field)
: get(data, column.field);
Expand All @@ -105,7 +106,7 @@ export const getRows = async (
}
break;
}
case 'time': {
case inputType.TIME: {
const value = column.default
? get(record, column.field)
: get(data, column.field);
Expand Down
33 changes: 17 additions & 16 deletions src/utils/files/getRowsFromMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import get from 'lodash/get';
import set from 'lodash/set';
import { getText } from '../form/getDisplayText';
import { Column } from './getColumnsFromMeta';
import { inputType, questionType } from '@services/form.service';

/**
* Set a row for multiselect type, handle specific behavior with ReferenceData
Expand Down Expand Up @@ -51,7 +52,7 @@ export const getRowsFromMeta = (
const row = {};
for (const column of columns) {
switch (column.type) {
case 'owner': {
case questionType.OWNER: {
let value: any = get(record, column.field);
const choices = column.meta.field.choices || [];
if (choices.length > 0) {
Expand All @@ -64,7 +65,7 @@ export const getRowsFromMeta = (
set(row, column.name, Array.isArray(value) ? value.join(',') : value);
break;
}
case 'users': {
case questionType.USERS: {
let value: any = get(record, column.field);
const choices = column.meta.field.choices || [];
if (choices.length > 0) {
Expand All @@ -77,12 +78,12 @@ export const getRowsFromMeta = (
set(row, column.name, Array.isArray(value) ? value.join(',') : value);
break;
}
case 'checkbox':
case 'tagbox': {
case questionType.CHECKBOX:
case questionType.TAGBOX: {
setMultiselectRow(column, record, row);
break;
}
case 'dropdown': {
case questionType.DROPDOWN: {
let value: any = get(record, column.field);
// Only enter if not reference data
if (!column.meta.field.graphQLFieldName) {
Expand All @@ -98,22 +99,22 @@ export const getRowsFromMeta = (
set(row, column.name, Array.isArray(value) ? value.join(',') : value);
break;
}
case 'multipletext': {
case questionType.MULTIPLE_TEXT: {
const value = get(record, column.name);
set(row, column.name, value);
break;
}
case 'matrix': {
case questionType.MATRIX: {
const value = get(record, column.name);
set(row, column.name, value);
break;
}
case 'matrixdropdown': {
case questionType.MATRIX_DROPDOWN: {
const value = get(record, column.name);
set(row, column.name, value);
break;
}
case 'resources': {
case questionType.RESOURCES: {
const value = get(record, column.field) || [];
if ((column.subColumns || []).length > 0) {
if (value && isArray(value)) {
Expand Down Expand Up @@ -146,7 +147,7 @@ export const getRowsFromMeta = (
}
break;
}
case 'date': {
case inputType.DATE: {
const value = get(record, column.field);
if (value) {
const date = new Date(value);
Expand All @@ -156,8 +157,8 @@ export const getRowsFromMeta = (
}
break;
}
case 'datetime':
case 'datetime-local': {
case inputType.DATETIME:
case inputType.DATETIME_LOCAL: {
const value = get(record, column.field);
if (value) {
const date = new Date(value);
Expand All @@ -174,7 +175,7 @@ export const getRowsFromMeta = (
}
break;
}
case 'time': {
case inputType.TIME: {
const value = get(record, column.field);
if (value) {
const date = new Date(value);
Expand All @@ -184,14 +185,14 @@ export const getRowsFromMeta = (
}
break;
}
case 'file': {
case questionType.FILE: {
const value = (get(record, `${column.field}`) || []).map(
(x) => x.name
);
set(row, column.name, value.join(','));
break;
}
case 'radiogroup': {
case questionType.RADIO_GROUP: {
if (isEmail) {
const radioValue = get(record, column.field);
const choices = column?.meta?.field?.choices || [];
Expand All @@ -200,7 +201,7 @@ export const getRowsFromMeta = (
break;
}
}
case 'geospatial': {
case questionType.GEOSPATIAL: {
if (isEmail) {
const geoValue = get(record, `${column.field}.properties`);
const lat = geoValue?.coordinates.lat;
Expand Down
Loading
Loading